| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "go/ast" |
| 5 | "go/parser" |
| 6 | gotoken "go/token" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/notion.git/notiontest" |
| 12 | ) |
| 13 | |
| 14 | // I16, the import never asks to write (Notion import spec §18). A whole |
| 15 | // run through a client WITHOUT the module's method pin, recording every |
| 16 | // request at the fake: only GETs, and POST /v1/search. With the pin in |
| 17 | // place the fake would never see a write, so only this version can go |
| 18 | // red for Mapa's own callers. |
| 19 | func TestI16TheImportOnlyReads(t *testing.T) { |
| 20 | doc := notiontest.NewDocumented() |
| 21 | world := newImportWorld(t, doc.Workspace) |
| 22 | world.app.notion = fakeNotion{server: world.server, unpinned: true} |
| 23 | world.connect(ana) |
| 24 | world.importAs(ana) |
| 25 | world.importAs(ana) |
| 26 | requests := world.server.Requests() |
| 27 | if len(requests) < 10 { |
| 28 | t.Fatalf("only %d requests", len(requests)) |
| 29 | } |
| 30 | for _, request := range requests { |
| 31 | read := request.Method == "GET" || |
| 32 | (request.Method == "POST" && request.Path == "/v1/search") |
| 33 | if !read { |
| 34 | t.Errorf("the import asked Notion for %s %s", request.Method, |
| 35 | request.Path) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // crashPoints is every named point of the run and the two methods. |
| 41 | var crashPoints = []string{"survey:read", "survey:walked", |
| 42 | "write:rendered", "create:page", "create:title", "write:committed", |
| 43 | "update:renamed", "update:styled", "update:edited", "write:fixed", |
| 44 | "pin:marked", "write:pinned"} |
| 45 | |
| 46 | // I17, crash safety (spec §14.1). The process dies between every pair |
| 47 | // of statements in the run, ImportCreate and ImportUpdate, at the first |
| 48 | // and the second arrival; boot interrupts the run and Resume finishes |
| 49 | // it. No second page, no false kept, and a map that agrees with the |
| 50 | // pages. |
| 51 | // |
| 52 | // Mutation (make mutate, first red 2026-11-17): ImportCreate leaves its |
| 53 | // map row to a later write. |
| 54 | func TestI17ACrashAnywhereResumesCleanly(t *testing.T) { |
| 55 | for _, point := range crashPoints { |
| 56 | for nth := 1; nth <= 2; nth++ { |
| 57 | for _, rerun := range []bool{false, true} { |
| 58 | t.Run(point, func(t *testing.T) { |
| 59 | crashThenResume(t, point, nth, rerun) |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // crashThenResume imports the small workspace, first (or, with rerun, |
| 67 | // again after changing two pages), dying at point's nth arrival. |
| 68 | func crashThenResume(t *testing.T, point string, nth int, rerun bool) { |
| 69 | ws := newSmall() |
| 70 | world := newImportWorld(t, ws.Workspace) |
| 71 | world.connect(ana) |
| 72 | if rerun { |
| 73 | world.importAs(ana) |
| 74 | ws.set(ws.handbook, 1, notiontest.Para(notiontest.Text("Changed."))) |
| 75 | ws.Retitle(ws.equipment, notiontest.Text("Kit")) |
| 76 | // A new icon too, so the styled revision is between the |
| 77 | // renamed and the edited (columns and icons spec I24). |
| 78 | ws.SetIcon(ws.equipment, notiontest.EmojiIcon("💻")) |
| 79 | ws.set(ws.equipment, 0, notiontest.Para(notiontest.Text("Phones."))) |
| 80 | } |
| 81 | lease, err := world.store.StartRun(ctx(), ana, false) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | crashOn(t, point, nth) |
| 86 | world.finish(lease, ana) |
| 87 | importCrash = nil |
| 88 | world.store.InterruptRuns(ctx()) |
| 89 | world.resumeIfNeeded(lease) |
| 90 | world.checkMapAgrees(3) |
| 91 | world.checkPinnedOnce(ana) |
| 92 | } |
| 93 | |
| 94 | // checkPinnedOnce is who's pins holding the import's roots, each once |
| 95 | // (I23: a crash around the pin write pins nothing twice). |
| 96 | func (world *importWorld) checkPinnedOnce(who string) { |
| 97 | world.t.Helper() |
| 98 | pins := pinTitles(world.t, world, who) |
| 99 | seen := map[string]bool{} |
| 100 | for _, title := range pins { |
| 101 | if seen[title] { |
| 102 | world.t.Errorf("%q pinned twice: %v", title, pins) |
| 103 | } |
| 104 | seen[title] = true |
| 105 | } |
| 106 | if len(pins) == 0 { |
| 107 | world.t.Error("the import pinned no root") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (world *importWorld) resumeIfNeeded(lease runLease) { |
| 112 | world.t.Helper() |
| 113 | if world.run(lease.run).State == runDone { |
| 114 | return |
| 115 | } |
| 116 | resumed, err := world.store.ResumeRun(ctx(), ana) |
| 117 | if err != nil { |
| 118 | world.t.Fatal(err) |
| 119 | } |
| 120 | if run := world.finish(resumed, ana); run.State != runDone || |
| 121 | run.Kept != 0 || run.Failed != 0 { |
| 122 | world.t.Errorf("after resume: %+v", run) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // checkMapAgrees is pages pages, each named by exactly one map row whose |
| 127 | // coordinate holds its current revision. |
| 128 | func (world *importWorld) checkMapAgrees(pages int64) { |
| 129 | world.t.Helper() |
| 130 | if got := world.count("pages", "kind = ?", KindPage); got != pages { |
| 131 | world.t.Errorf("%d pages, want %d: a second copy", got, pages) |
| 132 | } |
| 133 | var rows []NotionPage |
| 134 | world.store.database.Where("state NOT IN ?", []string{mapWorkspace}). |
| 135 | Find(&rows) |
| 136 | for _, row := range rows { |
| 137 | place, _ := world.store.openCoordinate(row) |
| 138 | page, found, _ := world.store.anyPage(ctx(), place.PageID) |
| 139 | if !found || page.Revision != place.Revision { |
| 140 | world.t.Errorf("row %d (%s) disagrees with its page: %+v", |
| 141 | row.ID, row.State, place) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // I17's other half: nothing but the run calls the two methods. |
| 147 | func TestI17OnlyTheRunWritesImports(t *testing.T) { |
| 148 | files, _ := filepath.Glob("*.go") |
| 149 | for _, file := range files { |
| 150 | if strings.HasSuffix(file, "_test.go") { |
| 151 | continue |
| 152 | } |
| 153 | parsed, err := parser.ParseFile(gotoken.NewFileSet(), file, nil, 0) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | ast.Inspect(parsed, func(node ast.Node) bool { |
| 158 | selector, ok := node.(*ast.SelectorExpr) |
| 159 | if ok && (selector.Sel.Name == "ImportCreate" || |
| 160 | selector.Sel.Name == "ImportUpdate") && |
| 161 | file != "import_write.go" && file != "import_job.go" { |
| 162 | t.Errorf("%s calls %s", file, selector.Sel.Name) |
| 163 | } |
| 164 | return true |
| 165 | }) |
| 166 | } |
| 167 | } |
| 168 | |