oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "net/http"
5 "net/url"
6 "testing"
7)
8
9// Gate L9's server half for Fable's final review of 58fd476: a commit
10// that makes no revision says so, and a closing tab still settling after
11// a reload writes its text either way.
12
13// An emptied draft on an unwritten day commits nothing and is dropped,
14// so the tab's next text starts a new draft instead of meeting the
15// empty one as newer (review H2).
16func TestL9CommittingAnEmptiedDayDropsTheDraft(t *testing.T) {
17 rig := newRig(t)
18 day := "/d/2026-11-21"
19 _, reply := rig.autosave(t, Ana, day, "0", "0", "A day.")
20 _, reply = rig.autosave(t, Ana, day, "0", reply["version"].(string), "")
21 revisions, events := rig.count(t, "revisions"), rig.count(t, "events")
22 code, saved := rig.postJSON(Ana, day+"/draft/save",
23 url.Values{"version": {reply["version"].(string)}})
24 if code != http.StatusOK || saved["saved"] != false ||
25 saved["empty"] != true || saved["revision"] != nil {
26 t.Fatalf("an emptied day's commit: %d %v", code, saved)
27 }
28 if rig.count(t, "drafts") != 0 || rig.count(t, "revisions") != revisions ||
29 rig.count(t, "events") != events {
30 t.Fatal("an emptied day's commit kept its draft or wrote")
31 }
32 code, next := rig.autosave(t, Ana, day, "0", "0", "Next.")
33 if code != http.StatusOK || next["version"] == nil {
34 t.Fatalf("the next text after an emptied commit: %d %v", code, next)
35 }
36}
37
38// A page deleted under the commit is not saved, and its draft stays
39// for Today's offer (review H2's race).
40func TestL9ACommitOnADeletedPageIsNotSaved(t *testing.T) {
41 rig := newRig(t)
42 acme := rig.createPage(t, Ana, "Acme", "One.")
43 _, reply := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0",
44 "One, mine.")
45 rig.post(t, Bo, acme+"/delete", nil)
46 code, saved := rig.postJSON(Ana, acme+"/draft/save",
47 url.Values{"version": {reply["version"].(string)}})
48 if code != http.StatusOK || saved["saved"] != false ||
49 saved["gone"] != true || saved["revision"] != nil {
50 t.Fatalf("a commit on a deleted page: %d %v", code, saved)
51 }
52 if rig.count(t, "drafts") != 1 {
53 t.Fatal("the deleted page's draft was not kept")
54 }
55}
56
57// A closing tab still settling after a reload sends the version its
58// page loaded with settling=1: committed meanwhile or not, the text is
59// written (build note 38, review M3). A stale version is still refused.
60func TestL9ClosingWhileSettlingWritesTheTextEitherWay(t *testing.T) {
61 rig := newRig(t)
62 acme := rig.createPage(t, Ana, "Acme", "One.")
63 first := revisionOf(t, rig, acme)
64 _, reply := rig.autosave(t, Ana, acme, first, "0", "One, mine.")
65 loaded := reply["version"].(string)
66 // The reload's own commit happened: no draft is left.
67 rig.postJSON(Ana, acme+"/draft/save", url.Values{"version": {loaded}})
68 // The text is a new draft from the old base, so it meets Ana's own
69 // committed block as a conflict: kept as her draft, never dropped.
70 code, saved := rig.postJSON(Ana, acme+"/draft", url.Values{
71 "commit": {"1"}, "settling": {"1"}, "version": {loaded},
72 "base": {first}, "body": {"One, mine. More."}})
73 if code != http.StatusOK ||
74 islandOf(t, rig.page(t, Ana, acme)).Source != "One, mine. More." {
75 t.Fatalf("settling after the commit: %d %v", code, saved)
76 }
77 rig.postJSON(Ana, acme+"/draft/discard", url.Values{
78 "version": {idText(rig.draftVersion(t))}})
79 // The reload's commit could not happen: the draft is still there.
80 second := revisionOf(t, rig, acme)
81 _, reply = rig.autosave(t, Ana, acme, second, "0", "One, again.")
82 held := reply["version"].(string)
83 code, _ = rig.postJSON(Ana, acme+"/draft", url.Values{
84 "commit": {"1"}, "settling": {"1"}, "version": {held},
85 "base": {second}, "body": {"One, again. More."}})
86 if code != http.StatusOK || committed(t, rig, acme) != "One, again. More." {
87 t.Fatalf("settling with the draft still stored: %d", code)
88 }
89 // Another tab's newer draft is never overwritten.
90 third := revisionOf(t, rig, acme)
91 _, reply = rig.autosave(t, Ana, acme, third, "0", "Tab A.")
92 _, newer := rig.autosave(t, Ana, acme, third, reply["version"].(string),
93 "Tab B.")
94 code, _ = rig.postJSON(Ana, acme+"/draft", url.Values{
95 "commit": {"1"}, "settling": {"1"},
96 "version": {reply["version"].(string)},
97 "base": {third}, "body": {"Tab A, closing."}})
98 if _, stored := rig.draftRow(t); code != http.StatusConflict ||
99 idText(stored) != newer["version"] {
100 t.Fatalf("settling from a stale version: %d", code)
101 }
102}
103
104// draftVersion is the person's one draft's version, as stored.
105func (rig *rig) draftVersion(t *testing.T) int64 {
106 t.Helper()
107 _, version := rig.draftRow(t)
108 return version
109}
110