| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Gate 1, scope leaks nothing (spec §11): Bo's search results and their |
| 13 | // order, ⌘K's Create offer, [[ autocomplete, backlinks, unlinked |
| 14 | // mentions, and the report from "Update links" are byte-identical |
| 15 | // whether or not Ana's private notes contain the term, link to the page, |
| 16 | // hold a pending link with that text, or link to a block in it. And |
| 17 | // linking from a private note never writes to the target page. |
| 18 | |
| 19 | // gate1Clock pins every timestamp, so "edited just now" cannot differ. |
| 20 | var gate1Clock = time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC) |
| 21 | |
| 22 | // gate1World is the same team in both worlds; with private set, Ana has |
| 23 | // also written everything the gate names into her private notes. |
| 24 | func gate1World(t *testing.T, private bool) (*rig, string) { |
| 25 | t.Helper() |
| 26 | rig := newRig(t) |
| 27 | rig.built.Store.SetClock(func() time.Time { return gate1Clock }) |
| 28 | rig.built.Store.SetNonces(streams()) |
| 29 | // Ana's private writes are interleaved with the team's pages, so a |
| 30 | // page id that counted pages would differ between the worlds (review |
| 31 | // M2: /p/1 then /p/5 told Bo that three private pages lay between). |
| 32 | aside := func(body string) { |
| 33 | if private { |
| 34 | rig.post(t, Ana, "/scratch", url.Values{"body": {body}}) |
| 35 | } |
| 36 | } |
| 37 | // Bo's own note, in both worlds: ⌘K's "Your notes" holds it for him, |
| 38 | // and never for Ana. Written first, so its nonce is the same in both. |
| 39 | rig.post(t, Bo, "/scratch", url.Values{"body": {"zebracorn musings"}}) |
| 40 | acme := rig.createPage(t, Ana, "Acme", "Acme is a customer. ^blk") |
| 41 | aside("first private aside") |
| 42 | rig.createPage(t, Ana, "Notes", "We met the acme folks about "+ |
| 43 | "zebracorn feed.") |
| 44 | aside("second") |
| 45 | aside("third") |
| 46 | rig.createPage(t, Bo, "Plan", "See [[Acme]] and [[Zebracorn kit]].") |
| 47 | // Two pages whose order for "zebracorn feed" turns on the two words' |
| 48 | // document frequencies: if Ana's notes counted, they would swap. |
| 49 | rig.createPage(t, Bo, "Feedlot", "zebracorn feed feed feed feed") |
| 50 | aside("fourth") |
| 51 | rig.createPage(t, Bo, "Herd", "zebracorn zebracorn zebracorn "+ |
| 52 | "zebracorn feed") |
| 53 | if !private { |
| 54 | return rig, acme |
| 55 | } |
| 56 | before := revisionOf(t, rig, acme) |
| 57 | rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": { |
| 58 | "zebracorn zebracorn zebracorn acme\n\n[[Acme]] and " + |
| 59 | "[[Acme#^blk|the block]]\n\n[[Zebracorn Plans]] " + |
| 60 | "[[Acme Ltd]] Acme"}}) |
| 61 | rig.post(t, Ana, "/scratch", url.Values{"body": { |
| 62 | "Zebracorn. [[Zebracorn kit]] [[acme]] acme acme"}}) |
| 63 | if revisionOf(t, rig, acme) != before { |
| 64 | t.Fatal("a private note's link wrote to the page it links to") |
| 65 | } |
| 66 | return rig, acme |
| 67 | } |
| 68 | |
| 69 | // streams draws team page nonces from one counter and private page |
| 70 | // nonces from another: what random nonces give in expectation, made |
| 71 | // repeatable so two worlds can be compared byte for byte. |
| 72 | func streams() func(rung string) int64 { |
| 73 | team, private := int64(1000), int64(900000) |
| 74 | return func(rung string) int64 { |
| 75 | if rung == "team" { |
| 76 | team++ |
| 77 | return team |
| 78 | } |
| 79 | private++ |
| 80 | return private |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func revisionOf(t *testing.T, rig *rig, path string) string { |
| 85 | t.Helper() |
| 86 | return revisionAs(t, rig, Bo, path) |
| 87 | } |
| 88 | |
| 89 | // revisionAs is the revision who's editor for path starts from. |
| 90 | func revisionAs(t *testing.T, rig *rig, who, path string) string { |
| 91 | t.Helper() |
| 92 | edit := rig.page(t, who, path+"/edit") |
| 93 | return between(edit, `name="revision" value="`, `"`) |
| 94 | } |
| 95 | |
| 96 | // boSees is everything the gate compares, as Bo receives it. |
| 97 | func boSees(t *testing.T, rig *rig, acme string) map[string]string { |
| 98 | t.Helper() |
| 99 | seen := map[string]string{} |
| 100 | for _, path := range []string{ |
| 101 | "/search?q=zebracorn", "/search?q=acme", "/search?q=ac", |
| 102 | "/search?q=zebracorn+feed", "/search?q=acme+ltd", |
| 103 | "/search/suggest?q=zeb", "/search/suggest?q=acme", |
| 104 | "/search/suggest?q=Zebracorn+Plans", "/search/suggest?q=acme+ltd", |
| 105 | "/search/suggest?q=zebracorn", "/search/suggest?q=today", |
| 106 | "/search/suggest?q=thursday", "/search/suggest?q=12+nov", |
| 107 | "/search/suggest?q=2026-11-12", "/search/suggest?q=2026", |
| 108 | "/search?q=2026-11-12", "/search?q=today", |
| 109 | "/links/suggest?q=zeb", "/links/suggest?q=ac", |
| 110 | acme, "/pages", "/d/2026-11-12", |
| 111 | // The scoped accessor's day dots, through each of its two reads |
| 112 | // alone: November's month beside a week in October, and the |
| 113 | // week of 12 November beside October's month (review: the |
| 114 | // mutation was caught through one read only). |
| 115 | "/d/2026-10-20?month=2026-11", "/d/2026-11-13?month=2026-10", |
| 116 | } { |
| 117 | seen[path] = rig.page(t, Bo, path) |
| 118 | } |
| 119 | // The editor's chip states, for texts Ana's notes type too. |
| 120 | _, states := rig.postJSON(Bo, "/links/resolve", url.Values{"target": { |
| 121 | "Acme", "Acme#^blk", "Zebracorn Plans", "Zebracorn kit", |
| 122 | "Acme Ltd", "2026-11-12"}}) |
| 123 | seen["links resolve"] = fmt.Sprint(states) |
| 124 | renamed := rig.post(t, Bo, acme+"/rename", |
| 125 | url.Values{"title": {"Acme Holdings"}}) |
| 126 | seen["rename"] = renamed.Header().Get("Location") |
| 127 | if strings.HasSuffix(seen["rename"], "/links") { |
| 128 | seen["links offer"] = rig.page(t, Bo, acme+"/links") |
| 129 | } |
| 130 | updated := rig.post(t, Bo, acme+"/links", nil) |
| 131 | seen["links report"] = updated.Header().Get("Location") |
| 132 | seen["after"] = rig.page(t, Bo, acme) |
| 133 | // The in-page title's answers (build note 77): the offer's count and |
| 134 | // the old titles, as JSON. Revisions are random in both worlds. |
| 135 | _, inPlace := rig.postJSON(Bo, acme+"/rename", |
| 136 | url.Values{"title": {"Acme Group"}}) |
| 137 | seen["rename in place"] = fmt.Sprint(inPlace["title"], inPlace["links"], |
| 138 | inPlace["count"], inPlace["aliases"], inPlace["href"]) |
| 139 | _, relinked := rig.postJSON(Bo, acme+"/links", nil) |
| 140 | seen["links in place"] = fmt.Sprint(relinked) |
| 141 | return seen |
| 142 | } |
| 143 | |
| 144 | func TestGate1ScopeLeaksNothing(t *testing.T) { |
| 145 | quiet, acme := gate1World(t, false) |
| 146 | busy, busyAcme := gate1World(t, true) |
| 147 | if acme != busyAcme { |
| 148 | t.Fatalf("the worlds differ before Ana writes: %s, %s", acme, |
| 149 | busyAcme) |
| 150 | } |
| 151 | without, with := boSees(t, quiet, acme), boSees(t, busy, acme) |
| 152 | for key, want := range without { |
| 153 | want, got := steadyRevisions(want), steadyRevisions(with[key]) |
| 154 | if got != want { |
| 155 | t.Errorf("Bo's %s differs when Ana's private notes hold "+ |
| 156 | "the term:\n%s", key, firstDifference(want, got)) |
| 157 | } |
| 158 | } |
| 159 | if !strings.Contains(without["/search?q=zebracorn"], "Notes") { |
| 160 | t.Fatal("the comparison is vacuous: Bo's search found nothing") |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Ana's own view does see her notes, or the gate would pass because |
| 165 | // nobody sees anything. |
| 166 | func TestGate1AnaSeesHerOwn(t *testing.T) { |
| 167 | busy, acme := gate1World(t, true) |
| 168 | if !strings.Contains(busy.page(t, Ana, "/search?q=zebracorn"), |
| 169 | "Thursday 12 November 2026") { |
| 170 | t.Fatal("Ana's search does not find her own daily note") |
| 171 | } |
| 172 | if !strings.Contains(busy.page(t, Ana, acme), "Thursday 12 November") { |
| 173 | t.Fatal("Ana's backlinks do not show her own note") |
| 174 | } |
| 175 | if !strings.Contains(busy.page(t, Ana, "/links/suggest?q=zebracorn+p"), |
| 176 | "Zebracorn Plans") { |
| 177 | t.Fatal("Ana is not offered her own pending link") |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // islandRevision is a revision in the editor's island. Revisions are |
| 182 | // random in both worlds (doctrine §6.6), so they say nothing about |
| 183 | // Ana's notes and are compared as one value. |
| 184 | var islandRevision = regexp.MustCompile(`"(base|version)":"[0-9]+"`) |
| 185 | |
| 186 | func steadyRevisions(page string) string { |
| 187 | return islandRevision.ReplaceAllString(page, `"$1":"R"`) |
| 188 | } |
| 189 | |
| 190 | func firstDifference(want, got string) string { |
| 191 | for at := range min(len(want), len(got)) { |
| 192 | if want[at] != got[at] { |
| 193 | start := max(0, at-120) |
| 194 | return "without: " + want[start:min(len(want), at+120)] + |
| 195 | "\nwith: " + got[start:min(len(got), at+120)] |
| 196 | } |
| 197 | } |
| 198 | return "lengths differ" |
| 199 | } |
| 200 | |
| 201 | // Update links reports only what the actor may edit, and never edits |
| 202 | // Ana's private notes (§3.4). |
| 203 | func TestUpdateLinksNeverTouchesSomeoneElsesNotes(t *testing.T) { |
| 204 | busy, acme := gate1World(t, true) |
| 205 | // The note's text, as its editor holds it. The rest of the screen |
| 206 | // (its links panel) rightly shows the page's new title. |
| 207 | text := func() string { |
| 208 | return between(busy.page(t, Ana, "/d/2026-11-12/edit"), |
| 209 | "<textarea", "</textarea>") |
| 210 | } |
| 211 | daily := text() |
| 212 | busy.post(t, Bo, acme+"/rename", url.Values{"title": {"Renamed"}}) |
| 213 | busy.post(t, Bo, acme+"/links", nil) |
| 214 | if text() != daily { |
| 215 | t.Fatal("Bo's Update links edited Ana's daily note") |
| 216 | } |
| 217 | } |
| 218 | |