| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // What the editor is given (level-up §6.8): the JSON island on every |
| 12 | // editable page, the draft served as the page body, and |
| 13 | // POST /links/resolve. L7: none of it writes. |
| 14 | |
| 15 | func TestTheIslandSurvivesABodyThatClosesTheScript(t *testing.T) { |
| 16 | rig := newRig(t) |
| 17 | body := "</script><script>alert(1)</script>\n\n<!-- a
b & c -->" |
| 18 | acme := rig.createPage(t, Ana, "Acme", body) |
| 19 | page := rig.page(t, Bo, acme) |
| 20 | if strings.Count(page, "<script>alert(1)") != 0 { |
| 21 | t.Fatal("the body's script reached the page as markup") |
| 22 | } |
| 23 | if got := islandOf(t, page); got.Source != body || got.Draft != nil || |
| 24 | got.Base != revisionOf(t, rig, acme) { |
| 25 | t.Fatalf("the island read back %+v", got) |
| 26 | } |
| 27 | if rig.get(t, Bo, acme).Header().Get("Cache-Control") != "no-store" { |
| 28 | t.Fatal("a page carrying the island may be cached") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestTheIslandCarriesTheDraftAndThePageShowsIt(t *testing.T) { |
| 33 | rig := newRig(t) |
| 34 | acme := rig.createPage(t, Ana, "Acme", "Saved text. [[Acme]]") |
| 35 | base := revisionOf(t, rig, acme) |
| 36 | _, reply := rig.autosave(t, Ana, acme, base, "0", |
| 37 | "Unsaved text. [[Nothing yet]] [[2026-11-12]]") |
| 38 | page := rig.page(t, Ana, acme) |
| 39 | data := islandOf(t, page) |
| 40 | if data.Draft == nil || data.Draft.Version != reply["version"] || |
| 41 | data.Draft.Base != base || data.Base != base || |
| 42 | !strings.HasPrefix(data.Source, "Unsaved text.") { |
| 43 | t.Fatalf("Ana's island: %+v", data) |
| 44 | } |
| 45 | if data.Links["Nothing yet"].Kind != "pending" || |
| 46 | data.Links["2026-11-12"].Kind != "date" || len(data.Links) != 2 { |
| 47 | t.Fatalf("the island's chips: %+v", data.Links) |
| 48 | } |
| 49 | article := between(page, "<article mapa-body rst-section>", "</article>") |
| 50 | if !strings.Contains(article, "Unsaved text.") || |
| 51 | !strings.Contains(page, "You're seeing your unsaved version.") || |
| 52 | !strings.Contains(page, `href="`+acme+`?saved=1"`) { |
| 53 | t.Fatal("the page does not show Ana her draft, with the bar") |
| 54 | } |
| 55 | saved := rig.page(t, Ana, acme+"?saved=1") |
| 56 | if !strings.Contains(between(saved, "<article mapa-body rst-section>", |
| 57 | "</article>"), "Saved text.") || |
| 58 | strings.Contains(saved, `id="mapa-editor"`) || |
| 59 | !strings.Contains(saved, "You're seeing the saved version.") { |
| 60 | t.Fatal("?saved=1 does not show the committed text") |
| 61 | } |
| 62 | if strings.Contains(rig.page(t, Bo, acme), "Unsaved text.") { |
| 63 | t.Fatal("Bo is shown Ana's draft") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestEveryEditablePageCarriesTheIsland(t *testing.T) { |
| 68 | rig := newRig(t) |
| 69 | rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Day."}}) |
| 70 | scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}) |
| 71 | for path, source := range map[string]string{ |
| 72 | "/d/2026-11-12": "Day.", "/d/2026-11-13": "", |
| 73 | scratch.Header().Get("Location"): "Mine.", "/scratch/new": "", |
| 74 | } { |
| 75 | if got := islandOf(t, rig.page(t, Ana, path)); got.Source != source { |
| 76 | t.Errorf("%s: the island holds %q", path, got.Source) |
| 77 | } |
| 78 | } |
| 79 | if islandOf(t, rig.page(t, Ana, "/d/2026-11-13")).Base != "0" { |
| 80 | t.Fatal("an unwritten day's island has a base") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestLinksResolveAnswersEachChipsState(t *testing.T) { |
| 85 | rig := newRig(t) |
| 86 | acme := rig.createPage(t, Ana, "Acme", "Customer. ^blk") |
| 87 | code, reply := rig.postJSON(Bo, "/links/resolve", url.Values{ |
| 88 | "target": {"Acme", "acme#^blk", "Acme#^gone", "Not yet", |
| 89 | "2026-11-12", "2026-02-30"}}) |
| 90 | state := func(key string) map[string]any { |
| 91 | found, _ := reply[key].(map[string]any) |
| 92 | return found |
| 93 | } |
| 94 | if code != http.StatusOK || state("Acme")["kind"] != "team" || |
| 95 | state("Acme")["href"] != acme || state("Acme")["title"] != "Acme" || |
| 96 | state("acme#^blk")["kind"] != "block" || |
| 97 | state("acme#^blk")["href"] != acme+"#b-blk" || |
| 98 | state("Acme#^gone")["kind"] != "block gone" || |
| 99 | state("Not yet")["kind"] != "pending" || |
| 100 | state("2026-11-12")["href"] != "/d/2026-11-12" || |
| 101 | state("2026-02-30")["kind"] != "broken" { |
| 102 | t.Fatalf("/links/resolve: %d %v", code, reply) |
| 103 | } |
| 104 | many := url.Values{} |
| 105 | for at := range 33 { |
| 106 | many.Add("target", fmt.Sprint("Page ", at)) |
| 107 | } |
| 108 | if code, _ := rig.postJSON(Bo, "/links/resolve", many); code != |
| 109 | http.StatusUnprocessableEntity { |
| 110 | t.Fatalf("33 targets: %d", code) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // L7: /links/resolve, the island's pages, a page with an offered draft |
| 115 | // opened and left, and New scratch page opened and left write nothing. |
| 116 | func TestL7TheEditorsReadsWriteNothing(t *testing.T) { |
| 117 | busy, acme := gate1World(t, true) |
| 118 | _, offered := busy.autosave(t, Ana, acme, revisionOf(t, busy, acme), |
| 119 | "0", "Offered.") |
| 120 | before := tableRowsHash(t, busy) |
| 121 | for _, who := range []string{Ana, Bo} { |
| 122 | for _, path := range []string{acme, acme + "?saved=1", "/", |
| 123 | "/d/2026-11-12", "/d/2026-11-13", "/scratch/new"} { |
| 124 | if code := busy.get(t, who, path).Code; code != http.StatusOK { |
| 125 | t.Fatalf("%s GET %s: %d", who, path, code) |
| 126 | } |
| 127 | } |
| 128 | code, _ := busy.postJSON(who, "/links/resolve", url.Values{ |
| 129 | "target": {"Acme", "Zebracorn Plans", "2026-11-12"}}) |
| 130 | if code != http.StatusOK { |
| 131 | t.Fatalf("%s /links/resolve: %d", who, code) |
| 132 | } |
| 133 | } |
| 134 | if tableRowsHash(t, busy) != before { |
| 135 | t.Fatal("an editor read changed the database's rows") |
| 136 | } |
| 137 | busy.autosave(t, Ana, acme, "0", offered["version"].(string), |
| 138 | "Typed into.") |
| 139 | if tableRowsHash(t, busy) == before { |
| 140 | t.Fatal("the hash is vacuous: a draft write did not change it") |
| 141 | } |
| 142 | } |
| 143 | |