//go:build browser package memoriatest import ( "context" "io" "log/slog" "net/http" "path/filepath" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" memoria "amadan.net/oficina/memoria/internal/memoria" ) // A list row with a link opens as a whole (Paul, 2026-10-31: "the link // should fill the whole list item"): a click on a row's empty part goes // where its link goes, and a Delete in the same row is still a Delete. // // MUTATION-TESTED 2026-10-31, each reverted: the ::after stretch removed // → "documents row"; the front page's row not rastrillo's list row // (rst-row-main dropped) → "recently updated"; Delete not lifted above // the stretch → "Delete". The first version named its rows rst-row, which // is rastrillo's own list row: its control-lifting rule kept Delete // working whatever this app's CSS said, and the Delete mutation stayed // green until the names were Memoria's own. func TestAWholeRowOpens(t *testing.T) { 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) } 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 { t.Fatal(err) } return mux }) ctx, cancel := context.WithTimeout(rig.Context(), 40*time.Second) defer cancel() // clickAt clicks a point inside the element, near its right edge: // past the link's own text, on the row's empty part. clickRight := func(sel string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { var box []float64 if _, err := evalInto(&box, `(function(){var r=document.querySelector(`+"`"+sel+"`"+`).getBoundingClientRect();return [r.right-8, r.top+r.height/2]})()`)(ctx, t); err != nil { return err } _, err := chromedp.MouseClickXY(box[0], box[1])(ctx, t) return err }) } var home, docs, del string err := chromedp.Do(ctx, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`[rst-home-list] li`)), clickRight(`[rst-home-list] li`), chromedp.Sleep(800*time.Millisecond), locationInto(&home), chromedp.Navigate(rig.Origin+"/documents"), chromedp.WaitVisible(chromedp.CSS(`tr[rst-click-row]`)), // The Updated cell: neither the title nor Delete. clickRight(`tr[rst-click-row] td:nth-child(4)`), chromedp.Sleep(800*time.Millisecond), locationInto(&docs), chromedp.Navigate(rig.Origin+"/documents"), chromedp.WaitVisible(chromedp.CSS(`tr[rst-click-row]`)), chromedp.Click(chromedp.CSS(`tr[rst-click-row] a[href$="/delete"]`)), chromedp.Sleep(800*time.Millisecond), locationInto(&del), ) if err != nil { t.Fatalf("rows: %v", err) } if !strings.Contains(home, "/documents/") || strings.HasSuffix(home, "/delete") { t.Errorf("recently updated: a click on the row went to %s", home) } if !strings.Contains(docs, "/documents/") || strings.HasSuffix(docs, "/delete") || strings.HasSuffix(docs, "/documents") { t.Errorf("documents row: a click on the row went to %s", docs) } if !strings.HasSuffix(del, "/delete") { t.Errorf("Delete: a click on it went to %s", del) } } // Clear (Paul, 2026-10-31: "there should be a way to clear search"): the // ✕ empties the box and keeps the caret in it, without leaving the page, // and shows only while the box holds something. // // MUTATION-TESTED 2026-10-31, each reverted: the click handler removed // (the link then goes to the front page) → "left the page"; syncClear // not wired to typing → "while typing". func TestClearEmptiesTheBox(t *testing.T) { 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, _, err := memoria.AppForTest(d, origin, testDEK(t), logger) if err != nil { t.Fatalf("AppForTest: %v", err) } return mux }) ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) defer cancel() in := `input[type="search"]` var value, loc string var hiddenAfter, hiddenEmpty, shownTyping, focused bool err := chromedp.Do(ctx, chromedp.Navigate(rig.Origin+"/?q=office"), chromedp.WaitVisible(chromedp.CSS(`[rst-search-clear]`)), chromedp.Click(chromedp.CSS(`[rst-search-clear]`)), chromedp.Sleep(400*time.Millisecond), valueInto(chromedp.CSS(in), &value), locationInto(&loc), evalInto(&hiddenAfter, `document.querySelector("[rst-search-clear]").hidden`), evalInto(&focused, `document.activeElement === document.querySelector('input[type="search"]')`), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(in)), evalInto(&hiddenEmpty, `document.querySelector("[rst-search-clear]").hidden`), chromedp.SendKeys(chromedp.CSS(in), "ab"), evalInto(&shownTyping, `!document.querySelector("[rst-search-clear]").hidden`), ) if err != nil { t.Fatalf("clear: %v", err) } if value != "" || !hiddenAfter || !focused { t.Errorf("after ✕: value %q, link hidden %v, caret in the box %v", value, hiddenAfter, focused) } if !strings.Contains(loc, "?q=office") { t.Errorf("left the page: ✕ went to %s", loc) } if !hiddenEmpty { t.Error("an empty box shows ✕") } if !shownTyping { t.Error("while typing: ✕ did not appear") } }