| 1 | //go:build browser |
| 2 | |
| 3 | // Typing suggests and Enter answers, in a real Chromium. Run with `make |
| 4 | // browser`. |
| 5 | // |
| 6 | // This file held live search's settle test until the typeahead replaced |
| 7 | // live search (Paul, 2026-10-29: "Google-style typeahead and then press |
| 8 | // enter to jump to the search page where it gives an answer"). What it |
| 9 | // pinned then — §8.9's model step skipped per keystroke and run once |
| 10 | // typing paused — is now two halves: the typeahead never runs the model |
| 11 | // step at all, and the results page Enter opens always does. |
| 12 | // |
| 13 | // MUTATION-TESTED 2026-11-02, each reverted: /suggest calling the full |
| 14 | // Search in place of SearchWhileTyping → "typing called the model"; the |
| 15 | // keydown handler swallowing a plain Enter → the results page never |
| 16 | // arrives (a timeout; with SendKeys' "\r" this survived, see below). |
| 17 | package memoriatest |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "io" |
| 22 | "log/slog" |
| 23 | "net/http" |
| 24 | "path/filepath" |
| 25 | "testing" |
| 26 | "time" |
| 27 | |
| 28 | "amadan.net/rastrillo/rastrillo/db" |
| 29 | "amadan.net/rastrillo/rastrillo/harness" |
| 30 | "github.com/chromedp/cdproto/input" |
| 31 | "github.com/chromedp/chromedp" |
| 32 | |
| 33 | memoria "amadan.net/oficina/memoria/internal/memoria" |
| 34 | ) |
| 35 | |
| 36 | func TestTypingSuggestsAndEnterAnswers(t *testing.T) { |
| 37 | m := &countingModel{} |
| 38 | rig := harness.New(t, func(origin string) http.Handler { |
| 39 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 40 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 41 | if err != nil { |
| 42 | t.Fatalf("db.Open: %v", err) |
| 43 | } |
| 44 | t.Cleanup(func() { d.Close() }) |
| 45 | mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 46 | if err != nil { |
| 47 | t.Fatalf("AppForTest: %v", err) |
| 48 | } |
| 49 | a.UseFakeModel(m) |
| 50 | if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{ |
| 51 | Source: "upload", Key: "office", Title: "Tito office", |
| 52 | Body: "## Contact\n\nOffice address: 12 Main Street, Dublin 2", |
| 53 | }); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | return mux |
| 57 | }) |
| 58 | var typed int |
| 59 | var path string |
| 60 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 61 | defer cancel() |
| 62 | err := chromedp.Do(ctx, |
| 63 | chromedp.Navigate(rig.Origin+"/"), |
| 64 | chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), |
| 65 | chromedp.SendKeys(chromedp.CSS(`input[type="search"]`), "office address"), |
| 66 | chromedp.WaitVisible(chromedp.CSS(`[rst-suggest-box] [rst-suggest-item]`)), |
| 67 | chromedp.Sleep(1000*time.Millisecond), |
| 68 | chromedp.Func(func(context.Context, *chromedp.Target) error { typed = m.calls; return nil }), |
| 69 | // One keyDown carrying the text, as a real key press is: Chromium |
| 70 | // then derives the keypress (and the form's implicit submission) |
| 71 | // from it, so a handler that cancels the keydown cancels the |
| 72 | // submit. SendKeys' separate "char" event submits whatever the |
| 73 | // keydown handler did, and could not see the second mutation. |
| 74 | cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{ |
| 75 | Type: input.DispatchKeyEventTypeKeyDown, Key: "Enter", Code: "Enter", |
| 76 | WindowsVirtualKeyCode: 13, Text: "\r", |
| 77 | }), |
| 78 | cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{ |
| 79 | Type: input.DispatchKeyEventTypeKeyUp, Key: "Enter", Code: "Enter", |
| 80 | WindowsVirtualKeyCode: 13, |
| 81 | }), |
| 82 | chromedp.WaitVisible(chromedp.CSS(`[rst-result-title]`)), |
| 83 | evalInto(&path, `location.pathname + location.search`), |
| 84 | ) |
| 85 | if err != nil { |
| 86 | t.Fatalf("typeahead: %v", err) |
| 87 | } |
| 88 | if typed != 0 { |
| 89 | t.Errorf("typing called the model %d times; the typeahead never runs §8.9", typed) |
| 90 | } |
| 91 | if path != "/?q=office+address" { |
| 92 | t.Errorf("Enter did not open the results page: at %q", path) |
| 93 | } |
| 94 | if m.calls != 1 { |
| 95 | t.Errorf("the results page called the model %d times, want 1", m.calls) |
| 96 | } |
| 97 | } |
| 98 | |