| 1 | //go:build browser |
| 2 | |
| 3 | // The whole result opens it (Paul, 2026-10-26: "Can the whole segment be |
| 4 | // clickable rather than just the title?"), without taking away text |
| 5 | // selection in the snippet (review, 2026-10-26: a stretched overlay made |
| 6 | // a double-click select nothing). Run with `make browser`. |
| 7 | // |
| 8 | // MUTATION-TESTED 2026-10-26, each reverted: the click handler removed → |
| 9 | // "clicking the second result's snippet left the page at …"; the |
| 10 | // destination taken from the first result → "went to …/documents/2#p0, |
| 11 | // want the second result's document"; the selection check when the wait |
| 12 | // ends removed → "a double-click to select a word left the page". A |
| 13 | // dblclick listener and a second-click cancel were each mutated away and |
| 14 | // stayed green beside that check, so both were deleted. |
| 15 | package memoriatest |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "io" |
| 20 | "log/slog" |
| 21 | "net/http" |
| 22 | "path/filepath" |
| 23 | "strings" |
| 24 | "testing" |
| 25 | "time" |
| 26 | |
| 27 | "amadan.net/rastrillo/rastrillo/db" |
| 28 | "amadan.net/rastrillo/rastrillo/harness" |
| 29 | "github.com/chromedp/chromedp" |
| 30 | |
| 31 | memoria "amadan.net/oficina/memoria/internal/memoria" |
| 32 | ) |
| 33 | |
| 34 | func TestTheWholeResultIsALink(t *testing.T) { |
| 35 | rig := harness.New(t, func(origin string) http.Handler { |
| 36 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 37 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 38 | if err != nil { |
| 39 | t.Fatalf("db.Open: %v", err) |
| 40 | } |
| 41 | t.Cleanup(func() { d.Close() }) |
| 42 | mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 43 | if err != nil { |
| 44 | t.Fatalf("AppForTest: %v", err) |
| 45 | } |
| 46 | for _, doc := range [][2]string{ |
| 47 | {"Office", "## Office Maintenance\n\nAlan Brady works for the management agency and can be contacted any weekday."}, |
| 48 | {"Kitchen", "## Supplies\n\nThe agency restocks the kitchen every Monday morning before nine."}, |
| 49 | } { |
| 50 | if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{ |
| 51 | Source: "upload", Key: doc[0], Title: doc[0], Body: doc[1], |
| 52 | }); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | } |
| 56 | return mux |
| 57 | }) |
| 58 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 59 | t.Helper() |
| 60 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 61 | defer cancel() |
| 62 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 63 | t.Fatalf("%s: %v", what, err) |
| 64 | } |
| 65 | } |
| 66 | search := rig.Origin + "/?q=agency" |
| 67 | |
| 68 | // Each result opens its own document: the second's snippet goes to |
| 69 | // the second's title link, not the first's. |
| 70 | var want, at string |
| 71 | run("click the second result", |
| 72 | chromedp.Navigate(search), |
| 73 | chromedp.WaitVisible(chromedp.CSS(`[rst-result]:nth-child(2) [rst-result-snippet]`)), |
| 74 | attributeInto(chromedp.CSS(`[rst-result]:nth-child(2) [rst-result-title]`), "href", &want, nil), |
| 75 | chromedp.Click(chromedp.CSS(`[rst-result]:nth-child(2) [rst-result-snippet]`)), |
| 76 | chromedp.Sleep(800*time.Millisecond), |
| 77 | locationInto(&at), |
| 78 | ) |
| 79 | if !strings.Contains(at, "/documents/") { |
| 80 | t.Fatalf("clicking the second result's snippet left the page at %s", at) |
| 81 | } |
| 82 | if !strings.HasSuffix(at, want) { |
| 83 | t.Errorf("went to %s, want the second result's document %s", at, want) |
| 84 | } |
| 85 | |
| 86 | // A double-click selects a word and goes nowhere. |
| 87 | var picked string |
| 88 | run("double-click a word", |
| 89 | chromedp.Navigate(search), |
| 90 | chromedp.WaitVisible(chromedp.CSS(`[rst-result-snippet]`)), |
| 91 | // A real double-click is two clicks: the first with a click |
| 92 | // count of one, then the second with two. chromedp's DoubleClick |
| 93 | // sends only the second, which skips the very path under test. |
| 94 | chromedp.Click(chromedp.CSS(`[rst-result-snippet] mark`)), |
| 95 | chromedp.DoubleClick(chromedp.CSS(`[rst-result-snippet] mark`)), |
| 96 | chromedp.Sleep(800*time.Millisecond), |
| 97 | evalInto(&picked, `String(window.getSelection())`), |
| 98 | locationInto(&at), |
| 99 | ) |
| 100 | if strings.Contains(at, "/documents/") { |
| 101 | t.Errorf("a double-click to select a word left the page for %s", at) |
| 102 | } |
| 103 | if strings.TrimSpace(picked) == "" { |
| 104 | t.Error("a double-click selected nothing") |
| 105 | } |
| 106 | |
| 107 | // Outside a result, a click is just a click. |
| 108 | run("click the heading", |
| 109 | chromedp.Navigate(search), |
| 110 | chromedp.WaitVisible(chromedp.CSS(`h2`)), |
| 111 | chromedp.Click(chromedp.CSS(`h2`)), |
| 112 | chromedp.Sleep(800*time.Millisecond), |
| 113 | locationInto(&at), |
| 114 | ) |
| 115 | if strings.Contains(at, "/documents/") { |
| 116 | t.Errorf("a click on the heading left the page for %s", at) |
| 117 | } |
| 118 | } |
| 119 | |