oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "html/template"
5 "strings"
6 "testing"
7
8 "amadan.net/oficina/mapa/internal/mapa/markup"
9)
10
11func TestEditSummaries(t *testing.T) {
12 base := "Intro.\n\n## Where things stand\n\nRenewal in February.\n\n" +
13 "## Contacts\n\nAna, budget."
14 for _, check := range []struct{ body, want string }{
15 {base, "Edited"},
16 {strings.Replace(base, "February", "March", 1),
17 "Edited Where things stand"},
18 {strings.Replace(base, "## Contacts", "## People", 1),
19 "Edited People"},
20 {base + "\n\nTom, signs.", "Added a block"},
21 {base + "\n\nTom, signs.\n\nBo, helps.", "Added 2 blocks"},
22 {strings.Replace(base, "Intro.\n\n", "", 1), "Removed a block"},
23 {"Intro.\n\n## Where things stand", "Removed 3 blocks"},
24 {strings.Replace(base, "Intro.", "Hello.", 1), "Edited"},
25 {strings.Replace(base, "Ana, budget.", "Ana, [[Budget]].", 1),
26 "Edited Contacts"},
27 } {
28 if got := editSummary(nil, markup.Parse(base),
29 markup.Parse(check.body)); got != check.want {
30 t.Errorf("%q: got %q, want %q", check.body, got, check.want)
31 }
32 }
33}
34
35func plainDraw(blocks []markup.Block) (template.HTML, error) {
36 return template.HTML("[" + template.HTMLEscapeString(
37 markup.Join(blocks)) + "]"), nil
38}
39
40// noLinks resolves every link nowhere: enough to draw with Render.
41type noLinks struct{}
42
43func (noLinks) Resolve(string, string) markup.Resolution {
44 return markup.Resolution{Href: "#"}
45}
46
47func renderDraw(blocks []markup.Block) (template.HTML, error) {
48 return markup.Render(blocks, noLinks{}), nil
49}
50
51func diffOf(t *testing.T, old, body string) string {
52 t.Helper()
53 drawn, err := diffHTML(markup.Parse(old), markup.Parse(body), plainDraw)
54 if err != nil {
55 t.Fatal(err)
56 }
57 return string(drawn)
58}
59
60func TestTheWordDiffMarksOnlyWhatChanged(t *testing.T) {
61 got := diffOf(t, "Same.\n\n- The window opens in February.",
62 "Same.\n\n- The window opens in March.")
63 want := `[Same.]<div mapa-diff="changed"><ul mapa-diff-level="0"><li>` +
64 `The window opens in <del>February.</del><ins>March.</ins>` +
65 "</li></ul></div>\n" + markup.EmptyParagraph
66 if got != want {
67 t.Fatalf("got %s\nwant %s", got, want)
68 }
69}
70
71func TestALinkIsOneWord(t *testing.T) {
72 got := diffOf(t, "See [[Acme Holdings|the client]] now.",
73 "See [[Acme Holdings|the clients]] now.")
74 if !strings.Contains(got, `<del><span mapa-diff-link>the client</span>`+
75 `</del><ins><span mapa-diff-link>the clients</span></ins>`) {
76 t.Fatalf("a link was not diffed whole: %s", got)
77 }
78}
79
80func TestAddedAndRemovedBlocksAreWhole(t *testing.T) {
81 got := diffOf(t, "# Old heading\n\nKept.", "Kept.\n\n> New quote")
82 for _, want := range []string{
83 `<div mapa-diff="removed"><h2><del>Old heading</del></h2></div>`,
84 `<div mapa-diff="added"><blockquote><p><ins>New quote</ins>` +
85 `</p></blockquote></div>`,
86 "[Kept.]",
87 } {
88 if !strings.Contains(got, want) {
89 t.Errorf("missing %s in %s", want, got)
90 }
91 }
92}
93
94// Everything typed is escaped: the diff never passes markup through.
95func TestTheDiffEscapes(t *testing.T) {
96 got := diffOf(t, "a <b>", "a <script>alert(1)</script> [[<x>]]")
97 if strings.Contains(got, "<script>") || strings.Contains(got, "<x>") ||
98 strings.Contains(got, "<b>") {
99 t.Fatalf("unescaped: %s", got)
100 }
101}
102
103func TestWordsKeepMultibyteText(t *testing.T) {
104 got := diffOf(t, "café naïve", "café naïf")
105 if !strings.Contains(got, "café <del>naïve</del><ins>naïf</ins>") {
106 t.Fatalf("got %s", got)
107 }
108}
109
110// A changed block pairs with the added block it is like, past blocks
111// inserted before it; an unlike one is removed and added whole.
112func TestBlocksPairByLikeness(t *testing.T) {
113 got := diffOf(t, "- Renewal is tied to the grant cycle\n- Old note",
114 "## Where things stand\n\n- Renewal is tied to the grant cycle "+
115 "in March.\n- Something else entirely")
116 for _, want := range []string{
117 `<div mapa-diff="added"><h3><ins>Where things stand</ins></h3>`,
118 `<div mapa-diff="changed"><ul mapa-diff-level="0"><li>Renewal ` +
119 `is tied to the grant cycle<ins> in March.</ins></li>`,
120 `<div mapa-diff="removed"><ul mapa-diff-level="0"><li><del>Old ` +
121 `note</del>`,
122 `<div mapa-diff="added"><ul mapa-diff-level="0"><li><ins>` +
123 `Something else entirely</ins>`,
124 } {
125 if !strings.Contains(got, want) {
126 t.Errorf("missing %s in\n%s", want, got)
127 }
128 }
129}
130
131func TestChangedBlocksDrawEmphasisAndNumbers(t *testing.T) {
132 got := diffOf(t, "1. Ana, budget.\n2. **Tom**, signs.",
133 "1. Ana, budget.\n2. **Tom**, signs today.")
134 want := `<ol start="2" mapa-diff-level="0"><li><strong>Tom</strong>,` +
135 ` <del>signs.</del><ins>signs today.</ins></li></ol>`
136 if !strings.Contains(got, want) {
137 t.Fatalf("got %s", got)
138 }
139}
140
141// Review L9: between two kept blocks, what was removed is drawn before
142// what was added, as diffBlocks says.
143func TestRemovedBlocksAreDrawnBeforeAddedOnes(t *testing.T) {
144 got := diffOf(t, "Kept.\n\nAn old line about budgets.\n\nAlso kept.",
145 "Kept.\n\n# Something new\n\n- Another\n\nAlso kept.")
146 removed := strings.Index(got, `mapa-diff="removed"`)
147 added := strings.Index(got, `mapa-diff="added"`)
148 if removed < 0 || added < 0 || removed > added {
149 t.Fatalf("removed is not drawn before added:\n%s", got)
150 }
151}
152
153// Review M4: the editor's empty paragraph is drawn once, at the end of
154// the whole diff and only when its last block is not a paragraph; never
155// after a kept list in the middle of the text.
156func TestTheEmptyParagraphEndsTheDiffOnce(t *testing.T) {
157 for _, check := range []struct{ old, body string }{
158 {"- a\n- b\n\nOld end.", "- a\n- b\n\nA new end entirely."},
159 {"- a\n- b\n\nMiddle.\n\n- c", "- a\n- b\n\nMiddle, now.\n\n- c"},
160 {"Start.\n\n- a", "Start, now.\n\n- a"},
161 } {
162 drawn, err := diffHTML(markup.Parse(check.old),
163 markup.Parse(check.body), renderDraw)
164 if err != nil {
165 t.Fatal(err)
166 }
167 got := string(drawn)
168 count := strings.Count(got, markup.EmptyParagraph)
169 ends := strings.HasSuffix(got, markup.EmptyParagraph)
170 last := markup.Parse(check.body)
171 wantEnd := last[len(last)-1].Kind != markup.Paragraph
172 if count > 1 || (count == 1) != ends || ends != wantEnd {
173 t.Errorf("%q: %d empty paragraphs in\n%s", check.body, count,
174 got)
175 }
176 }
177}
178
179// Review L8: a numbered list kept after a change goes on from its own
180// number rather than starting again at 1.
181func TestAContinuedNumberedListKeepsItsNumbers(t *testing.T) {
182 drawn, err := diffHTML(markup.Parse("1. One\n2. Two\n3. Three\n4. Four"),
183 markup.Parse("1. One\n2. Two, now\n3. Three\n4. Four"), renderDraw)
184 if err != nil {
185 t.Fatal(err)
186 }
187 got := string(drawn)
188 if !strings.HasPrefix(got, "<ol><li><p>One") ||
189 !strings.Contains(got, `</div>`+"\n"+`<ol start="3"><li><p>Three`) {
190 t.Fatalf("got %s", got)
191 }
192}
193