| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // Gate 8, event completeness (spec §11): every write path logs an event, |
| 15 | // except draft writes and retention, which are exempt by name (§5). |
| 16 | // The test drives every state-changing route the router has, and fails |
| 17 | // if one is never driven, or if a route changes any table but drafts |
| 18 | // without logging an event. Retention runs on wake, not from a route, |
| 19 | // so no step here reaches it. |
| 20 | |
| 21 | // recorded is the tables a write changes and S1 would have to report. |
| 22 | // drafts is not here: draft writes are the named exemption. Rastrillo's |
| 23 | // session and sign-in rows are not Mapa's writes. |
| 24 | var recorded = []string{"members", "pages", "aliases", "title_prints", |
| 25 | "daily_prints", "revisions", "images", "image_trash"} |
| 26 | |
| 27 | // snapshot is every row of every recorded table, as text. |
| 28 | func snapshot(t *testing.T, rig *rig) string { |
| 29 | t.Helper() |
| 30 | var out strings.Builder |
| 31 | for _, table := range recorded { |
| 32 | rows, err := rig.db.G.Raw("SELECT * FROM " + table).Rows() |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | columns, _ := rows.Columns() |
| 37 | for rows.Next() { |
| 38 | values := make([]any, len(columns)) |
| 39 | pointers := make([]any, len(columns)) |
| 40 | for at := range values { |
| 41 | pointers[at] = &values[at] |
| 42 | } |
| 43 | if err := rows.Scan(pointers...); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | fmt.Fprintf(&out, "%s %v\n", table, values) |
| 47 | } |
| 48 | rows.Close() |
| 49 | } |
| 50 | return out.String() |
| 51 | } |
| 52 | |
| 53 | // step is one write, driven through its route. |
| 54 | type step struct { |
| 55 | route string |
| 56 | do func(*rig) *http.Response |
| 57 | } |
| 58 | |
| 59 | func TestGate8EveryWriteLogsAnEvent(t *testing.T) { |
| 60 | rig := newRig(t) |
| 61 | driven := map[string]bool{} |
| 62 | for _, write := range gate8Steps(t, rig) { |
| 63 | before, events := snapshot(t, rig), rig.count(t, "events") |
| 64 | response := write.do(rig) |
| 65 | if response.StatusCode >= 400 { |
| 66 | t.Errorf("%s answered %d", write.route, response.StatusCode) |
| 67 | } |
| 68 | changed := snapshot(t, rig) != before |
| 69 | if changed && rig.count(t, "events") == events { |
| 70 | t.Errorf("%s changed the database and logged no event", |
| 71 | write.route) |
| 72 | } |
| 73 | driven[write.route] = true |
| 74 | } |
| 75 | for _, route := range rig.built.WriteRoutes() { |
| 76 | if !driven[route] { |
| 77 | t.Errorf("%s is a write route gate 8 never drives", route) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (rig *rig) send(who, method, target string, |
| 83 | form url.Values) *http.Response { |
| 84 | form = counted(target, form) |
| 85 | request, _ := http.NewRequest(method, target, |
| 86 | strings.NewReader(form.Encode())) |
| 87 | request.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 88 | request.Header.Set("Origin", testOrigin) |
| 89 | recorder := httptest.NewRecorder() |
| 90 | rig.built.As(who).ServeHTTP(recorder, request) |
| 91 | return recorder.Result() |
| 92 | } |
| 93 | |
| 94 | func posting(route, who, target string, form url.Values) step { |
| 95 | return step{route: "POST " + route, do: func(rig *rig) *http.Response { |
| 96 | return rig.send(who, http.MethodPost, target, form) |
| 97 | }} |
| 98 | } |
| 99 | |
| 100 | // gate8Steps is a short life of a team, one step per write route. |
| 101 | func gate8Steps(t *testing.T, world *rig) []step { |
| 102 | acme := world.createPage(t, Ana, "Acme", "One.\n\nAcme mention.") |
| 103 | notes := world.createPage(t, Ana, "Notes", "We met Acme.") |
| 104 | rev := func(path string) string { return revisionOf(t, world, path) } |
| 105 | day := "/d/2026-11-12" |
| 106 | steps := []step{ |
| 107 | posting("/pages", Bo, "/pages", url.Values{"title": {"New"}}), |
| 108 | posting("/p/{id}", Bo, acme, url.Values{"revision": {rev(acme)}, |
| 109 | "body": {"One, Bo.\n\nAcme mention."}}), |
| 110 | posting("/p/{id}/mentions", Bo, acme+"/mentions", |
| 111 | url.Values{"source": {strings.TrimPrefix(notes, "/p/")}, |
| 112 | "block": {"0"}}), |
| 113 | posting("/p/{id}/blocks", Bo, acme+"/blocks", |
| 114 | url.Values{"block": {"0"}}), |
| 115 | posting("/p/{id}/draft", Ana, acme+"/draft", |
| 116 | url.Values{"base": {"1"}, "body": {"Draft."}}), |
| 117 | discarding(t, "/p/{id}/draft/discard", acme), |
| 118 | discarding(t, "/d/{date}/draft/discard", day), |
| 119 | posting("/d/{date}/draft", Ana, day+"/draft", |
| 120 | url.Values{"body": {"Day draft."}}), |
| 121 | posting("/d/{date}/draft/save", Ana, day+"/draft/save", nil), |
| 122 | posting("/d/{date}", Ana, day, url.Values{"body": {"Saved day."}, |
| 123 | "revision": {"0"}}), |
| 124 | posting("/preview", Ana, "/preview", url.Values{"body": {"x"}}), |
| 125 | // The editor's chip states: JSON, written nowhere (level-up §6.8). |
| 126 | posting("/links/resolve", Ana, "/links/resolve", |
| 127 | url.Values{"target": {"Acme", "2026-11-12", "Nothing yet"}}), |
| 128 | posting("/scratch", Ana, "/scratch", url.Values{"body": {"Mine."}}), |
| 129 | posting("/settings", Ana, "/settings", |
| 130 | url.Values{"zone": {"Europe/Madrid"}}), |
| 131 | posting("/settings/locale", Ana, "/settings/locale", |
| 132 | url.Values{"locale": {"de"}}), |
| 133 | posting("/appearance", Ana, "/appearance", |
| 134 | url.Values{"mode": {"dark"}, "return": {"/"}}), |
| 135 | // View preferences: a cookie, no row (level-up §3.2). |
| 136 | posting("/view/sidebar", Ana, "/view/sidebar", |
| 137 | url.Values{"state": {"rail"}, "return": {"/"}}), |
| 138 | posting("/view/links", Ana, "/view/links", |
| 139 | url.Values{"state": {"closed"}, "return": {"/"}}), |
| 140 | // The sidebar's arrows: a cookie, no row (columns and icons |
| 141 | // spec §5.3). |
| 142 | posting("/view/open", Ana, "/view/open", |
| 143 | url.Values{"id": {"x"}, "open": {"1"}, "return": {"/"}}), |
| 144 | posting("/p/{id}/pin", Bo, acme+"/pin", url.Values{"pin": {"1"}}), |
| 145 | // A team page's look: a styled revision (columns and icons |
| 146 | // spec §4.1). |
| 147 | posting("/p/{id}/look", Bo, acme+"/look", |
| 148 | url.Values{"do": {"emoji"}, "emoji": {"🧭"}}), |
| 149 | } |
| 150 | steps = append(steps, gate8Images(t)...) |
| 151 | return append(steps, gate8Later(t, acme, notes)...) |
| 152 | } |
| 153 | |
| 154 | // gate8Later are the steps that need state the first ones made. |
| 155 | func gate8Later(t *testing.T, acme, notes string) []step { |
| 156 | return []step{ |
| 157 | {route: "POST /p/{id}/draft/save", do: func(rig *rig) *http.Response { |
| 158 | rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "D.") |
| 159 | return rig.send(Ana, http.MethodPost, acme+"/draft/save", nil) |
| 160 | }}, |
| 161 | {route: "POST /p/{id}/resolve", do: func(rig *rig) *http.Response { |
| 162 | return resolveStep(t, rig, acme) |
| 163 | }}, |
| 164 | {route: "POST /p/{id}/history/{revision}", |
| 165 | do: func(rig *rig) *http.Response { |
| 166 | history := rig.page(t, Bo, acme+"/history") |
| 167 | revision := lastBetween(history, acme+`/history/`, `"`) |
| 168 | return rig.send(Bo, http.MethodPost, |
| 169 | acme+"/history/"+revision, nil) |
| 170 | }}, |
| 171 | posting("/p/{id}/rename", Bo, acme+"/rename", |
| 172 | url.Values{"title": {"Acme Ltd"}}), |
| 173 | posting("/p/{id}/links", Bo, acme+"/links", nil), |
| 174 | {route: "POST /p/{id}/aliases/{alias}/free", |
| 175 | do: func(rig *rig) *http.Response { |
| 176 | form := rig.page(t, Bo, acme+"/rename") |
| 177 | alias := between(form, "/aliases/", "/free") |
| 178 | return rig.send(Bo, http.MethodPost, |
| 179 | acme+"/aliases/"+alias+"/free", nil) |
| 180 | }}, |
| 181 | {route: "POST /p/{id}/share", do: func(rig *rig) *http.Response { |
| 182 | day := rig.page(t, Ana, "/d/2026-11-12") |
| 183 | note := between(day, `href="/p/`, `/share"`) |
| 184 | form := rig.page(t, Ana, "/p/"+note+"/share") |
| 185 | return rig.send(Ana, http.MethodPost, "/p/"+note+"/share", |
| 186 | url.Values{"title": {"Shared"}, "block": {"0"}, |
| 187 | "revision": {formRevision(form)}}) |
| 188 | }}, |
| 189 | // The closing tab's commit=1, and a new scratch page's first |
| 190 | // autosave (level-up §6.5, D12): each a commit, with its event. |
| 191 | {route: "POST /p/{id}/draft", do: func(rig *rig) *http.Response { |
| 192 | _, reply := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), |
| 193 | "0", "Closing.") |
| 194 | version, _ := reply["version"].(string) |
| 195 | return rig.send(Ana, http.MethodPost, acme+"/draft", |
| 196 | url.Values{"commit": {"1"}, "version": {version}, |
| 197 | "body": {"Closed."}}) |
| 198 | }}, |
| 199 | posting("/scratch", Ana, "/scratch", url.Values{ |
| 200 | "first": {"gate-eight-token-0001"}, "body": {"First."}}), |
| 201 | {route: "POST /p/{id}/draft/scratch", |
| 202 | do: func(rig *rig) *http.Response { |
| 203 | rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "E.") |
| 204 | rig.send(Bo, http.MethodPost, acme+"/delete", nil) |
| 205 | return rig.send(Ana, http.MethodPost, acme+"/draft/scratch", |
| 206 | nil) |
| 207 | }}, |
| 208 | posting("/p/{id}/delete", Ana, notes+"/delete", nil), |
| 209 | posting("/deleted/{id}", Ana, strings.Replace(notes, "/p/", |
| 210 | "/deleted/", 1), nil), |
| 211 | posting("/signout", Ana, "/signout", nil), |
| 212 | posting("/auth/begin", Ana, "/auth/begin", |
| 213 | url.Values{"address": {"stranger@example.com"}}), |
| 214 | posting("/auth/verify", Ana, "/auth/verify", |
| 215 | url.Values{"token": {"not-a-token"}}), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // discarding is a Discard of the draft at path, with the version the |
| 220 | // offer shows (the draft is first written, if there is none). |
| 221 | func discarding(t *testing.T, route, path string) step { |
| 222 | return step{route: "POST " + route, do: func(rig *rig) *http.Response { |
| 223 | // A first draft answers its version; an existing one answers |
| 224 | // "stale" with its current version. Either is the one to send. |
| 225 | _, reply := rig.autosave(t, Ana, path, "0", "0", "To discard.") |
| 226 | version, _ := reply["version"].(string) |
| 227 | return rig.send(Ana, http.MethodPost, path+"/draft/discard", |
| 228 | url.Values{"version": {version}}) |
| 229 | }} |
| 230 | } |
| 231 | |
| 232 | func resolveStep(t *testing.T, rig *rig, acme string) *http.Response { |
| 233 | base := revisionOf(t, rig, acme) |
| 234 | rig.autosave(t, Ana, acme, base, "0", "Clash, Ana.") |
| 235 | rig.send(Bo, http.MethodPost, acme, url.Values{"revision": {base}, |
| 236 | "body": {"Clash, Bo."}}) |
| 237 | rig.send(Ana, http.MethodPost, acme+"/draft/save", nil) |
| 238 | view := rig.page(t, Ana, acme+"/conflict") |
| 239 | return rig.send(Ana, http.MethodPost, acme+"/resolve", url.Values{ |
| 240 | "current": {between(view, `name="current" value="`, `"`)}, |
| 241 | "version": {between(view, `name="version" value="`, `"`)}, |
| 242 | "choice-0": {"mine"}}) |
| 243 | } |
| 244 | |
| 245 | // lastBetween is the last text after open and before close. |
| 246 | func lastBetween(text, open, close string) string { |
| 247 | parts := strings.Split(text, open) |
| 248 | value, _, _ := strings.Cut(parts[len(parts)-1], close) |
| 249 | return value |
| 250 | } |
| 251 | |
| 252 | func TestGate8KnowsEveryWriteRoute(t *testing.T) { |
| 253 | routes := newRig(t).built.WriteRoutes() |
| 254 | sort.Strings(routes) |
| 255 | if len(routes) < 25 { |
| 256 | t.Fatalf("only %d write routes found: %v", len(routes), routes) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // gate8Images is the images' writes (IMG3): an upload, a copy and a |
| 261 | // removal, each with its event. |
| 262 | func gate8Images(t *testing.T) []step { |
| 263 | var first uploaded |
| 264 | return []step{ |
| 265 | {route: "POST /images", do: func(rig *rig) *http.Response { |
| 266 | reply := rig.sendFile(t, Bo, "/images", nil, "a.png", |
| 267 | pngOf(t, 1)) |
| 268 | _ = json.Unmarshal(reply.Body.Bytes(), &first) |
| 269 | return reply.Result() |
| 270 | }}, |
| 271 | {route: "POST /images/copy", do: func(rig *rig) *http.Response { |
| 272 | return rig.send(Bo, http.MethodPost, "/images/copy", |
| 273 | url.Values{"id": {first.ID}}) |
| 274 | }}, |
| 275 | {route: "POST /i/{id}/remove", do: func(rig *rig) *http.Response { |
| 276 | return rig.send(Bo, http.MethodPost, first.Src+"/remove", |
| 277 | url.Values{"back": {"/"}}) |
| 278 | }}, |
| 279 | } |
| 280 | } |
| 281 | |