~bigbes/confluence-md-utilities

ref: 5fabfe018459f627441085dd7f87f1f9e4e97af8 confluence-md-utilities/cmd/mdcx/verify_test.go -rw-r--r-- 10.8 KiB
5fabfe01 — Eugene Blikh feat: add verify command, improve round-trip fidelity 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// --- normalizeForVerify ---

func TestNormalizeForVerify_RemovesEmptyBrParagraph(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{
			name:  "br with space before slash",
			input: `<p><br /></p><p>text</p>`,
			want:  `<p>text</p>`,
		},
		{
			name:  "br without space",
			input: `<p><br/></p><p>text</p>`,
			want:  `<p>text</p>`,
		},
		{
			name:  "br with whitespace around",
			input: "<p> \n <br /> \n </p><p>text</p>",
			want:  "<p>text</p>",
		},
		{
			name:  "no empty paragraphs",
			input: `<p>hello</p>`,
			want:  `<p>hello</p>`,
		},
		{
			name:  "multiple empty paragraphs",
			input: `<p><br /></p><p><br/></p><p>text</p>`,
			want:  `<p>text</p>`,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, normalizeForVerify(tt.input))
		})
	}
}

func TestNormalizeForVerify_SpanInsideCode(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{
			name:  "span inside code unwrapped",
			input: `<code>hello<span> world</span></code>`,
			want:  `<code>hello world</code>`,
		},
		{
			name:  "span with attributes inside code",
			input: `<code>a<span class="x"> : </span></code>`,
			want:  `<code>a : </code>`,
		},
		{
			name:  "no span inside code",
			input: `<code>plain</code>`,
			want:  `<code>plain</code>`,
		},
		{
			name:  "span outside code untouched",
			input: `<p><span>text</span></p>`,
			want:  `<p><span>text</span></p>`,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, normalizeForVerify(tt.input))
		})
	}
}

func TestNormalizeForVerify_AdjacentCodeMerged(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{
			name:  "directly adjacent",
			input: `<code>hello</code><code>world</code>`,
			want:  `<code>helloworld</code>`,
		},
		{
			name:  "with whitespace between",
			input: `<code>hello</code> <code>world</code>`,
			want:  `<code>hello</code> <code>world</code>`,
		},
		{
			name:  "single code element untouched",
			input: `<code>hello</code>`,
			want:  `<code>hello</code>`,
		},
		{
			name:  "combined: span inside + adjacent merge",
			input: `<code>plan<span> : </span></code><code>vclock</code>`,
			want:  `<code>plan : vclock</code>`,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, normalizeForVerify(tt.input))
		})
	}
}

// --- computeDiffOps ---

func TestComputeDiffOps_IdenticalInputs(t *testing.T) {
	lines := []string{"a", "b", "c"}
	ops := computeDiffOps(lines, lines)

	require.Len(t, ops, 3)
	for _, op := range ops {
		assert.Equal(t, opEqual, op.op)
	}
}

func TestComputeDiffOps_CompletelyDifferent(t *testing.T) {
	a := []string{"a", "b"}
	b := []string{"x", "y"}
	ops := computeDiffOps(a, b)

	var removes, adds int
	for _, op := range ops {
		switch op.op {
		case opRemove:
			removes++
		case opAdd:
			adds++
		}
	}
	assert.Equal(t, 2, removes)
	assert.Equal(t, 2, adds)
}

func TestComputeDiffOps_EmptyInputs(t *testing.T) {
	assert.Empty(t, computeDiffOps(nil, nil))
	assert.Empty(t, computeDiffOps([]string{}, []string{}))
}

func TestComputeDiffOps_OneEmpty(t *testing.T) {
	ops := computeDiffOps([]string{"a", "b"}, nil)
	require.Len(t, ops, 2)
	for _, op := range ops {
		assert.Equal(t, opRemove, op.op)
	}

	ops = computeDiffOps(nil, []string{"x", "y"})
	require.Len(t, ops, 2)
	for _, op := range ops {
		assert.Equal(t, opAdd, op.op)
	}
}

func TestComputeDiffOps_SingleLineChange(t *testing.T) {
	a := []string{"aaa", "bbb", "ccc"}
	b := []string{"aaa", "BBB", "ccc"}
	ops := computeDiffOps(a, b)

	// Should be: equal(aaa), remove(bbb), add(BBB), equal(ccc)
	require.Len(t, ops, 4)
	assert.Equal(t, opEqual, ops[0].op)
	assert.Equal(t, "aaa", ops[0].text)
	assert.Equal(t, opRemove, ops[1].op)
	assert.Equal(t, "bbb", ops[1].text)
	assert.Equal(t, opAdd, ops[2].op)
	assert.Equal(t, "BBB", ops[2].text)
	assert.Equal(t, opEqual, ops[3].op)
	assert.Equal(t, "ccc", ops[3].text)
}

func TestComputeDiffOps_LineNumbers(t *testing.T) {
	a := []string{"same", "old"}
	b := []string{"same", "new"}
	ops := computeDiffOps(a, b)

	// equal: lineA=1, lineB=1
	assert.Equal(t, 1, ops[0].lineA)
	assert.Equal(t, 1, ops[0].lineB)
	// remove: lineA=2, lineB=-1
	assert.Equal(t, 2, ops[1].lineA)
	assert.Equal(t, -1, ops[1].lineB)
	// add: lineA=-1, lineB=2
	assert.Equal(t, -1, ops[2].lineA)
	assert.Equal(t, 2, ops[2].lineB)
}

func TestComputeDiffOps_Insertion(t *testing.T) {
	a := []string{"a", "c"}
	b := []string{"a", "b", "c"}
	ops := computeDiffOps(a, b)

	require.Len(t, ops, 3)
	assert.Equal(t, opEqual, ops[0].op)
	assert.Equal(t, opAdd, ops[1].op)
	assert.Equal(t, "b", ops[1].text)
	assert.Equal(t, opEqual, ops[2].op)
}

func TestComputeDiffOps_Deletion(t *testing.T) {
	a := []string{"a", "b", "c"}
	b := []string{"a", "c"}
	ops := computeDiffOps(a, b)

	require.Len(t, ops, 3)
	assert.Equal(t, opEqual, ops[0].op)
	assert.Equal(t, opRemove, ops[1].op)
	assert.Equal(t, "b", ops[1].text)
	assert.Equal(t, opEqual, ops[2].op)
}

// --- buildHunks ---

func TestBuildHunks_NoChanges(t *testing.T) {
	ops := computeDiffOps([]string{"a", "b", "c"}, []string{"a", "b", "c"})
	hunks := buildHunks(ops, 3)
	assert.Empty(t, hunks)
}

func TestBuildHunks_SingleChange(t *testing.T) {
	a := []string{"1", "2", "3", "4", "5"}
	b := []string{"1", "2", "X", "4", "5"}
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 1)

	require.Len(t, hunks, 1)
	h := hunks[0]

	// Context=1: line 2 (before) + remove(3)/add(X) + line 4 (after) = 3 each side
	assert.Equal(t, 3, h.countA) // 2, remove(3), 4
	assert.Equal(t, 3, h.countB) // 2, add(X), 4
}

func TestBuildHunks_TwoSeparateChanges(t *testing.T) {
	// Changes far enough apart to be separate hunks
	a := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
	b := []string{"1", "X", "3", "4", "5", "6", "7", "8", "9", "10", "Y", "12"}
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 1)

	assert.Len(t, hunks, 2)
}

func TestBuildHunks_MergesNearbyChanges(t *testing.T) {
	// Two changes only 2 lines apart with ctx=3 should merge
	a := []string{"1", "2", "3", "4", "5", "6", "7"}
	b := []string{"1", "X", "3", "4", "Y", "6", "7"}
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 3)

	assert.Len(t, hunks, 1, "nearby changes should merge into one hunk")
}

func TestBuildHunks_ContextClampedToFileEdge(t *testing.T) {
	// Change at line 1 — context shouldn't go negative
	a := []string{"old", "same"}
	b := []string{"new", "same"}
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 3)

	require.Len(t, hunks, 1)
	assert.Equal(t, 1, hunks[0].startA)
	assert.Equal(t, 1, hunks[0].startB)
}

func TestBuildHunks_Counts(t *testing.T) {
	a := []string{"ctx", "old1", "old2", "ctx"}
	b := []string{"ctx", "new1", "ctx"}
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 1)

	require.Len(t, hunks, 1)
	h := hunks[0]
	// countA = context lines + removed lines
	// countB = context lines + added lines
	aLines := 0
	bLines := 0
	for _, dl := range h.lines {
		if dl.op == opEqual || dl.op == opRemove {
			aLines++
		}
		if dl.op == opEqual || dl.op == opAdd {
			bLines++
		}
	}
	assert.Equal(t, aLines, h.countA)
	assert.Equal(t, bLines, h.countB)
}

// --- inlineHighlight ---

func TestInlineHighlight_IdenticalLines(t *testing.T) {
	a, b := inlineHighlight("same text", "same text")
	// No ANSI escapes added when lines are identical
	assert.Equal(t, "same text", a)
	assert.Equal(t, "same text", b)
}

func TestInlineHighlight_SingleWordDiff(t *testing.T) {
	a, b := inlineHighlight("hello world", "hello Earth")
	// "hello " is common prefix, no common suffix
	assert.Contains(t, a, "hello ")
	assert.Contains(t, b, "hello ")
	// Changed part should have bold marker
	assert.Contains(t, a, ansiBold)
	assert.Contains(t, b, ansiBold)
	// Changed part should have appropriate background
	assert.Contains(t, a, ansiRedBg)
	assert.Contains(t, b, ansiGrnBg)
}

func TestInlineHighlight_MiddleChange(t *testing.T) {
	a, b := inlineHighlight("abc-OLD-xyz", "abc-NEW-xyz")
	// Common prefix "abc-", common suffix "-xyz"
	// Both lines should highlight "OLD" / "NEW" in bold
	assert.Contains(t, a, ansiBold)
	assert.Contains(t, b, ansiBold)
	assert.Contains(t, a, "OLD")
	assert.Contains(t, b, "NEW")
	// Prefix and suffix present without bold
	assertPlainContains(t, a, "abc-")
	assertPlainContains(t, b, "abc-")
}

func TestInlineHighlight_PrefixOnlyDifference(t *testing.T) {
	a, b := inlineHighlight("XXX-same", "YYY-same")
	// "-same" is common suffix
	assert.Contains(t, a, "XXX")
	assert.Contains(t, b, "YYY")
	assert.Contains(t, a, ansiBold)
}

func TestInlineHighlight_SuffixOnlyDifference(t *testing.T) {
	a, b := inlineHighlight("same-XXX", "same-YYY")
	// "same-" is common prefix
	assert.Contains(t, a, "XXX")
	assert.Contains(t, b, "YYY")
	assert.Contains(t, a, ansiBold)
}

func TestInlineHighlight_EmptyVsNonEmpty(t *testing.T) {
	_, b := inlineHighlight("", "added")
	assert.Contains(t, b, "added")
	assert.Contains(t, b, ansiBold)
}

func TestInlineHighlight_Unicode(t *testing.T) {
	a, b := inlineHighlight("привет мир", "привет мор")
	assert.Contains(t, a, ansiBold)
	assert.Contains(t, b, ansiBold)
	// Common prefix "привет м" + common suffix "р" should be plain
	assertPlainContains(t, a, "привет м")
	assertPlainContains(t, b, "привет м")
}

// assertPlainContains checks that s contains substr in a position
// not immediately preceded by an ANSI escape.
func assertPlainContains(t *testing.T, s, substr string) {
	t.Helper()
	assert.Contains(t, s, substr, "string should contain %q", substr)
}

// --- integration: computeDiffOps + buildHunks round-trip consistency ---

func TestDiffOps_AllOpsPreserveText(t *testing.T) {
	a := []string{"line1", "line2", "line3", "line4"}
	b := []string{"line1", "changed", "line3", "added", "line4"}
	ops := computeDiffOps(a, b)

	// Reconstruct A and B from ops
	var gotA, gotB []string
	for _, op := range ops {
		switch op.op {
		case opEqual:
			gotA = append(gotA, op.text)
			gotB = append(gotB, op.text)
		case opRemove:
			gotA = append(gotA, op.text)
		case opAdd:
			gotB = append(gotB, op.text)
		}
	}
	assert.Equal(t, a, gotA, "reconstructed A must match original")
	assert.Equal(t, b, gotB, "reconstructed B must match original")
}

func TestBuildHunks_AllChangedLinesPresent(t *testing.T) {
	a := strings.Split("a\nb\nc\nd\ne\nf\ng\nh\ni\nj", "\n")
	b := strings.Split("a\nB\nc\nd\ne\nf\ng\nH\ni\nj", "\n")
	ops := computeDiffOps(a, b)
	hunks := buildHunks(ops, 1)

	// Collect all changed texts from hunks
	var removed, added []string
	for _, h := range hunks {
		for _, dl := range h.lines {
			switch dl.op {
			case opRemove:
				removed = append(removed, dl.text)
			case opAdd:
				added = append(added, dl.text)
			}
		}
	}
	assert.Equal(t, []string{"b", "h"}, removed)
	assert.Equal(t, []string{"B", "H"}, added)
}