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 "testing"
6 "time"
7
8 "gorm.io/gorm"
9)
10
11// crashOn makes the nth arrival at point a crash, and clears itself
12// when the test ends.
13func crashOn(t *testing.T, point string, nth int) {
14 t.Helper()
15 seen := 0
16 importCrash = func(at string) error {
17 if at == point {
18 seen++
19 if seen == nth {
20 return errCrashed
21 }
22 }
23 return nil
24 }
25 t.Cleanup(func() { importCrash = nil })
26}
27
28// I12, resuming (Notion import spec §8.3): boot interrupts a run left
29// surveying, and Resume finishes it; a resumed survey asks Notion
30// nothing for the pages it already staged.
31//
32// Mutation (make mutate, first red 2026-11-17): a resumed survey reads
33// every page again.
34func TestI12ResumeContinuesTheSurvey(t *testing.T) {
35 ws := newSmall()
36 world := newImportWorld(t, ws.Workspace)
37 world.connect(ana)
38 lease, err := world.store.StartRun(ctx(), ana, false)
39 if err != nil {
40 t.Fatal(err)
41 }
42 crashOn(t, "survey:read", 3)
43 world.finish(lease, ana)
44 importCrash = nil
45 if err := world.store.InterruptRuns(ctx()); err != nil {
46 t.Fatal(err)
47 }
48 if run := world.run(lease.run); run.State != runInterrupted ||
49 run.Surveyed != 2 {
50 t.Fatalf("after boot: %+v", run)
51 }
52 world.server.Reset()
53 resumed, err := world.store.ResumeRun(ctx(), ana)
54 if err != nil {
55 t.Fatal(err)
56 }
57 if run := world.finish(resumed, ana); run.State != runDone ||
58 run.Created != 3 {
59 t.Fatalf("after resume: %+v", run)
60 }
61 for _, id := range []string{ws.handbook, ws.onboarding} {
62 if world.server.Count("/v1/pages/"+id) != 0 {
63 t.Errorf("the resumed survey read %s again", id)
64 }
65 }
66}
67
68// Stop ends a run at once even when its goroutine ignores its context:
69// the goroutine's next write of any kind, bookkeeping included, is
70// refused.
71func TestI12StopRefusesTheOldGoroutine(t *testing.T) {
72 world := newImportWorld(t, newSmall().Workspace)
73 world.connect(ana)
74 lease, err := world.store.StartRun(ctx(), ana, false)
75 if err != nil {
76 t.Fatal(err)
77 }
78 if _, err := world.store.StopRun(ctx(), ana); err != nil {
79 t.Fatal(err)
80 }
81 if run := world.run(lease.run); run.State != runStopped {
82 t.Fatalf("after Stop: %+v", run)
83 }
84 err = world.store.bookkeep(ctx(), lease, func(db *gorm.DB) error {
85 return db.Model(&NotionRun{}).Where("id = ?", lease.run).
86 Update("surveyed", 99).Error
87 })
88 if !errors.Is(err, errLeaseLost) {
89 t.Errorf("bookkeeping after Stop: %v", err)
90 }
91 _, err = world.store.ImportCreate(ctx(), importWrite{lease: lease,
92 who: ana, title: "Late", body: "x"})
93 if !errors.Is(err, errLeaseLost) || world.count("pages") != 0 {
94 t.Errorf("a page write after Stop: %v", err)
95 }
96}
97
98// Disconnect, Finish and Forget are refused during a run and work after
99// Stop; Disconnect ends a resumable run and clears its staged trees.
100func TestI12ActsWaitForTheRun(t *testing.T) {
101 world := newImportWorld(t, newSmall().Workspace)
102 world.connect(ana)
103 lease, err := world.store.StartRun(ctx(), ana, false)
104 if err != nil {
105 t.Fatal(err)
106 }
107 world.job(lease, ana).survey(ctx())
108 store := world.store
109 for name, act := range map[string]func() error{
110 "disconnect": func() error { return store.Disconnect(ctx(), ana) },
111 "finish": func() error { return store.Finish(ctx(), ana) },
112 "forget": func() error { return store.Forget(ctx(), ana) },
113 } {
114 if err := act(); !errors.Is(err, ErrRunActive) {
115 t.Errorf("%s during a run: %v", name, err)
116 }
117 }
118 if _, err := world.store.StopRun(ctx(), ana); err != nil {
119 t.Fatal(err)
120 }
121 if err := world.store.Disconnect(ctx(), ana); err != nil {
122 t.Fatal(err)
123 }
124 run := world.run(lease.run)
125 staged := world.count("notion_pages", "sealed_staged IS NOT NULL")
126 if run.State != runFailed || run.ErrorCode != runDisconnected ||
127 staged != 0 {
128 t.Errorf("after Disconnect: %+v, %d staged", run, staged)
129 }
130}
131
132// A token for another workspace is refused while a map exists; Start
133// over clears staged trees; a run with a live ticker and no progress for
134// ten minutes may be reclaimed.
135func TestI12WorkspaceStartOverAndLiveness(t *testing.T) {
136 world := newImportWorld(t, newSmall().Workspace)
137 world.connect(ana)
138 world.importAs(ana)
139 world.server.Workspace.Me["bot"].(map[string]any)["workspace_name"] =
140 "Another workspace"
141 world.app.notion = fakeNotion{server: world.server}
142 workspace, _ := world.app.notion.Verify(ctx(), "secret_other")
143 err := world.store.Connect(ctx(), ana, "secret_other", workspace)
144 if !errors.Is(err, ErrOtherWorkspace) {
145 t.Errorf("another workspace's token: %v", err)
146 }
147 lease, err := world.store.StartRun(ctx(), ana, false)
148 if err != nil {
149 t.Fatal(err)
150 }
151 world.job(lease, ana).survey(ctx())
152 now := time.Now().UTC()
153 world.store.database.Model(&NotionRun{}).Where("id = ?", lease.run).
154 Updates(map[string]any{"heartbeat_at": now,
155 "progress_at": now.Add(-11 * time.Minute)})
156 if !resumable(world.run(lease.run), now) {
157 t.Error("a run with no progress for ten minutes is not reclaimable")
158 }
159 over, err := world.store.StartRun(ctx(), ana, true)
160 if err != nil || world.count("notion_pages",
161 "sealed_staged IS NOT NULL") != 0 || over.run == lease.run {
162 t.Errorf("Start over: %v", err)
163 }
164}
165
166// Disconnect, Finish and Forget work on a stopped run, and Finish keeps
167// the map, set aside.
168//
169// Mutation (make mutate, first red 2026-11-17): Finish deletes where
170// the images went (Fable round 4, L3: it keeps them, like the map).
171func TestFinishKeepsTheMap(t *testing.T) {
172 ws, _ := imagePage(t)
173 world := newImportWorld(t, ws.Workspace)
174 world.useImages()
175 world.connect(ana)
176 world.importAs(ana)
177 places := world.count("notion_images")
178 if err := world.store.Finish(ctx(), ana); err != nil {
179 t.Fatal(err)
180 }
181 if world.count("notion_connections") != 0 ||
182 world.count("notion_pages", "state = ?", mapWorkspace) != 1 ||
183 places == 0 || world.count("notion_images") != places {
184 t.Error("Finish did not delete the token and keep the map")
185 }
186}
187