| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | "amadan.net/oficina/notion.git/notiontest" |
| 9 | ) |
| 10 | |
| 11 | // linksOf is the [[link]] targets in the page titled title. |
| 12 | func (world *importWorld) linksOf(title string) []string { |
| 13 | world.t.Helper() |
| 14 | _, record, found := world.pageTitled(title) |
| 15 | if !found { |
| 16 | world.t.Fatalf("no page titled %q", title) |
| 17 | } |
| 18 | var targets []string |
| 19 | for _, link := range markup.Links(record.Body) { |
| 20 | targets = append(targets, link.Target) |
| 21 | } |
| 22 | return targets |
| 23 | } |
| 24 | |
| 25 | // I5, links (spec §11.2, §13.5). Mentions resolve to final titles, a |
| 26 | // collision-renamed one included. A mention of a page not shared is |
| 27 | // plain text and counted; sharing it and running again links it where |
| 28 | // the source is untouched. A Notion URL to an imported page is a link. |
| 29 | // |
| 30 | // Mutation (make mutate, first red 2026-11-17): the resolver answers a |
| 31 | // page's Notion title instead of its planned one. |
| 32 | func TestI5LinksResolveAndHeal(t *testing.T) { |
| 33 | ws := notiontest.NewWorkspace("links") |
| 34 | first := ws.Page("", notiontest.Text("Week 1")) |
| 35 | second := ws.Page("", notiontest.Text("Week 1")) |
| 36 | late := ws.Page("", notiontest.Text("Shared later")) |
| 37 | ws.Search = ws.Search[:2] |
| 38 | ws.Add(first, notiontest.Para(notiontest.PageMention(second, "Week 1"), |
| 39 | notiontest.Text(" and "), notiontest.PageMention(late, "Untitled"), |
| 40 | notiontest.Text(" and "), notiontest.Link("the other", |
| 41 | "https://www.notion.so/Week-1-"+compactID(second)))) |
| 42 | world := newImportWorld(t, ws) |
| 43 | world.connect(ana) |
| 44 | world.importAs(ana) |
| 45 | links := world.linksOf("Week 1") |
| 46 | if !slices.Contains(links, "Week 1 (Notion)") || len(links) != 2 { |
| 47 | t.Errorf("links on the first page: %v; want the second's final "+ |
| 48 | "title twice (the mention and the URL)", links) |
| 49 | } |
| 50 | ws.Share(late) |
| 51 | world.importAs(ana) |
| 52 | if links := world.linksOf("Week 1"); !slices.Contains(links, |
| 53 | "Shared later") { |
| 54 | t.Errorf("sharing the page did not heal the link: %v", links) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func compactID(id string) string { |
| 59 | return notionKey(id) |
| 60 | } |
| 61 | |
| 62 | // I6, titles (spec §9.2, §9.3, D12). Syntax, length, emptiness and dates |
| 63 | // are fitted; duplicates take the parent's title, then a number; an |
| 64 | // existing Mapa page with the title is never written to; the same |
| 65 | // workspace gives the same titles twice. |
| 66 | func TestI6TitlesFit(t *testing.T) { |
| 67 | for raw, want := range map[string]string{ |
| 68 | "Issue #42 [draft] | x^2 # note": "Issue 42 (draft) / x2 note", |
| 69 | " ": "Untitled", |
| 70 | "": "Untitled", |
| 71 | } { |
| 72 | if got := fitTitle(raw); got != want { |
| 73 | t.Errorf("fitTitle(%q) = %q, want %q", raw, got, want) |
| 74 | } |
| 75 | } |
| 76 | long := fitTitle(repeatWord("word ", 40)) |
| 77 | if len([]rune(long)) > maxTitle || long[len(long)-3:] != "…" { |
| 78 | t.Errorf("a long title fitted to %q", long) |
| 79 | } |
| 80 | got := candidates("2024-03-05", "Journal", 3) |
| 81 | want := []string{"2024-03-05 (Journal)", "2024-03-05 (Journal) 2", |
| 82 | "2024-03-05 (Journal) 3"} |
| 83 | if !slices.Equal(got, want) { |
| 84 | t.Errorf("date candidates %v, want %v", got, want) |
| 85 | } |
| 86 | for _, candidate := range candidates(repeatWord("x", 130), |
| 87 | repeatWord("parent ", 20), 4) { |
| 88 | if len([]rune(candidate)) > maxTitle { |
| 89 | t.Errorf("a candidate is %d long", len([]rune(candidate))) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func repeatWord(word string, times int) string { |
| 95 | out := "" |
| 96 | for range times { |
| 97 | out += word |
| 98 | } |
| 99 | return out |
| 100 | } |
| 101 | |
| 102 | // I6's run half: an existing Mapa page is never written to, duplicates |
| 103 | // and a date title fit, and two runs over the same workspace agree. |
| 104 | // |
| 105 | // Mutation (make mutate, first red 2026-11-17): a candidate is free |
| 106 | // when the title is held by another page. |
| 107 | func TestI6ImportedTitlesNeverTakeAPage(t *testing.T) { |
| 108 | titles := func() []string { |
| 109 | ws := notiontest.NewWorkspace("titles") |
| 110 | journal := ws.Page("", notiontest.Text("Acme")) |
| 111 | ws.Page(journal, notiontest.Text("2024-03-05")) |
| 112 | ws.Page("", notiontest.Text("Acme")) |
| 113 | world := newImportWorld(t, ws) |
| 114 | if _, err := world.store.CreateTeamPage(ctx(), bo, "Acme", |
| 115 | "Bo's own."); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | world.connect(ana) |
| 119 | world.importAs(ana) |
| 120 | if _, record, _ := world.pageTitled("Acme"); record.Body != |
| 121 | "Bo's own." { |
| 122 | t.Errorf("the import wrote to Bo's Acme: %q", record.Body) |
| 123 | } |
| 124 | var out []string |
| 125 | for _, title := range []string{"Acme (Notion)", "Acme (Notion) 2", |
| 126 | "2024-03-05 (Acme (Notion))"} { |
| 127 | if _, _, found := world.pageTitled(title); found { |
| 128 | out = append(out, title) |
| 129 | } |
| 130 | } |
| 131 | return out |
| 132 | } |
| 133 | first, second := titles(), titles() |
| 134 | if len(first) != 3 || !slices.Equal(first, second) { |
| 135 | t.Errorf("titles %v then %v", first, second) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // I7, attribution (P7). Every import write names the person who pressed |
| 140 | // Start or Resume; a Resume by a second importer names the second. |
| 141 | // |
| 142 | // Mutations (make mutate, first red 2026-11-17): the run names the |
| 143 | // connection's creator; ImportUpdate names the page's owner. |
| 144 | func TestI7TheRunnerIsTheActor(t *testing.T) { |
| 145 | ws := newSmall() |
| 146 | world := newImportWorld(t, ws.Workspace) |
| 147 | world.app.config.Importers = []string{ana, bo} |
| 148 | world.connect(ana) |
| 149 | lease, err := world.store.StartRun(ctx(), bo, false) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | world.finish(lease, bo) |
| 154 | world.expectActor(bo, VerbCreated) |
| 155 | ws.set(ws.handbook, 1, notiontest.Para(notiontest.Text("New."))) |
| 156 | lease, err = world.store.StartRun(ctx(), bo, false) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | world.store.InterruptRuns(ctx()) |
| 161 | resumed, err := world.store.ResumeRun(ctx(), ana) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | if resumed.run != lease.run { |
| 166 | t.Fatalf("resumed run %d, started %d", resumed.run, lease.run) |
| 167 | } |
| 168 | world.finish(resumed, ana) |
| 169 | world.expectActor(ana, VerbEdited) |
| 170 | } |
| 171 | |
| 172 | // expectActor is every page event of verb naming who. |
| 173 | func (world *importWorld) expectActor(who, verb string) { |
| 174 | world.t.Helper() |
| 175 | var member Member |
| 176 | fingerprint, _ := world.store.addressPrint(who) |
| 177 | world.store.database.Where("address_print = ?", fingerprint). |
| 178 | First(&member) |
| 179 | var events []Event |
| 180 | world.store.database.Where("object_kind = ? AND verb = ?", ObjectPage, |
| 181 | verb).Find(&events) |
| 182 | if len(events) == 0 { |
| 183 | world.t.Fatalf("no %s events", verb) |
| 184 | } |
| 185 | for _, event := range events { |
| 186 | if event.ActorID != member.ID { |
| 187 | world.t.Errorf("a %s event names member %d, not %s", |
| 188 | verb, event.ActorID, who) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Fable H1: seventy pages with one title under one parent are numbered |
| 194 | // on without end (D12); a title never fails the run. |
| 195 | // |
| 196 | // Mutation (make mutate, first red 2026-11-17): the candidates bounded |
| 197 | // at 64 again. |
| 198 | func TestI6SeventySameTitlesAllImport(t *testing.T) { |
| 199 | ws := notiontest.NewWorkspace("weekly") |
| 200 | parent := ws.Page("", notiontest.Text("Meetings")) |
| 201 | for range 70 { |
| 202 | ws.Page(parent, notiontest.Text("Weekly sync")) |
| 203 | } |
| 204 | world := newImportWorld(t, ws) |
| 205 | world.connect(ana) |
| 206 | run := world.importAs(ana) |
| 207 | if run.State != runDone || run.Created != 71 || run.Failed != 0 { |
| 208 | t.Fatalf("70 siblings called Weekly sync: %+v", run) |
| 209 | } |
| 210 | for _, title := range []string{"Weekly sync", "Weekly sync (Meetings)", |
| 211 | "Weekly sync (Meetings) 69"} { |
| 212 | if _, _, found := world.pageTitled(title); !found { |
| 213 | t.Errorf("no page titled %q", title) |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Fable L4: a page deleted in Mapa holds no title in the plan, so a new |
| 219 | // page with that title takes it. |
| 220 | func TestI6ADeletedPageHoldsNoTitle(t *testing.T) { |
| 221 | ws := notiontest.NewWorkspace("deleted") |
| 222 | ws.Page("", notiontest.Text("Acme")) |
| 223 | world := newImportWorld(t, ws) |
| 224 | world.connect(ana) |
| 225 | world.importAs(ana) |
| 226 | page, _, _ := world.pageTitled("Acme") |
| 227 | if err := world.store.Delete(ctx(), bo, page.ID); err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | ws.Page("", notiontest.Text("Acme")) |
| 231 | run := world.importAs(ana) |
| 232 | if _, _, found := world.pageTitled("Acme"); !found || run.Created != 1 { |
| 233 | t.Errorf("the new Acme did not take the free title: %+v", run) |
| 234 | } |
| 235 | } |
| 236 | |