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 "strings"
5 "testing"
6
7 "amadan.net/oficina/notion.git/notiontest"
8)
9
10// small is a three-page workspace: Handbook, its child Onboarding, and
11// Onboarding's child Equipment, which mentions Handbook.
12type small struct {
13 *notiontest.Workspace
14 handbook, onboarding, equipment string
15}
16
17// set replaces the index'th block under parent, keeping its id, as an
18// edit in Notion does.
19func (world *small) set(parent string, index int, block map[string]any) {
20 block["id"] = world.Children[parent][index]["id"]
21 world.Children[parent][index] = block
22}
23
24func newSmall() *small {
25 ws := notiontest.NewWorkspace("small")
26 world := &small{Workspace: ws}
27 world.handbook = ws.Page("", notiontest.Text("Handbook"))
28 world.onboarding = ws.Page(world.handbook, notiontest.Text("Onboarding"))
29 world.equipment = ws.Page(world.onboarding, notiontest.Text("Equipment"))
30 ws.Add(world.handbook, notiontest.Para(notiontest.Text("Welcome.")))
31 ws.Add(world.onboarding, notiontest.Para(notiontest.Text("Week one.")))
32 ws.Add(world.equipment, notiontest.Para(notiontest.Text("Laptops. See "),
33 notiontest.PageMention(world.handbook, "Handbook")))
34 return world
35}
36
37// pageEvents is the events of object kind page.
38func (world *importWorld) pageEvents() int64 {
39 return world.count("events", "object_kind = ?", ObjectPage)
40}
41
42// I2, an idempotent re-run (Notion import spec §18): run, then run again
43// on the same workspace. No revision, no page event, no image download,
44// every page unchanged.
45//
46// Mutation (make mutate, first red 2026-11-17): "changed" decided by
47// Notion's edit time read as always newer, so every page is updated.
48func TestI2ARerunWritesNothing(t *testing.T) {
49 doc := notiontest.NewDocumented()
50 world := newImportWorld(t, doc.Workspace)
51 world.connect(ana)
52 first := world.importAs(ana)
53 if first.State != runDone || first.Created != 6 {
54 t.Fatalf("first run: %+v", first)
55 }
56 events, revisions := world.pageEvents(), world.count("revisions")
57 world.server.Reset()
58 second := world.importAs(ana)
59 if second.State != runDone || second.Unchanged != 6 ||
60 second.Created+second.Updated+second.Kept != 0 {
61 t.Errorf("second run: %+v", second)
62 }
63 if world.pageEvents() != events || world.count("revisions") != revisions {
64 t.Error("a re-run of an unchanged workspace wrote a revision")
65 }
66 for _, request := range world.server.Requests() {
67 if request.File {
68 t.Errorf("a re-run downloaded %s", request.Path)
69 }
70 }
71}
72
73// I2's other half, from the first real run: Notion rounds
74// last_edited_time to the minute (spec §25 note 17), so an edit made in
75// the same minute as the last run's survey carries the time that run
76// saw, on the page and on the block. It is picked up anyway: "changed"
77// is the rendered body against what Mapa holds (§13.2), never the edit
78// time. Two such edits in a row, each in that minute, are each picked
79// up, and a run after them writes nothing.
80//
81// Mutation (make mutate, first red 2026-11-22): a page whose Notion edit
82// time is the one last written is read as unchanged.
83func TestI2AnEditInTheSameMinuteIsPickedUp(t *testing.T) {
84 ws := newSmall()
85 world := newImportWorld(t, ws.Workspace)
86 world.connect(ana)
87 world.importAs(ana)
88 minute := ws.Objects[ws.handbook]["last_edited_time"]
89 for _, words := range []string{"Hello.", "Hello again."} {
90 ws.set(ws.handbook, 1, notiontest.Para(notiontest.Text(words)))
91 if ws.Objects[ws.handbook]["last_edited_time"] != minute ||
92 ws.Children[ws.handbook][1]["last_edited_time"] != minute {
93 t.Fatal("the edit moved an edit time: the test needs it held")
94 }
95 run := world.importAs(ana)
96 _, record, _ := world.pageTitled("Handbook")
97 if run.Updated != 1 || !strings.Contains(record.Body, words) {
98 t.Fatalf("an edit in the same minute: %+v, body %q", run,
99 record.Body)
100 }
101 }
102 if run := world.importAs(ana); run.Unchanged != 3 ||
103 run.Updated != 0 {
104 t.Errorf("the run after: %+v", run)
105 }
106}
107
108// I3, untouched and kept (spec §13.2, D8). Two pages change in Notion,
109// one of them renamed; one of those is edited in Mapa, and a third is
110// renamed in Mapa. The untouched one updates as the runner, renamed
111// then edited, with a teammate's draft carried to the rename; the
112// edited and renamed ones are kept.
113//
114// Mutation (make mutate, first red 2026-11-17): untouched reads any
115// live page as untouched.
116func TestI3UntouchedUpdatesAndEditedIsKept(t *testing.T) {
117 ws := newSmall()
118 world := newImportWorld(t, ws.Workspace)
119 world.connect(ana)
120 world.importAs(ana)
121 handbook, _, _ := world.pageTitled("Handbook")
122 onboarding, _, _ := world.pageTitled("Onboarding")
123 equipment, _, _ := world.pageTitled("Equipment")
124 draft, err := world.store.SaveDraft(ctx(), bo,
125 DraftKey{PageID: handbook.ID}, handbook.Revision, 0, "Bo's draft.")
126 if err != nil || draft == 0 {
127 t.Fatalf("draft: %v", err)
128 }
129 ws.Retitle(ws.handbook, notiontest.Text("The Handbook"))
130 ws.set(ws.handbook, 1, notiontest.Para(notiontest.Text("Hello.")))
131 ws.set(ws.onboarding, 1, notiontest.Para(notiontest.Text("Two.")))
132 if _, err := world.store.Commit(ctx(), bo, onboarding.ID,
133 onboarding.Revision, "Bo's words."); err != nil {
134 t.Fatal(err)
135 }
136 if err := world.store.Rename(ctx(), bo, equipment.ID, "Kit"); err != nil {
137 t.Fatal(err)
138 }
139 run := world.importAs(ana)
140 if run.Updated != 1 || run.Kept != 2 {
141 t.Fatalf("re-run: %+v", run)
142 }
143 world.checkUpdated(handbook.ID)
144 world.checkDraftCarried(handbook.ID)
145 if _, record, _ := world.pageTitled("Onboarding"); record.Body !=
146 "Bo's words." {
147 t.Errorf("the page edited in Mapa was not kept: %q", record.Body)
148 }
149 if _, _, found := world.pageTitled("Kit"); !found {
150 t.Error("the page renamed in Mapa was renamed back")
151 }
152}
153
154// checkUpdated is the untouched page's update: renamed then edited, as
155// the runner, in one transaction (one write's two events).
156func (world *importWorld) checkUpdated(pageID int64) {
157 world.t.Helper()
158 var events []Event
159 world.store.database.Where("object_kind = ? AND object_id = ?",
160 ObjectPage, pageID).Order("id").Find(&events)
161 last := events[len(events)-2:]
162 if last[0].Verb != VerbRenamed || last[1].Verb != VerbEdited ||
163 !last[0].At.Equal(last[1].At) {
164 world.t.Errorf("the update logged %v", last)
165 }
166 page, record, found := world.pageTitled("The Handbook")
167 if !found || page.ID != pageID || !strings.Contains(record.Body,
168 "Hello.") || !record.Imported {
169 world.t.Errorf("the untouched page was not updated: %+v", record)
170 }
171}
172
173// checkDraftCarried is the teammate's draft, moved onto the rename's
174// revision, which holds the body it was written over.
175func (world *importWorld) checkDraftCarried(pageID int64) {
176 world.t.Helper()
177 var draft Draft
178 world.store.database.Where("page_id = ?", pageID).First(&draft)
179 var renamed Event
180 world.store.database.Where("object_id = ? AND verb = ?", pageID,
181 VerbRenamed).Last(&renamed)
182 if draft.BaseRevision != renamed.Revision {
183 world.t.Errorf("the draft's base is %d, the rename's revision %d",
184 draft.BaseRevision, renamed.Revision)
185 }
186}
187
188// I4, never delete on absence (P6). A page the fake stops sharing is
189// left standing, and listed as not found; an interrupted survey reports
190// no absence at all.
191//
192// Mutation (make mutate, first red 2026-11-17): absence counted before
193// the survey is complete.
194func TestI4AbsenceDeletesNothing(t *testing.T) {
195 ws := newSmall()
196 world := newImportWorld(t, ws.Workspace)
197 world.connect(ana)
198 world.importAs(ana)
199 pages := world.count("pages", "gone = ?", false)
200 ws.Children[ws.handbook] = ws.Children[ws.handbook][1:]
201 run := world.importAs(ana)
202 if run.State != runDone || world.count("pages", "gone = ?",
203 false) != pages {
204 t.Fatalf("absence deleted pages: %+v", run)
205 }
206 report, err := world.store.report(ctx(), run)
207 if err != nil || report.NotFound != 2 || len(report.NotFoundPages) != 2 {
208 t.Errorf("not found: %+v, %v", report, err)
209 }
210 lease, err := world.store.StartRun(ctx(), ana, false)
211 if err != nil {
212 t.Fatal(err)
213 }
214 world.store.InterruptRuns(ctx())
215 report, err = world.store.report(ctx(), world.run(lease.run))
216 if err != nil || report.SurveyDone || report.NotFound != 0 {
217 t.Errorf("an interrupted survey reported absence: %+v", report)
218 }
219}
220