| 1 | //go:build browser |
| 2 | |
| 3 | package memoriatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "io" |
| 8 | "log/slog" |
| 9 | "net/http" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/db" |
| 16 | "amadan.net/rastrillo/rastrillo/harness" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | |
| 19 | memoria "amadan.net/oficina/memoria/internal/memoria" |
| 20 | ) |
| 21 | |
| 22 | // A list row with a link opens as a whole (Paul, 2026-10-31: "the link |
| 23 | // should fill the whole list item"): a click on a row's empty part goes |
| 24 | // where its link goes, and a Delete in the same row is still a Delete. |
| 25 | // |
| 26 | // MUTATION-TESTED 2026-10-31, each reverted: the ::after stretch removed |
| 27 | // → "documents row"; the front page's row not rastrillo's list row |
| 28 | // (rst-row-main dropped) → "recently updated"; Delete not lifted above |
| 29 | // the stretch → "Delete". The first version named its rows rst-row, which |
| 30 | // is rastrillo's own list row: its control-lifting rule kept Delete |
| 31 | // working whatever this app's CSS said, and the Delete mutation stayed |
| 32 | // green until the names were Memoria's own. |
| 33 | func TestAWholeRowOpens(t *testing.T) { |
| 34 | rig := harness.New(t, func(origin string) http.Handler { |
| 35 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 36 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 37 | if err != nil { |
| 38 | t.Fatalf("db.Open: %v", err) |
| 39 | } |
| 40 | t.Cleanup(func() { d.Close() }) |
| 41 | mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 42 | if err != nil { |
| 43 | t.Fatalf("AppForTest: %v", err) |
| 44 | } |
| 45 | if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: "o", Title: "Office", Body: "Office phone: +353 (0)1 547 7854"}); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | return mux |
| 49 | }) |
| 50 | ctx, cancel := context.WithTimeout(rig.Context(), 40*time.Second) |
| 51 | defer cancel() |
| 52 | // clickAt clicks a point inside the element, near its right edge: |
| 53 | // past the link's own text, on the row's empty part. |
| 54 | clickRight := func(sel string) chromedp.Action[chromedp.Void] { |
| 55 | return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { |
| 56 | var box []float64 |
| 57 | if _, err := evalInto(&box, `(function(){var r=document.querySelector(`+"`"+sel+"`"+`).getBoundingClientRect();return [r.right-8, r.top+r.height/2]})()`)(ctx, t); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | _, err := chromedp.MouseClickXY(box[0], box[1])(ctx, t) |
| 61 | return err |
| 62 | }) |
| 63 | } |
| 64 | var home, docs, del string |
| 65 | err := chromedp.Do(ctx, |
| 66 | chromedp.Navigate(rig.Origin+"/"), |
| 67 | chromedp.WaitVisible(chromedp.CSS(`[rst-home-list] li`)), |
| 68 | clickRight(`[rst-home-list] li`), |
| 69 | chromedp.Sleep(800*time.Millisecond), |
| 70 | locationInto(&home), |
| 71 | chromedp.Navigate(rig.Origin+"/documents"), |
| 72 | chromedp.WaitVisible(chromedp.CSS(`tr[rst-click-row]`)), |
| 73 | // The Updated cell: neither the title nor Delete. |
| 74 | clickRight(`tr[rst-click-row] td:nth-child(4)`), |
| 75 | chromedp.Sleep(800*time.Millisecond), |
| 76 | locationInto(&docs), |
| 77 | chromedp.Navigate(rig.Origin+"/documents"), |
| 78 | chromedp.WaitVisible(chromedp.CSS(`tr[rst-click-row]`)), |
| 79 | chromedp.Click(chromedp.CSS(`tr[rst-click-row] a[href$="/delete"]`)), |
| 80 | chromedp.Sleep(800*time.Millisecond), |
| 81 | locationInto(&del), |
| 82 | ) |
| 83 | if err != nil { |
| 84 | t.Fatalf("rows: %v", err) |
| 85 | } |
| 86 | if !strings.Contains(home, "/documents/") || strings.HasSuffix(home, "/delete") { |
| 87 | t.Errorf("recently updated: a click on the row went to %s", home) |
| 88 | } |
| 89 | if !strings.Contains(docs, "/documents/") || strings.HasSuffix(docs, "/delete") || strings.HasSuffix(docs, "/documents") { |
| 90 | t.Errorf("documents row: a click on the row went to %s", docs) |
| 91 | } |
| 92 | if !strings.HasSuffix(del, "/delete") { |
| 93 | t.Errorf("Delete: a click on it went to %s", del) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Clear (Paul, 2026-10-31: "there should be a way to clear search"): the |
| 98 | // ✕ empties the box and keeps the caret in it, without leaving the page, |
| 99 | // and shows only while the box holds something. |
| 100 | // |
| 101 | // MUTATION-TESTED 2026-10-31, each reverted: the click handler removed |
| 102 | // (the link then goes to the front page) → "left the page"; syncClear |
| 103 | // not wired to typing → "while typing". |
| 104 | func TestClearEmptiesTheBox(t *testing.T) { |
| 105 | rig := harness.New(t, func(origin string) http.Handler { |
| 106 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 107 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 108 | if err != nil { |
| 109 | t.Fatalf("db.Open: %v", err) |
| 110 | } |
| 111 | t.Cleanup(func() { d.Close() }) |
| 112 | mux, _, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| 113 | if err != nil { |
| 114 | t.Fatalf("AppForTest: %v", err) |
| 115 | } |
| 116 | return mux |
| 117 | }) |
| 118 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 119 | defer cancel() |
| 120 | in := `input[type="search"]` |
| 121 | var value, loc string |
| 122 | var hiddenAfter, hiddenEmpty, shownTyping, focused bool |
| 123 | err := chromedp.Do(ctx, |
| 124 | chromedp.Navigate(rig.Origin+"/?q=office"), |
| 125 | chromedp.WaitVisible(chromedp.CSS(`[rst-search-clear]`)), |
| 126 | chromedp.Click(chromedp.CSS(`[rst-search-clear]`)), |
| 127 | chromedp.Sleep(400*time.Millisecond), |
| 128 | valueInto(chromedp.CSS(in), &value), |
| 129 | locationInto(&loc), |
| 130 | evalInto(&hiddenAfter, `document.querySelector("[rst-search-clear]").hidden`), |
| 131 | evalInto(&focused, `document.activeElement === document.querySelector('input[type="search"]')`), |
| 132 | chromedp.Navigate(rig.Origin+"/"), |
| 133 | chromedp.WaitVisible(chromedp.CSS(in)), |
| 134 | evalInto(&hiddenEmpty, `document.querySelector("[rst-search-clear]").hidden`), |
| 135 | chromedp.SendKeys(chromedp.CSS(in), "ab"), |
| 136 | evalInto(&shownTyping, `!document.querySelector("[rst-search-clear]").hidden`), |
| 137 | ) |
| 138 | if err != nil { |
| 139 | t.Fatalf("clear: %v", err) |
| 140 | } |
| 141 | if value != "" || !hiddenAfter || !focused { |
| 142 | t.Errorf("after ✕: value %q, link hidden %v, caret in the box %v", value, hiddenAfter, focused) |
| 143 | } |
| 144 | if !strings.Contains(loc, "?q=office") { |
| 145 | t.Errorf("left the page: ✕ went to %s", loc) |
| 146 | } |
| 147 | if !hiddenEmpty { |
| 148 | t.Error("an empty box shows ✕") |
| 149 | } |
| 150 | if !shownTyping { |
| 151 | t.Error("while typing: ✕ did not appear") |
| 152 | } |
| 153 | } |
| 154 | |