package mapatest import ( "context" "encoding/json" "net/http" "net/url" "strings" "testing" "time" ) // Gate 11, drafts (spec §11, §5). // clock is a rig's movable time. type clock struct{ now time.Time } func (rig *rig) clocked(t *testing.T) *clock { t.Helper() moving := &clock{now: time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)} rig.built.Store.SetClock(func() time.Time { return moving.now }) return moving } func (rig *rig) autosave(t *testing.T, who, page, base, version, body string) (int, map[string]any) { t.Helper() saved := rig.post(t, who, page+"/draft", url.Values{"base": {base}, "version": {version}, "body": {body}}) reply := map[string]any{} _ = json.Unmarshal(saved.Body.Bytes(), &reply) return saved.Code, reply } func TestGate11AutosaveNeverTouchesThePage(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Committed text.") revision := revisionOf(t, rig, acme) events := rig.count(t, "events") code, reply := rig.autosave(t, Ana, acme, revision, "0", "Draft text.") if code != http.StatusOK || reply["version"] == "" { t.Fatalf("autosave: %d %v", code, reply) } for _, path := range []string{acme, acme + "/edit", "/", "/pages"} { rig.get(t, Ana, path) } if revisionOf(t, rig, acme) != revision || !strings.Contains(rig.page(t, Bo, acme), "Committed text.") || strings.Contains(rig.page(t, Bo, acme), "Draft text.") { t.Fatal("an autosave or a GET changed the page") } if rig.count(t, "events") != events || rig.count(t, "drafts") != 1 { t.Fatal("an autosave logged an event, or a GET committed") } } func TestGate11AClosedTabsDraftIsOfferedAsSaveOrDiscard(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Committed.") rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Left open.") offer := rig.page(t, Ana, acme) if !strings.Contains(offer, "Unsaved changes from") || !strings.Contains(offer, `action="`+acme+`/draft/save"`) || !strings.Contains(offer, `action="`+acme+`/draft/discard"`) { t.Fatal("the draft is not offered as Save or Discard") } if strings.Contains(rig.page(t, Bo, acme), "Unsaved changes") { t.Fatal("Ana's draft is offered to Bo") } if rig.post(t, Ana, acme+"/draft/save", nil).Code != http.StatusSeeOther { t.Fatal("saving the draft failed") } if !strings.Contains(rig.page(t, Bo, acme), "Left open.") || rig.count(t, "drafts") != 0 { t.Fatal("the saved draft did not become the page") } _, unwanted := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Unwanted.") rig.post(t, Ana, acme+"/draft/discard", url.Values{"version": {unwanted["version"].(string)}}) if rig.count(t, "drafts") != 0 || strings.Contains(rig.page(t, Ana, acme), "Unwanted.") { t.Fatal("discard kept the draft or committed it") } } func TestGate11ASameBlockConflictLeavesTheDraftUncommitted(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") base := revisionOf(t, rig, acme) rig.autosave(t, Ana, acme, base, "0", "One, Ana.\n\nTwo.") rig.post(t, Bo, acme, url.Values{"revision": {base}, "body": {"One, Bo.\n\nTwo, Bo."}}) saved := rig.post(t, Ana, acme+"/draft/save", nil) if saved.Header().Get("Location") != acme+"/conflict" { t.Fatalf("Ana's commit went to %q", saved.Header().Get("Location")) } if rig.count(t, "drafts") != 1 || !strings.Contains(rig.page(t, Bo, acme), "One, Bo.") { t.Fatal("a conflicted draft was committed, or lost") } view := rig.page(t, Ana, acme+"/conflict") rig.post(t, Ana, acme+"/resolve", url.Values{ "current": {between(view, `name="current" value="`, `"`)}, "version": {between(view, `name="version" value="`, `"`)}, "choice-0": {"mine"}}) if body := rig.page(t, Bo, acme); !strings.Contains(body, "One, Ana.") || !strings.Contains(body, "Two, Bo.") { t.Fatal("the choice did not keep Ana's block and Bo's other edit") } } func TestGate11ABasePastTheWindowIsShownSideBySide(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) acme := rig.createPage(t, Ana, "Acme", "Old.") base := revisionOf(t, rig, acme) moving.now = moving.now.Add(91 * 24 * time.Hour) rig.autosave(t, Ana, acme, base, "0", "Mine.") rig.post(t, Bo, acme, url.Values{"revision": {base}, "body": {"Bo's."}}) if err := rig.built.Store.Sweep(context.Background()); err != nil { t.Fatal(err) } rig.post(t, Ana, acme+"/draft/save", nil) view := rig.page(t, Ana, acme+"/conflict") if !strings.Contains(view, "side by side") || !strings.Contains(view, `name="whole" value="mine"`) { t.Fatal("no side-by-side choice when the base is gone") } } func TestGate11ADeletedPagesDraftIsOfferedAsAScratchPage(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Text.") rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Rescued.") rig.post(t, Bo, acme+"/delete", nil) today := rig.page(t, Ana, "/") if !strings.Contains(today, `action="`+acme+`/draft/scratch"`) { t.Fatal("today's note does not offer the orphaned draft") } rescued := rig.post(t, Ana, acme+"/draft/scratch", nil) if !strings.Contains(rig.page(t, Ana, rescued.Header().Get("Location")), "Rescued.") || rig.count(t, "drafts") != 0 { t.Fatal("the draft did not become a scratch page") } } func TestGate11AStaleTabCannotOverwriteANewerDraft(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Text.") base := revisionOf(t, rig, acme) _, first := rig.autosave(t, Ana, acme, base, "0", "Tab A.") code, reply := rig.autosave(t, Ana, acme, base, "0", "Tab B.") if code != http.StatusConflict || reply["body"] != "Tab A." { t.Fatalf("a second tab with no draft: %d %v", code, reply) } version := first["version"].(string) _, second := rig.autosave(t, Ana, acme, base, version, "Tab A again.") code, _ = rig.autosave(t, Ana, acme, base, version, "Tab B late.") if code != http.StatusConflict || second["version"] == version { t.Fatal("a stale tab overwrote the newer draft") } if !strings.Contains(rig.page(t, Ana, acme+"/edit"), "Tab A again.") { t.Fatal("the newer draft was lost") } } func TestGate11DraftsAreDiscarded30DaysAfterTheirLastWrite(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) acme := rig.createPage(t, Ana, "Acme", "Text.") rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Old draft.") moving.now = moving.now.Add(29 * 24 * time.Hour) if !strings.Contains(rig.page(t, Ana, acme), "Unsaved changes") { t.Fatal("a 29-day-old draft is gone") } moving.now = moving.now.Add(2 * 24 * time.Hour) if strings.Contains(rig.page(t, Ana, acme), "Unsaved changes") { t.Fatal("a 31-day-old draft is still offered") } rig.built.Store.Sweep(context.Background()) if rig.count(t, "drafts") != 0 { t.Fatal("the sweep kept an expired draft") } } func TestGate11EditingShowsOnTeamPagesOnly(t *testing.T) { rig := newRig(t) moving := rig.clocked(t) acme := rig.createPage(t, Ana, "Acme", "Text.") rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Typing.") if !strings.Contains(rig.page(t, Bo, acme), Ana+" is editing") { t.Fatal("Bo is not told Ana is editing") } moving.now = moving.now.Add(3 * time.Minute) if strings.Contains(rig.page(t, Bo, acme), "is editing") { t.Fatal("a draft older than two minutes still shows") } scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}) path := scratch.Header().Get("Location") rig.autosave(t, Ana, path, revisionAs(t, rig, Ana, path), "0", "Typing.") if strings.Contains(rig.page(t, Ana, path), "is editing") { t.Fatal("\"is editing\" appeared on a private page") } } // Review M1, over HTTP: a Save or a Discard from a tab that had not seen // the newest draft leaves that draft as it is. func TestAStaleTabsSaveOrDiscardKeepsTheNewerDraft(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.") base := revisionOf(t, rig, acme) _, first := rig.autosave(t, Ana, acme, base, "0", "One.\n\nTwo, A.") old := first["version"].(string) rig.autosave(t, Ana, acme, base, old, "One.\n\nTwo, newer.") saved := rig.post(t, Ana, acme, url.Values{"revision": {base}, "version": {old}, "body": {"One, stale tab.\n\nTwo."}}) if saved.Header().Get("Location") != acme+"?done=kept" { t.Fatalf("a stale Save went to %q", saved.Header().Get("Location")) } if !strings.Contains(rig.page(t, Ana, acme+"/edit"), "Two, newer.") { t.Fatal("a stale Save destroyed the newer draft") } // Review N4: the editor a refused stale Save shows again must not // autosave over the newer draft; only an explicit Save replaces it. refused := rig.post(t, Ana, acme, url.Values{"revision": {base}, "version": {old}, "body": {"One, clashing.\n\nTwo."}}) if refused.Code != http.StatusConflict || !strings.Contains(refused.Body.String(), `data-drafts=""`) { t.Fatalf("the refused Save's editor autosaves: %d", refused.Code) } discarded := rig.post(t, Ana, acme+"/draft/discard", url.Values{"version": {old}}) if !strings.HasSuffix(discarded.Header().Get("Location"), "done=newer") || rig.count(t, "drafts") != 1 { t.Fatal("a stale Discard threw the newer draft away") } } // Review N1: a stale tab's first Save of an unwritten day does not // strand a newer draft for the day. The draft moves onto the new note, // is offered there, and commits through the side-by-side view. func TestGate11AStaleFirstSaveOfADayKeepsTheNewerDraft(t *testing.T) { rig := newRig(t) day := "/d/2026-11-20" _, phone := rig.autosave(t, Ana, day, "0", "0", "Phone text.") laptop := rig.post(t, Ana, day, url.Values{"revision": {"0"}, "version": {"0"}, "body": {"Laptop text."}}) if laptop.Header().Get("Location") != day { t.Fatalf("the laptop's save went to %q", laptop.Header().Get("Location")) } offer := rig.page(t, Ana, day) if !strings.Contains(offer, "Unsaved changes from") || !strings.Contains(offer, phone["version"].(string)) { t.Fatal("the phone's draft is not offered on the note") } saved := rig.post(t, Ana, day+"/draft/save", url.Values{"version": {phone["version"].(string)}}) conflict := saved.Header().Get("Location") if !strings.HasSuffix(conflict, "/conflict") { t.Fatalf("the phone's draft went to %q", conflict) } view := rig.page(t, Ana, conflict) if !strings.Contains(view, "Phone text.") || !strings.Contains(view, "Laptop text.") || !strings.Contains(view, `name="whole"`) { t.Fatal("the two texts are not side by side") } } // Review N2: a deleted page's draft can be discarded from Today's offer, // which lands back on Today. func TestAnOrphanDraftCanBeDiscarded(t *testing.T) { rig := newRig(t) acme := rig.createPage(t, Ana, "Acme", "Text.") rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Orphan.") rig.post(t, Bo, acme+"/delete", nil) today := rig.page(t, Ana, "/") form := between(today, `action="`+acme+`/draft/discard">`, "") version := between(form, `name="version" value="`, `"`) if version == "" { t.Fatal("the orphan's Discard carries no version") } discarded := rig.post(t, Ana, acme+"/draft/discard", url.Values{"version": {version}}) if discarded.Header().Get("Location") != "/?done=discarded" || rig.count(t, "drafts") != 0 { t.Fatalf("discard: %q, %d drafts left", discarded.Header().Get("Location"), rig.count(t, "drafts")) } } // Review N4, for a day: the refused stale Save's editor has no autosave. func TestAStaleDaySavesEditorDoesNotAutosave(t *testing.T) { rig := newRig(t) day := "/d/2026-11-21" rig.post(t, Ana, day, url.Values{"body": {"Written."}}) base := revisionAs(t, rig, Ana, day) rig.autosave(t, Ana, day, base, "0", "Written, newer.") refused := rig.post(t, Ana, day, url.Values{"revision": {"1"}, "version": {"0"}, "body": {"Clashing."}}) if refused.Code != http.StatusConflict || !strings.Contains(refused.Body.String(), `data-drafts=""`) { t.Fatalf("the refused day Save's editor autosaves: %d", refused.Code) } if !strings.Contains(rig.page(t, Ana, day+"/edit"), `data-drafts="`+day+`/draft"`) { t.Fatal("the ordinary day editor lost its autosave") } }