oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/mapa/internal/mapa/notionmd"
10 "amadan.net/oficina/notion.git/notiontest"
11)
12
13// I23 (columns and icons spec §6.3, §9): the import pins its roots to
14// the runner's sidebar, after their own pins, once; a re-run pins
15// nothing; a root the person unpinned stays unpinned; nobody else's
16// pins change.
17
18func pinTitles(t *testing.T, world *importWorld, who string) []string {
19 t.Helper()
20 viewer, err := world.store.Viewer(ctx(), who, nil)
21 if err != nil {
22 t.Fatal(err)
23 }
24 ids, err := world.store.Pins(ctx(), viewer)
25 if err != nil {
26 t.Fatal(err)
27 }
28 var titles []string
29 for _, id := range ids {
30 var page Page
31 if err := world.store.database.First(&page, id).Error; err != nil {
32 t.Fatal(err)
33 }
34 record, err := world.store.OpenPage(page)
35 if err != nil {
36 t.Fatal(err)
37 }
38 titles = append(titles, record.Title)
39 }
40 return titles
41}
42
43func TestI23TheImportPinsItsRootsOnce(t *testing.T) {
44 doc := notiontest.NewDocumented()
45 world := newImportWorld(t, doc.Workspace)
46 mine := mustCreate(t, world.store, "Mine", "Text.")
47 viewer, _ := world.store.Viewer(ctx(), ana, nil)
48 if err := world.store.SetPin(ctx(), viewer, mine.ID, true); err != nil {
49 t.Fatal(err)
50 }
51 world.connect(ana)
52 run := world.importAs(ana)
53 if run.State != runDone {
54 t.Fatalf("first run: %+v", run)
55 }
56 pins := pinTitles(t, world, ana)
57 report, err := world.store.report(ctx(), run)
58 if err != nil || report.Pinned != len(pins)-1 ||
59 report.Offered != report.Pinned {
60 t.Fatalf("the report's pins: %+v %v", report, err)
61 }
62 if len(pins) < 2 || pins[0] != "Mine" {
63 t.Fatalf("Ana's pins: %v", pins)
64 }
65 for _, title := range pins[1:] {
66 _, record, ok := world.pageTitled(title)
67 if !ok || strings.HasPrefix(record.Body, "In: ") {
68 t.Errorf("pinned %q, which is not a root", title)
69 }
70 }
71 if got := pinTitles(t, world, bo); len(got) != 0 {
72 t.Fatalf("Bo's pins changed: %v", got)
73 }
74 root, _, _ := world.pageTitled(pins[1])
75 viewer, _ = world.store.Viewer(ctx(), ana, nil)
76 if err := world.store.SetPin(ctx(), viewer, root.ID, false); err != nil {
77 t.Fatal(err)
78 }
79 events := world.count("events", "verb = ?", VerbPinsSet)
80 world.server.Reset()
81 if run := world.importAs(ana); run.State != runDone {
82 t.Fatalf("second run: %+v", run)
83 }
84 if world.count("events", "verb = ?", VerbPinsSet) != events {
85 t.Fatal("a re-run wrote the pins again")
86 }
87 for _, title := range pinTitles(t, world, ana) {
88 if title == pins[1] {
89 t.Fatal("a root Ana unpinned was pinned again")
90 }
91 }
92}
93
94// I23, no free slot: a sidebar already holding maxPins pins gains none,
95// every root is still marked, listed by the report, and one pins event
96// is logged; a re-run offers nothing again.
97func TestI23WithNoFreeSlotRootsAreMarkedAndListed(t *testing.T) {
98 doc := notiontest.NewDocumented()
99 world := newImportWorld(t, doc.Workspace)
100 viewer, _ := world.store.Viewer(ctx(), ana, nil)
101 for at := range maxPins {
102 page := mustCreate(t, world.store, "Full "+strings.Repeat("x", at+1),
103 "Text.")
104 if err := world.store.SetPin(ctx(), viewer, page.ID,
105 true); err != nil {
106 t.Fatal(err)
107 }
108 }
109 before := pinTitles(t, world, ana)
110 events := world.count("events", "verb = ?", VerbPinsSet)
111 world.connect(ana)
112 run := world.importAs(ana)
113 report, err := world.store.report(ctx(), run)
114 if err != nil || report.Pinned != 0 || report.Offered == 0 ||
115 len(report.Unpinned) != report.Offered ||
116 report.PinLimit != maxPins {
117 t.Fatalf("the report: %+v %v", report, err)
118 }
119 if got := pinTitles(t, world, ana); strings.Join(got, "|") !=
120 strings.Join(before, "|") {
121 t.Fatalf("a full sidebar changed: %v", got)
122 }
123 if world.count("events", "verb = ?", VerbPinsSet) != events+1 {
124 t.Fatal("the marks were written without their one pins event")
125 }
126 world.server.Reset()
127 world.importAs(ana)
128 if world.count("events", "verb = ?", VerbPinsSet) != events+1 {
129 t.Fatal("a re-run offered the marked roots again")
130 }
131}
132
133// clearPinMarks takes every pin mark away, as the map stood before this
134// feature: the next run offers the roots an earlier run created.
135func (world *importWorld) clearPinMarks() {
136 world.t.Helper()
137 var rows []NotionPage
138 world.store.database.Find(&rows)
139 for _, row := range rows {
140 place, err := world.store.openCoordinate(row)
141 if err != nil || place.Pin == nil {
142 continue
143 }
144 place.Pin = nil
145 sealed, err := world.store.sealCoordinate(place)
146 if err != nil {
147 world.t.Fatal(err)
148 }
149 world.store.database.Model(&NotionPage{}).Where("id = ?", row.ID).
150 Update("sealed_coordinate", sealed)
151 }
152}
153
154// I23, an earlier run's roots: a map with no marks (Paul's existing
155// import) has its roots pinned by the next run, and a root the person
156// pinned already counts as pinned, not as one with no slot (Fable M7).
157func TestI23AnEarlierRunsRootsArePinnedOnce(t *testing.T) {
158 doc := notiontest.NewDocumented()
159 world := newImportWorld(t, doc.Workspace)
160 world.connect(ana)
161 first := world.importAs(ana)
162 report, _ := world.store.report(ctx(), first)
163 roots := report.Offered
164 viewer, _ := world.store.Viewer(ctx(), ana, nil)
165 pins := pinTitles(t, world, ana)
166 for _, title := range pins[1:] {
167 page, _, _ := world.pageTitled(title)
168 if err := world.store.SetPin(ctx(), viewer, page.ID,
169 false); err != nil {
170 t.Fatal(err)
171 }
172 }
173 world.clearPinMarks()
174 world.server.Reset()
175 again := world.importAs(ana)
176 report, err := world.store.report(ctx(), again)
177 if err != nil || report.Offered != roots || report.Pinned != roots ||
178 len(report.Unpinned) != 0 {
179 t.Fatalf("an earlier run's roots: %+v %v", report, err)
180 }
181 if got := pinTitles(t, world, ana); len(got) != roots {
182 t.Fatalf("pinned %v, want the %d roots once each", got, roots)
183 }
184}
185
186// I23, whose sidebar: a run Ana started and Bo resumed pins the roots to
187// Bo's sidebar, the run's actor at the pin write, and Ana's is untouched.
188func TestI23AResumedRunPinsToTheResumer(t *testing.T) {
189 doc := notiontest.NewDocumented()
190 world := newImportWorld(t, doc.Workspace)
191 world.connect(ana)
192 lease, err := world.store.StartRun(ctx(), ana, false)
193 if err != nil {
194 t.Fatal(err)
195 }
196 crashOn(t, "write:fixed", 1)
197 world.finish(lease, ana)
198 importCrash = nil
199 world.store.InterruptRuns(ctx())
200 resumed, err := world.store.ResumeRun(ctx(), bo)
201 if err != nil {
202 t.Fatal(err)
203 }
204 if run := world.finish(resumed, bo); run.State != runDone {
205 t.Fatalf("resumed by Bo: %+v", run)
206 }
207 if got := pinTitles(t, world, ana); len(got) != 0 {
208 t.Fatalf("Ana's pins changed: %v", got)
209 }
210 if got := pinTitles(t, world, bo); len(got) == 0 {
211 t.Fatal("the resumer's sidebar has no roots")
212 }
213}
214
215// I23, roots only: a page whose parent failed this run has no In: line,
216// but it is not top-level in Notion, so it is neither pinned nor marked
217// (Fable on the build, M6).
218func TestI23AFailedParentsChildIsNoRoot(t *testing.T) {
219 doc := notiontest.NewDocumented()
220 world := newImportWorld(t, doc.Workspace)
221 world.connect(ana)
222 saved := renderBudgetFor
223 renderBudgetFor = func(page notionmd.Page) time.Duration {
224 if page.ID == doc.Child {
225 return time.Nanosecond
226 }
227 return time.Minute
228 }
229 t.Cleanup(func() { renderBudgetFor = saved })
230 run := world.importAs(ana)
231 for _, title := range pinTitles(t, world, ana) {
232 if title == "Grandchild" {
233 t.Fatal("a failed parent's child was pinned as a root")
234 }
235 }
236 report, _ := world.store.report(ctx(), run)
237 for _, page := range report.Unpinned {
238 if page.Title == "Grandchild" {
239 t.Fatal("a failed parent's child was offered")
240 }
241 }
242 row, _, _ := world.store.mapRowOf(world.store.database, doc.Grandchild)
243 if place, err := world.store.openCoordinate(row); err != nil ||
244 place.Pin != nil {
245 t.Fatalf("a failed parent's child was marked: %v", err)
246 }
247}
248
249// I23, the report's sentence (Fable on the build, L4), in its four
250// forms: one root pinned, every root pinned, some of them, and none for
251// a full sidebar.
252func TestI23TheReportSaysWhatWasPinned(t *testing.T) {
253 sentence := func(t *testing.T, ws *notiontest.Workspace, full int,
254 want string) {
255 t.Helper()
256 world := newImportWorld(t, ws)
257 viewer, _ := world.store.Viewer(ctx(), ana, nil)
258 for at := range full {
259 page := mustCreate(t, world.store,
260 "Full "+strings.Repeat("x", at+1), "Text.")
261 if err := world.store.SetPin(ctx(), viewer, page.ID,
262 true); err != nil {
263 t.Fatal(err)
264 }
265 }
266 world.connect(ana)
267 run := world.importAs(ana)
268 report, _ := world.store.report(ctx(), run)
269 want = strings.NewReplacer("{n}", fmt.Sprint(report.Offered),
270 "{p}", fmt.Sprint(report.Pinned)).Replace(want)
271 if text := world.importPageText(); !strings.Contains(text, want) {
272 t.Errorf("the report does not say %q", want)
273 }
274 }
275 sentence(t, newSmall().Workspace, 0,
276 "Pinned your top-level team page to your sidebar.")
277 sentence(t, newSmall().Workspace, maxPins, "Your sidebar already "+
278 "holds 20 pins, so your top-level team page wasn’t pinned.")
279 documented := func() *notiontest.Workspace {
280 return notiontest.NewDocumented().Workspace
281 }
282 sentence(t, documented(), 0,
283 "Pinned your {n} top-level team pages to your sidebar.")
284 sentence(t, documented(), maxPins-1, "Pinned 1 of your {n} top-level "+
285 "team pages to your sidebar. Your sidebar holds 20 pins.")
286 sentence(t, documented(), maxPins, "Your sidebar already holds 20 "+
287 "pins, so your {n} top-level team pages weren’t pinned.")
288}
289