| 1 | package fichastest |
| 2 | |
| 3 | // Editing in place over HTTP (level-up §5): the one-field route with and |
| 4 | // without script, the edit form's was and its conflict page, Undo, the |
| 5 | // card beside the list, History, and gates LU5 and LU17. |
| 6 | |
| 7 | import ( |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "log/slog" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "net/url" |
| 15 | "path/filepath" |
| 16 | "sort" |
| 17 | "strconv" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "amadan.net/rastrillo/rastrillo/db" |
| 22 | |
| 23 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 24 | ) |
| 25 | |
| 26 | // editRig is the app, its database and a customer type with a Number |
| 27 | // field ("Budget"), which the catalogue's Customer does not have. |
| 28 | type editRig struct { |
| 29 | h http.Handler |
| 30 | d *db.DB |
| 31 | cfg fichas.Config |
| 32 | budget string // the Budget field's key |
| 33 | } |
| 34 | |
| 35 | func newEditRig(t *testing.T) editRig { |
| 36 | t.Helper() |
| 37 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 38 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 39 | if err != nil { |
| 40 | t.Fatalf("db.Open: %v", err) |
| 41 | } |
| 42 | t.Cleanup(func() { d.Close() }) |
| 43 | cfg := testConfig(t) |
| 44 | h, err := fichas.AppForTest(d, cfg, logger, TestOwner) |
| 45 | if err != nil { |
| 46 | t.Fatalf("AppForTest: %v", err) |
| 47 | } |
| 48 | m, err := fichas.EnsureMember(t.Context(), d.G, cfg.Sealer, TestOwner) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | fd, err := fichas.AddField(t.Context(), d.G, cfg.Sealer, fichas.TypeCustomer, fichas.Person(m.ID), |
| 53 | fichas.FieldSpec{Label: "Budget", Kind: fichas.KindNumber, Tier: fichas.TierSealed}) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | return editRig{h: h, d: d, cfg: cfg, budget: fd.Key} |
| 58 | } |
| 59 | |
| 60 | func (r editRig) card(t *testing.T, values map[string]string) int64 { |
| 61 | t.Helper() |
| 62 | m, _ := fichas.EnsureMember(t.Context(), r.d.G, r.cfg.Sealer, TestOwner) |
| 63 | c, err := fichas.CreateCard(t.Context(), r.d.G, r.cfg.Sealer, fichas.TypeCustomer, fichas.Person(m.ID), values) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | return c.ID |
| 68 | } |
| 69 | |
| 70 | func (r editRig) value(t *testing.T, id int64, key string) string { |
| 71 | t.Helper() |
| 72 | c, err := fichas.LoadCard(t.Context(), r.d.G, r.cfg.Sealer, id) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | return c.Field(key).Value |
| 77 | } |
| 78 | |
| 79 | // postJSON is a script's post: form-encoded, JSON back (§6.8). |
| 80 | func postJSON(t *testing.T, h http.Handler, target string, form url.Values) (int, map[string]any) { |
| 81 | t.Helper() |
| 82 | req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode())) |
| 83 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 84 | req.Header.Set("Accept", "application/json") |
| 85 | req.Header.Set("Origin", testOrigin) |
| 86 | rec := httptest.NewRecorder() |
| 87 | h.ServeHTTP(rec, req) |
| 88 | var out map[string]any |
| 89 | _ = json.Unmarshal(rec.Body.Bytes(), &out) |
| 90 | return rec.Code, out |
| 91 | } |
| 92 | |
| 93 | func getAs(t *testing.T, h http.Handler, target, lang string) *httptest.ResponseRecorder { |
| 94 | t.Helper() |
| 95 | req := httptest.NewRequest(http.MethodGet, target, nil) |
| 96 | req.Header.Set("Accept-Language", lang) |
| 97 | rec := httptest.NewRecorder() |
| 98 | h.ServeHTTP(rec, req) |
| 99 | return rec |
| 100 | } |
| 101 | |
| 102 | func cardPath(id int64) string { return "/cards/" + strconv.FormatInt(id, 10) } |
| 103 | |
| 104 | func TestAFieldSavesAtOnceWithScript(t *testing.T) { |
| 105 | r := newEditRig(t) |
| 106 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead"}) |
| 107 | code, out := postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{ |
| 108 | fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}, |
| 109 | }) |
| 110 | if code != 200 || out["message"] != "Stage changed to Won." || out["event"].(float64) == 0 || !strings.Contains(out["cell"].(string), "Won") { |
| 111 | t.Fatalf("a script's edit: %d %v", code, out) |
| 112 | } |
| 113 | if r.value(t, id, fichas.FieldStage) != "Won" { |
| 114 | t.Fatal("the edit did not land") |
| 115 | } |
| 116 | // A teammate's stale editor: 409, naming who and what, writing nothing. |
| 117 | code, out = postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{ |
| 118 | fichas.FieldStage: {"Lost"}, "was." + fichas.FieldStage: {"Lead"}, |
| 119 | }) |
| 120 | if code != http.StatusConflict || out["message"] != "You changed this to Won in another tab." || out["theirs"] != "Won" { |
| 121 | t.Fatalf("a stale edit: %d %v", code, out) |
| 122 | } |
| 123 | // Unknown fields and cards are the app's 404. |
| 124 | if code, _ := postJSON(t, r.h, cardPath(id)+"/fields/f_nosuch", url.Values{"f_nosuch": {"x"}}); code != 404 { |
| 125 | t.Errorf("an unknown field: %d", code) |
| 126 | } |
| 127 | if code, _ := postJSON(t, r.h, "/cards/99999/fields/"+fichas.FieldStage, url.Values{fichas.FieldStage: {"Won"}}); code != 404 { |
| 128 | t.Errorf("an unknown card: %d", code) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestAFieldSavesWithoutScriptAndComesBackWithUndo(t *testing.T) { |
| 133 | r := newEditRig(t) |
| 134 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc"}) |
| 135 | rec := post(t, r.h, cardPath(id)+"/fields/"+fichas.FieldEmail, url.Values{ |
| 136 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, "return_to": {"/t/t_customer"}, |
| 137 | }) |
| 138 | loc := rec.Header().Get("Location") |
| 139 | if rec.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/t/t_customer?saved=") { |
| 140 | t.Fatalf("a scriptless edit: %d %q", rec.Code, loc) |
| 141 | } |
| 142 | page := get(t, r.h, loc).Body.String() |
| 143 | event := strings.TrimPrefix(loc, "/t/t_customer?saved=") |
| 144 | if !strings.Contains(page, "Saved.") || !strings.Contains(page, `name="event" value="`+event+`"`) || !strings.Contains(page, `action="`+cardPath(id)+`/undo"`) { |
| 145 | t.Fatal("the list does not say Saved. with an Undo form") |
| 146 | } |
| 147 | undo := post(t, r.h, cardPath(id)+"/undo", url.Values{"event": {event}, "return_to": {"/t/t_customer"}}) |
| 148 | if undo.Code != http.StatusSeeOther || r.value(t, id, fichas.FieldEmail) != "" { |
| 149 | t.Fatalf("the scriptless Undo: %d, email %q", undo.Code, r.value(t, id, fichas.FieldEmail)) |
| 150 | } |
| 151 | if !strings.Contains(get(t, r.h, undo.Header().Get("Location")).Body.String(), "Undone.") { |
| 152 | t.Fatal("the Undo does not come back saying so") |
| 153 | } |
| 154 | // A crafted return_to goes nowhere else. |
| 155 | away := post(t, r.h, cardPath(id)+"/fields/"+fichas.FieldEmail, url.Values{ |
| 156 | fichas.FieldEmail: {"c@d.example"}, "was." + fichas.FieldEmail: {""}, "return_to": {"//evil.example/x"}, |
| 157 | }) |
| 158 | if loc := away.Header().Get("Location"); !strings.HasPrefix(loc, cardPath(id)+"?") { |
| 159 | t.Fatalf("return_to //evil.example went to %q", loc) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // LU4 for the edit form: all or nothing, with theirs and mine (§5.2). |
| 164 | func TestTheEditFormsConflictPageOffersTheirsAndMine(t *testing.T) { |
| 165 | r := newEditRig(t) |
| 166 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead"}) |
| 167 | // Someone else moves the stage on. |
| 168 | postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}}) |
| 169 | rec := post(t, r.h, cardPath(id), url.Values{ |
| 170 | fichas.FieldName: {"Cnoc"}, "was." + fichas.FieldName: {"Cnoc"}, |
| 171 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, |
| 172 | fichas.FieldStage: {"Lost"}, "was." + fichas.FieldStage: {"Lead"}, |
| 173 | }) |
| 174 | body := rec.Body.String() |
| 175 | if rec.Code != http.StatusConflict { |
| 176 | t.Fatalf("a conflicting form: status %d", rec.Code) |
| 177 | } |
| 178 | if r.value(t, id, fichas.FieldEmail) != "" { |
| 179 | t.Fatal("the conflicting form wrote the field that did not conflict") |
| 180 | } |
| 181 | for _, want := range []string{ |
| 182 | `type="radio" name="f_stage" value="Won" checked`, `type="radio" name="f_stage" value="Lost"`, |
| 183 | `name="was.f_stage" value="Won"`, `value="a@b.example"`, |
| 184 | } { |
| 185 | if !strings.Contains(body, want) { |
| 186 | t.Errorf("the conflict page lacks %s", want) |
| 187 | } |
| 188 | } |
| 189 | // Use mine: the form posted again as it now stands. |
| 190 | again := post(t, r.h, cardPath(id), url.Values{ |
| 191 | fichas.FieldName: {"Cnoc"}, "was." + fichas.FieldName: {"Cnoc"}, |
| 192 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, |
| 193 | fichas.FieldStage: {"Lost"}, "was." + fichas.FieldStage: {"Won"}, |
| 194 | }) |
| 195 | if again.Code != http.StatusSeeOther || r.value(t, id, fichas.FieldStage) != "Lost" || r.value(t, id, fichas.FieldEmail) != "a@b.example" { |
| 196 | t.Fatalf("Use mine: %d", again.Code) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // §5.1: an edit that takes a card out of the list says so. |
| 201 | func TestAnEditThatLeavesTheViewSaysSo(t *testing.T) { |
| 202 | r := newEditRig(t) |
| 203 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead"}) |
| 204 | _, out := postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{ |
| 205 | fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}, "list": {"f_stage=Lead"}, |
| 206 | }) |
| 207 | if out["outOfView"] != true { |
| 208 | t.Fatalf("leaving a Stage is Lead list: %v", out) |
| 209 | } |
| 210 | _, out = postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldEmail, url.Values{ |
| 211 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, "list": {"f_stage=Won"}, |
| 212 | }) |
| 213 | if out["outOfView"] != false { |
| 214 | t.Fatalf("an edit that keeps the card in the list: %v", out) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestTheCardBesideTheListWalksTheWholeList(t *testing.T) { |
| 219 | r := newEditRig(t) |
| 220 | var ids []int64 |
| 221 | for i := 0; i < 102; i++ { |
| 222 | ids = append(ids, r.card(t, map[string]string{fichas.FieldName: fmt.Sprintf("Card %03d", i)})) |
| 223 | } |
| 224 | sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) |
| 225 | // Sorted by name, the 100th card's Next is the 101st, on page 2. |
| 226 | body := get(t, r.h, "/t/t_customer?sort=f_name&card="+strconv.FormatInt(ids[99], 10)).Body.String() |
| 227 | next := `rel="next" href="/t/t_customer?card=` + strconv.FormatInt(ids[100], 10) + `&sort=f_name"` |
| 228 | if !strings.Contains(body, next) || !strings.Contains(body, "100 of 102") { |
| 229 | i := strings.Index(body, `rel="next"`) |
| 230 | t.Fatalf("the 100th card's Next is not the 101st across the page: %.200s", body[max(i, 0):]) |
| 231 | } |
| 232 | page2 := get(t, r.h, "/t/t_customer?sort=f_name&card="+strconv.FormatInt(ids[100], 10)).Body.String() |
| 233 | if !strings.Contains(page2, `aria-current="true"`) || !strings.Contains(page2, "Card 100") || strings.Contains(page2, ">Card 000<") { |
| 234 | t.Fatal("the narrowed list did not move to the open card's page") |
| 235 | } |
| 236 | // A card not in the list opens, says so, and has no Next. |
| 237 | other := get(t, r.h, "/t/t_customer?q=nothing-matches&card="+strconv.FormatInt(ids[0], 10)).Body.String() |
| 238 | if !strings.Contains(other, "Not in this view") || strings.Contains(other, `rel="next"`) { |
| 239 | t.Fatal("a card outside the list does not say so") |
| 240 | } |
| 241 | if get(t, r.h, "/t/t_customer?card=999999").Code != 404 { |
| 242 | t.Fatal("an unknown card beside the list is not the app's 404") |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestHistoryShowsAndRestores(t *testing.T) { |
| 247 | r := newEditRig(t) |
| 248 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead"}) |
| 249 | postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}}) |
| 250 | body := get(t, r.h, cardPath(id)+"/history").Body.String() |
| 251 | if !strings.Contains(body, "changed Stage") || !strings.Contains(body, "Restore this value") { |
| 252 | t.Fatal("History does not show the change with Restore") |
| 253 | } |
| 254 | restore := post(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{ |
| 255 | fichas.FieldStage: {"Lead"}, "was." + fichas.FieldStage: {"Won"}, "return_to": {cardPath(id) + "/history"}, |
| 256 | }) |
| 257 | if restore.Code != http.StatusSeeOther || r.value(t, id, fichas.FieldStage) != "Lead" { |
| 258 | t.Fatalf("Restore: %d", restore.Code) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // LU17: drawn in the reader's format, never stored in it (§6.8). |
| 263 | func TestNumbersAreDrawnAndReadInTheReadersFormat(t *testing.T) { |
| 264 | r := newEditRig(t) |
| 265 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", r.budget: "1200.5"}) |
| 266 | de := getAs(t, r.h, "/t/t_customer?card="+strconv.FormatInt(id, 10), "de-DE,de;q=0.9").Body.String() |
| 267 | if !strings.Contains(de, `data-locale="de"`) || !strings.Contains(de, `value="1200,5"`) || !strings.Contains(de, `name="`+r.budget+`.locale" value="de"`) { |
| 268 | t.Fatal("a German reader's card does not seed 1200,5 with its locale") |
| 269 | } |
| 270 | list := getAs(t, r.h, "/", "de-DE").Body.String() |
| 271 | if !strings.Contains(list, `<span fichas-number>1200,5</span>`) || !strings.Contains(list, `data-seed="1200,5"`) || !strings.Contains(list, `data-was="1200.5"`) { |
| 272 | t.Fatal("a German reader's table does not draw 1200,5 over a canonical was") |
| 273 | } |
| 274 | if en := getAs(t, r.h, "/", "en").Body.String(); !strings.Contains(en, `<span fichas-number>1200.5</span>`) { |
| 275 | t.Fatal("an Irish reader's table does not draw 1200.5") |
| 276 | } |
| 277 | |
| 278 | // The same change from a German page and from an English page stores |
| 279 | // the same canonical value. |
| 280 | code, out := postJSON(t, r.h, cardPath(id)+"/fields/"+r.budget, url.Values{ |
| 281 | r.budget: {"1.300,25"}, "was." + r.budget: {"1200.5"}, r.budget + ".locale": {"de"}, |
| 282 | }) |
| 283 | if code != 200 || r.value(t, id, r.budget) != "1300.25" || out["seed"] != "1300,25" { |
| 284 | t.Fatalf("a German edit: %d %v, stored %q", code, out, r.value(t, id, r.budget)) |
| 285 | } |
| 286 | code, _ = postJSON(t, r.h, cardPath(id)+"/fields/"+r.budget, url.Values{ |
| 287 | r.budget: {"1,400.75"}, "was." + r.budget: {"1300.25"}, r.budget + ".locale": {"en-IE"}, |
| 288 | }) |
| 289 | if code != 200 || r.value(t, id, r.budget) != "1400.75" { |
| 290 | t.Fatalf("an English edit: %d, stored %q", code, r.value(t, id, r.budget)) |
| 291 | } |
| 292 | // An unchanged seed is no change, in any locale. |
| 293 | code, out = postJSON(t, r.h, cardPath(id)+"/fields/"+r.budget, url.Values{ |
| 294 | r.budget: {"1400,75"}, "was." + r.budget: {"1400.75"}, r.budget + ".locale": {"de"}, |
| 295 | }) |
| 296 | if code != 200 || out["event"].(float64) != 0 { |
| 297 | t.Fatalf("an unchanged German seed: %d %v", code, out) |
| 298 | } |
| 299 | // What is not the page's own convention is refused, and writes nothing. |
| 300 | code, out = postJSON(t, r.h, cardPath(id)+"/fields/"+r.budget, url.Values{ |
| 301 | r.budget: {"1,5"}, "was." + r.budget: {"1400.75"}, r.budget + ".locale": {"en-IE"}, |
| 302 | }) |
| 303 | if code != 422 || out["error"] != "Enter a number, like 1200.5." || r.value(t, id, r.budget) != "1400.75" { |
| 304 | t.Fatalf("1,5 on an English page: %d %v", code, out) |
| 305 | } |
| 306 | // A page that names no locale Fichas reads in cannot say what it meant. |
| 307 | for _, bad := range []string{"", "xx-YY", "de-DE"} { |
| 308 | code, out = postJSON(t, r.h, cardPath(id)+"/fields/"+r.budget, url.Values{ |
| 309 | r.budget: {"5"}, "was." + r.budget: {"1400.75"}, r.budget + ".locale": {bad}, |
| 310 | }) |
| 311 | if code != 422 || out["error"] != "Enter the value again." || r.value(t, id, r.budget) != "1400.75" { |
| 312 | t.Fatalf("locale echo %q: %d %v", bad, code, out) |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // LU5: a GET writes nothing. A logical dump of every table is the same |
| 318 | // before and after every page Phase 1 draws. |
| 319 | // |
| 320 | // Mutation, 2026-11-17: making showCard bump the card's revision (a |
| 321 | // write on a GET) failed this test at /cards/{id}. |
| 322 | func TestAGetWritesNothing(t *testing.T) { |
| 323 | r := newEditRig(t) |
| 324 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead", r.budget: "7"}) |
| 325 | _, out := postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldStage, url.Values{fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}}) |
| 326 | event := strconv.FormatInt(int64(out["event"].(float64)), 10) |
| 327 | before := dump(t, r.d) |
| 328 | for _, path := range []string{ |
| 329 | "/", "/t/t_customer", "/t/t_customer?view=cards", "/t/t_customer?card=" + strconv.FormatInt(id, 10), |
| 330 | "/t/t_customer?card=" + strconv.FormatInt(id, 10) + "&saved=" + event, "/t/t_customer?q=cnoc&f_stage=Won&sort=f_name", |
| 331 | cardPath(id), cardPath(id) + "/edit", cardPath(id) + "/edit?field=f_stage", cardPath(id) + "/history", |
| 332 | cardPath(id) + "?undone=" + event, "/t/t_customer?card=999999", |
| 333 | } { |
| 334 | for _, lang := range []string{"en-IE", "de-DE"} { |
| 335 | getAs(t, r.h, path, lang) |
| 336 | } |
| 337 | if after := dump(t, r.d); after != before { |
| 338 | t.Fatalf("GET %s wrote to the database", path) |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // dump is every row of every table, in a fixed order. |
| 344 | func dump(t *testing.T, d *db.DB) string { |
| 345 | t.Helper() |
| 346 | var tables []string |
| 347 | if err := d.G.Raw("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name").Scan(&tables).Error; err != nil { |
| 348 | t.Fatal(err) |
| 349 | } |
| 350 | var b strings.Builder |
| 351 | for _, table := range tables { |
| 352 | rows, err := d.G.Raw(`SELECT * FROM "` + table + `"`).Rows() |
| 353 | if err != nil { |
| 354 | t.Fatal(err) |
| 355 | } |
| 356 | cols, _ := rows.Columns() |
| 357 | var lines []string |
| 358 | for rows.Next() { |
| 359 | vals := make([]any, len(cols)) |
| 360 | ptrs := make([]any, len(cols)) |
| 361 | for i := range vals { |
| 362 | ptrs[i] = &vals[i] |
| 363 | } |
| 364 | if err := rows.Scan(ptrs...); err != nil { |
| 365 | t.Fatal(err) |
| 366 | } |
| 367 | lines = append(lines, fmt.Sprint(vals...)) |
| 368 | } |
| 369 | rows.Close() |
| 370 | sort.Strings(lines) |
| 371 | fmt.Fprintf(&b, "%s\n%s\n", table, strings.Join(lines, "\n")) |
| 372 | } |
| 373 | return b.String() |
| 374 | } |
| 375 | |
| 376 | // The rail's fold is a preference (§3.2, §9's named exemption): a |
| 377 | // cookie, no row, no event, and it returns only to a local path. |
| 378 | func TestTheFoldIsACookieOnly(t *testing.T) { |
| 379 | r := newEditRig(t) |
| 380 | before := dump(t, r.d) |
| 381 | rec := post(t, r.h, "/view/rail", url.Values{"rail": {"rail"}, "return_to": {"/t/t_customer?q=x"}}) |
| 382 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/t/t_customer?q=x" { |
| 383 | t.Fatalf("the fold: %d %q", rec.Code, rec.Header().Get("Location")) |
| 384 | } |
| 385 | if !strings.Contains(rec.Header().Get("Set-Cookie"), "fichas-rail=rail") { |
| 386 | t.Fatal("the fold set no cookie") |
| 387 | } |
| 388 | if dump(t, r.d) != before { |
| 389 | t.Fatal("the fold wrote to the database") |
| 390 | } |
| 391 | if loc := post(t, r.h, "/view/rail", url.Values{"rail": {"open"}, "return_to": {"https://evil.example/"}}).Header().Get("Location"); loc != "/" { |
| 392 | t.Fatalf("the fold returned to %q", loc) |
| 393 | } |
| 394 | folded := withCookies(t, r.h, "/", rec) |
| 395 | if !strings.Contains(folded, `<div fichas-app data-rail="rail">`) { |
| 396 | t.Fatal("a folded rail is not drawn folded") |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // LU17, which locale (§6.8, as Mapa's build note 85): the browser's |
| 401 | // Accept-Language among the six offered, a region falling to its |
| 402 | // language, another English to the team's; no path prefix and no |
| 403 | // rastrillo cookie. |
| 404 | // |
| 405 | // Mutation, 2026-11-17: negotiating with rastrillo's middleware rules |
| 406 | // (honouring the rastrillo_locale cookie) failed the cookie case. |
| 407 | func TestTheReadersLocaleComesFromTheBrowserOnly(t *testing.T) { |
| 408 | r := newEditRig(t) |
| 409 | r.card(t, map[string]string{fichas.FieldName: "Cnoc"}) |
| 410 | for header, want := range map[string]string{ |
| 411 | "de-AT,de;q=0.9": "de", "fr-CA": "fr", "en-AU": "en-IE", "en": "en-IE", "en-US,en;q=0.8": "en", |
| 412 | "ja,pt-BR": "en-IE", "": "en-IE", "es-MX;q=0.4, en-GB;q=0.9": "en-GB", |
| 413 | } { |
| 414 | if body := getAs(t, r.h, "/", header).Body.String(); !strings.Contains(body, `<body data-locale="`+want+`">`) { |
| 415 | t.Errorf("Accept-Language %q: want the %s format", header, want) |
| 416 | } |
| 417 | } |
| 418 | req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 419 | req.Header.Set("Accept-Language", "en-IE") |
| 420 | req.AddCookie(&http.Cookie{Name: "rastrillo_locale", Value: "de"}) |
| 421 | rec := httptest.NewRecorder() |
| 422 | r.h.ServeHTTP(rec, req) |
| 423 | if !strings.Contains(rec.Body.String(), `<body data-locale="en-IE">`) { |
| 424 | t.Error("rastrillo's locale cookie outranked the browser's header") |
| 425 | } |
| 426 | if got := getAs(t, r.h, "/de/t/t_customer", "de").Code; got != http.StatusNotFound { |
| 427 | t.Errorf("a /de/ prefix answered %d; it is no route", got) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // LU17, dates: a date and a moment are written as the reader's locale |
| 432 | // writes them, by Mapa's patterns, marked with its lang. |
| 433 | // |
| 434 | // Mutation, 2026-11-17: dropping dateLang's lang from the cell failed it. |
| 435 | func TestDatesAreWrittenInTheReadersLocale(t *testing.T) { |
| 436 | r := newEditRig(t) |
| 437 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldNextAction: "2030-03-04"}) |
| 438 | for lang, want := range map[string]string{ |
| 439 | "en-IE": "4 March 2030", "en-US": "March 4, 2030", "de": "4. März 2030", "fr": "4 mars 2030", "es": "4 de marzo de 2030", |
| 440 | } { |
| 441 | list := getAs(t, r.h, "/", lang).Body.String() |
| 442 | // The date is in the reader's locale's words, and says so (WCAG |
| 443 | // 3.1.2): lang is the locale's own code. |
| 444 | if !strings.Contains(list, `title="`+want+`" lang="`+lang+`">`+want+`</time>`) { |
| 445 | t.Errorf("%s: the table does not write the next action as %q", lang, want) |
| 446 | } |
| 447 | hist := getAs(t, r.h, cardPath(id)+"/history", lang).Body.String() |
| 448 | if lang == "en-US" && !strings.Contains(hist, "PM\"") && !strings.Contains(hist, "AM\"") { |
| 449 | t.Errorf("en-US: History's times are not 12-hour") |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // The scriptless forms change only what a person changed (§5.2): an |
| 455 | // untouched web address drawn without its scheme, and a Choice the field |
| 456 | // no longer offers, are kept, not rewritten or cleared (Fable, Phase 1 |
| 457 | // review, L1 and L2). |
| 458 | func TestTheFormKeepsWhatWasNotTouched(t *testing.T) { |
| 459 | r := newEditRig(t) |
| 460 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldDomain: "http://cnoc.example", fichas.FieldStage: "Lead"}) |
| 461 | m, _ := fichas.EnsureMember(t.Context(), r.d.G, r.cfg.Sealer, TestOwner) |
| 462 | if err := fichas.UpdateField(t.Context(), r.d.G, r.cfg.Sealer, fichas.FieldStage, fichas.Person(m.ID), "Stage", []string{"Contacted", "Won"}); err != nil { |
| 463 | t.Fatal(err) |
| 464 | } |
| 465 | form := get(t, r.h, cardPath(id)+"/edit").Body.String() |
| 466 | if !strings.Contains(form, `<option value="Lead" selected>Lead (no longer offered)</option>`) { |
| 467 | t.Fatal("the form does not keep a Choice the field no longer offers") |
| 468 | } |
| 469 | // The form as drawn, with only the email typed. |
| 470 | rec := post(t, r.h, cardPath(id), url.Values{ |
| 471 | fichas.FieldName: {"Cnoc"}, "was." + fichas.FieldName: {"Cnoc"}, |
| 472 | fichas.FieldDomain: {"http://cnoc.example"}, "was." + fichas.FieldDomain: {"http://cnoc.example"}, |
| 473 | fichas.FieldStage: {"Lead"}, "was." + fichas.FieldStage: {"Lead"}, |
| 474 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, |
| 475 | }) |
| 476 | if rec.Code != http.StatusSeeOther { |
| 477 | t.Fatalf("saving the form: %d", rec.Code) |
| 478 | } |
| 479 | if r.value(t, id, fichas.FieldDomain) != "http://cnoc.example" || r.value(t, id, fichas.FieldStage) != "Lead" { |
| 480 | t.Fatalf("an untouched field changed: domain %q, stage %q", r.value(t, id, fichas.FieldDomain), r.value(t, id, fichas.FieldStage)) |
| 481 | } |
| 482 | // The address as the field draws it, without its scheme, is no change. |
| 483 | if code, out := postJSON(t, r.h, cardPath(id)+"/fields/"+fichas.FieldDomain, url.Values{ |
| 484 | fichas.FieldDomain: {"cnoc.example"}, "was." + fichas.FieldDomain: {"http://cnoc.example"}, |
| 485 | }); code != 200 || out["event"].(float64) != 0 { |
| 486 | t.Fatalf("an untouched address: %d %v", code, out) |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // A refused number on the New form comes back as typed (L3). |
| 491 | func TestARefusedNumberComesBackAsTyped(t *testing.T) { |
| 492 | r := newEditRig(t) |
| 493 | rec := post(t, r.h, "/cards", url.Values{"type": {"t_customer"}, fichas.FieldName: {"Cnoc"}, r.budget: {"1,5"}, r.budget + ".locale": {"en-IE"}}) |
| 494 | if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), `value="1,5"`) { |
| 495 | t.Fatalf("a refused number: %d, typed value kept %v", rec.Code, strings.Contains(rec.Body.String(), `value="1,5"`)) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // A scriptless Undo that left a field says so (§5.3; L5). |
| 500 | func TestAPartialUndoSaysWhatItLeft(t *testing.T) { |
| 501 | r := newEditRig(t) |
| 502 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldStage: "Lead"}) |
| 503 | rec := post(t, r.h, cardPath(id), url.Values{ |
| 504 | fichas.FieldStage: {"Won"}, "was." + fichas.FieldStage: {"Lead"}, |
| 505 | fichas.FieldEmail: {"a@b.example"}, "was." + fichas.FieldEmail: {""}, |
| 506 | }) |
| 507 | event := strings.TrimPrefix(rec.Header().Get("Location"), cardPath(id)+"?saved=") |
| 508 | m, _ := fichas.EnsureMember(t.Context(), r.d.G, r.cfg.Sealer, "niamh@example.com") |
| 509 | if _, err := fichas.SetField(t.Context(), r.d.G, r.cfg.Sealer, id, fichas.Person(m.ID), fichas.FieldStage, "Lost", "Won"); err != nil { |
| 510 | t.Fatal(err) |
| 511 | } |
| 512 | undo := post(t, r.h, cardPath(id)+"/undo", url.Values{"event": {event}}) |
| 513 | page := get(t, r.h, undo.Header().Get("Location")).Body.String() |
| 514 | if !strings.Contains(page, "Undone except where a value was changed again.") { |
| 515 | t.Fatalf("a partial Undo does not say what it left (%s)", undo.Header().Get("Location")) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // A form posts its textareas and hidden fields with CRLF; Notes stored |
| 520 | // with LF are the same text (Fable, Phase 1 review, H1). Through the |
| 521 | // scriptless form's route, end to end. |
| 522 | // |
| 523 | // Mutation, 2026-11-17: comparing was byte for byte in EditFields (the |
| 524 | // one place the rule lives) failed it with a 409. |
| 525 | func TestAFormsCRLFIsTheSameText(t *testing.T) { |
| 526 | r := newEditRig(t) |
| 527 | id := r.card(t, map[string]string{fichas.FieldName: "Cnoc", fichas.FieldNotes: "line one\nline two"}) |
| 528 | rec := post(t, r.h, cardPath(id), url.Values{ |
| 529 | fichas.FieldNotes: {"line one\r\nline two\r\nline three"}, "was." + fichas.FieldNotes: {"line one\r\nline two"}, |
| 530 | }) |
| 531 | if rec.Code != http.StatusSeeOther || r.value(t, id, fichas.FieldNotes) != "line one\nline two\nline three" { |
| 532 | t.Fatalf("a CRLF form over LF notes: %d, notes %q", rec.Code, r.value(t, id, fichas.FieldNotes)) |
| 533 | } |
| 534 | // An untouched CRLF textarea is no change at all. |
| 535 | again := post(t, r.h, cardPath(id), url.Values{ |
| 536 | fichas.FieldNotes: {"line one\r\nline two\r\nline three"}, "was." + fichas.FieldNotes: {"line one\r\nline two\r\nline three"}, |
| 537 | }) |
| 538 | if loc := again.Header().Get("Location"); strings.Contains(loc, "saved=") { |
| 539 | t.Fatalf("an untouched CRLF textarea was saved as a change (%s)", loc) |
| 540 | } |
| 541 | } |
| 542 | |