oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichastest
2
3import (
4 "html"
5 "net/http"
6 "net/url"
7 "regexp"
8 "strings"
9 "testing"
10)
11
12var cardHref = regexp.MustCompile(`/cards/(\d+)"`)
13
14// createACustomer walks the real form flow and returns the new card's path.
15func createACustomer(t *testing.T, h http.Handler, name string) string {
16 t.Helper()
17 rec := post(t, h, "/cards", url.Values{
18 "f_name": {name},
19 "f_email": {"ana@example.com"},
20 "f_stage": {"Lead"},
21 })
22 if rec.Code != http.StatusSeeOther {
23 t.Fatalf("POST /cards: status %d, want 303\n%s", rec.Code, rec.Body.String())
24 }
25 loc := rec.Header().Get("Location")
26 if !strings.HasPrefix(loc, "/cards/") {
27 t.Fatalf("POST /cards redirected to %q", loc)
28 }
29 return loc
30}
31
32func TestCreateShowAndLogACall(t *testing.T) {
33 h := newApp(t)
34
35 path := createACustomer(t, h, "Cnoc Teoranta")
36
37 rec := get(t, h, path)
38 if rec.Code != http.StatusOK {
39 t.Fatalf("GET %s: status %d", path, rec.Code)
40 }
41 body := rec.Body.String()
42 if !strings.Contains(body, "Cnoc Teoranta") {
43 t.Errorf("the card does not show its own name:\n%s", body)
44 }
45 if !strings.Contains(body, "No calls logged") {
46 t.Error("a card with no entries has no empty state for them")
47 }
48
49 rec = post(t, h, path+"/entries", url.Values{
50 "f_body": {"talked about the renewal"},
51 "f_occurred_at": {"2026-09-21"},
52 })
53 if rec.Code != http.StatusSeeOther {
54 t.Fatalf("POST entries: status %d\n%s", rec.Code, rec.Body.String())
55 }
56
57 body = get(t, h, path).Body.String()
58 if !strings.Contains(body, "talked about the renewal") {
59 t.Errorf("the logged call does not appear on the card:\n%s", body)
60 }
61
62 // And it is on the list, by name.
63 if !strings.Contains(get(t, h, "/").Body.String(), "Cnoc Teoranta") {
64 t.Error("a created customer does not appear on the index")
65 }
66}
67
68func TestUpdateACustomer(t *testing.T) {
69 h := newApp(t)
70 path := createACustomer(t, h, "Cnoc Teoranta")
71
72 rec := post(t, h, path, url.Values{
73 "f_name": {"Cnoc Teoranta"},
74 "f_stage": {"Proposal"},
75 "f_notes": {"renewal is due in March"},
76 })
77 if rec.Code != http.StatusSeeOther {
78 t.Fatalf("POST %s: status %d\n%s", path, rec.Code, rec.Body.String())
79 }
80 body := get(t, h, path).Body.String()
81 if !strings.Contains(body, "renewal is due in March") {
82 t.Errorf("the update did not take:\n%s", body)
83 }
84}
85
86// A destructive action gets its own URL and a confirm page, never a modal
87// and never a bare link that acts (AGENTS.md).
88func TestDeleteIsConfirmedOnItsOwnPage(t *testing.T) {
89 h := newApp(t)
90 path := createACustomer(t, h, "Cnoc Teoranta")
91
92 rec := get(t, h, path+"/delete")
93 if rec.Code != http.StatusOK {
94 t.Fatalf("GET %s/delete: status %d", path, rec.Code)
95 }
96 if !strings.Contains(rec.Body.String(), "Delete this customer") {
97 t.Error("the confirm page does not say what it will do")
98 }
99
100 if rec := post(t, h, path+"/delete", url.Values{}); rec.Code != http.StatusSeeOther {
101 t.Fatalf("POST delete: status %d", rec.Code)
102 }
103 if got := get(t, h, path).Code; got != http.StatusNotFound {
104 t.Errorf("a deleted card still renders: status %d", got)
105 }
106 if strings.Contains(get(t, h, "/").Body.String(), "Cnoc Teoranta") {
107 t.Error("a deleted card is still on the index")
108 }
109}
110
111// A card that is not there is 404, never 403 — the suite's rule that a row
112// you cannot have is a row that does not exist.
113func TestAMissingCardIs404(t *testing.T) {
114 h := newApp(t)
115 for _, p := range []string{"/cards/9999", "/cards/nonsense", "/cards/0", "/cards/-1"} {
116 if got := get(t, h, p).Code; got != http.StatusNotFound {
117 t.Errorf("GET %s: status %d, want 404", p, got)
118 }
119 }
120}
121
122// Everything works with scripts off, so every mutation is a form POST and
123// no page depends on a script to be usable.
124func TestNoPageNeedsJavaScript(t *testing.T) {
125 h := newApp(t)
126 path := createACustomer(t, h, "Cnoc Teoranta")
127 for _, p := range []string{"/", "/cards/new", path, path + "/delete"} {
128 body := get(t, h, p).Body.String()
129 if strings.Contains(body, "<noscript") {
130 continue
131 }
132 if strings.Contains(body, "onclick=") || strings.Contains(body, "javascript:") {
133 t.Errorf("%s carries inline script behaviour", p)
134 }
135 if strings.Contains(body, "<form") && !strings.Contains(body, "method=\"post\"") {
136 t.Errorf("%s has a form that does not POST", p)
137 }
138 }
139}
140
141// The card is always editable (level-up §5.1): every field a quiet input
142// carrying what it was drawn with, so a save never crosses a teammate's
143// (§5.2). It used to be a read view with an Edit page; the Edit page
144// stays, as the scriptless cell's target (§5.7).
145func TestTheCardIsAlwaysEditable(t *testing.T) {
146 h := newApp(t)
147 path := createACustomer(t, h, "Cnoc Teoranta")
148 body := get(t, h, path).Body.String()
149 for _, field := range []string{`name="f_name"`, `name="f_email"`, `name="f_stage"`, `name="was.f_name" value="Cnoc Teoranta"`, `name="was.f_email"`} {
150 if !strings.Contains(body, field) {
151 t.Errorf("the card has no %s", field)
152 }
153 }
154 if !strings.Contains(body, `href="`+path+`/history"`) {
155 t.Error("the card does not link to its History")
156 }
157 // Logging a call stays on the card: it is adding, not editing.
158 if !strings.Contains(body, `name="f_body"`) {
159 t.Error("the card lost the log-a-call form")
160 }
161}
162
163func TestEditingHasItsOwnPage(t *testing.T) {
164 h := newApp(t)
165 path := createACustomer(t, h, "Cnoc Teoranta")
166 rec := get(t, h, path+"/edit")
167 if rec.Code != http.StatusOK {
168 t.Fatalf("GET %s/edit: status %d", path, rec.Code)
169 }
170 if !strings.Contains(rec.Body.String(), `value="Cnoc Teoranta"`) {
171 t.Error("the edit form does not carry the current value")
172 }
173 // Saving lands back on the read view, not on the form.
174 save := post(t, h, path, url.Values{"f_name": {"Cnoc Teoranta"}, "f_notes": {"renewal is due in March"}})
175 if loc := save.Header().Get("Location"); !strings.HasPrefix(loc, path+"?saved=") {
176 t.Errorf("saving redirected to %q, want the card with Saved. Undo", loc)
177 }
178}
179
180// Table and Cards are two views of one list, switched by a link so it works
181// with scripts off and can be bookmarked. The test follows the tabs' own
182// links rather than matching a URL spelling.
183func TestTheListHasTableAndCardsViews(t *testing.T) {
184 h := newApp(t)
185 createACustomer(t, h, "Cnoc Teoranta")
186 tab := func(body, label string) string {
187 m := regexp.MustCompile(`<a href="([^"]*)"[^>]*>` + label + `</a>`).FindStringSubmatch(body)
188 if m == nil {
189 t.Fatalf("no %s tab", label)
190 }
191 return html.UnescapeString(m[1])
192 }
193 table := get(t, h, "/").Body.String()
194 if !strings.Contains(table, "fichas-grid") || strings.Contains(table, "fichas-card-grid") {
195 t.Fatal("the default view is not the table")
196 }
197 cards := get(t, h, tab(table, "Cards")).Body.String()
198 if !strings.Contains(cards, "fichas-card-grid") {
199 t.Fatal("the Cards tab does not show cards")
200 }
201 back := get(t, h, tab(cards, "Table")).Body.String()
202 if !strings.Contains(back, "fichas-grid") || strings.Contains(back, "fichas-card-grid") {
203 t.Fatal("the Table tab does not show the table")
204 }
205}
206
207// ⌘↩ submits from a textarea. The behaviour is the browser's; what can be
208// pinned here is that every app page carries the script and it is served.
209func TestTheSubmitShortcutIsWiredIn(t *testing.T) {
210 h := newApp(t)
211 path := createACustomer(t, h, "Cnoc Teoranta")
212 for _, p := range []string{"/", path, path + "/edit"} {
213 body := get(t, h, p).Body.String()
214 m := regexp.MustCompile(`/static/shortcuts\.[0-9a-f]{16}\.js`).FindString(body)
215 if m == "" {
216 t.Errorf("%s does not load shortcuts.js", p)
217 continue
218 }
219 if rec := get(t, h, m); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "requestSubmit") {
220 t.Errorf("%s: shortcuts.js is not served", m)
221 }
222 }
223}
224
225// Search, filter and sort through the real page. The index's own tests pin
226// the semantics; these pin that the page wires them up and keeps each state
227// a link.
228func TestSearchFilterAndSortOnTheList(t *testing.T) {
229 h := newApp(t)
230 for _, c := range []struct{ name, stage string }{
231 {"Brightwater Labs", "Proposal"}, {"Cedar & Pike", "Lead"}, {"Halden Robotics", "Won"},
232 } {
233 rec := post(t, h, "/cards", url.Values{"f_name": {c.name}, "f_stage": {c.stage}})
234 if rec.Code != http.StatusSeeOther {
235 t.Fatalf("creating %s: %d", c.name, rec.Code)
236 }
237 }
238
239 body := get(t, h, "/?q=bright").Body.String()
240 if !strings.Contains(body, "Brightwater Labs") || strings.Contains(body, "Halden Robotics") {
241 t.Error("search did not narrow the list")
242 }
243 if !strings.Contains(body, `value="bright"`) {
244 t.Error("the search box does not show the current search")
245 }
246
247 body = get(t, h, "/?f_stage=Won").Body.String()
248 if !strings.Contains(body, "Halden Robotics") || strings.Contains(body, "Brightwater Labs") {
249 t.Error("the Stage filter did not apply")
250 }
251 if !strings.Contains(body, "Stage: Won") {
252 t.Error("the filter menu does not say which filter is on")
253 }
254
255 body = get(t, h, "/?sort=f_name").Body.String()
256 if a, c := strings.Index(body, "Brightwater Labs"), strings.Index(body, "Halden Robotics"); a < 0 || c < 0 || a > c {
257 t.Error("sorting by name did not order the rows")
258 }
259 if !strings.Contains(body, `aria-sort="ascending"`) {
260 t.Error("the sorted column does not say so to assistive technology")
261 }
262
263 body = get(t, h, "/?q=nothingmatchesthis").Body.String()
264 if !strings.Contains(body, "Nothing matches") || !strings.Contains(body, "Clear the search and filters") {
265 t.Error("a search with no results has no way back")
266 }
267 if !strings.Contains(body, "data-live-results") {
268 t.Error("the results region search.js swaps is missing")
269 }
270}
271