package twitch_test

import (
	"net/http/httptest"
	"strings"
	"testing"

	"git.sunturtle.xyz/zephyr/kaiyan/twitch"
)

func TestReceive(t *testing.T) {
	// Request generated by Twitch CLI using "gotohhitori" as the secret.
	// This should pass.
	body := `{"subscription":{"id":"0dd34bc7-6631-b1d8-4306-18a49c3b3ba8","status":"enabled","type":"user.authorization.grant","version":"1","condition":{"client_id":"689a99bb72fc9d068f7089ab1c8104"},"transport":{"method":"webhook","callback":"null"},"created_at":"2025-04-08T16:54:43.7141633Z","cost":1},"event":{"user_id":"16396008","user_login":"testFromUser","user_name":"testFromUser","client_id":"689a99bb72fc9d068f7089ab1c8104"}}`
	req := httptest.NewRequest("POST", "http://bocchi.rocks/", strings.NewReader(body))
	header := map[string]string{
		"Accept-Encoding":                      "gzip",
		"Content-Type":                         "application/json",
		"Twitch-Eventsub-Message-Id":           "7d5c6ab3-74a3-3583-b0c1-d33ca5490910",
		"Twitch-Eventsub-Message-Retry":        "0",
		"Twitch-Eventsub-Message-Signature":    "sha256=2f409c88eb8e8515f22b2bfb50dc97f6ff3fde7986d3a6013030e55840e5c8b8",
		"Twitch-Eventsub-Message-Timestamp":    "2025-04-08T16:54:43.7141633Z",
		"Twitch-Eventsub-Message-Type":         "notification",
		"Twitch-Eventsub-Subscription-Type":    "user.authorization.grant",
		"Twitch-Eventsub-Subscription-Version": "1",
		"User-Agent":                           "twitch-cli/source",
	}
	for k, v := range header {
		req.Header.Set(k, v)
	}
	got, err := twitch.Receive(nil, []byte("gotohhitori"), req)
	if err != nil {
		t.Error(err)
	}
	if string(got) != body {
		t.Errorf("wrong body:\nwant %q\ngot  %q", body, got)
	}
}

func TestSecret(t *testing.T) {
	got := twitch.Secret()
	if len(got) < 10 || len(got) > 100 {
		t.Errorf("secret has invalid length %d", len(got))
	}
	for _, c := range got {
		if c < 0x20 || c > 0x7f {
			t.Errorf("secret contains invalid character %q", rune(c))
		}
	}
}