oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/url"
8 "strings"
9 "testing"
10 "time"
11)
12
13// Gate 11, drafts (spec §11, §5).
14
15// clock is a rig's movable time.
16type clock struct{ now time.Time }
17
18func (rig *rig) clocked(t *testing.T) *clock {
19 t.Helper()
20 moving := &clock{now: time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)}
21 rig.built.Store.SetClock(func() time.Time { return moving.now })
22 return moving
23}
24
25func (rig *rig) autosave(t *testing.T, who, page, base, version,
26 body string) (int, map[string]any) {
27 t.Helper()
28 saved := rig.post(t, who, page+"/draft", url.Values{"base": {base},
29 "version": {version}, "body": {body}})
30 reply := map[string]any{}
31 _ = json.Unmarshal(saved.Body.Bytes(), &reply)
32 return saved.Code, reply
33}
34
35func TestGate11AutosaveNeverTouchesThePage(t *testing.T) {
36 rig := newRig(t)
37 acme := rig.createPage(t, Ana, "Acme", "Committed text.")
38 revision := revisionOf(t, rig, acme)
39 events := rig.count(t, "events")
40 code, reply := rig.autosave(t, Ana, acme, revision, "0", "Draft text.")
41 if code != http.StatusOK || reply["version"] == "" {
42 t.Fatalf("autosave: %d %v", code, reply)
43 }
44 for _, path := range []string{acme, acme + "/edit", "/", "/pages"} {
45 rig.get(t, Ana, path)
46 }
47 if revisionOf(t, rig, acme) != revision ||
48 !strings.Contains(rig.page(t, Bo, acme), "Committed text.") ||
49 strings.Contains(rig.page(t, Bo, acme), "Draft text.") {
50 t.Fatal("an autosave or a GET changed the page")
51 }
52 if rig.count(t, "events") != events || rig.count(t, "drafts") != 1 {
53 t.Fatal("an autosave logged an event, or a GET committed")
54 }
55}
56
57func TestGate11AClosedTabsDraftIsOfferedAsSaveOrDiscard(t *testing.T) {
58 rig := newRig(t)
59 acme := rig.createPage(t, Ana, "Acme", "Committed.")
60 rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Left open.")
61 offer := rig.page(t, Ana, acme)
62 if !strings.Contains(offer, "Unsaved changes</strong> from") ||
63 !strings.Contains(offer, `action="`+acme+`/draft/save"`) ||
64 !strings.Contains(offer, `action="`+acme+`/draft/discard"`) {
65 t.Fatal("the draft is not offered as Save or Discard")
66 }
67 if strings.Contains(rig.page(t, Bo, acme), "Unsaved changes</strong>") {
68 t.Fatal("Ana's draft is offered to Bo")
69 }
70 if rig.post(t, Ana, acme+"/draft/save", nil).Code != http.StatusSeeOther {
71 t.Fatal("saving the draft failed")
72 }
73 if !strings.Contains(rig.page(t, Bo, acme), "Left open.") ||
74 rig.count(t, "drafts") != 0 {
75 t.Fatal("the saved draft did not become the page")
76 }
77 _, unwanted := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0",
78 "Unwanted.")
79 rig.post(t, Ana, acme+"/draft/discard",
80 url.Values{"version": {unwanted["version"].(string)}})
81 if rig.count(t, "drafts") != 0 ||
82 strings.Contains(rig.page(t, Ana, acme), "Unwanted.") {
83 t.Fatal("discard kept the draft or committed it")
84 }
85}
86
87func TestGate11ASameBlockConflictLeavesTheDraftUncommitted(t *testing.T) {
88 rig := newRig(t)
89 acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.")
90 base := revisionOf(t, rig, acme)
91 rig.autosave(t, Ana, acme, base, "0", "One, Ana.\n\nTwo.")
92 rig.post(t, Bo, acme, url.Values{"revision": {base},
93 "body": {"One, Bo.\n\nTwo, Bo."}})
94 saved := rig.post(t, Ana, acme+"/draft/save", nil)
95 if saved.Header().Get("Location") != acme+"/conflict" {
96 t.Fatalf("Ana's commit went to %q", saved.Header().Get("Location"))
97 }
98 if rig.count(t, "drafts") != 1 ||
99 !strings.Contains(rig.page(t, Bo, acme), "One, Bo.") {
100 t.Fatal("a conflicted draft was committed, or lost")
101 }
102 view := rig.page(t, Ana, acme+"/conflict")
103 rig.post(t, Ana, acme+"/resolve", url.Values{
104 "current": {between(view, `name="current" value="`, `"`)},
105 "version": {between(view, `name="version" value="`, `"`)},
106 "choice-0": {"mine"}})
107 if body := rig.page(t, Bo, acme); !strings.Contains(body, "One, Ana.") ||
108 !strings.Contains(body, "Two, Bo.") {
109 t.Fatal("the choice did not keep Ana's block and Bo's other edit")
110 }
111}
112
113func TestGate11ABasePastTheWindowIsShownSideBySide(t *testing.T) {
114 rig := newRig(t)
115 moving := rig.clocked(t)
116 acme := rig.createPage(t, Ana, "Acme", "Old.")
117 base := revisionOf(t, rig, acme)
118 moving.now = moving.now.Add(91 * 24 * time.Hour)
119 rig.autosave(t, Ana, acme, base, "0", "Mine.")
120 rig.post(t, Bo, acme, url.Values{"revision": {base}, "body": {"Bo's."}})
121 if err := rig.built.Store.Sweep(context.Background()); err != nil {
122 t.Fatal(err)
123 }
124 rig.post(t, Ana, acme+"/draft/save", nil)
125 view := rig.page(t, Ana, acme+"/conflict")
126 if !strings.Contains(view, "side by side") ||
127 !strings.Contains(view, `name="whole" value="mine"`) {
128 t.Fatal("no side-by-side choice when the base is gone")
129 }
130}
131
132func TestGate11ADeletedPagesDraftIsOfferedAsAScratchPage(t *testing.T) {
133 rig := newRig(t)
134 acme := rig.createPage(t, Ana, "Acme", "Text.")
135 rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Rescued.")
136 rig.post(t, Bo, acme+"/delete", nil)
137 today := rig.page(t, Ana, "/")
138 if !strings.Contains(today, `action="`+acme+`/draft/scratch"`) {
139 t.Fatal("today's note does not offer the orphaned draft")
140 }
141 rescued := rig.post(t, Ana, acme+"/draft/scratch", nil)
142 if !strings.Contains(rig.page(t, Ana, rescued.Header().Get("Location")),
143 "Rescued.") || rig.count(t, "drafts") != 0 {
144 t.Fatal("the draft did not become a scratch page")
145 }
146}
147
148func TestGate11AStaleTabCannotOverwriteANewerDraft(t *testing.T) {
149 rig := newRig(t)
150 acme := rig.createPage(t, Ana, "Acme", "Text.")
151 base := revisionOf(t, rig, acme)
152 _, first := rig.autosave(t, Ana, acme, base, "0", "Tab A.")
153 code, reply := rig.autosave(t, Ana, acme, base, "0", "Tab B.")
154 if code != http.StatusConflict || reply["body"] != "Tab A." {
155 t.Fatalf("a second tab with no draft: %d %v", code, reply)
156 }
157 version := first["version"].(string)
158 _, second := rig.autosave(t, Ana, acme, base, version, "Tab A again.")
159 code, _ = rig.autosave(t, Ana, acme, base, version, "Tab B late.")
160 if code != http.StatusConflict || second["version"] == version {
161 t.Fatal("a stale tab overwrote the newer draft")
162 }
163 if !strings.Contains(rig.page(t, Ana, acme+"/edit"), "Tab A again.") {
164 t.Fatal("the newer draft was lost")
165 }
166}
167
168func TestGate11DraftsAreDiscarded30DaysAfterTheirLastWrite(t *testing.T) {
169 rig := newRig(t)
170 moving := rig.clocked(t)
171 acme := rig.createPage(t, Ana, "Acme", "Text.")
172 rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Old draft.")
173 moving.now = moving.now.Add(29 * 24 * time.Hour)
174 if !strings.Contains(rig.page(t, Ana, acme), "Unsaved changes</strong>") {
175 t.Fatal("a 29-day-old draft is gone")
176 }
177 moving.now = moving.now.Add(2 * 24 * time.Hour)
178 if strings.Contains(rig.page(t, Ana, acme), "Unsaved changes</strong>") {
179 t.Fatal("a 31-day-old draft is still offered")
180 }
181 rig.built.Store.Sweep(context.Background())
182 if rig.count(t, "drafts") != 0 {
183 t.Fatal("the sweep kept an expired draft")
184 }
185}
186
187func TestGate11EditingShowsOnTeamPagesOnly(t *testing.T) {
188 rig := newRig(t)
189 moving := rig.clocked(t)
190 acme := rig.createPage(t, Ana, "Acme", "Text.")
191 rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Typing.")
192 if !strings.Contains(rig.page(t, Bo, acme), Ana+" is editing") {
193 t.Fatal("Bo is not told Ana is editing")
194 }
195 moving.now = moving.now.Add(3 * time.Minute)
196 if strings.Contains(rig.page(t, Bo, acme), "is editing") {
197 t.Fatal("a draft older than two minutes still shows")
198 }
199 scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}})
200 path := scratch.Header().Get("Location")
201 rig.autosave(t, Ana, path, revisionAs(t, rig, Ana, path), "0", "Typing.")
202 if strings.Contains(rig.page(t, Ana, path), "is editing") {
203 t.Fatal("\"is editing\" appeared on a private page")
204 }
205}
206
207// Review M1, over HTTP: a Save or a Discard from a tab that had not seen
208// the newest draft leaves that draft as it is.
209func TestAStaleTabsSaveOrDiscardKeepsTheNewerDraft(t *testing.T) {
210 rig := newRig(t)
211 acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.")
212 base := revisionOf(t, rig, acme)
213 _, first := rig.autosave(t, Ana, acme, base, "0", "One.\n\nTwo, A.")
214 old := first["version"].(string)
215 rig.autosave(t, Ana, acme, base, old, "One.\n\nTwo, newer.")
216 saved := rig.post(t, Ana, acme, url.Values{"revision": {base},
217 "version": {old}, "body": {"One, stale tab.\n\nTwo."}})
218 if saved.Header().Get("Location") != acme+"?done=kept" {
219 t.Fatalf("a stale Save went to %q", saved.Header().Get("Location"))
220 }
221 if !strings.Contains(rig.page(t, Ana, acme+"/edit"), "Two, newer.") {
222 t.Fatal("a stale Save destroyed the newer draft")
223 }
224 // Review N4: the editor a refused stale Save shows again must not
225 // autosave over the newer draft; only an explicit Save replaces it.
226 refused := rig.post(t, Ana, acme, url.Values{"revision": {base},
227 "version": {old}, "body": {"One, clashing.\n\nTwo."}})
228 if refused.Code != http.StatusConflict ||
229 !strings.Contains(refused.Body.String(), `data-drafts=""`) {
230 t.Fatalf("the refused Save's editor autosaves: %d", refused.Code)
231 }
232 discarded := rig.post(t, Ana, acme+"/draft/discard",
233 url.Values{"version": {old}})
234 if !strings.HasSuffix(discarded.Header().Get("Location"), "done=newer") ||
235 rig.count(t, "drafts") != 1 {
236 t.Fatal("a stale Discard threw the newer draft away")
237 }
238}
239
240// Review N1: a stale tab's first Save of an unwritten day does not
241// strand a newer draft for the day. The draft moves onto the new note,
242// is offered there, and commits through the side-by-side view.
243func TestGate11AStaleFirstSaveOfADayKeepsTheNewerDraft(t *testing.T) {
244 rig := newRig(t)
245 day := "/d/2026-11-20"
246 _, phone := rig.autosave(t, Ana, day, "0", "0", "Phone text.")
247 laptop := rig.post(t, Ana, day, url.Values{"revision": {"0"},
248 "version": {"0"}, "body": {"Laptop text."}})
249 if laptop.Header().Get("Location") != day {
250 t.Fatalf("the laptop's save went to %q",
251 laptop.Header().Get("Location"))
252 }
253 offer := rig.page(t, Ana, day)
254 if !strings.Contains(offer, "Unsaved changes</strong> from") ||
255 !strings.Contains(offer, phone["version"].(string)) {
256 t.Fatal("the phone's draft is not offered on the note")
257 }
258 saved := rig.post(t, Ana, day+"/draft/save",
259 url.Values{"version": {phone["version"].(string)}})
260 conflict := saved.Header().Get("Location")
261 if !strings.HasSuffix(conflict, "/conflict") {
262 t.Fatalf("the phone's draft went to %q", conflict)
263 }
264 view := rig.page(t, Ana, conflict)
265 if !strings.Contains(view, "Phone text.") ||
266 !strings.Contains(view, "Laptop text.") ||
267 !strings.Contains(view, `name="whole"`) {
268 t.Fatal("the two texts are not side by side")
269 }
270}
271
272// Review N2: a deleted page's draft can be discarded from Today's offer,
273// which lands back on Today.
274func TestAnOrphanDraftCanBeDiscarded(t *testing.T) {
275 rig := newRig(t)
276 acme := rig.createPage(t, Ana, "Acme", "Text.")
277 rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Orphan.")
278 rig.post(t, Bo, acme+"/delete", nil)
279 today := rig.page(t, Ana, "/")
280 form := between(today, `action="`+acme+`/draft/discard">`, "</form>")
281 version := between(form, `name="version" value="`, `"`)
282 if version == "" {
283 t.Fatal("the orphan's Discard carries no version")
284 }
285 discarded := rig.post(t, Ana, acme+"/draft/discard",
286 url.Values{"version": {version}})
287 if discarded.Header().Get("Location") != "/?done=discarded" ||
288 rig.count(t, "drafts") != 0 {
289 t.Fatalf("discard: %q, %d drafts left",
290 discarded.Header().Get("Location"), rig.count(t, "drafts"))
291 }
292}
293
294// Review N4, for a day: the refused stale Save's editor has no autosave.
295func TestAStaleDaySavesEditorDoesNotAutosave(t *testing.T) {
296 rig := newRig(t)
297 day := "/d/2026-11-21"
298 rig.post(t, Ana, day, url.Values{"body": {"Written."}})
299 base := revisionAs(t, rig, Ana, day)
300 rig.autosave(t, Ana, day, base, "0", "Written, newer.")
301 refused := rig.post(t, Ana, day, url.Values{"revision": {"1"},
302 "version": {"0"}, "body": {"Clashing."}})
303 if refused.Code != http.StatusConflict ||
304 !strings.Contains(refused.Body.String(), `data-drafts=""`) {
305 t.Fatalf("the refused day Save's editor autosaves: %d", refused.Code)
306 }
307 if !strings.Contains(rig.page(t, Ana, day+"/edit"),
308 `data-drafts="`+day+`/draft"`) {
309 t.Fatal("the ordinary day editor lost its autosave")
310 }
311}
312