package mapatest import ( "fmt" "net/url" "regexp" "strings" "testing" "time" ) // Gate 1, scope leaks nothing (spec §11): Bo's search results and their // order, ⌘K's Create offer, [[ autocomplete, backlinks, unlinked // mentions, and the report from "Update links" are byte-identical // whether or not Ana's private notes contain the term, link to the page, // hold a pending link with that text, or link to a block in it. And // linking from a private note never writes to the target page. // gate1Clock pins every timestamp, so "edited just now" cannot differ. var gate1Clock = time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC) // gate1World is the same team in both worlds; with private set, Ana has // also written everything the gate names into her private notes. func gate1World(t *testing.T, private bool) (*rig, string) { t.Helper() rig := newRig(t) rig.built.Store.SetClock(func() time.Time { return gate1Clock }) rig.built.Store.SetNonces(streams()) // Ana's private writes are interleaved with the team's pages, so a // page id that counted pages would differ between the worlds (review // M2: /p/1 then /p/5 told Bo that three private pages lay between). aside := func(body string) { if private { rig.post(t, Ana, "/scratch", url.Values{"body": {body}}) } } // Bo's own note, in both worlds: ⌘K's "Your notes" holds it for him, // and never for Ana. Written first, so its nonce is the same in both. rig.post(t, Bo, "/scratch", url.Values{"body": {"zebracorn musings"}}) acme := rig.createPage(t, Ana, "Acme", "Acme is a customer. ^blk") aside("first private aside") rig.createPage(t, Ana, "Notes", "We met the acme folks about "+ "zebracorn feed.") aside("second") aside("third") rig.createPage(t, Bo, "Plan", "See [[Acme]] and [[Zebracorn kit]].") // Two pages whose order for "zebracorn feed" turns on the two words' // document frequencies: if Ana's notes counted, they would swap. rig.createPage(t, Bo, "Feedlot", "zebracorn feed feed feed feed") aside("fourth") rig.createPage(t, Bo, "Herd", "zebracorn zebracorn zebracorn "+ "zebracorn feed") if !private { return rig, acme } before := revisionOf(t, rig, acme) rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": { "zebracorn zebracorn zebracorn acme\n\n[[Acme]] and " + "[[Acme#^blk|the block]]\n\n[[Zebracorn Plans]] " + "[[Acme Ltd]] Acme"}}) rig.post(t, Ana, "/scratch", url.Values{"body": { "Zebracorn. [[Zebracorn kit]] [[acme]] acme acme"}}) if revisionOf(t, rig, acme) != before { t.Fatal("a private note's link wrote to the page it links to") } return rig, acme } // streams draws team page nonces from one counter and private page // nonces from another: what random nonces give in expectation, made // repeatable so two worlds can be compared byte for byte. func streams() func(rung string) int64 { team, private := int64(1000), int64(900000) return func(rung string) int64 { if rung == "team" { team++ return team } private++ return private } } func revisionOf(t *testing.T, rig *rig, path string) string { t.Helper() return revisionAs(t, rig, Bo, path) } // revisionAs is the revision who's editor for path starts from. func revisionAs(t *testing.T, rig *rig, who, path string) string { t.Helper() edit := rig.page(t, who, path+"/edit") return between(edit, `name="revision" value="`, `"`) } // boSees is everything the gate compares, as Bo receives it. func boSees(t *testing.T, rig *rig, acme string) map[string]string { t.Helper() seen := map[string]string{} for _, path := range []string{ "/search?q=zebracorn", "/search?q=acme", "/search?q=ac", "/search?q=zebracorn+feed", "/search?q=acme+ltd", "/search/suggest?q=zeb", "/search/suggest?q=acme", "/search/suggest?q=Zebracorn+Plans", "/search/suggest?q=acme+ltd", "/search/suggest?q=zebracorn", "/search/suggest?q=today", "/search/suggest?q=thursday", "/search/suggest?q=12+nov", "/search/suggest?q=2026-11-12", "/search/suggest?q=2026", "/search?q=2026-11-12", "/search?q=today", "/links/suggest?q=zeb", "/links/suggest?q=ac", acme, "/pages", "/d/2026-11-12", // The scoped accessor's day dots, through each of its two reads // alone: November's month beside a week in October, and the // week of 12 November beside October's month (review: the // mutation was caught through one read only). "/d/2026-10-20?month=2026-11", "/d/2026-11-13?month=2026-10", } { 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") if strings.HasSuffix(seen["rename"], "/links") { seen["links offer"] = rig.page(t, Bo, acme+"/links") } updated := rig.post(t, Bo, acme+"/links", nil) seen["links report"] = updated.Header().Get("Location") seen["after"] = rig.page(t, Bo, acme) // The in-page title's answers (build note 77): the offer's count and // the old titles, as JSON. Revisions are random in both worlds. _, inPlace := rig.postJSON(Bo, acme+"/rename", url.Values{"title": {"Acme Group"}}) seen["rename in place"] = fmt.Sprint(inPlace["title"], inPlace["links"], inPlace["count"], inPlace["aliases"], inPlace["href"]) _, relinked := rig.postJSON(Bo, acme+"/links", nil) seen["links in place"] = fmt.Sprint(relinked) return seen } func TestGate1ScopeLeaksNothing(t *testing.T) { quiet, acme := gate1World(t, false) busy, busyAcme := gate1World(t, true) if acme != busyAcme { t.Fatalf("the worlds differ before Ana writes: %s, %s", acme, busyAcme) } without, with := boSees(t, quiet, acme), boSees(t, busy, acme) for key, want := range without { want, got := steadyRevisions(want), steadyRevisions(with[key]) if got != want { t.Errorf("Bo's %s differs when Ana's private notes hold "+ "the term:\n%s", key, firstDifference(want, got)) } } if !strings.Contains(without["/search?q=zebracorn"], "Notes") { t.Fatal("the comparison is vacuous: Bo's search found nothing") } } // Ana's own view does see her notes, or the gate would pass because // nobody sees anything. func TestGate1AnaSeesHerOwn(t *testing.T) { busy, acme := gate1World(t, true) if !strings.Contains(busy.page(t, Ana, "/search?q=zebracorn"), "Thursday 12 November 2026") { t.Fatal("Ana's search does not find her own daily note") } if !strings.Contains(busy.page(t, Ana, acme), "Thursday 12 November") { t.Fatal("Ana's backlinks do not show her own note") } if !strings.Contains(busy.page(t, Ana, "/links/suggest?q=zebracorn+p"), "Zebracorn Plans") { t.Fatal("Ana is not offered her own pending link") } } // islandRevision is a revision in the editor's island. Revisions are // random in both worlds (doctrine §6.6), so they say nothing about // Ana's notes and are compared as one value. var islandRevision = regexp.MustCompile(`"(base|version)":"[0-9]+"`) func steadyRevisions(page string) string { return islandRevision.ReplaceAllString(page, `"$1":"R"`) } func firstDifference(want, got string) string { for at := range min(len(want), len(got)) { if want[at] != got[at] { start := max(0, at-120) return "without: " + want[start:min(len(want), at+120)] + "\nwith: " + got[start:min(len(got), at+120)] } } return "lengths differ" } // Update links reports only what the actor may edit, and never edits // Ana's private notes (§3.4). func TestUpdateLinksNeverTouchesSomeoneElsesNotes(t *testing.T) { busy, acme := gate1World(t, true) // The note's text, as its editor holds it. The rest of the screen // (its links panel) rightly shows the page's new title. text := func() string { return between(busy.page(t, Ana, "/d/2026-11-12/edit"), "") } daily := text() busy.post(t, Bo, acme+"/rename", url.Values{"title": {"Renamed"}}) busy.post(t, Bo, acme+"/links", nil) if text() != daily { t.Fatal("Bo's Update links edited Ana's daily note") } }