| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "flag" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // Gate I1, fidelity (import spec §18). For every fixture the mapper's |
| 14 | // body equals its committed golden, passes the structure check against |
| 15 | // the meaning computed first (no fixture produces the fallback), and |
| 16 | // loses nothing outside §10 and §11's tables: each loss is in the |
| 17 | // committed counts. The bodies join markup's corpus (import.json), so |
| 18 | // gates L1 to L4 hold the editor to them: each, opened in the editor |
| 19 | // and left alone, serialises to the same bytes. |
| 20 | // |
| 21 | // make golden-notion (or go test -run TestI1 -update) rewrites the |
| 22 | // fixtures' JSON, the goldens and import.json; review the diff. |
| 23 | // |
| 24 | // Mutations (make mutate, first red 2026-11-17): the line-start rule |
| 25 | // dropped; [[ left alone; backticks left alone; the structure check |
| 26 | // passing everything. |
| 27 | var update = flag.Bool("update", false, "rewrite the goldens") |
| 28 | |
| 29 | const ( |
| 30 | fixtureDir = "testdata/fixtures" |
| 31 | goldenDir = "testdata/golden" |
| 32 | corpusPath = "../markup/testdata/corpus/import.json" |
| 33 | ) |
| 34 | |
| 35 | func allFixtures() []fixture { |
| 36 | all := append(blockFixtures(), mediaFixtures()...) |
| 37 | all = append(all, runFixtures()...) |
| 38 | all = append(all, literalFixtures()...) |
| 39 | all = append(all, reviewFixtures()...) |
| 40 | all = append(all, reviewTwoFixtures()...) |
| 41 | all = append(all, reviewThreeFixtures()...) |
| 42 | return append(all, reviewFourFixtures()...) |
| 43 | } |
| 44 | |
| 45 | // golden is what a fixture's render is held to, beside its body. |
| 46 | type golden struct { |
| 47 | Result Result `json:"result"` |
| 48 | } |
| 49 | |
| 50 | func TestI1Fidelity(t *testing.T) { |
| 51 | var corpus []corpusEntry |
| 52 | for _, each := range allFixtures() { |
| 53 | result, err := Render(each.Page, each.options()) |
| 54 | if err != nil { |
| 55 | t.Errorf("%s: %v", each.Name, err) |
| 56 | continue |
| 57 | } |
| 58 | if result.Fallbacks != 0 { |
| 59 | t.Errorf("%s produced %d fallbacks", each.Name, |
| 60 | result.Fallbacks) |
| 61 | } |
| 62 | if strings.TrimRight(result.Body, " \t\n") != result.Body { |
| 63 | t.Errorf("%s: the body ends in whitespace", each.Name) |
| 64 | } |
| 65 | corpus = append(corpus, corpusEntry{Name: "import/" + each.Name, |
| 66 | Body: result.Body}) |
| 67 | compareGolden(t, each, result) |
| 68 | } |
| 69 | compareFile(t, corpusPath, encodeCorpus(corpus)) |
| 70 | } |
| 71 | |
| 72 | // corpusEntry is one body in markup's corpus. |
| 73 | type corpusEntry struct { |
| 74 | Name string `json:"name"` |
| 75 | Body string `json:"body"` |
| 76 | } |
| 77 | |
| 78 | func compareGolden(t *testing.T, each fixture, result Result) { |
| 79 | t.Helper() |
| 80 | body := result.Body |
| 81 | result.Body = "" |
| 82 | encoded := encode(t, golden{Result: result}) |
| 83 | compareFile(t, filepath.Join(fixtureDir, each.Name+".json"), |
| 84 | encode(t, each)) |
| 85 | compareFile(t, filepath.Join(goldenDir, each.Name+".md"), |
| 86 | []byte(body+"\n")) |
| 87 | compareFile(t, filepath.Join(goldenDir, each.Name+".json"), encoded) |
| 88 | } |
| 89 | |
| 90 | // compareFile holds a committed file to what the test made, or writes |
| 91 | // it under -update. |
| 92 | func compareFile(t *testing.T, path string, made []byte) { |
| 93 | t.Helper() |
| 94 | if *update { |
| 95 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if err := os.WriteFile(path, made, 0o644); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | return |
| 102 | } |
| 103 | committed, err := os.ReadFile(path) |
| 104 | if err != nil { |
| 105 | t.Errorf("%s: %v (run with -update and review it)", path, err) |
| 106 | return |
| 107 | } |
| 108 | if !bytes.Equal(committed, made) { |
| 109 | t.Errorf("%s differs:\n--- committed\n%s\n--- made\n%s", path, |
| 110 | committed, made) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func encode(t *testing.T, value any) []byte { |
| 115 | t.Helper() |
| 116 | var out bytes.Buffer |
| 117 | encoder := json.NewEncoder(&out) |
| 118 | encoder.SetEscapeHTML(false) |
| 119 | encoder.SetIndent("", " ") |
| 120 | if err := encoder.Encode(value); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | return out.Bytes() |
| 124 | } |
| 125 | |
| 126 | // encodeCorpus writes one body per line, as markup's corpus files are. |
| 127 | func encodeCorpus(entries []corpusEntry) []byte { |
| 128 | var out bytes.Buffer |
| 129 | out.WriteString("[\n") |
| 130 | for index, entry := range entries { |
| 131 | var each bytes.Buffer |
| 132 | encoder := json.NewEncoder(&each) |
| 133 | encoder.SetEscapeHTML(false) |
| 134 | _ = encoder.Encode(entry) |
| 135 | out.Write(bytes.TrimSuffix(each.Bytes(), []byte("\n"))) |
| 136 | if index < len(entries)-1 { |
| 137 | out.WriteString(",") |
| 138 | } |
| 139 | out.WriteString("\n") |
| 140 | } |
| 141 | out.WriteString("]\n") |
| 142 | return out.Bytes() |
| 143 | } |
| 144 | |
| 145 | // The committed fixtures are what the builders make: a fixture changed |
| 146 | // in Go and not rewritten would leave its JSON saying something else. |
| 147 | func TestFixturesAreUnique(t *testing.T) { |
| 148 | seen := map[string]bool{} |
| 149 | for _, each := range allFixtures() { |
| 150 | if seen[each.Name] { |
| 151 | t.Errorf("two fixtures are called %s", each.Name) |
| 152 | } |
| 153 | seen[each.Name] = true |
| 154 | } |
| 155 | } |
| 156 | |