Repository Public
1import { describe, expect, it } from 'vitest';2import { reverseApplyUnifiedDiff } from '@/lib/patch';34const hdr = 'diff --git a/f b/f\nindex 1..2 100644\n--- a/f\n+++ b/f\n';5describe('reverseApplyUnifiedDiff', () => {6 it('added line', () => {7 expect(reverseApplyUnifiedDiff('a\nb\n', `${hdr}@@ -1 +1,2 @@\n a\n+b\n`)).toBe('a\n');8 });9 it('removed and changed lines in two hunks', () => {10 const original = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'].join('\n') + '\n';11 const modified = ['1', 'two', '3', '4', '5', '6', '7', '8', '10'].join('\n') + '\n';12 const diff = `${hdr}@@ -1,3 +1,3 @@\n 1\n-2\n+two\n 3\n@@ -8,3 +8,2 @@\n 8\n-9\n 10\n`;13 expect(reverseApplyUnifiedDiff(modified, diff)).toBe(original);14 });15 it('new file and pure deletion hunk', () => {16 expect(reverseApplyUnifiedDiff('new file\n', '--- /dev/null\n+++ b/f\n@@ -0,0 +1 @@\n+new file\n')).toBe('');17 expect(reverseApplyUnifiedDiff('a\nc\n', `${hdr}@@ -2 +1,0 @@\n-b\n`)).toBe('a\nb\nc\n');18 });19 it('no trailing newline markers', () => {20 expect(reverseApplyUnifiedDiff('a\nb', `${hdr}@@ -1 +1,2 @@\n-a\n\\ No newline at end of file\n+a\n+b\n\\ No newline at end of file\n`)).toBe('a');21 });22 it('returns null when the diff does not match the text; empty diff = unchanged', () => {23 expect(reverseApplyUnifiedDiff('zzz\n', `${hdr}@@ -1 +1,2 @@\n a\n+b\n`)).toBeNull();24 expect(reverseApplyUnifiedDiff('same\n', '')).toBe('same\n');25 expect(reverseApplyUnifiedDiff('x\n', 'Binary files a/f and b/f differ\n')).toBeNull();26 });27});28