oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package notionmd
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "amadan.net/oficina/mapa/internal/mapa/markup"
9)
10
11// The fallback (§11.4): a block that fails the check is not dropped and
12// does not fail the page. It is a note, then a fence holding its text,
13// and it is counted. The check is made to fail here because no fixture
14// may reach it.
15func TestAFailedBlockFallsBackToAFencedCopy(t *testing.T) {
16 defer func(saved func(written) bool) { checkBlock = saved }(checkBlock)
17 checkBlock = func(block written) bool {
18 return block.kind != markup.Paragraph ||
19 !strings.Contains(block.inline(), "Broken")
20 }
21 result, err := Render(Page{Blocks: []Block{para(txt("Before.")),
22 para(txt("Broken\n```fence")), para(txt("After."))}}, Options{})
23 if err != nil {
24 t.Fatal(err)
25 }
26 want := "Before.\n\n> From Notion: " + fallbackNote + "\n\n" +
27 "~~~\nBroken\n```fence\n~~~\n\nAfter."
28 if result.Body != want || result.Fallbacks != 1 {
29 t.Errorf("fallback body %q, %d fallbacks; want %q", result.Body,
30 result.Fallbacks, want)
31 }
32}
33
34// A page fails with render_failed only when the fallback fails too.
35func TestRenderFailsOnlyWhenTheFallbackFails(t *testing.T) {
36 defer func(saved func(written) bool) { checkBlock = saved }(checkBlock)
37 checkBlock = func(written) bool { return false }
38 _, err := Render(Page{Blocks: []Block{para(txt("x"))}}, Options{})
39 if !errors.Is(err, ErrRenderFailed) {
40 t.Errorf("err %v, want ErrRenderFailed", err)
41 }
42}
43
44// The check is real: a block whose literal text reads as markup, or
45// whose generated link is not read as one, fails it.
46func TestTheCheckRefusesWhatWasNotMeant(t *testing.T) {
47 for name, block := range map[string]written{
48 "a heading in a paragraph": {kind: markup.Paragraph,
49 lines: []line{{lit("# heading")}}},
50 "a code span in literal text": {kind: markup.Paragraph,
51 lines: []line{{lit("a `b` c")}}},
52 "a link swallowed": {kind: markup.Paragraph, lines: []line{{
53 lit("["), {kind: web, text: "x", url: "https://a.b"},
54 lit(" later ]]")}}},
55 "a label swallowing a link": {kind: markup.Paragraph,
56 lines: []line{{lit("a [b "),
57 {kind: web, text: "x", url: "https://a.b"}}}},
58 "a marker": {kind: markup.Paragraph,
59 lines: []line{{lit("text ^id")}}},
60 "a task box in a bullet": {kind: markup.Item,
61 lines: []line{{lit("[ ] no")}}},
62 "a fence the code closes": {kind: markup.Code, fence: "```",
63 code: "```\nx"},
64 } {
65 if check(block) {
66 t.Errorf("%s passed the check", name)
67 }
68 }
69 good := written{kind: markup.Paragraph, lines: []line{{lit("see "),
70 {kind: wiki, target: "Acme"}, lit(" and https://a.b/c")}}}
71 if !check(good) {
72 t.Error("a link and a bare address failed the check")
73 }
74}
75
76// Marks are drawn as Notion had them: the bodies are rendered by Mapa's
77// own renderer, and its emphasis read back.
78func TestMarksRenderAsNotionHadThem(t *testing.T) {
79 result, err := Render(Page{Blocks: []Block{para(txt("plain "),
80 bold("bold"), txt(" "), italic("it"), txt(" "), both("both"),
81 txt(" "), bold("a "), both("b"), bold(" c"))}}, Options{})
82 if err != nil {
83 t.Fatal(err)
84 }
85 html := string(markup.Render(markup.Parse(result.Body), nil))
86 for _, want := range []string{"<strong>bold</strong>",
87 "<em>it</em>", "<em><strong>both</strong></em>",
88 "<strong>a</strong> <em><strong>b</strong></em> " +
89 "<strong>c</strong>"} {
90 if !strings.Contains(html, want) {
91 t.Errorf("%s lacks %s", html, want)
92 }
93 }
94}
95
96// Every placeholder is a quote, and records its Notion id.
97func TestPlaceholdersAreQuotesWithIDs(t *testing.T) {
98 for _, each := range allFixtures() {
99 result, err := Render(each.Page, each.options())
100 if err != nil {
101 t.Fatal(err)
102 }
103 notes := strings.Count(result.Body, "> From Notion: ") -
104 strings.Count(result.Body, fallbackNote)
105 withIDs := 0
106 for _, id := range result.Placeholders {
107 if id != "" {
108 withIDs++
109 }
110 }
111 if notes != withIDs {
112 t.Errorf("%s: %d placeholders, %d ids", each.Name, notes,
113 withIDs)
114 }
115 }
116}
117
118// An item whose whole text is a block marker's shape is neutralised
119// too: "- ^solo" would be an empty item marked "solo". Not in the
120// corpus: the editor's settle flips such an item's marker on every
121// reparse once an edit takes the U+200B out (found by L1's seeded
122// edits, 2026-11-17), which is the editor's to fix.
123func TestAnItemOfOnlyAMarkerIsNeutralised(t *testing.T) {
124 result, err := Render(Page{Blocks: []Block{
125 blk("bulleted_list_item", txt("^solo"))}}, Options{})
126 if err != nil || result.Body != "- "+zeroWidth+"^solo" {
127 t.Errorf("body %q, err %v", result.Body, err)
128 }
129}
130