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 "context"
5 "errors"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "gorm.io/gorm"
12
13 "amadan.net/oficina/notion.git/notiontest"
14)
15
16// Gates for Fable's round-3 lows on the image step.
17
18// countingImages wraps the real store, counting copies and adds, and
19// can call back from Held, where the run asks before a copy.
20type countingImages struct {
21 storeImages
22 copies, adds int
23 onHeld func()
24 unheld bool
25}
26
27func (images *countingImages) Held(ctx context.Context, publicID string,
28 pageID int64) bool {
29 if images.onHeld != nil {
30 images.onHeld()
31 }
32 if images.unheld {
33 return false
34 }
35 return images.storeImages.Held(ctx, publicID, pageID)
36}
37
38func (images *countingImages) Copy(ctx context.Context, who,
39 publicID string) (AddedImage, error) {
40 images.copies++
41 return images.storeImages.Copy(ctx, who, publicID)
42}
43
44func (images *countingImages) Add(ctx context.Context, who string,
45 data []byte) (AddedImage, error) {
46 images.adds++
47 return images.storeImages.Add(ctx, who, data)
48}
49
50// L1: a crash right after a page's write keeps where its images went,
51// so the next run neither fetches again nor writes a second revision.
52//
53// Mutation (make mutate, first red 2026-11-17): placeRow keeps no image
54// places.
55func TestL1ACrashAfterTheWriteKeepsImagePlaces(t *testing.T) {
56 ws, _ := imagePage(t)
57 world := newImportWorld(t, ws.Workspace)
58 world.useImages()
59 world.connect(ana)
60 lease, err := world.store.StartRun(ctx(), ana, false)
61 if err != nil {
62 t.Fatal(err)
63 }
64 crashOn(t, "write:committed", 1)
65 world.finish(lease, ana)
66 importCrash = nil
67 world.store.InterruptRuns(ctx())
68 world.resumeIfNeeded(lease)
69 world.importAs(ana)
70 page, _, _ := world.pageTitled("Handbook")
71 if revisions := world.count("revisions", "page_id = ?",
72 page.ID); revisions != 1 || world.fileRequests() != 1 {
73 t.Errorf("after a crash: %d revisions, %d fetched", revisions,
74 world.fileRequests())
75 }
76}
77
78// L2: one block shown twice on a page is one image.
79func TestL2ABlockTwiceOnAPageIsOneImage(t *testing.T) {
80 ws, block := imagePage(t)
81 again, _ := ws.Block(block)
82 ws.Children[ws.handbook] = append(ws.Children[ws.handbook], again)
83 world := newImportWorld(t, ws.Workspace)
84 world.useImages()
85 world.connect(ana)
86 world.importAs(ana)
87 page, _, _ := world.pageTitled("Handbook")
88 if attached, _ := world.imagesOf(page.ID); attached != 1 ||
89 world.fileRequests() != 1 {
90 t.Errorf("one block twice: %d attached, %d fetched", attached,
91 world.fileRequests())
92 }
93}
94
95// L3: "Images stopped" is about this run: a page kept since an earlier
96// run that stopped carries no stale counts.
97func TestL3TheStopIsThisRuns(t *testing.T) {
98 ws, _ := imagePage(t)
99 world := newImportWorld(t, ws.Workspace)
100 world.useImages(1, 1<<30)
101 world.connect(ana)
102 world.importAs(ana)
103 page, _, _ := world.pageTitled("Handbook")
104 world.store.Commit(ctx(), bo, page.ID, page.Revision, "Bo's.")
105 world.useImages()
106 run := world.importAs(ana)
107 report, _ := world.store.report(ctx(), run)
108 if len(report.ImageFailures) != 0 {
109 t.Errorf("a later run reports %v", report.ImageFailures)
110 }
111}
112
113// importPageText is the import page as Ana sees it.
114func (world *importWorld) importPageText() string {
115 request := httptest.NewRequest(http.MethodGet, importPath, nil)
116 request = request.WithContext(context.WithValue(request.Context(),
117 testPersonKey{}, ana))
118 world.app.testIdentity, world.app.allowUnauthenticated = ana, true
119 page := httptest.NewRecorder()
120 world.app.importPage(page, request)
121 return page.Body.String()
122}
123
124// L4: with no object store bound, the Start page and the report say so.
125func TestL4NoImageStoreIsSaid(t *testing.T) {
126 ws, _ := imagePage(t)
127 world := newImportWorld(t, ws.Workspace)
128 world.connect(ana)
129 if !strings.Contains(world.importPageText(),
130 "Images aren't set up in Mapa yet") {
131 t.Error("the Start page does not say images aren't set up")
132 }
133 world.importAs(ana)
134 if !strings.Contains(world.importPageText(),
135 "No images were imported: images aren't set up in Mapa yet") {
136 t.Error("the report does not say images can't be stored")
137 }
138}
139
140// L5: an image whose address the file pin refuses is tried again next
141// run: the pin may yet be told of its host (§19 step 4).
142//
143// Mutation (make mutate, first red 2026-11-17): a refused host is
144// remembered as permanent.
145func TestL5ARefusedHostIsTriedAgain(t *testing.T) {
146 ws := newSmall()
147 block := notiontest.B("image", map[string]any{"type": "file",
148 "file": map[string]any{"url": "https://elsewhere.example/" +
149 "secure.notion-static.com/x.png"}, "caption": []any{}})
150 ids := ws.Add(ws.handbook, block)
151 world := newImportWorld(t, ws.Workspace)
152 world.useImages()
153 world.connect(ana)
154 world.importAs(ana)
155 world.server.Reset()
156 world.importAs(ana)
157 if world.server.Count("/v1/blocks/"+ids[0]) != 1 {
158 t.Error("an image on a refused host was not tried again")
159 }
160}
161
162// L7: a copy is made only from a page that still holds the image.
163//
164// Mutation (make mutate, first red 2026-11-17): the copy source's Held
165// check removed.
166func TestL7ACopyNeedsAHeldSource(t *testing.T) {
167 ws, block := imagePage(t)
168 world := newImportWorld(t, ws.Workspace)
169 world.useImages()
170 world.connect(ana)
171 world.importAs(ana)
172 shared, _ := ws.Block(block)
173 ws.Children[ws.onboarding] = append(ws.Children[ws.onboarding], shared)
174 counting := &countingImages{storeImages: storeImages{world.store},
175 unheld: true}
176 world.app.images = counting
177 world.importAs(ana)
178 if counting.copies != 0 {
179 t.Errorf("%d copies from a page that no longer holds the image",
180 counting.copies)
181 }
182}
183
184// L7: the lease is checked before a copy and again before an add, each
185// the store's own write outside the page's transaction (§12).
186//
187// Mutations (make mutate, first red 2026-11-17): either check removed.
188func TestL7BothImageWritesHoldTheLease(t *testing.T) {
189 for _, place := range []string{"copy", "add"} {
190 t.Run(place, func(t *testing.T) {
191 ws, block := imagePage(t)
192 world := newImportWorld(t, ws.Workspace)
193 world.useImages()
194 world.connect(ana)
195 world.importAs(ana)
196 shared, _ := ws.Block(block)
197 ws.Children[ws.onboarding] = append(
198 ws.Children[ws.onboarding], shared)
199 lease, err := world.store.StartRun(ctx(), ana, false)
200 if err != nil {
201 t.Fatal(err)
202 }
203 job := world.job(lease, ana)
204 if err := job.survey(ctx()); err != nil {
205 t.Fatal(err)
206 }
207 counting := leaseMovesAt(world, lease, place)
208 job.images = counting
209 set, _ := job.loadSet(ctx())
210 entry := set.byKey[notionKey(ws.onboarding)]
211 _, err = job.prepareImages(ctx(), entry)
212 if !errors.Is(err, errLeaseLost) ||
213 counting.copies+counting.adds != 0 {
214 t.Errorf("%s after the lease moved: %v, %d copies, %d adds",
215 place, err, counting.copies, counting.adds)
216 }
217 })
218 }
219}
220
221// leaseMovesAt moves the run's lease before the copy's check (now), or
222// between the two checks: from Held, which the run asks of the other
223// page before it would copy, answering "not held" so the run goes on to
224// download and add.
225func leaseMovesAt(world *importWorld, lease runLease,
226 place string) *countingImages {
227 move := func() {
228 world.store.bookkeep(ctx(), runLease{}, func(db *gorm.DB) error {
229 return db.Model(&NotionRun{}).Where("id = ?", lease.run).
230 Update("lease", 1).Error
231 })
232 }
233 counting := &countingImages{storeImages: storeImages{world.store}}
234 if place == "copy" {
235 move()
236 return counting
237 }
238 counting.unheld = true
239 counting.onHeld = move
240 return counting
241}
242
243// L7: the team's side of a quota refusal names the team's allowance.
244//
245// Mutation (make mutate, first red 2026-11-17): quotaReason always names
246// the person's.
247func TestL7TheTeamAllowanceIsNamed(t *testing.T) {
248 ws, _ := imagePage(t)
249 world := newImportWorld(t, ws.Workspace)
250 world.useImages(1<<30, 1)
251 world.connect(ana)
252 world.importAs(ana)
253 _, record, _ := world.pageTitled("Handbook")
254 if !strings.Contains(record.Body, "the team's image allowance is full") {
255 t.Errorf("over the team's allowance: %q", record.Body)
256 }
257}
258