//go:build browser package memoriatest import ( "context" "io" "log/slog" "net/http" "path/filepath" "testing" "time" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" cdpbrowser "github.com/chromedp/cdproto/browser" "github.com/chromedp/chromedp" memoria "amadan.net/oficina/memoria/internal/memoria" ) // The typeahead's answer row copies its value (Paul, 2026-10-31: "cmd+c // to copy, and a little Copy button"): ⌘C/Ctrl+C with nothing selected // in the box, and the Copy button; and a selection in the box is still // the ordinary copy of that selection. // // MUTATION-TESTED 2026-10-31, each reverted: the keydown handler removed // → "⌘C"; the selection check removed → "a selection in the box"; the // button's click handler removed → "the button"; the button left hidden // → "no Copy button". func TestTheTypeaheadAnswerCopies(t *testing.T) { rig := harness.New(t, func(origin string) http.Handler { logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } t.Cleanup(func() { d.Close() }) mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) if err != nil { t.Fatalf("AppForTest: %v", err) } if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: "o", Title: "Office", Body: "## Contact\n\nOffice phone: +353 (0)1 547 7854"}); err != nil { t.Fatal(err) } return mux }) ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() in := `input[type="search"]` var visible bool var afterKey, afterSelection, afterButton string mod := chromedp.ModifierMeta err := chromedp.Do(ctx, grantPermissions(rig.Origin, cdpbrowser.PermissionTypeClipboardReadWrite, cdpbrowser.PermissionTypeClipboardSanitizedWrite), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(in)), chromedp.SendKeys(chromedp.CSS(in), "office phone"), chromedp.WaitVisible(chromedp.CSS(`[rst-suggest-copy]`)), evalInto(&visible, `!document.querySelector("[rst-suggest-copy]").hidden`), chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), chromedp.KeyEvent("c", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(300*time.Millisecond), evalAwait(`navigator.clipboard.readText()`, &afterKey), // A selection in the box: the keystroke copies that, not the value. evalOnly(`navigator.clipboard.writeText("untouched"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(0, 6); true`), chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), chromedp.KeyEvent("c", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(300*time.Millisecond), evalAwait(`navigator.clipboard.readText()`, &afterSelection), evalOnly(`navigator.clipboard.writeText("before the button"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(12, 12); true`), chromedp.Click(chromedp.CSS(`[rst-suggest-copy]`)), chromedp.Sleep(300*time.Millisecond), evalAwait(`navigator.clipboard.readText()`, &afterButton), ) if err != nil { t.Fatalf("copy: %v", err) } if !visible { t.Fatal("no Copy button on the answer row") } if afterKey != "+353 (0)1 547 7854" { t.Errorf("⌘C: the clipboard holds %q, want the number", afterKey) } if afterSelection == "+353 (0)1 547 7854" { t.Errorf("a selection in the box: ⌘C copied the answer over it") } if afterButton != "+353 (0)1 547 7854" { t.Errorf("the button: the clipboard holds %q, want the number", afterButton) } } // evalAwait evaluates a promise and waits for its value. func evalAwait[T any](js string, out *T) chromedp.Action[chromedp.Void] { return evalInto(out, js, chromedp.EvalAwaitPromise) }