| 1 | //go:build browser |
| 2 | |
| 3 | // Β§8.11's browser race, in a real Chromium. Run with `make browser`. |
| 4 | // |
| 5 | // An answer for a query the screen has moved on from must never be |
| 6 | // painted. The fake model is slow (1.5 s) and answers with its own |
| 7 | // question, so a stale answer is visible by its words. |
| 8 | // |
| 9 | // Until 2026-10-29 the box searched as it was typed into, and a stale |
| 10 | // answer could land on newer live results; two defences and a slow-typing |
| 11 | // test kept it off (MUTATION-TESTED 2026-10-27). The typeahead replaced |
| 12 | // live search: the answer is now asked for once, by the results page, for |
| 13 | // the query that page was rendered for, and a new search is a new page. |
| 14 | // What is left to pin is that the box does not disturb the answer and a |
| 15 | // new page does not inherit the old one's. |
| 16 | package memoriatest |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "io" |
| 21 | "log/slog" |
| 22 | "net/http" |
| 23 | "path/filepath" |
| 24 | "strings" |
| 25 | "sync/atomic" |
| 26 | "testing" |
| 27 | "time" |
| 28 | |
| 29 | "amadan.net/rastrillo/rastrillo/db" |
| 30 | "amadan.net/rastrillo/rastrillo/harness" |
| 31 | "github.com/chromedp/chromedp" |
| 32 | |
| 33 | memoria "amadan.net/oficina/memoria/internal/memoria" |
| 34 | "amadan.net/oficina/memoria/internal/memoria/provider" |
| 35 | ) |
| 36 | |
| 37 | // slowAI answers after 1.5 s with its own question, and counts the |
| 38 | // answers abandoned by the caller before then. |
| 39 | type slowAI struct{ abandoned atomic.Int32 } |
| 40 | |
| 41 | func (*slowAI) ExtractAddresses(context.Context, []string) ([]provider.Pointer, error) { |
| 42 | return []provider.Pointer{}, nil |
| 43 | } |
| 44 | |
| 45 | func (f *slowAI) Answer(ctx context.Context, q string, _ []string) ([]provider.Sentence, error) { |
| 46 | select { |
| 47 | case <-time.After(1500 * time.Millisecond): |
| 48 | case <-ctx.Done(): |
| 49 | f.abandoned.Add(1) |
| 50 | return nil, ctx.Err() |
| 51 | } |
| 52 | return []provider.Sentence{{Text: "ANSWER FOR " + strings.ToUpper(q), Cite: []int{0}}}, nil |
| 53 | } |
| 54 | |
| 55 | // aiRig serves memoria with ai as its model and two documents stored. |
| 56 | // wrap, if set, sits in front of the app with the stored ids to hand. |
| 57 | func aiRig(t *testing.T, ai *slowAI, wrap func(next http.Handler, ids []int64, store *memoria.Store) http.Handler) *harness.Rig { |
| 58 | return harness.New(t, func(origin string) http.Handler { |
| 59 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 60 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 61 | if err != nil { |
| 62 | t.Fatalf("db.Open: %v", err) |
| 63 | } |
| 64 | t.Cleanup(func() { d.Close() }) |
| 65 | mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 66 | if err != nil { |
| 67 | t.Fatalf("AppForTest: %v", err) |
| 68 | } |
| 69 | a.UseFakeModel(ai) |
| 70 | var ids []int64 |
| 71 | for _, doc := range [][2]string{ |
| 72 | {"Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer."}, |
| 73 | {"Kitchen", "## Rota\n\nThe kitchen rota is pinned by the kettle."}, |
| 74 | } { |
| 75 | id, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: doc[0], Title: doc[0], Body: doc[1]}) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | ids = append(ids, id) |
| 80 | } |
| 81 | if wrap != nil { |
| 82 | return wrap(mux, ids, a.Store()) |
| 83 | } |
| 84 | return mux |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | func TestAStaleAIAnswerIsNeverPainted(t *testing.T) { |
| 89 | rig := aiRig(t, &slowAI{}, nil) |
| 90 | var page string |
| 91 | var final string |
| 92 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 93 | defer cancel() |
| 94 | err := chromedp.Do(ctx, |
| 95 | chromedp.Navigate(rig.Origin+"/?q=paul+campbell"), |
| 96 | chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), |
| 97 | // The first answer is in flight; a new search is typed and sent. |
| 98 | chromedp.Sleep(300*time.Millisecond), |
| 99 | evalOnly(`document.querySelector('input[type="search"]').value = ""; true`), |
| 100 | chromedp.SendKeys(chromedp.CSS(`input[type="search"]`), "kitchen rota\r"), |
| 101 | chromedp.WaitVisible(chromedp.CSS(`[rst-search-results-for="kitchen rota"]`)), |
| 102 | chromedp.Sleep(3000*time.Millisecond), |
| 103 | evalInto(&page, `document.body.textContent`), |
| 104 | evalInto(&final, `(document.querySelector("[rst-answer]") || {}).textContent || ""`), |
| 105 | ) |
| 106 | if err != nil { |
| 107 | t.Fatalf("search: %v", err) |
| 108 | } |
| 109 | if strings.Contains(page, "ANSWER FOR PAUL CAMPBELL") { |
| 110 | t.Fatal("an answer for the old query was painted on the new page") |
| 111 | } |
| 112 | if !strings.Contains(final, "ANSWER FOR KITCHEN ROTA") { |
| 113 | t.Errorf("the current query's answer is not on the page: %q", final) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // The ways a slot could be left wrong (Astra, 2026-10-27; reworked for |
| 118 | // the typeahead 2026-11-02, when "a new search abandons the answer" moved |
| 119 | // to TestAStaleAIAnswerIsNeverPainted, where a new search is a new page). |
| 120 | // |
| 121 | // MUTATION-TESTED 2026-10-27, reverted: the 204 branch leaving the slot β |
| 122 | // "slot left". 2026-11-02, reverted: the box's input handler emptying the |
| 123 | // results region (where the slot is) β "stuck". Clearing only an |
| 124 | // [rst-answer] there survived: at 300 ms the answer has not arrived, so |
| 125 | // there is nothing of that name to clear. |
| 126 | func TestTheAnswerSlotSettles(t *testing.T) { |
| 127 | t.Run("a space typed while asking", func(t *testing.T) { |
| 128 | rig := aiRig(t, &slowAI{}, nil) |
| 129 | var final string |
| 130 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 131 | defer cancel() |
| 132 | err := chromedp.Do(ctx, |
| 133 | chromedp.Navigate(rig.Origin+"/?q=kitchen+rota"), |
| 134 | chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), |
| 135 | // The answer is in flight; typing in the box is not a search. |
| 136 | chromedp.Sleep(300*time.Millisecond), |
| 137 | chromedp.SendKeys(chromedp.CSS(`input[type="search"]`), " "), |
| 138 | chromedp.Sleep(3500*time.Millisecond), |
| 139 | evalInto(&final, `(document.querySelector("[rst-answer]") || {}).textContent || ""`), |
| 140 | ) |
| 141 | if err != nil { |
| 142 | t.Fatalf("search: %v", err) |
| 143 | } |
| 144 | if !strings.Contains(final, "ANSWER FOR KITCHEN ROTA") { |
| 145 | t.Errorf("stuck: the slot says %q", strings.TrimSpace(final)) |
| 146 | } |
| 147 | }) |
| 148 | |
| 149 | t.Run("no answer wanted any more", func(t *testing.T) { |
| 150 | // The document goes between the page and /answer, so /answer's |
| 151 | // own search finds nothing and says 204. |
| 152 | rig := aiRig(t, &slowAI{}, func(next http.Handler, ids []int64, st *memoria.Store) http.Handler { |
| 153 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 154 | if r.URL.Path == "/answer" { |
| 155 | for _, id := range ids { |
| 156 | _ = st.Delete(r.Context(), id) |
| 157 | } |
| 158 | } |
| 159 | next.ServeHTTP(w, r) |
| 160 | }) |
| 161 | }) |
| 162 | var slots int |
| 163 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 164 | defer cancel() |
| 165 | err := chromedp.Do(ctx, |
| 166 | chromedp.Navigate(rig.Origin+"/?q=kitchen+rota"), |
| 167 | chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), |
| 168 | chromedp.Sleep(1500*time.Millisecond), |
| 169 | evalInto(&slots, `document.querySelectorAll("[rst-answer]").length`), |
| 170 | ) |
| 171 | if err != nil { |
| 172 | t.Fatalf("load: %v", err) |
| 173 | } |
| 174 | if slots != 0 { |
| 175 | t.Errorf("slot left after a 204: %d [rst-answer] on the page", slots) |
| 176 | } |
| 177 | }) |
| 178 | |
| 179 | } |
| 180 | |
| 181 | // π saves in place: the page does not move, the row says "Thanks.", and |
| 182 | // the rating is on Reports. MUTATION-TESTED 2026-10-27, reverted: the |
| 183 | // submit listener removed β "navigated" (the form's own submit reloads |
| 184 | // the search). |
| 185 | func TestThumbsUpSavesInPlace(t *testing.T) { |
| 186 | rig := aiRig(t, &slowAI{}, nil) |
| 187 | var said, path, reports string |
| 188 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 189 | defer cancel() |
| 190 | err := chromedp.Do(ctx, |
| 191 | chromedp.Navigate(rig.Origin+"/?q=kitchen+rota"), |
| 192 | chromedp.WaitVisible(chromedp.CSS(`[rst-ai-feedback] button[value="up"]`)), |
| 193 | evalOnly(`window.__stayed = true; true`), |
| 194 | chromedp.Click(chromedp.CSS(`[rst-ai-feedback] button[value="up"]`)), |
| 195 | chromedp.Sleep(1000*time.Millisecond), |
| 196 | evalInto(&said, `window.__stayed ? (document.querySelector("[rst-ai-feedback]") || {}).textContent || "" : "navigated"`), |
| 197 | evalInto(&path, `location.pathname + location.search`), |
| 198 | chromedp.Navigate(rig.Origin+"/reports"), |
| 199 | evalInto(&reports, `document.body.textContent`), |
| 200 | ) |
| 201 | if err != nil { |
| 202 | t.Fatalf("thumbs up: %v", err) |
| 203 | } |
| 204 | if strings.TrimSpace(said) != "Thanks." { |
| 205 | t.Errorf("navigated, or no thanks: %q (at %s)", said, path) |
| 206 | } |
| 207 | if !strings.Contains(reports, "π") || !strings.Contains(reports, "kitchen rota") { |
| 208 | t.Errorf("the rating is not on Reports") |
| 209 | } |
| 210 | } |
| 211 | |