package fichastest import ( "html" "net/http" "net/url" "regexp" "strings" "testing" ) var cardHref = regexp.MustCompile(`/cards/(\d+)"`) // createACustomer walks the real form flow and returns the new card's path. func createACustomer(t *testing.T, h http.Handler, name string) string { t.Helper() rec := post(t, h, "/cards", url.Values{ "f_name": {name}, "f_email": {"ana@example.com"}, "f_stage": {"Lead"}, }) if rec.Code != http.StatusSeeOther { t.Fatalf("POST /cards: status %d, want 303\n%s", rec.Code, rec.Body.String()) } loc := rec.Header().Get("Location") if !strings.HasPrefix(loc, "/cards/") { t.Fatalf("POST /cards redirected to %q", loc) } return loc } func TestCreateShowAndLogACall(t *testing.T) { h := newApp(t) path := createACustomer(t, h, "Cnoc Teoranta") rec := get(t, h, path) if rec.Code != http.StatusOK { t.Fatalf("GET %s: status %d", path, rec.Code) } body := rec.Body.String() if !strings.Contains(body, "Cnoc Teoranta") { t.Errorf("the card does not show its own name:\n%s", body) } if !strings.Contains(body, "No calls logged") { t.Error("a card with no entries has no empty state for them") } rec = post(t, h, path+"/entries", url.Values{ "f_body": {"talked about the renewal"}, "f_occurred_at": {"2026-09-21"}, }) if rec.Code != http.StatusSeeOther { t.Fatalf("POST entries: status %d\n%s", rec.Code, rec.Body.String()) } body = get(t, h, path).Body.String() if !strings.Contains(body, "talked about the renewal") { t.Errorf("the logged call does not appear on the card:\n%s", body) } // And it is on the list, by name. if !strings.Contains(get(t, h, "/").Body.String(), "Cnoc Teoranta") { t.Error("a created customer does not appear on the index") } } func TestUpdateACustomer(t *testing.T) { h := newApp(t) path := createACustomer(t, h, "Cnoc Teoranta") rec := post(t, h, path, url.Values{ "f_name": {"Cnoc Teoranta"}, "f_stage": {"Proposal"}, "f_notes": {"renewal is due in March"}, }) if rec.Code != http.StatusSeeOther { t.Fatalf("POST %s: status %d\n%s", path, rec.Code, rec.Body.String()) } body := get(t, h, path).Body.String() if !strings.Contains(body, "renewal is due in March") { t.Errorf("the update did not take:\n%s", body) } } // A destructive action gets its own URL and a confirm page, never a modal // and never a bare link that acts (AGENTS.md). func TestDeleteIsConfirmedOnItsOwnPage(t *testing.T) { h := newApp(t) path := createACustomer(t, h, "Cnoc Teoranta") rec := get(t, h, path+"/delete") if rec.Code != http.StatusOK { t.Fatalf("GET %s/delete: status %d", path, rec.Code) } if !strings.Contains(rec.Body.String(), "Delete this customer") { t.Error("the confirm page does not say what it will do") } if rec := post(t, h, path+"/delete", url.Values{}); rec.Code != http.StatusSeeOther { t.Fatalf("POST delete: status %d", rec.Code) } if got := get(t, h, path).Code; got != http.StatusNotFound { t.Errorf("a deleted card still renders: status %d", got) } if strings.Contains(get(t, h, "/").Body.String(), "Cnoc Teoranta") { t.Error("a deleted card is still on the index") } } // A card that is not there is 404, never 403 — the suite's rule that a row // you cannot have is a row that does not exist. func TestAMissingCardIs404(t *testing.T) { h := newApp(t) for _, p := range []string{"/cards/9999", "/cards/nonsense", "/cards/0", "/cards/-1"} { if got := get(t, h, p).Code; got != http.StatusNotFound { t.Errorf("GET %s: status %d, want 404", p, got) } } } // Everything works with scripts off, so every mutation is a form POST and // no page depends on a script to be usable. func TestNoPageNeedsJavaScript(t *testing.T) { h := newApp(t) path := createACustomer(t, h, "Cnoc Teoranta") for _, p := range []string{"/", "/cards/new", path, path + "/delete"} { body := get(t, h, p).Body.String() if strings.Contains(body, "]*>` + label + ``).FindStringSubmatch(body) if m == nil { t.Fatalf("no %s tab", label) } return html.UnescapeString(m[1]) } table := get(t, h, "/").Body.String() if !strings.Contains(table, "fichas-grid") || strings.Contains(table, "fichas-card-grid") { t.Fatal("the default view is not the table") } cards := get(t, h, tab(table, "Cards")).Body.String() if !strings.Contains(cards, "fichas-card-grid") { t.Fatal("the Cards tab does not show cards") } back := get(t, h, tab(cards, "Table")).Body.String() if !strings.Contains(back, "fichas-grid") || strings.Contains(back, "fichas-card-grid") { t.Fatal("the Table tab does not show the table") } } // ⌘↩ submits from a textarea. The behaviour is the browser's; what can be // pinned here is that every app page carries the script and it is served. func TestTheSubmitShortcutIsWiredIn(t *testing.T) { h := newApp(t) path := createACustomer(t, h, "Cnoc Teoranta") for _, p := range []string{"/", path, path + "/edit"} { body := get(t, h, p).Body.String() m := regexp.MustCompile(`/static/shortcuts\.[0-9a-f]{16}\.js`).FindString(body) if m == "" { t.Errorf("%s does not load shortcuts.js", p) continue } if rec := get(t, h, m); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "requestSubmit") { t.Errorf("%s: shortcuts.js is not served", m) } } } // Search, filter and sort through the real page. The index's own tests pin // the semantics; these pin that the page wires them up and keeps each state // a link. func TestSearchFilterAndSortOnTheList(t *testing.T) { h := newApp(t) for _, c := range []struct{ name, stage string }{ {"Brightwater Labs", "Proposal"}, {"Cedar & Pike", "Lead"}, {"Halden Robotics", "Won"}, } { rec := post(t, h, "/cards", url.Values{"f_name": {c.name}, "f_stage": {c.stage}}) if rec.Code != http.StatusSeeOther { t.Fatalf("creating %s: %d", c.name, rec.Code) } } body := get(t, h, "/?q=bright").Body.String() if !strings.Contains(body, "Brightwater Labs") || strings.Contains(body, "Halden Robotics") { t.Error("search did not narrow the list") } if !strings.Contains(body, `value="bright"`) { t.Error("the search box does not show the current search") } body = get(t, h, "/?f_stage=Won").Body.String() if !strings.Contains(body, "Halden Robotics") || strings.Contains(body, "Brightwater Labs") { t.Error("the Stage filter did not apply") } if !strings.Contains(body, "Stage: Won") { t.Error("the filter menu does not say which filter is on") } body = get(t, h, "/?sort=f_name").Body.String() if a, c := strings.Index(body, "Brightwater Labs"), strings.Index(body, "Halden Robotics"); a < 0 || c < 0 || a > c { t.Error("sorting by name did not order the rows") } if !strings.Contains(body, `aria-sort="ascending"`) { t.Error("the sorted column does not say so to assistive technology") } body = get(t, h, "/?q=nothingmatchesthis").Body.String() if !strings.Contains(body, "Nothing matches") || !strings.Contains(body, "Clear the search and filters") { t.Error("a search with no results has no way back") } if !strings.Contains(body, "data-live-results") { t.Error("the results region search.js swaps is missing") } }