| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "sort" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | "amadan.net/oficina/notion.git/notiontest" |
| 12 | ) |
| 13 | |
| 14 | // I1 over the module's documented fixtures (Notion import spec §18): |
| 15 | // the whole set imported through the real survey, converter, mapper and |
| 16 | // write, each page's stored body equal to a committed golden, written |
| 17 | // with no fallback. notionmd's own goldens hold the mapper's rows one by |
| 18 | // one; this holds the path from Notion's JSON to the stored body. |
| 19 | // Rewrite with go test -run TestI1Documented -update. |
| 20 | func TestI1DocumentedImportsAsTheGoldens(t *testing.T) { |
| 21 | doc := notiontest.NewDocumented() |
| 22 | world := newImportWorld(t, doc.Workspace) |
| 23 | world.connect(ana) |
| 24 | if run := world.importAs(ana); run.State != runDone || |
| 25 | run.Created != 6 || run.Failed != 0 { |
| 26 | t.Fatalf("the documented import: %+v", run) |
| 27 | } |
| 28 | report, err := world.store.report(ctx(), world.run(1)) |
| 29 | if err != nil || report.Fallbacks != 0 { |
| 30 | t.Fatalf("fallbacks %d, %v", report.Fallbacks, err) |
| 31 | } |
| 32 | // N-M1: the page inside a database row is imported, with no In: |
| 33 | // line, and listed. Mutation: the survey forgets UnderRow. |
| 34 | if len(report.UnderRow) != 1 || |
| 35 | report.UnderRow[0].Title != "Inside a row" { |
| 36 | t.Errorf("pages under a row: %+v", report.UnderRow) |
| 37 | } |
| 38 | for title, body := range world.bodies() { |
| 39 | path := filepath.Join("testdata", "notion", |
| 40 | strings.ReplaceAll(strings.ToLower(title), " ", "-")+".md") |
| 41 | if *updateCorpus { |
| 42 | os.MkdirAll(filepath.Dir(path), 0o755) |
| 43 | os.WriteFile(path, []byte(body), 0o644) |
| 44 | continue |
| 45 | } |
| 46 | want, err := os.ReadFile(path) |
| 47 | if err != nil || string(want) != body { |
| 48 | t.Errorf("%s differs from %s (%v):\n%s", title, path, err, body) |
| 49 | } |
| 50 | if strings.Contains(markup.Normalise(body), "couldn't be copied") { |
| 51 | t.Errorf("%s holds a fallback", title) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // bodies is every live team page's body, by title. |
| 57 | func (world *importWorld) bodies() map[string]string { |
| 58 | world.t.Helper() |
| 59 | var pages []Page |
| 60 | world.store.database.Where("kind = ? AND gone = ?", KindPage, false). |
| 61 | Find(&pages) |
| 62 | out := map[string]string{} |
| 63 | for _, page := range pages { |
| 64 | record, err := world.store.OpenPage(page) |
| 65 | if err != nil { |
| 66 | world.t.Fatal(err) |
| 67 | } |
| 68 | out[record.Title] = record.Body |
| 69 | } |
| 70 | return out |
| 71 | } |
| 72 | |
| 73 | // I10, no Notion content in logs (memoria N13). A run against a fake |
| 74 | // whose 404 bodies carry a canary, with one page missing, logs neither |
| 75 | // the canary, nor a title, nor a body, nor a Notion id. |
| 76 | // |
| 77 | // Mutation (make mutate, first red 2026-11-17): a page's failure logged |
| 78 | // with Notion's response. |
| 79 | func TestI10NoNotionContentInLogs(t *testing.T) { |
| 80 | doc := notiontest.NewDocumented() |
| 81 | world := newImportWorld(t, doc.Workspace) |
| 82 | world.server.Missing[doc.Child] = true |
| 83 | world.connect(ana) |
| 84 | world.importAs(ana) |
| 85 | world.importAs(ana) |
| 86 | logs := world.logs.String() |
| 87 | secrets := []string{notiontest.Canary, "Every block", "Every run", |
| 88 | "Grandchild", "Inside the toggle", "secret_test"} |
| 89 | for _, id := range []string{doc.Blocks, doc.Child, doc.Unshared} { |
| 90 | secrets = append(secrets, id, notionKey(id)) |
| 91 | } |
| 92 | sort.Strings(secrets) |
| 93 | for _, secret := range secrets { |
| 94 | if strings.Contains(logs, secret) { |
| 95 | t.Errorf("the log holds %q", secret) |
| 96 | } |
| 97 | } |
| 98 | if !strings.Contains(logs, "migrate") { |
| 99 | t.Fatal("the log sink saw nothing at all") |
| 100 | } |
| 101 | } |
| 102 | |