package console import ( "encoding/json" "os" "strings" "testing" ) func TestSigninCopyMatchesApproved(t *testing.T) { raw, err := os.ReadFile("../../docs/superpowers/specs/2026-10-21-console-email-signin-copy-approved.json") if err != nil { t.Fatal(err) } var approved struct { Action string `json:"action"` Strings []struct { ID string `json:"id"` Text string `json:"text"` } `json:"strings"` } if err := json.Unmarshal(raw, &approved); err != nil { t.Fatal(err) } if approved.Action != "approve" { t.Fatalf("action = %q, want approve", approved.Action) } seen := map[string]bool{} for _, s := range approved.Strings { seen[s.ID] = true got, ok := signinCopy[s.ID] if !ok { t.Errorf("approved id %q missing from signinCopy", s.ID) continue } if got != s.Text { t.Errorf("%s = %q, approved %q", s.ID, got, s.Text) } } for id := range signinCopy { if !seen[id] { t.Errorf("signinCopy has %q, which is not in the approved copy", id) } } if len(signinCopy) != len(approved.Strings) { t.Errorf("signinCopy has %d strings, approved copy has %d", len(signinCopy), len(approved.Strings)) } } func TestCopyTextFillsPlaceholders(t *testing.T) { for id, text := range signinCopy { if got := copyText(id); got != text { t.Errorf("copyText(%q) with no arguments changed the text", id) } } for _, tc := range []struct { id string kv []string want string }{ {"check.sent", []string{"address", "bob@example.com"}, "A sign-in link and code are on their way to bob@example.com. Both work once, for 15 minutes."}, {"email.signin.body", []string{"console", "Example Console", "address", "bob@example.com", "link", "https://console.example.com/auth/email/link?t=TOKEN", "code", "123456"}, "Someone, hopefully you, asked to sign in to Example Console as bob@example.com.\n\n" + "Open this link to sign in:\n\nhttps://console.example.com/auth/email/link?t=TOKEN\n\n" + "Or type this code on the page that's waiting: 123456\n\n" + "The link and the code work once, for 15 minutes. If this wasn't you, ignore this email. Nothing happens without the link or the code."}, } { got := copyText(tc.id, tc.kv...) if got != tc.want { t.Errorf("copyText(%q, %q) = %q, want %q", tc.id, tc.kv, got, tc.want) } if strings.ContainsAny(got, "{}") { t.Errorf("copyText(%q) left a placeholder: %q", tc.id, got) } } defer func() { if recover() == nil { t.Error("copyText of an unknown id did not panic") } }() copyText("no.such.id") }