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 "bytes"
5 "image"
6 "image/color"
7 "image/png"
8 "math/rand/v2"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/notion.git/notiontest"
13)
14
15// I20, I21 and I25 (columns and icons spec §6.1, §9): a Notion page's
16// icon and cover become its look, through the pinned file transport;
17// what Mapa cannot hold is counted, and the page is written without it.
18
19// widePicture is a PNG of one colour, small in bytes and w×h in pixels.
20func widePicture(t *testing.T, width, height int) []byte {
21 t.Helper()
22 canvas := image.NewRGBA(image.Rect(0, 0, width, height))
23 for column := range width {
24 for line := range height {
25 canvas.Set(column, line, color.RGBA{10, 20, 30, 255})
26 }
27 }
28 var out bytes.Buffer
29 if err := png.Encode(&out, canvas); err != nil {
30 t.Fatal(err)
31 }
32 return out.Bytes()
33}
34
35// noisyPicture is a PNG of noise, w×h, too large in bytes for an icon
36// at 300×300.
37func noisyPicture(t *testing.T, width, height int) []byte {
38 t.Helper()
39 random := rand.New(rand.NewPCG(1, 2))
40 canvas := image.NewRGBA(image.Rect(0, 0, width, height))
41 for column := range width {
42 for line := range height {
43 canvas.Set(column, line, color.RGBA{uint8(random.IntN(256)),
44 uint8(random.IntN(256)), uint8(random.IntN(256)), 255})
45 }
46 }
47 var out bytes.Buffer
48 if err := png.Encode(&out, canvas); err != nil {
49 t.Fatal(err)
50 }
51 return out.Bytes()
52}
53
54// unhost takes a hosted file away, so its download fails.
55func unhost(ws *notiontest.Workspace, url string) {
56 path := strings.TrimPrefix(url, strings.TrimSuffix(
57 notiontest.HostedPrefix, "/"))
58 path, _, _ = strings.Cut(path, "?")
59 delete(ws.Files, path)
60}
61
62// looksWorld is a workspace with every look AddLooks makes, plus an
63// emoji Mapa lacks, an icon too wide, and an icon whose file is gone.
64func looksWorld(t *testing.T) (*importWorld, notiontest.Looks) {
65 ws := notiontest.NewWorkspace("looks")
66 looks := ws.AddLooks(picture(t, 1), picture(t, 2))
67 odd := ws.Page("", notiontest.Text("Odd emoji"))
68 ws.SetIcon(odd, notiontest.EmojiIcon("A"))
69 wide := ws.Page("", notiontest.Text("Wide icon"))
70 ws.SetIcon(wide, notiontest.FileIcon(ws.Host("wide.png",
71 widePicture(t, 600, 10))))
72 heavy := ws.Page("", notiontest.Text("Heavy icon"))
73 ws.SetIcon(heavy, notiontest.FileIcon(ws.Host("heavy.png",
74 noisyPicture(t, 300, 300))))
75 gone := ws.Page("", notiontest.Text("Lost icon"))
76 lost := ws.Host("lost.png", picture(t, 3))
77 ws.SetIcon(gone, notiontest.FileIcon(lost))
78 unhost(ws, lost)
79 // I25: a custom emoji on a host the transport does not pin.
80 foreign := ws.Page("", notiontest.Text("Foreign emoji"))
81 ws.SetIcon(foreign, notiontest.CustomEmojiIcon("far",
82 "https://example.com/far.png"))
83 world := newImportWorld(t, ws)
84 world.useImages()
85 world.connect(ana)
86 return world, looks
87}
88
89func (world *importWorld) lookOf(title string) (Page, Record) {
90 world.t.Helper()
91 page, record, ok := world.pageTitled(title)
92 if !ok {
93 world.t.Fatalf("no page %q", title)
94 }
95 return page, record
96}
97
98// I20 and I25, icons: one on a host the transport does not pin is
99// refused before dialling, and counted.
100//
101// Mutation (make mutate): an emoji icon written without the list's
102// check.
103func TestI20IconsAreImported(t *testing.T) {
104 world, _ := looksWorld(t)
105 run := world.importAs(ana)
106 if run.State != runDone || run.Failed != 0 {
107 t.Fatalf("the run: %+v", run)
108 }
109 if _, record := world.lookOf("Emoji icon"); record.Icon == nil ||
110 record.Icon.Emoji != "🚀" {
111 t.Errorf("the emoji icon: %+v", record.Icon)
112 }
113 for _, title := range []string{"Custom emoji icon", "Uploaded icon"} {
114 page, record := world.lookOf(title)
115 if record.Icon == nil || record.Icon.Image == "" {
116 t.Errorf("%s: icon %+v", title, record.Icon)
117 continue
118 }
119 if attached, _ := world.imagesOf(page.ID); attached != 1 {
120 t.Errorf("%s: %d images attached", title, attached)
121 }
122 }
123 for _, title := range []string{"Notion icon", "Odd emoji", "Wide icon",
124 "Heavy icon", "Lost icon", "Foreign emoji"} {
125 if _, record := world.lookOf(title); record.Icon != nil {
126 t.Errorf("%s was given an icon: %+v", title, record.Icon)
127 }
128 }
129 if _, pending := world.imagesOf(0); pending != 0 {
130 t.Errorf("%d images left pending: the wide icon holds none", pending)
131 }
132 report, _ := world.store.report(ctx(), run)
133 want := map[string]int{lookIconSet: 1, lookEmoji: 1, lookIconLarge: 2,
134 "couldn't be downloaded": 1, "not from Notion's file store": 1}
135 if report.Icons != 3 || !sameCounts(report.IconFailures, want) {
136 t.Errorf("the report's icons: %d, %+v", report.Icons,
137 report.IconFailures)
138 }
139}
140
141func sameCounts(got []countView, want map[string]int) bool {
142 if len(got) != len(want) {
143 return false
144 }
145 for _, each := range got {
146 if want[each.Label] != each.Count {
147 return false
148 }
149 }
150 return true
151}
152
153// I21 and I25, covers: an uploaded cover and one from Notion's gallery
154// become banners, centred; an Unsplash cover is counted, never fetched.
155//
156// Mutation (make mutate): an external cover fetched without asking the
157// pin.
158func TestI21CoversAreImportedFromThePinOnly(t *testing.T) {
159 world, _ := looksWorld(t)
160 run := world.importAs(ana)
161 for _, title := range []string{"Uploaded cover", "Gallery cover"} {
162 _, record := world.lookOf(title)
163 if record.Banner == nil || record.Banner.Image == "" ||
164 record.Banner.Y != 50 {
165 t.Errorf("%s: banner %+v", title, record.Banner)
166 }
167 }
168 if _, record := world.lookOf("Unsplash cover"); record.Banner != nil {
169 t.Errorf("an Unsplash cover was imported: %+v", record.Banner)
170 }
171 for _, request := range world.server.Requests() {
172 if request.File && strings.Contains(request.Path, "unsplash") {
173 t.Errorf("fetched %s", request.Path)
174 }
175 }
176 report, _ := world.store.report(ctx(), run)
177 if report.Covers != 2 || !sameCounts(report.CoverFailures,
178 map[string]int{lookElsewhere: 1}) {
179 t.Errorf("the report's covers: %d, %+v", report.Covers,
180 report.CoverFailures)
181 }
182 text := world.importPageText()
183 for _, want := range []string{"Icons imported", "Banners not imported",
184 lookElsewhere} {
185 if !strings.Contains(text, want) {
186 t.Errorf("the report does not say %q", want)
187 }
188 }
189}
190
191// I24, re-runs: an unchanged look downloads nothing and writes nothing;
192// an untouched page whose Notion icon changed gets one styled revision;
193// a page whose icon was changed in Mapa is kept.
194//
195// Mutation (make mutate): "unchanged" decided without the look.
196func TestI24ALookChangedInNotionIsStyled(t *testing.T) {
197 ws := notiontest.NewWorkspace("rerun")
198 looks := ws.AddLooks(picture(t, 1), picture(t, 2))
199 world := newImportWorld(t, ws)
200 world.useImages()
201 world.connect(ana)
202 world.importAs(ana)
203 world.server.Reset()
204 events := world.pageEvents()
205 if run := world.importAs(ana); run.Updated != 0 ||
206 world.fileRequests() != 0 || world.pageEvents() != events {
207 t.Fatalf("an unchanged re-run: %+v, %d fetched", run,
208 world.fileRequests())
209 }
210 ws.SetIcon(looks.Emoji, notiontest.EmojiIcon("🌍"))
211 ws.SetIcon(looks.Custom, notiontest.EmojiIcon("🎉"))
212 mine, _ := world.lookOf("Custom emoji icon")
213 if _, err := world.store.SetLook(ctx(), bo, mine.ID, LookChange{
214 SetIcon: true, Icon: &Icon{Emoji: "📌"}}); err != nil {
215 t.Fatal(err)
216 }
217 world.server.Reset()
218 run := world.importAs(ana)
219 if run.Updated != 1 || run.Kept != 1 {
220 t.Fatalf("after Notion's change: %+v", run)
221 }
222 page, record := world.lookOf("Emoji icon")
223 if record.Icon == nil || record.Icon.Emoji != "🌍" {
224 t.Fatalf("the icon: %+v", record.Icon)
225 }
226 world.expectVerbs(page.ID, VerbCreated, VerbStyled)
227 viewer, _ := world.store.Viewer(ctx(), ana, nil)
228 list, err := world.store.History(ctx(), viewer, page.ID, 0)
229 if err != nil || len(list.Versions) != 2 || !strings.HasSuffix(
230 list.Versions[0].Summary, " (from Notion)") ||
231 strings.Contains(list.Versions[0].Summary, "edited there") {
232 t.Errorf("the styled revision reads %+v, %v", list, err)
233 }
234 if _, record := world.lookOf("Custom emoji icon"); record.Icon == nil ||
235 record.Icon.Emoji != "📌" {
236 t.Errorf("a page styled in Mapa lost its icon: %+v", record.Icon)
237 }
238}
239
240// expectVerbs is a page's events' verbs, in order.
241func (world *importWorld) expectVerbs(pageID int64, verbs ...string) {
242 world.t.Helper()
243 var got []string
244 world.store.database.Model(&Event{}).Where("object_kind = ? AND "+
245 "object_id = ?", ObjectPage, pageID).Order("id").Pluck("verb", &got)
246 if strings.Join(got, " ") != strings.Join(verbs, " ") {
247 world.t.Errorf("verbs %v, want %v", got, verbs)
248 }
249}
250
251// I24's order: a page renamed, restyled and edited in Notion gets the
252// three revisions, renamed, styled, edited, in one write.
253func TestI24RenamedStyledEditedInOrder(t *testing.T) {
254 ws := newSmall()
255 world := newImportWorld(t, ws.Workspace)
256 world.connect(ana)
257 world.importAs(ana)
258 ws.Retitle(ws.equipment, notiontest.Text("Kit"))
259 ws.SetIcon(ws.equipment, notiontest.EmojiIcon("💻"))
260 ws.set(ws.equipment, 0, notiontest.Para(notiontest.Text("Phones.")))
261 world.server.Reset()
262 if run := world.importAs(ana); run.Failed != 0 || run.Kept != 0 {
263 t.Fatalf("the re-run: %+v", run)
264 }
265 page, record := world.lookOf("Kit")
266 if record.Icon == nil || record.Icon.Emoji != "💻" ||
267 !strings.Contains(record.Body, "Phones.") {
268 t.Fatalf("the page: %+v", record)
269 }
270 world.expectVerbs(page.ID, VerbCreated, VerbRenamed, VerbStyled,
271 VerbEdited)
272}
273
274// A fix-up after a Resume rewrites a page that has an icon: the page
275// keeps it, with no styled revision (Fable on phase 4, L6).
276//
277// Mutation (make mutate): lookRecord gives no look when the run
278// prepared none.
279func TestI24AFixUpAfterAResumeKeepsTheIcon(t *testing.T) {
280 ws := notiontest.NewWorkspace("fixup")
281 alpha := ws.Page("", notiontest.Text("Alpha"))
282 ws.SetIcon(alpha, notiontest.EmojiIcon("🚀"))
283 beta := ws.Page("", notiontest.Text("Beta"))
284 ws.Add(alpha, notiontest.Para(notiontest.PageMention(beta, "Beta")))
285 world := newImportWorld(t, ws)
286 world.connect(ana)
287 lease, err := world.store.StartRun(ctx(), ana, false)
288 if err != nil {
289 t.Fatal(err)
290 }
291 seen := 0
292 importCrash = func(point string) error {
293 if point == "write:rendered" {
294 if seen++; seen == 2 {
295 world.store.CreateTeamPage(ctx(), bo, "Beta", "Bo's.")
296 return errCrashed
297 }
298 }
299 return nil
300 }
301 t.Cleanup(func() { importCrash = nil })
302 world.finish(lease, ana)
303 importCrash = nil
304 world.store.InterruptRuns(ctx())
305 world.resumeIfNeeded(lease)
306 page, record := world.lookOf("Alpha")
307 if record.Icon == nil || record.Icon.Emoji != "🚀" ||
308 !strings.Contains(record.Body, "Beta (Notion)") {
309 t.Fatalf("Alpha after its fix-up: %+v", record)
310 }
311 world.expectVerbs(page.ID, VerbCreated, VerbEdited)
312}
313
314// A changed icon whose download fails for now leaves the page with the
315// icon it has: no revision now, and none to undo next run (Fable on
316// phase 4, L7).
317func TestI24APassingFailureKeepsTheIcon(t *testing.T) {
318 ws := notiontest.NewWorkspace("passing")
319 page := ws.Page("", notiontest.Text("Iconic"))
320 first := ws.Host("one.png", picture(t, 1))
321 ws.SetIcon(page, notiontest.FileIcon(first))
322 world := newImportWorld(t, ws)
323 world.useImages()
324 world.connect(ana)
325 world.importAs(ana)
326 mine, before := world.lookOf("Iconic")
327 next := ws.Host("two.png", picture(t, 2))
328 ws.SetIcon(page, notiontest.FileIcon(next))
329 unhost(ws, next)
330 world.server.Reset()
331 if run := world.importAs(ana); run.Updated != 0 || run.Unchanged != 1 {
332 t.Fatalf("the re-run: %+v", run)
333 }
334 if _, after := world.lookOf("Iconic"); after.Icon == nil ||
335 *after.Icon != *before.Icon {
336 t.Errorf("the icon went from %+v to %+v", before.Icon, after.Icon)
337 }
338 world.expectVerbs(mine.ID, VerbCreated)
339 // Notion goes back to the first file: its row was kept, so nothing
340 // is downloaded and nothing written (Fable's re-check, low 2).
341 ws.SetIcon(page, notiontest.FileIcon(first))
342 world.server.Reset()
343 if run := world.importAs(ana); run.Unchanged != 1 ||
344 world.fileRequests() != 0 {
345 t.Fatalf("back to the first file: %+v, %d fetched", run,
346 world.fileRequests())
347 }
348 world.expectVerbs(mine.ID, VerbCreated)
349}
350
351// A look's prints are in a domain of their own: an icon's row shares no
352// print with a block named "icon:" on the same page, nor any block's
353// block print, so no copy is ever taken from it (Fable's re-check of
354// phase 4, low 4).
355func TestI20LookPrintsAreTheirOwn(t *testing.T) {
356 world, _ := looksWorld(t)
357 page := notiontest.ID(7)
358 lookPlace, lookAlone, err := world.store.lookPrints(page, iconPlace)
359 if err != nil {
360 t.Fatal(err)
361 }
362 blockPlace, blockAlone, err := world.store.imagePrints(page, iconPlace)
363 if err != nil {
364 t.Fatal(err)
365 }
366 if lookPlace == blockPlace || lookAlone == blockAlone ||
367 lookAlone != lookPlace {
368 t.Error("a look's prints meet a block's")
369 }
370}
371