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, "