//go:build browser // Typing suggests and Enter answers, in a real Chromium. Run with `make // browser`. // // This file held live search's settle test until the typeahead replaced // live search (Paul, 2026-10-29: "Google-style typeahead and then press // enter to jump to the search page where it gives an answer"). What it // pinned then — §8.9's model step skipped per keystroke and run once // typing paused — is now two halves: the typeahead never runs the model // step at all, and the results page Enter opens always does. // // MUTATION-TESTED 2026-11-02, each reverted: /suggest calling the full // Search in place of SearchWhileTyping → "typing called the model"; the // keydown handler swallowing a plain Enter → the results page never // arrives (a timeout; with SendKeys' "\r" this survived, see below). package memoriatest import ( "context" "io" "log/slog" "net/http" "path/filepath" "testing" "time" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" memoria "amadan.net/oficina/memoria/internal/memoria" ) func TestTypingSuggestsAndEnterAnswers(t *testing.T) { m := &countingModel{} 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) } a.UseFakeModel(m) if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{ Source: "upload", Key: "office", Title: "Tito office", Body: "## Contact\n\nOffice address: 12 Main Street, Dublin 2", }); err != nil { t.Fatal(err) } return mux }) var typed int var path string ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) defer cancel() err := chromedp.Do(ctx, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`input[type="search"]`)), chromedp.SendKeys(chromedp.CSS(`input[type="search"]`), "office address"), chromedp.WaitVisible(chromedp.CSS(`[rst-suggest-box] [rst-suggest-item]`)), chromedp.Sleep(1000*time.Millisecond), chromedp.Func(func(context.Context, *chromedp.Target) error { typed = m.calls; return nil }), // One keyDown carrying the text, as a real key press is: Chromium // then derives the keypress (and the form's implicit submission) // from it, so a handler that cancels the keydown cancels the // submit. SendKeys' separate "char" event submits whatever the // keydown handler did, and could not see the second mutation. cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyDown, Key: "Enter", Code: "Enter", WindowsVirtualKeyCode: 13, Text: "\r", }), cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyUp, Key: "Enter", Code: "Enter", WindowsVirtualKeyCode: 13, }), chromedp.WaitVisible(chromedp.CSS(`[rst-result-title]`)), evalInto(&path, `location.pathname + location.search`), ) if err != nil { t.Fatalf("typeahead: %v", err) } if typed != 0 { t.Errorf("typing called the model %d times; the typeahead never runs §8.9", typed) } if path != "/?q=office+address" { t.Errorf("Enter did not open the results page: at %q", path) } if m.calls != 1 { t.Errorf("the results page called the model %d times, want 1", m.calls) } }