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 "errors"
5 "slices"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/mapa/internal/mapa/notionmd"
11 "amadan.net/oficina/notion.git/notion"
12 "amadan.net/oficina/notion.git/notiontest"
13)
14
15// Gates for the lows of Fable's first review: L6 to L9 and L15.
16
17// fileRequests is how many requests the fake saw for hosted files.
18func (world *importWorld) fileRequests() int {
19 count := 0
20 for _, request := range world.server.Requests() {
21 if request.File {
22 count++
23 }
24 }
25 return count
26}
27
28// L6: the fix-up pass runs after a Resume. A member takes a page's
29// planned title, the run dies before writing it, and the Resume plans
30// afresh: the page linking to it must still be fixed up, and stay
31// "created".
32//
33// Mutation (make mutate, first red 2026-11-17): the fix-up writes a
34// page created this run as updated.
35func TestL6TheFixUpRunsAfterAResume(t *testing.T) {
36 ws := notiontest.NewWorkspace("fixup")
37 alpha := ws.Page("", notiontest.Text("Alpha"))
38 beta := ws.Page("", notiontest.Text("Beta"))
39 ws.Add(alpha, notiontest.Para(notiontest.PageMention(beta, "Beta")))
40 world := newImportWorld(t, ws)
41 world.connect(ana)
42 lease, err := world.store.StartRun(ctx(), ana, false)
43 if err != nil {
44 t.Fatal(err)
45 }
46 seen := 0
47 importCrash = func(point string) error {
48 if point == "write:rendered" {
49 if seen++; seen == 2 {
50 world.store.CreateTeamPage(ctx(), bo, "Beta", "Bo's.")
51 return errCrashed
52 }
53 }
54 return nil
55 }
56 t.Cleanup(func() { importCrash = nil })
57 world.finish(lease, ana)
58 importCrash = nil
59 world.store.InterruptRuns(ctx())
60 world.resumeIfNeeded(lease)
61 if !slices.Contains(world.linksOf("Alpha"), "Beta (Notion)") {
62 t.Errorf("Alpha links %v after the resume", world.linksOf("Alpha"))
63 }
64 row, _, _ := world.store.mapRowOf(world.store.database, alpha)
65 if row.State != mapCreated {
66 t.Errorf("Alpha's row is %s after its fix-up", row.State)
67 }
68}
69
70// L7: a rename in Notion to a title a member takes mid-run takes the
71// next candidate, as a create does, instead of failing the page.
72//
73// Mutation (make mutate, first red 2026-11-17): a rename collision
74// fails the page.
75func TestL7ARenameCollisionTakesTheNextTitle(t *testing.T) {
76 ws := newSmall()
77 world := newImportWorld(t, ws.Workspace)
78 world.connect(ana)
79 world.importAs(ana)
80 ws.Retitle(ws.equipment, notiontest.Text("Kit"))
81 atPoint(t, "write:rendered", 3, func() {
82 world.store.CreateTeamPage(ctx(), bo, "Kit", "Bo's kit.")
83 })
84 run := world.importAs(ana)
85 page, record, found := world.pageTitled("Kit (Onboarding)")
86 if !found || !record.Imported || run.Failed != 0 {
87 t.Errorf("the renamed page: %v %+v, run %+v", found, page, run)
88 }
89}
90
91// failingNotion builds no client.
92type failingNotion struct{ fakeNotion }
93
94func (failingNotion) Client(string) (*notion.Client, error) {
95 return nil, errors.New("no client")
96}
97
98// L8: a run whose launch fails is ended at once, not left "Reading
99// Notion" until it could be reclaimed.
100//
101// Mutation (make mutate, first red 2026-11-17): begin leaves the run.
102func TestL8AFailedLaunchEndsTheRun(t *testing.T) {
103 world := newImportWorld(t, newSmall().Workspace)
104 world.connect(ana)
105 world.app.notion = failingNotion{fakeNotion{server: world.server}}
106 lease, err := world.store.StartRun(ctx(), ana, false)
107 if err != nil {
108 t.Fatal(err)
109 }
110 if err := world.app.begin(lease, ana); err == nil {
111 t.Fatal("a launch with no client succeeded")
112 }
113 if run := world.run(lease.run); run.State != runFailed {
114 t.Errorf("after a failed launch the run is %s", run.State)
115 }
116}
117
118// L9: a finished import's page that a new import finds trashed is not
119// the new import's: it keeps no Mapa page (§25.9).
120//
121// Mutation (make mutate, first red 2026-11-17): a finished row decided
122// without staging keeps its old page.
123func TestL9AFinishedRowTrashedLaterKeepsNoPage(t *testing.T) {
124 ws := newSmall()
125 world := newImportWorld(t, ws.Workspace)
126 world.connect(ana)
127 world.importAs(ana)
128 if err := world.store.Finish(ctx(), ana); err != nil {
129 t.Fatal(err)
130 }
131 world.connect(ana)
132 ws.Trash(ws.equipment)
133 world.importAs(ana)
134 row, _, _ := world.store.mapRowOf(world.store.database, ws.equipment)
135 place, _ := world.store.openCoordinate(row)
136 if row.State != mapTrashed || place.PageID != 0 {
137 t.Errorf("the trashed row is %s with page %d", row.State,
138 place.PageID)
139 }
140}
141
142// L11: History says which revisions the import wrote, with Notion's own
143// edit time, and the kicker says where the page came from (§14.2). A
144// teammate's later edit reads as theirs.
145//
146// Mutation (make mutate, first red 2026-11-17): History ignores
147// imported.
148func TestL11HistoryAndKickerShowTheImport(t *testing.T) {
149 ws := newSmall()
150 ws.Objects[ws.handbook]["created_time"] = "2024-03-03T09:00:00.000Z"
151 world := newImportWorld(t, ws.Workspace)
152 world.connect(ana)
153 world.importAs(ana)
154 page, _, _ := world.pageTitled("Handbook")
155 if _, err := world.store.Commit(ctx(), bo, page.ID, page.Revision,
156 "Bo's."); err != nil {
157 t.Fatal(err)
158 }
159 viewer, err := world.store.Viewer(ctx(), bo, time.UTC)
160 if err != nil {
161 t.Fatal(err)
162 }
163 list, err := world.store.History(ctx(), viewer, page.ID, 0)
164 if err != nil || len(list.Versions) != 2 {
165 t.Fatalf("history %+v, %v", list, err)
166 }
167 if got := list.Versions[1].Summary; got !=
168 "Imported this from Notion · edited there 1 September 2026" {
169 t.Errorf("the imported revision reads %q", got)
170 }
171 if strings.Contains(list.Versions[0].Summary, "Notion") {
172 t.Errorf("Bo's edit reads %q", list.Versions[0].Summary)
173 }
174 view, err := world.app.index.View(ctx(), ScopeOf(viewer), page.ID)
175 if err != nil || view.From != "From Notion, created there 3 March 2024" {
176 t.Errorf("the kicker says %q (%v)", view.From, err)
177 }
178}
179
180// Fable round 2, M1: a page whose render outruns its budget fails
181// alone, render_failed, and the run goes on to done.
182//
183// Mutation (make mutate, first red 2026-11-17): the page's render runs
184// without its budget.
185func TestM1ASlowPageFailsAlone(t *testing.T) {
186 ws := newSmall()
187 world := newImportWorld(t, ws.Workspace)
188 world.connect(ana)
189 saved := renderBudgetFor
190 renderBudgetFor = func(page notionmd.Page) time.Duration {
191 if page.ID == ws.onboarding {
192 return time.Nanosecond
193 }
194 return time.Minute
195 }
196 t.Cleanup(func() { renderBudgetFor = saved })
197 run := world.importAs(ana)
198 row, _, _ := world.store.mapRowOf(world.store.database, ws.onboarding)
199 if run.State != runDone || run.Created != 2 || run.Failed != 1 ||
200 row.ErrorCode != "render_failed" {
201 t.Errorf("a slow page: run %+v, its row %s %s", run, row.State,
202 row.ErrorCode)
203 }
204}
205
206// hostedImage is the first Notion-hosted image block on a page.
207func hostedImage(ws *notiontest.Workspace, page string) map[string]any {
208 for _, block := range ws.Children[page] {
209 payload, _ := block["image"].(map[string]any)
210 if payload != nil && payload["type"] == "file" {
211 return block
212 }
213 }
214 panic("no hosted image on the page")
215}
216
217// Fable round 2, M2: the run's own untouched check, held alone. A page
218// edited in Mapa and renamed in Notion is kept, so its title stays, and
219// a page linking to it still links its Mapa title. ImportUpdate's
220// re-check would keep the page, but not the links that the plan had
221// already pointed at a new title.
222//
223// Mutation (make mutate, first red 2026-11-17): untouched reads any
224// live page as untouched.
225func TestM2AKeptPageKeepsItsTitleForLinks(t *testing.T) {
226 ws := newSmall()
227 world := newImportWorld(t, ws.Workspace)
228 world.connect(ana)
229 world.importAs(ana)
230 handbook, _, _ := world.pageTitled("Handbook")
231 if _, err := world.store.Commit(ctx(), bo, handbook.ID,
232 handbook.Revision, "Bo's handbook."); err != nil {
233 t.Fatal(err)
234 }
235 ws.Retitle(ws.handbook, notiontest.Text("The Handbook"))
236 world.importAs(ana)
237 if links := world.linksOf("Equipment"); !slices.Contains(links,
238 "Handbook") {
239 t.Errorf("Equipment links %v; Handbook was kept", links)
240 }
241}
242