oficina / mapa Public

Gates L9 and L7 for the editor's server half, and /links/resolve in gate 1

saving_test.go: a clean rebase moves the base and the next commit is
Saved with M's bytes; rebase refused (no draft, stale version, another
page's revision, a revision past the window) and nothing moves; a
conflict reply's hunks and Resolve's JSON, committing once; a stale
Resolve commits nothing; commit=1 refuses a stale (offered) version and
commits the current one, with or without text; saves that change
nothing write nothing, a Merged result included; Make team page refuses
a stale revision; the first scratch commit is single-flight and
ErrEmpty holds; noBase, gone and the unwritten day's replies.

island_test.go: the island parses back a body that closes the script;
it carries the draft, which the page shows with its bar, and ?saved=1
the committed text; every editable page carries it; /links/resolve's
states and its cap; L7, the island's pages, an offered draft opened and
left, New scratch page and /links/resolve change no table's rows.

Gate 1 compares Bo's /links/resolve answer for texts Ana's notes hold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 06f195caeefac442f7c0f7cad381d937635498cd parent d792d32
3 files changed, +544 −0
  • internal/mapatest/gate1_test.go +6 −0
  • internal/mapatest/island_test.go +142 −0
  • internal/mapatest/saving_test.go +396 −0
diff --git a/internal/mapatest/gate1_test.go b/internal/mapatest/gate1_test.go
index 27c2108..6bae5f0 100644
--- a/internal/mapatest/gate1_test.go
+++ b/internal/mapatest/gate1_test.go
@@ -1,6 +1,7 @@
package mapatest
import (
+ "fmt"
"net/url"
"regexp"
"strings"
@@ -110,6 +111,11 @@ func boSees(t *testing.T, rig *rig, acme string) map[string]string {
} {
seen[path] = rig.page(t, Bo, path)
}
+ // The editor's chip states, for texts Ana's notes type too.
+ _, states := rig.postJSON(Bo, "/links/resolve", url.Values{"target": {
+ "Acme", "Acme#^blk", "Zebracorn Plans", "Zebracorn kit",
+ "Acme Ltd", "2026-11-12"}})
+ seen["links resolve"] = fmt.Sprint(states)
renamed := rig.post(t, Bo, acme+"/rename",
url.Values{"title": {"Acme Holdings"}})
seen["rename"] = renamed.Header().Get("Location")
diff --git a/internal/mapatest/island_test.go b/internal/mapatest/island_test.go
new file mode 100644
index 0000000..f634e5d
--- /dev/null
+++ b/internal/mapatest/island_test.go
@@ -0,0 +1,142 @@
+package mapatest
+
+import (
+ "fmt"
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+// What the editor is given (level-up §6.8): the JSON island on every
+// editable page, the draft served as the page body, and
+// POST /links/resolve. L7: none of it writes.
+
+func TestTheIslandSurvivesABodyThatClosesTheScript(t *testing.T) {
+ rig := newRig(t)
+ body := "</script><script>alert(1)</script>\n\n<!-- a
b & c -->"
+ acme := rig.createPage(t, Ana, "Acme", body)
+ page := rig.page(t, Bo, acme)
+ if strings.Count(page, "<script>alert(1)") != 0 {
+ t.Fatal("the body's script reached the page as markup")
+ }
+ if got := islandOf(t, page); got.Source != body || got.Draft != nil ||
+ got.Base != revisionOf(t, rig, acme) {
+ t.Fatalf("the island read back %+v", got)
+ }
+ if rig.get(t, Bo, acme).Header().Get("Cache-Control") != "no-store" {
+ t.Fatal("a page carrying the island may be cached")
+ }
+}
+
+func TestTheIslandCarriesTheDraftAndThePageShowsIt(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "Saved text. [[Acme]]")
+ base := revisionOf(t, rig, acme)
+ _, reply := rig.autosave(t, Ana, acme, base, "0",
+ "Unsaved text. [[Nothing yet]] [[2026-11-12]]")
+ page := rig.page(t, Ana, acme)
+ data := islandOf(t, page)
+ if data.Draft == nil || data.Draft.Version != reply["version"] ||
+ data.Draft.Base != base || data.Base != base ||
+ !strings.HasPrefix(data.Source, "Unsaved text.") {
+ t.Fatalf("Ana's island: %+v", data)
+ }
+ if data.Links["Nothing yet"].Kind != "pending" ||
+ data.Links["2026-11-12"].Kind != "date" || len(data.Links) != 2 {
+ t.Fatalf("the island's chips: %+v", data.Links)
+ }
+ article := between(page, "<article mapa-body>", "</article>")
+ if !strings.Contains(article, "Unsaved text.") ||
+ !strings.Contains(page, "You're seeing your unsaved version.") ||
+ !strings.Contains(page, `href="`+acme+`?saved=1"`) {
+ t.Fatal("the page does not show Ana her draft, with the bar")
+ }
+ saved := rig.page(t, Ana, acme+"?saved=1")
+ if !strings.Contains(between(saved, "<article mapa-body>",
+ "</article>"), "Saved text.") ||
+ strings.Contains(saved, `id="mapa-editor"`) ||
+ !strings.Contains(saved, "You're seeing the saved version.") {
+ t.Fatal("?saved=1 does not show the committed text")
+ }
+ if strings.Contains(rig.page(t, Bo, acme), "Unsaved text.") {
+ t.Fatal("Bo is shown Ana's draft")
+ }
+}
+
+func TestEveryEditablePageCarriesTheIsland(t *testing.T) {
+ rig := newRig(t)
+ rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Day."}})
+ scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}})
+ for path, source := range map[string]string{
+ "/d/2026-11-12": "Day.", "/d/2026-11-13": "",
+ scratch.Header().Get("Location"): "Mine.", "/scratch/new": "",
+ } {
+ if got := islandOf(t, rig.page(t, Ana, path)); got.Source != source {
+ t.Errorf("%s: the island holds %q", path, got.Source)
+ }
+ }
+ if islandOf(t, rig.page(t, Ana, "/d/2026-11-13")).Base != "0" {
+ t.Fatal("an unwritten day's island has a base")
+ }
+}
+
+func TestLinksResolveAnswersEachChipsState(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "Customer. ^blk")
+ code, reply := rig.postJSON(Bo, "/links/resolve", url.Values{
+ "target": {"Acme", "acme#^blk", "Acme#^gone", "Not yet",
+ "2026-11-12", "2026-02-30"}})
+ state := func(key string) map[string]any {
+ found, _ := reply[key].(map[string]any)
+ return found
+ }
+ if code != http.StatusOK || state("Acme")["kind"] != "team" ||
+ state("Acme")["href"] != acme || state("Acme")["title"] != "Acme" ||
+ state("acme#^blk")["kind"] != "block" ||
+ state("acme#^blk")["href"] != acme+"#b-blk" ||
+ state("Acme#^gone")["kind"] != "block gone" ||
+ state("Not yet")["kind"] != "pending" ||
+ state("2026-11-12")["href"] != "/d/2026-11-12" ||
+ state("2026-02-30")["kind"] != "broken" {
+ t.Fatalf("/links/resolve: %d %v", code, reply)
+ }
+ many := url.Values{}
+ for at := range 33 {
+ many.Add("target", fmt.Sprint("Page ", at))
+ }
+ if code, _ := rig.postJSON(Bo, "/links/resolve", many); code !=
+ http.StatusUnprocessableEntity {
+ t.Fatalf("33 targets: %d", code)
+ }
+}
+
+// L7: /links/resolve, the island's pages, a page with an offered draft
+// opened and left, and New scratch page opened and left write nothing.
+func TestL7TheEditorsReadsWriteNothing(t *testing.T) {
+ busy, acme := gate1World(t, true)
+ _, offered := busy.autosave(t, Ana, acme, revisionOf(t, busy, acme),
+ "0", "Offered.")
+ before := tableRowsHash(t, busy)
+ for _, who := range []string{Ana, Bo} {
+ for _, path := range []string{acme, acme + "?saved=1", "/",
+ "/d/2026-11-12", "/d/2026-11-13", "/scratch/new"} {
+ if code := busy.get(t, who, path).Code; code != http.StatusOK {
+ t.Fatalf("%s GET %s: %d", who, path, code)
+ }
+ }
+ code, _ := busy.postJSON(who, "/links/resolve", url.Values{
+ "target": {"Acme", "Zebracorn Plans", "2026-11-12"}})
+ if code != http.StatusOK {
+ t.Fatalf("%s /links/resolve: %d", who, code)
+ }
+ }
+ if tableRowsHash(t, busy) != before {
+ t.Fatal("an editor read changed the database's rows")
+ }
+ busy.autosave(t, Ana, acme, "0", offered["version"].(string),
+ "Typed into.")
+ if tableRowsHash(t, busy) == before {
+ t.Fatal("the hash is vacuous: a draft write did not change it")
+ }
+}
diff --git a/internal/mapatest/saving_test.go b/internal/mapatest/saving_test.go
new file mode 100644
index 0000000..fd1f112
--- /dev/null
+++ b/internal/mapatest/saving_test.go
@@ -0,0 +1,396 @@
+package mapatest
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+// Gate L9, drafts, rebase and commits (level-up §6.5, §7): the server's
+// half. The editor's half (the caret, the table, the three tries) is
+// editor/'s, in Node and the browser.
+
+// postJSON is a POST whose answer the script reads.
+func (rig *rig) postJSON(who, target string,
+ form url.Values) (int, map[string]any) {
+ request := httptest.NewRequest(http.MethodPost, target,
+ strings.NewReader(form.Encode()))
+ request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ request.Header.Set("Origin", testOrigin)
+ request.Header.Set("Accept", "application/json")
+ recorder := httptest.NewRecorder()
+ rig.built.As(who).ServeHTTP(recorder, request)
+ reply := map[string]any{}
+ _ = json.Unmarshal(recorder.Body.Bytes(), &reply)
+ return recorder.Code, reply
+}
+
+// editorData is the island a page carries (level-up §6.8).
+type editorData struct {
+ Source string `json:"source"`
+ Base string `json:"base"`
+ Draft *struct {
+ Version string `json:"version"`
+ Base string `json:"base"`
+ } `json:"draft"`
+ Links map[string]struct {
+ Kind, Href, Title string
+ } `json:"links"`
+}
+
+// islandOf reads the island out of a page, or fails.
+func islandOf(t *testing.T, page string) editorData {
+ t.Helper()
+ raw := between(page, `<script type="application/json" id="mapa-editor">`,
+ "</script>")
+ var data editorData
+ if err := json.Unmarshal([]byte(raw), &data); err != nil {
+ t.Fatalf("the island does not parse: %v\n%s", err, raw)
+ }
+ return data
+}
+
+// committed is a page's committed text, as Bo's island holds it (Bo
+// has no draft in these tests).
+func committed(t *testing.T, rig *rig, path string) string {
+ t.Helper()
+ return islandOf(t, rig.page(t, Bo, path)).Source
+}
+
+// draftRow is the person's one draft's base and version, as stored.
+func (rig *rig) draftRow(t *testing.T) (base, version int64) {
+ t.Helper()
+ row := rig.db.G.Raw("SELECT base_revision, version FROM drafts").Row()
+ if err := row.Scan(&base, &version); err != nil {
+ t.Fatal(err)
+ }
+ return base, version
+}
+
+const fourBlocks = "One.\n\nTwo.\n\nThree.\n\nFour."
+
+func mergeOf(t *testing.T, reply map[string]any) map[string]any {
+ t.Helper()
+ merge, ok := reply["merge"].(map[string]any)
+ if !ok {
+ t.Fatalf("no merge in %v", reply)
+ }
+ return merge
+}
+
+func TestL9ACleanRebaseMovesTheBaseAndTheCommitIsSaved(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", fourBlocks)
+ first := revisionOf(t, rig, acme)
+ _, reply := rig.autosave(t, Ana, acme, first, "0",
+ "One.\n\nTwo, Ana.\n\nThree.\n\nFour.")
+ if reply["revision"] != first || reply["merge"] != nil {
+ t.Fatalf("an autosave over the current revision: %v", reply)
+ }
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"One.\n\nTwo.\n\nThree, Bo.\n\n\n\nFour."}})
+ second := revisionOf(t, rig, acme)
+ _, reply = rig.autosave(t, Ana, acme, first,
+ reply["version"].(string), "One.\n\nTwo, Ana!\n\nThree.\n\nFour.")
+ merge := mergeOf(t, reply)
+ want := "One.\n\nTwo, Ana!\n\nThree, Bo.\n\n\n\nFour."
+ if reply["revision"] != second || merge["against"] != second ||
+ merge["body"] != want {
+ t.Fatalf("the reply's merge: %v", reply)
+ }
+ code, rebased := rig.postJSON(Ana, acme+"/draft", url.Values{
+ "rebase": {second}, "body": {want},
+ "version": {reply["version"].(string)}})
+ if code != http.StatusOK || rebased["merge"] != nil {
+ t.Fatalf("the rebase: %d %v", code, rebased)
+ }
+ if base, _ := rig.draftRow(t); idText(base) != second {
+ t.Fatal("the rebase did not move the draft's base")
+ }
+ saved := rig.post(t, Ana, acme+"/draft/save",
+ url.Values{"version": {rebased["version"].(string)}})
+ if saved.Header().Get("Location") != acme {
+ t.Fatalf("the commit after a rebase went to %q, not Saved",
+ saved.Header().Get("Location"))
+ }
+ if got := committed(t, rig, acme); got != want {
+ t.Fatalf("the commit did not keep M's bytes: %q", got)
+ }
+}
+
+func TestL9ARebaseIsRefusedAndMovesNothing(t *testing.T) {
+ rig := newRig(t)
+ moving := rig.clocked(t)
+ acme := rig.createPage(t, Ana, "Acme", fourBlocks)
+ other := rig.createPage(t, Ana, "Other", "Elsewhere.")
+ old := revisionOf(t, rig, acme)
+ if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{
+ "rebase": {old}, "body": {"x"}}); code !=
+ http.StatusUnprocessableEntity {
+ t.Fatalf("a rebase with no draft: %d", code)
+ }
+ moving.now = moving.now.Add(91 * 24 * time.Hour)
+ rig.post(t, Bo, acme, url.Values{"revision": {old},
+ "body": {"One, Bo."}})
+ current := revisionOf(t, rig, acme)
+ _, reply := rig.autosave(t, Ana, acme, current, "0", "Mine.")
+ version := reply["version"].(string)
+ for name, form := range map[string]url.Values{
+ "a stale version": {"rebase": {current}, "version": {"1"}},
+ "another page's revision": {"rebase": {revisionOf(t, rig, other)},
+ "version": {version}},
+ "a revision past the window": {"rebase": {old},
+ "version": {version}},
+ } {
+ form.Set("body", "Moved?")
+ code, _ := rig.postJSON(Ana, acme+"/draft", form)
+ base, stored := rig.draftRow(t)
+ if code < 400 || idText(base) != current ||
+ idText(stored) != version {
+ t.Errorf("%s: %d, and the draft moved", name, code)
+ }
+ }
+ if !strings.Contains(rig.page(t, Ana, acme), "Mine.") {
+ t.Fatal("a refused rebase wrote the draft's text")
+ }
+}
+
+func TestL9AConflictCarriesItsHunksAndResolveAnswersJSON(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", fourBlocks)
+ first := revisionOf(t, rig, acme)
+ _, reply := rig.autosave(t, Ana, acme, first, "0",
+ "One.\n\nTwo, Ana.\n\nThree.\n\nFour.")
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"One.\n\nTwo, Bo.\n\nThree.\n\nFour, Bo."}})
+ second := revisionOf(t, rig, acme)
+ _, reply = rig.autosave(t, Ana, acme, first,
+ reply["version"].(string), "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree."+
+ "\n\nFour.")
+ merge := mergeOf(t, reply)
+ hunks, _ := merge["hunks"].([]any)
+ if merge["conflict"] != true || merge["against"] != second ||
+ len(hunks) != 1 {
+ t.Fatalf("the conflict reply: %v", merge)
+ }
+ hunk := hunks[0].(map[string]any)
+ if hunk["at"] != 2.0 || hunk["count"] != 1.0 ||
+ hunk["base"] != "Two." || hunk["mine"] != "Two, Ana." ||
+ hunk["theirs"] != "Two, Bo." {
+ t.Fatalf("the hunk: %v", hunk)
+ }
+ events := rig.count(t, "events")
+ code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{
+ "current": {second}, "version": {reply["version"].(string)},
+ "choice-0": {"mine"}})
+ want := "Zero.\n\nOne.\n\nTwo, Ana.\n\nThree.\n\nFour, Bo."
+ if code != http.StatusOK || resolved["outcome"] != "saved" ||
+ resolved["body"] != want || resolved["revision"] == second {
+ t.Fatalf("Resolve's JSON: %d %v", code, resolved)
+ }
+ if rig.count(t, "events") != events+1 || rig.count(t, "drafts") != 0 ||
+ committed(t, rig, acme) != want {
+ t.Fatal("Resolve did not commit the choice exactly once")
+ }
+}
+
+func TestL9AResolveFromAStaleVersionCommitsNothing(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ first := revisionOf(t, rig, acme)
+ _, reply := rig.autosave(t, Ana, acme, first, "0", "One, Ana.")
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"One, Bo."}})
+ second := revisionOf(t, rig, acme)
+ rig.autosave(t, Ana, acme, first, reply["version"].(string),
+ "One, Ana, again.")
+ code, resolved := rig.postJSON(Ana, acme+"/resolve", url.Values{
+ "current": {second}, "version": {reply["version"].(string)},
+ "choice-0": {"mine"}})
+ if code != http.StatusOK || resolved["outcome"] != "conflicted" ||
+ resolved["revision"] != second || resolved["body"] != "One, Bo." {
+ t.Fatalf("a stale Resolve: %d %v", code, resolved)
+ }
+ if revisionOf(t, rig, acme) != second {
+ t.Fatal("a stale Resolve committed")
+ }
+}
+
+func TestL9CommitOneNeedsTheCurrentVersion(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ first := revisionOf(t, rig, acme)
+ _, tabA := rig.autosave(t, Ana, acme, first, "0", "One, tab A.")
+ offered := tabA["version"].(string)
+ // Another tab's draft is offered to this one; it never typed.
+ _, tabB := rig.autosave(t, Ana, acme, first, offered, "One, tab B.")
+ events := rig.count(t, "events")
+ code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{"commit": {"1"},
+ "version": {offered}, "body": {"One, closing tab A."}})
+ if code != http.StatusConflict || rig.count(t, "events") != events ||
+ revisionOf(t, rig, acme) != first {
+ t.Fatalf("commit=1 with a stale version: %d", code)
+ }
+ if _, stored := rig.draftRow(t); idText(stored) != tabB["version"] {
+ t.Fatal("commit=1 with a stale version wrote the draft")
+ }
+ code, saved := rig.postJSON(Ana, acme+"/draft", url.Values{
+ "commit": {"1"}, "version": {tabB["version"].(string)},
+ "body": {"One, closing tab B."}})
+ if code != http.StatusOK || saved["saved"] != true ||
+ committed(t, rig, acme) != "One, closing tab B." ||
+ rig.count(t, "drafts") != 0 {
+ t.Fatalf("commit=1 from the current version: %d %v", code, saved)
+ }
+}
+
+// Past the keepalive cap the closing tab sends no text: the draft as
+// last autosaved is committed.
+func TestL9CommitOneWithoutABodyCommitsTheStoredDraft(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ _, reply := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0",
+ "One, autosaved.")
+ if code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{
+ "commit": {"1"}}); code != http.StatusConflict {
+ t.Fatalf("commit=1 with no version and no text: %d", code)
+ }
+ code, _ := rig.postJSON(Ana, acme+"/draft", url.Values{
+ "commit": {"1"}, "version": {reply["version"].(string)}})
+ if code != http.StatusOK ||
+ committed(t, rig, acme) != "One, autosaved." {
+ t.Fatalf("commit=1 with no text: %d", code)
+ }
+}
+
+func TestL9ASaveThatChangesNothingWritesNothing(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.\n\nTwo.")
+ first := revisionOf(t, rig, acme)
+ day := "/d/2026-11-20"
+ rig.post(t, Ana, day, url.Values{"body": {"Written."}})
+ revisions, events := rig.count(t, "revisions"), rig.count(t, "events")
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"One.\n\nTwo.\n\n"}})
+ rig.post(t, Ana, day, url.Values{"body": {"Written."},
+ "revision": {revisionAs(t, rig, Ana, day)}})
+ if rig.count(t, "revisions") != revisions ||
+ rig.count(t, "events") != events || revisionOf(t, rig, acme) != first {
+ t.Fatal("a save of the same text wrote a revision")
+ }
+ // Merged, to exactly the current body: Ana's draft changed nothing
+ // that Bo's revision had not changed too.
+ _, reply := rig.autosave(t, Ana, acme, first, "0", "One.\n\nTwo, both.")
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"One.\n\nTwo, both."}})
+ revisions, events = rig.count(t, "revisions"), rig.count(t, "events")
+ saved := rig.post(t, Ana, acme+"/draft/save",
+ url.Values{"version": {reply["version"].(string)}})
+ if saved.Code != http.StatusSeeOther ||
+ rig.count(t, "revisions") != revisions ||
+ rig.count(t, "events") != events || rig.count(t, "drafts") != 0 {
+ t.Fatal("a merge equal to the current body wrote, or kept the draft")
+ }
+}
+
+func TestL9MakeTeamPageRefusesAStaleRevision(t *testing.T) {
+ rig := newRig(t)
+ day := "/d/2026-11-12"
+ rig.post(t, Ana, day, url.Values{"body": {"Mine.\n\nShare this."}})
+ note := between(rig.page(t, Ana, day), `href="/p/`, `/share"`)
+ form := rig.page(t, Ana, "/p/"+note+"/share")
+ rig.post(t, Ana, day, url.Values{"body": {"New first.\n\nMine." +
+ "\n\nShare this."}, "revision": {revisionAs(t, rig, Ana, day)}})
+ pages, events := rig.count(t, "pages"), rig.count(t, "events")
+ stale := rig.post(t, Ana, "/p/"+note+"/share", url.Values{
+ "title": {"Shared"}, "block": {"1"},
+ "revision": {formRevision(form)}})
+ if stale.Code != http.StatusUnprocessableEntity ||
+ !strings.Contains(stale.Body.String(), "Choose them again") {
+ t.Fatalf("a stale Make team page: %d", stale.Code)
+ }
+ if rig.count(t, "pages") != pages || rig.count(t, "events") != events {
+ t.Fatal("a stale Make team page wrote something")
+ }
+}
+
+func TestL9TheFirstScratchCommitIsSingleFlight(t *testing.T) {
+ rig := newRig(t)
+ token := "tab-token-0123456789"
+ if code, reply := rig.postJSON(Ana, "/scratch", url.Values{
+ "first": {"tab-empty-0123456789"}, "body": {" \n"}}); code !=
+ http.StatusUnprocessableEntity || reply["empty"] != true ||
+ rig.count(t, "pages") != 0 {
+ t.Fatalf("an empty first autosave: %d %v", code, reply)
+ }
+ var group sync.WaitGroup
+ replies := make([]map[string]any, 2)
+ for at := range replies {
+ group.Add(1)
+ go func() {
+ defer group.Done()
+ _, replies[at] = rig.postJSON(Ana, "/scratch", url.Values{
+ "first": {token}, "body": {"First words."}})
+ }()
+ }
+ group.Wait()
+ if rig.count(t, "pages") != 1 || replies[0]["page"] == nil ||
+ replies[0]["page"] != replies[1]["page"] {
+ t.Fatalf("two quick autosaves: %d pages, %v", rig.count(t, "pages"),
+ replies)
+ }
+ code, later := rig.postJSON(Ana, "/scratch", url.Values{
+ "first": {token}, "body": {"First words, and more."},
+ "version": {eitherVersion(replies[0]["version"],
+ replies[1]["version"])}})
+ if code != http.StatusOK || later["version"] == nil ||
+ rig.count(t, "pages") != 1 || rig.count(t, "drafts") != 1 {
+ t.Fatalf("a later autosave is not a draft on the page: %d %v", code,
+ later)
+ }
+}
+
+// eitherVersion is whichever reply carried a version: the one that became a
+// draft on the page the other made.
+func eitherVersion(values ...any) string {
+ for _, value := range values {
+ if text, ok := value.(string); ok {
+ return text
+ }
+ }
+ return "0"
+}
+
+func TestL9NoBaseAndGoneReplies(t *testing.T) {
+ rig := newRig(t)
+ moving := rig.clocked(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ first := revisionOf(t, rig, acme)
+ moving.now = moving.now.Add(91 * 24 * time.Hour)
+ _, reply := rig.autosave(t, Ana, acme, first, "0", "Mine.")
+ rig.post(t, Bo, acme, url.Values{"revision": {first},
+ "body": {"Bo's."}})
+ _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string),
+ "Mine, still.")
+ if mergeOf(t, reply)["noBase"] != true {
+ t.Fatalf("an aged base: %v", reply)
+ }
+ rig.post(t, Bo, acme+"/delete", nil)
+ _, reply = rig.autosave(t, Ana, acme, first, reply["version"].(string),
+ "Mine, after.")
+ if mergeOf(t, reply)["gone"] != true || reply["revision"] != nil {
+ t.Fatalf("a deleted page: %v", reply)
+ }
+ _, day := rig.autosave(t, Ana, "/d/2026-11-30", "0", "0", "A day.")
+ if day["revision"] != nil || day["merge"] != nil || day["version"] == "" {
+ t.Fatalf("an unwritten day's autosave: %v", day)
+ }
+}
+
+func idText(value int64) string { return strconv.FormatInt(value, 10) }