|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { diff, formatDiff, diffStats } from "../src/index.js"; |
| 3 | + |
| 4 | +// ── diff() ──────────────────────────────────────────────────────────────────── |
| 5 | + |
| 6 | +describe("diff()", () => { |
| 7 | + it("returns empty array for identical strings", () => { |
| 8 | + const lines = diff("hello\nworld", "hello\nworld"); |
| 9 | + expect(lines.every((l) => l.type === "unchanged")).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it("detects added lines", () => { |
| 13 | + const lines = diff("hello", "hello\nworld"); |
| 14 | + expect(lines).toContainEqual({ type: "added", content: "world" }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("detects removed lines", () => { |
| 18 | + const lines = diff("hello\nworld", "hello"); |
| 19 | + expect(lines).toContainEqual({ type: "removed", content: "world" }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("detects changed lines as remove + add", () => { |
| 23 | + const lines = diff("hello\nworld", "hello\nearth"); |
| 24 | + expect(lines).toContainEqual({ type: "removed", content: "world" }); |
| 25 | + expect(lines).toContainEqual({ type: "added", content: "earth" }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("handles empty original string", () => { |
| 29 | + const lines = diff("", "hello\nworld"); |
| 30 | + expect(lines.every((l) => l.type === "added")).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it("handles empty modified string", () => { |
| 34 | + const lines = diff("hello\nworld", ""); |
| 35 | + expect(lines.every((l) => l.type === "removed")).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("handles both empty strings", () => { |
| 39 | + const lines = diff("", ""); |
| 40 | + expect(lines).toEqual([]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("preserves line order", () => { |
| 44 | + const lines = diff("a\nb\nc", "a\nx\nc"); |
| 45 | + expect(lines[0]).toEqual({ type: "unchanged", content: "a" }); |
| 46 | + expect(lines[lines.length - 1]).toEqual({ |
| 47 | + type: "unchanged", |
| 48 | + content: "c", |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("handles single line change", () => { |
| 53 | + const lines = diff("foo", "bar"); |
| 54 | + expect(lines).toContainEqual({ type: "removed", content: "foo" }); |
| 55 | + expect(lines).toContainEqual({ type: "added", content: "bar" }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("handles multiline with multiple changes", () => { |
| 59 | + const a = "a\nb\nc\nd\ne"; |
| 60 | + const b = "a\nB\nc\nD\ne"; |
| 61 | + const lines = diff(a, b); |
| 62 | + const changed = lines.filter((l) => l.type !== "unchanged"); |
| 63 | + expect(changed.length).toBe(4); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +// ── formatDiff() ────────────────────────────────────────────────────────────── |
| 68 | + |
| 69 | +describe("formatDiff()", () => { |
| 70 | + it("returns empty array when no changes", () => { |
| 71 | + const lines = diff("hello\nworld", "hello\nworld"); |
| 72 | + expect(formatDiff(lines)).toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns one chunk for a single change", () => { |
| 76 | + const lines = diff("a\nb\nc", "a\nx\nc"); |
| 77 | + const chunks = formatDiff(lines, { context: 1 }); |
| 78 | + expect(chunks.length).toBe(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("respects context: 0", () => { |
| 82 | + const lines = diff("a\nb\nc", "a\nx\nc"); |
| 83 | + const chunks = formatDiff(lines, { context: 0 }); |
| 84 | + expect(chunks[0].every((l) => l.type !== "unchanged")).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("merges adjacent chunks", () => { |
| 88 | + const a = "1\n2\n3\n4\n5"; |
| 89 | + const b = "1\nX\n3\nX\n5"; |
| 90 | + const chunks = formatDiff(diff(a, b), { context: 0 }); |
| 91 | + // both changes are close — with context 1 they should merge |
| 92 | + const chunksCtx1 = formatDiff(diff(a, b), { context: 1 }); |
| 93 | + expect(chunksCtx1.length).toBeLessThanOrEqual(chunks.length); |
| 94 | + }); |
| 95 | + |
| 96 | + it("uses default context of 3", () => { |
| 97 | + const lines = diff("a\nb\nc\nd\ne\nf\ng", "a\nb\nc\nX\ne\nf\ng"); |
| 98 | + const chunks = formatDiff(lines); |
| 99 | + // should include 3 lines of context around the change |
| 100 | + expect(chunks[0].length).toBeGreaterThanOrEqual(3); |
| 101 | + }); |
| 102 | + |
| 103 | + it("throws on negative context", () => { |
| 104 | + const lines = diff("a", "b"); |
| 105 | + expect(() => formatDiff(lines, { context: -1 })).toThrow(RangeError); |
| 106 | + }); |
| 107 | + |
| 108 | + it("does not exceed array bounds with large context", () => { |
| 109 | + const lines = diff("a\nb", "a\nx"); |
| 110 | + expect(() => formatDiff(lines, { context: 100 })).not.toThrow(); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +// ── diffStats() ─────────────────────────────────────────────────────────────── |
| 115 | + |
| 116 | +describe("diffStats()", () => { |
| 117 | + it("returns all unchanged for identical strings", () => { |
| 118 | + const stats = diffStats(diff("hello\nworld", "hello\nworld")); |
| 119 | + expect(stats.added).toBe(0); |
| 120 | + expect(stats.removed).toBe(0); |
| 121 | + expect(stats.unchanged).toBe(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it("counts added lines correctly", () => { |
| 125 | + const stats = diffStats(diff("a", "a\nb\nc")); |
| 126 | + expect(stats.added).toBe(2); |
| 127 | + }); |
| 128 | + |
| 129 | + it("counts removed lines correctly", () => { |
| 130 | + const stats = diffStats(diff("a\nb\nc", "a")); |
| 131 | + expect(stats.removed).toBe(2); |
| 132 | + }); |
| 133 | + |
| 134 | + it("counts mixed changes correctly", () => { |
| 135 | + const stats = diffStats(diff("a\nb\nc", "a\nx\nc")); |
| 136 | + expect(stats.added).toBe(1); |
| 137 | + expect(stats.removed).toBe(1); |
| 138 | + expect(stats.unchanged).toBe(2); |
| 139 | + }); |
| 140 | + |
| 141 | + it("returns zeros for empty input", () => { |
| 142 | + const stats = diffStats([]); |
| 143 | + expect(stats).toEqual({ added: 0, removed: 0, unchanged: 0 }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments