|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +// Faithful JS mirror of plugin/pi-edit/src/hashline/apply.ts (pure functions) |
| 5 | +const HASH_SEP = "\u2502"; |
| 6 | + |
| 7 | +function buildIdx(content) { |
| 8 | + const fileLines = content.split("\n"); |
| 9 | + const lineStarts = []; |
| 10 | + let offset = 0; |
| 11 | + for (let index = 0; index < fileLines.length; index++) { |
| 12 | + lineStarts.push(offset); |
| 13 | + offset += fileLines[index].length; |
| 14 | + if (index < fileLines.length - 1) offset += 1; |
| 15 | + } |
| 16 | + return { fileLines, lineStarts }; |
| 17 | +} |
| 18 | + |
| 19 | +function visLines(text) { |
| 20 | + if (text.length === 0) return []; |
| 21 | + const lines = text.split("\n"); |
| 22 | + return text.endsWith("\n") ? lines.slice(0, -1) : lines; |
| 23 | +} |
| 24 | + |
| 25 | +function changedRange(original, result) { |
| 26 | + if (original === result) return null; |
| 27 | + if (original.length === 0) return { firstChangedLine: 1, lastChangedLine: visLines(result).length }; |
| 28 | + if (result.startsWith(original) && original.endsWith("\n")) { |
| 29 | + return { firstChangedLine: visLines(original).length + 1, lastChangedLine: visLines(result).length }; |
| 30 | + } |
| 31 | + let firstDiff = 0; |
| 32 | + const minLen = Math.min(original.length, result.length); |
| 33 | + while (firstDiff < minLen && original[firstDiff] === result[firstDiff]) firstDiff++; |
| 34 | + if (firstDiff === minLen && original.length === result.length) return null; |
| 35 | + let lastOrig = original.length - 1; |
| 36 | + let lastRes = result.length - 1; |
| 37 | + while (lastOrig >= firstDiff && lastRes >= firstDiff && original[lastOrig] === result[lastRes]) { |
| 38 | + lastOrig--; |
| 39 | + lastRes--; |
| 40 | + } |
| 41 | + function idxToLine(charIdx, text) { |
| 42 | + let line = 1; |
| 43 | + for (let i = 0; i < charIdx && i < text.length; i++) if (text[i] === "\n") line++; |
| 44 | + return line; |
| 45 | + } |
| 46 | + const firstChangedLine = idxToLine(firstDiff + 1, result); |
| 47 | + let lastChangedLine; |
| 48 | + if (lastRes < firstDiff) { |
| 49 | + lastChangedLine = result.length === 0 ? 1 : visLines(result).length; |
| 50 | + } else if (firstDiff === 0 && original.length > 0 && result.endsWith(original)) { |
| 51 | + lastChangedLine = firstChangedLine; |
| 52 | + } else { |
| 53 | + lastChangedLine = idxToLine(lastRes + 1, result); |
| 54 | + } |
| 55 | + return { firstChangedLine, lastChangedLine }; |
| 56 | +} |
| 57 | + |
| 58 | +function fmtRegion(hashes, lines) { |
| 59 | + if (hashes.length !== lines.length) { |
| 60 | + throw new Error(`fmtRegion: hashes.length (${hashes.length}) must match lines.length (${lines.length}).`); |
| 61 | + } |
| 62 | + return lines.map((line, index) => `${hashes[index]}${HASH_SEP}${line}`).join("\n"); |
| 63 | +} |
| 64 | + |
| 65 | +test("buildIdx empty string", () => { |
| 66 | + const idx = buildIdx(""); |
| 67 | + assert.deepEqual(idx.fileLines, [""]); |
| 68 | + assert.deepEqual(idx.lineStarts, [0]); |
| 69 | +}); |
| 70 | + |
| 71 | +test("buildIdx single line no newline", () => { |
| 72 | + const idx = buildIdx("hello"); |
| 73 | + assert.deepEqual(idx.fileLines, ["hello"]); |
| 74 | + assert.deepEqual(idx.lineStarts, [0]); |
| 75 | +}); |
| 76 | + |
| 77 | +test("buildIdx two lines no trailing newline", () => { |
| 78 | + const idx = buildIdx("line1\nline2"); |
| 79 | + assert.deepEqual(idx.fileLines, ["line1", "line2"]); |
| 80 | + assert.deepEqual(idx.lineStarts, [0, 6]); |
| 81 | +}); |
| 82 | + |
| 83 | +test("buildIdx three lines with trailing newline", () => { |
| 84 | + const idx = buildIdx("a\nb\nc\n"); |
| 85 | + assert.deepEqual(idx.fileLines, ["a", "b", "c", ""]); |
| 86 | + assert.deepEqual(idx.lineStarts, [0, 2, 4, 6]); |
| 87 | +}); |
| 88 | + |
| 89 | +test("buildIdx lineStarts offset account for newline separator", () => { |
| 90 | + const idx = buildIdx("ab\ncd\nef"); |
| 91 | + // line 0 starts at 0, line 1 at 3 (2 chars + 1 newline), line 2 at 6 |
| 92 | + assert.deepEqual(idx.lineStarts, [0, 3, 6]); |
| 93 | +}); |
| 94 | + |
| 95 | +test("changedRange identical returns null", () => { |
| 96 | + assert.equal(changedRange("same\ncontent\n", "same\ncontent\n"), null); |
| 97 | +}); |
| 98 | + |
| 99 | +test("changedRange empty original to content", () => { |
| 100 | + const range = changedRange("", "new content\n"); |
| 101 | + assert.deepEqual(range, { firstChangedLine: 1, lastChangedLine: 1 }); |
| 102 | +}); |
| 103 | + |
| 104 | +test("changedRange append at end with newline", () => { |
| 105 | + const original = "line1\nline2\n"; |
| 106 | + const result = "line1\nline2\nline3\n"; |
| 107 | + const range = changedRange(original, result); |
| 108 | + assert.deepEqual(range, { firstChangedLine: 3, lastChangedLine: 3 }); |
| 109 | +}); |
| 110 | + |
| 111 | +test("changedRange prepend at start", () => { |
| 112 | + const original = "line2\nline3\n"; |
| 113 | + const result = "line1\nline2\nline3\n"; |
| 114 | + const range = changedRange(original, result); |
| 115 | + assert.deepEqual(range, { firstChangedLine: 1, lastChangedLine: 2 }); |
| 116 | +}); |
| 117 | + |
| 118 | + |
| 119 | +test("changedRange middle change", () => { |
| 120 | + const original = "line1\nline2\nline3\n"; |
| 121 | + const result = "line1\nCHANGED\nline3\n"; |
| 122 | + const range = changedRange(original, result); |
| 123 | + assert.deepEqual(range, { firstChangedLine: 2, lastChangedLine: 2 }); |
| 124 | +}); |
| 125 | + |
| 126 | +test("changedRange full replacement", () => { |
| 127 | + const original = "a\nb\nc\n"; |
| 128 | + const result = "x\ny\nz\n"; |
| 129 | + const range = changedRange(original, result); |
| 130 | + assert.deepEqual(range, { firstChangedLine: 1, lastChangedLine: 3 }); |
| 131 | +}); |
| 132 | + |
| 133 | +test("changedRange shrink (delete lines)", () => { |
| 134 | + const original = "a\nb\nc\nd\n"; |
| 135 | + const result = "a\nd\n"; |
| 136 | + const range = changedRange(original, result); |
| 137 | + // Line 2 changes from b to d |
| 138 | + assert.ok(range !== null); |
| 139 | + assert.equal(range.firstChangedLine, 2); |
| 140 | +}); |
| 141 | + |
| 142 | +test("fmtRegion formats with hash separator", () => { |
| 143 | + const result = fmtRegion(["h1", "h2"], ["line1", "line2"]); |
| 144 | + assert.equal(result, "h1\u2502line1\nh2\u2502line2"); |
| 145 | +}); |
| 146 | + |
| 147 | +test("fmtRegion single line", () => { |
| 148 | + assert.equal(fmtRegion(["abc"], ["only line"]), "abc\u2502only line"); |
| 149 | +}); |
| 150 | + |
| 151 | +test("fmtRegion mismatched lengths throws", () => { |
| 152 | + assert.throws(() => fmtRegion(["h1", "h2"], ["only one"]), /fmtRegion/); |
| 153 | +}); |
| 154 | + |
| 155 | +test("fmtRegion empty arrays", () => { |
| 156 | + assert.equal(fmtRegion([], []), ""); |
| 157 | +}); |
0 commit comments