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 "strings"
7 "testing"
8 "time"
9)
10
11// traceTables is every table a written day would leave a row in.
12var traceTables = []string{"members", "pages", "drafts", "events",
13 "daily_prints", "revisions", "title_prints"}
14
15// Gate 10: opening today's note and leaving without typing creates no
16// row, no draft, no event, and so no S1 entry (S1 is the event log).
17func TestGate10AnUnwrittenDayLeavesNoTrace(t *testing.T) {
18 rig := newRig(t)
19 for _, path := range []string{"/", "/d/2026-11-12",
20 "/d/2026-11-12/edit", "/d/1999-01-01", "/settings", "/scratch/new"} {
21 if code := rig.get(t, Ana, path).Code; code != http.StatusOK {
22 t.Fatalf("GET %s: %d", path, code)
23 }
24 }
25 empty := rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {" "}})
26 if empty.Code != http.StatusUnprocessableEntity {
27 t.Fatalf("an empty save: %d", empty.Code)
28 }
29 for _, table := range traceTables {
30 if rows := rig.count(t, table); rows != 0 {
31 t.Errorf("visiting left %d rows in %s", rows, table)
32 }
33 }
34}
35
36func TestTheFirstSaveCreatesTheDay(t *testing.T) {
37 rig := newRig(t)
38 saved := rig.post(t, Ana, "/d/2026-11-12",
39 url.Values{"body": {"Called Acme."}})
40 if saved.Header().Get("Location") != "/d/2026-11-12" {
41 t.Fatalf("save: %d %q", saved.Code, saved.Header().Get("Location"))
42 }
43 if !strings.Contains(rig.page(t, Ana, "/d/2026-11-12"), "Called Acme.") {
44 t.Fatal("the note does not show its text")
45 }
46 if strings.Contains(rig.page(t, Bo, "/d/2026-11-12"), "Called Acme.") {
47 t.Fatal("Bo's 12 November shows Ana's note")
48 }
49}
50
51// Date links open the reader's own note (§3.4, ruled): the same link in a
52// team page leads Ana to hers and Bo to his.
53func TestADateLinkOpensTheReadersOwnNote(t *testing.T) {
54 rig := newRig(t)
55 rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Ana writes here."}})
56 rig.post(t, Bo, "/d/2026-11-12", url.Values{"body": {"Bo writes here."}})
57 plan := rig.createPage(t, Ana, "Plan", "See [[2026-11-12]].")
58 for _, who := range []string{Ana, Bo} {
59 drawn := rig.page(t, who, plan)
60 if !strings.Contains(drawn,
61 `href="/d/2026-11-12" mapa-chip="date"`) ||
62 !strings.Contains(drawn, `</svg>Thursday, 12 November`) {
63 t.Fatal("the date link does not lead to /d/2026-11-12, " +
64 "named in words")
65 }
66 }
67 anaDay := rig.page(t, Ana, "/d/2026-11-12")
68 boDay := rig.page(t, Bo, "/d/2026-11-12")
69 if !strings.Contains(anaDay, "Ana writes here.") ||
70 !strings.Contains(boDay, "Bo writes here.") ||
71 strings.Contains(boDay, "Ana writes here.") {
72 t.Fatal("a date link reached someone else's note")
73 }
74 if !strings.Contains(boDay, ">Plan</a>") {
75 t.Fatal("the team page linking the day is not a backlink")
76 }
77}
78
79// "Today" is in the person's own time zone, defaulting to the team's.
80func TestTodayIsInThePersonsTimeZone(t *testing.T) {
81 rig := newRig(t)
82 rig.built.Store.SetClock(func() time.Time {
83 return time.Date(2026, 11, 12, 23, 30, 0, 0, time.UTC)
84 })
85 if !strings.Contains(rig.page(t, Ana, "/"), "Thursday 12 November") {
86 t.Fatal("the team's zone (Dublin) does not make it the 12th")
87 }
88 set := rig.post(t, Ana, "/settings",
89 url.Values{"zone": {"Pacific/Auckland"}})
90 if set.Code != http.StatusSeeOther {
91 t.Fatalf("setting a zone: %d", set.Code)
92 }
93 if !strings.Contains(rig.page(t, Ana, "/"), "Friday 13 November") {
94 t.Fatal("Ana's own zone is not used")
95 }
96 if !strings.Contains(rig.page(t, Bo, "/"), "Thursday 12 November") {
97 t.Fatal("Ana's setting changed Bo's day")
98 }
99 bad := rig.post(t, Ana, "/settings", url.Values{"zone": {"Mars/Base"}})
100 if bad.Code != http.StatusUnprocessableEntity {
101 t.Fatalf("a bad zone: %d", bad.Code)
102 }
103}
104
105func TestMakeTeamPageFromADailyNote(t *testing.T) {
106 rig := newRig(t)
107 rig.post(t, Ana, "/d/2026-11-12",
108 url.Values{"body": {"Mine.\n\nThe team should know this."}})
109 day := rig.page(t, Ana, "/d/2026-11-12")
110 share := between(day, `href="/p/`, `/share"`)
111 form := rig.page(t, Ana, "/p/"+share+"/share")
112 if !strings.Contains(form, `name="block" value="1"`) {
113 t.Fatal("the share form offers no blocks")
114 }
115 made := rig.post(t, Ana, "/p/"+share+"/share",
116 url.Values{"title": {"Known"}, "block": {"1"},
117 "revision": {formRevision(form)}})
118 if made.Code != http.StatusSeeOther {
119 t.Fatalf("share: %d", made.Code)
120 }
121 if !strings.Contains(rig.page(t, Bo, made.Header().Get("Location")),
122 "The team should know this.") {
123 t.Fatal("Bo cannot read the shared page")
124 }
125 again := rig.page(t, Ana, "/p/"+share+"/share")
126 taken := rig.post(t, Ana, "/p/"+share+"/share",
127 url.Values{"title": {"known"}, "block": {"0"},
128 "revision": {formRevision(again)}})
129 if taken.Code != http.StatusUnprocessableEntity ||
130 !strings.Contains(taken.Body.String(), `value="0" checked`) {
131 t.Fatal("a taken title must ask again, keeping the choice")
132 }
133}
134
135func TestAPrivatePageIsNotThereForAnyoneElse(t *testing.T) {
136 rig := newRig(t)
137 created := rig.post(t, Ana, "/scratch", url.Values{"body": {"secret"}})
138 path := created.Header().Get("Location")
139 if rig.get(t, Ana, path).Code != http.StatusOK {
140 t.Fatal("Ana cannot open her scratch page")
141 }
142 for _, suffix := range []string{"", "/edit", "/delete", "/share"} {
143 if code := rig.get(t, Bo, path+suffix).Code; code != 404 {
144 t.Errorf("Bo GET %s%s: %d, want 404", path, suffix, code)
145 }
146 }
147 // Ana's private page and a page that does not exist must answer Bo
148 // alike, every write route included (review N3).
149 missing := "/p/zzzzzzzzzz"
150 for _, suffix := range []string{"", "/delete", "/share", "/draft",
151 "/draft/save", "/draft/discard", "/draft/scratch", "/resolve",
152 "/blocks", "/rename", "/links", "/mentions"} {
153 form := url.Values{"body": {"x"}, "revision": {"1"},
154 "title": {"T"}, "block": {"0"}, "version": {"0"}}
155 private := rig.post(t, Bo, path+suffix, form).Code
156 absent := rig.post(t, Bo, missing+suffix, form).Code
157 if private != http.StatusNotFound || absent != private {
158 t.Errorf("Bo POST %s: %d on Ana's page, %d on none; want 404 "+
159 "for both", suffix, private, absent)
160 }
161 }
162}
163
164// formRevision is the revision a form names: the one its blocks or its
165// text were drawn from.
166func formRevision(form string) string {
167 return between(form, `name="revision" value="`, `"`)
168}
169