| 1 | //go:build browser |
| 2 | |
| 3 | package memoriatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "net/http" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | cdpbrowser "github.com/chromedp/cdproto/browser" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | |
| 19 | memoria "amadan.net/oficina/memoria/internal/memoria" |
| 20 | ) |
| 21 | |
| 22 | // The typeahead's answer row copies its value (Paul, 2026-10-31: "cmd+c |
| 23 | // to copy, and a little Copy button"): ⌘C/Ctrl+C with nothing selected |
| 24 | // in the box, and the Copy button; and a selection in the box is still |
| 25 | // the ordinary copy of that selection. |
| 26 | // |
| 27 | // MUTATION-TESTED 2026-10-31, each reverted: the keydown handler removed |
| 28 | // → "⌘C"; the selection check removed → "a selection in the box"; the |
| 29 | // button's click handler removed → "the button"; the button left hidden |
| 30 | // → "no Copy button". |
| 31 | func TestTheTypeaheadAnswerCopies(t *testing.T) { |
| 32 | rig := harness.New(t, func(origin string) http.Handler { |
| 33 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 34 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 35 | if err != nil { |
| 36 | t.Fatalf("db.Open: %v", err) |
| 37 | } |
| 38 | t.Cleanup(func() { d.Close() }) |
| 39 | mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 40 | if err != nil { |
| 41 | t.Fatalf("AppForTest: %v", err) |
| 42 | } |
| 43 | if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: "o", Title: "Office", |
| 44 | Body: "## Contact\n\nOffice phone: +353 (0)1 547 7854"}); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | return mux |
| 48 | }) |
| 49 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 50 | defer cancel() |
| 51 | in := `input[type="search"]` |
| 52 | var visible bool |
| 53 | var afterKey, afterSelection, afterButton string |
| 54 | mod := chromedp.ModifierMeta |
| 55 | err := chromedp.Do(ctx, |
| 56 | grantPermissions(rig.Origin, cdpbrowser.PermissionTypeClipboardReadWrite, cdpbrowser.PermissionTypeClipboardSanitizedWrite), |
| 57 | chromedp.Navigate(rig.Origin+"/"), |
| 58 | chromedp.WaitVisible(chromedp.CSS(in)), |
| 59 | chromedp.SendKeys(chromedp.CSS(in), "office phone"), |
| 60 | chromedp.WaitVisible(chromedp.CSS(`[rst-suggest-copy]`)), |
| 61 | evalInto(&visible, `!document.querySelector("[rst-suggest-copy]").hidden`), |
| 62 | chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), |
| 63 | chromedp.KeyEvent("c", chromedp.KeyModifiers(chromedp.ModifierCtrl)), |
| 64 | chromedp.Sleep(300*time.Millisecond), |
| 65 | evalAwait(`navigator.clipboard.readText()`, &afterKey), |
| 66 | // A selection in the box: the keystroke copies that, not the value. |
| 67 | evalOnly(`navigator.clipboard.writeText("untouched"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(0, 6); true`), |
| 68 | chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), |
| 69 | chromedp.KeyEvent("c", chromedp.KeyModifiers(chromedp.ModifierCtrl)), |
| 70 | chromedp.Sleep(300*time.Millisecond), |
| 71 | evalAwait(`navigator.clipboard.readText()`, &afterSelection), |
| 72 | evalOnly(`navigator.clipboard.writeText("before the button"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(12, 12); true`), |
| 73 | chromedp.Click(chromedp.CSS(`[rst-suggest-copy]`)), |
| 74 | chromedp.Sleep(300*time.Millisecond), |
| 75 | evalAwait(`navigator.clipboard.readText()`, &afterButton), |
| 76 | ) |
| 77 | if err != nil { |
| 78 | t.Fatalf("copy: %v", err) |
| 79 | } |
| 80 | if !visible { |
| 81 | t.Fatal("no Copy button on the answer row") |
| 82 | } |
| 83 | if afterKey != "+353 (0)1 547 7854" { |
| 84 | t.Errorf("⌘C: the clipboard holds %q, want the number", afterKey) |
| 85 | } |
| 86 | if afterSelection == "+353 (0)1 547 7854" { |
| 87 | t.Errorf("a selection in the box: ⌘C copied the answer over it") |
| 88 | } |
| 89 | if afterButton != "+353 (0)1 547 7854" { |
| 90 | t.Errorf("the button: the clipboard holds %q, want the number", afterButton) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // evalAwait evaluates a promise and waits for its value. |
| 95 | func evalAwait[T any](js string, out *T) chromedp.Action[chromedp.Void] { |
| 96 | return evalInto(out, js, chromedp.EvalAwaitPromise) |
| 97 | } |
| 98 | |