| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestSigninCopyMatchesApproved(t *testing.T) { |
| 11 | raw, err := os.ReadFile("../../docs/superpowers/specs/2026-10-21-console-email-signin-copy-approved.json") |
| 12 | if err != nil { |
| 13 | t.Fatal(err) |
| 14 | } |
| 15 | var approved struct { |
| 16 | Action string `json:"action"` |
| 17 | Strings []struct { |
| 18 | ID string `json:"id"` |
| 19 | Text string `json:"text"` |
| 20 | } `json:"strings"` |
| 21 | } |
| 22 | if err := json.Unmarshal(raw, &approved); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if approved.Action != "approve" { |
| 26 | t.Fatalf("action = %q, want approve", approved.Action) |
| 27 | } |
| 28 | seen := map[string]bool{} |
| 29 | for _, s := range approved.Strings { |
| 30 | seen[s.ID] = true |
| 31 | got, ok := signinCopy[s.ID] |
| 32 | if !ok { |
| 33 | t.Errorf("approved id %q missing from signinCopy", s.ID) |
| 34 | continue |
| 35 | } |
| 36 | if got != s.Text { |
| 37 | t.Errorf("%s = %q, approved %q", s.ID, got, s.Text) |
| 38 | } |
| 39 | } |
| 40 | for id := range signinCopy { |
| 41 | if !seen[id] { |
| 42 | t.Errorf("signinCopy has %q, which is not in the approved copy", id) |
| 43 | } |
| 44 | } |
| 45 | if len(signinCopy) != len(approved.Strings) { |
| 46 | t.Errorf("signinCopy has %d strings, approved copy has %d", len(signinCopy), len(approved.Strings)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestCopyTextFillsPlaceholders(t *testing.T) { |
| 51 | for id, text := range signinCopy { |
| 52 | if got := copyText(id); got != text { |
| 53 | t.Errorf("copyText(%q) with no arguments changed the text", id) |
| 54 | } |
| 55 | } |
| 56 | for _, tc := range []struct { |
| 57 | id string |
| 58 | kv []string |
| 59 | want string |
| 60 | }{ |
| 61 | {"check.sent", []string{"address", "bob@example.com"}, |
| 62 | "A sign-in link and code are on their way to bob@example.com. Both work once, for 15 minutes."}, |
| 63 | {"email.signin.body", []string{"console", "Example Console", "address", "bob@example.com", |
| 64 | "link", "https://console.example.com/auth/email/link?t=TOKEN", "code", "123456"}, |
| 65 | "Someone, hopefully you, asked to sign in to Example Console as bob@example.com.\n\n" + |
| 66 | "Open this link to sign in:\n\nhttps://console.example.com/auth/email/link?t=TOKEN\n\n" + |
| 67 | "Or type this code on the page that's waiting: 123456\n\n" + |
| 68 | "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."}, |
| 69 | } { |
| 70 | got := copyText(tc.id, tc.kv...) |
| 71 | if got != tc.want { |
| 72 | t.Errorf("copyText(%q, %q) = %q, want %q", tc.id, tc.kv, got, tc.want) |
| 73 | } |
| 74 | if strings.ContainsAny(got, "{}") { |
| 75 | t.Errorf("copyText(%q) left a placeholder: %q", tc.id, got) |
| 76 | } |
| 77 | } |
| 78 | defer func() { |
| 79 | if recover() == nil { |
| 80 | t.Error("copyText of an unknown id did not panic") |
| 81 | } |
| 82 | }() |
| 83 | copyText("no.such.id") |
| 84 | } |
| 85 | |