section 11: Phase 11 — Diff sources: contract and git reader

This commit is contained in:
Chris Scott committed 2026-10-08 15:53:27 -04:00
1 parent df1891a52d
commit 11c23da67e
7 files changed
+750 -7

No files matched your search

+159 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { parseDiffLines, splitDiffHunks } from '@opencode-manager/shared/utils';
import { parseDiffLines, splitDiffHunks, splitUnifiedDiffByFile } from '@opencode-manager/shared/utils';
const DIFF = [
'diff --git a/src/app.ts b/src/app.ts',
@@ -117,3 +117,161 @@ describe('splitDiffHunks', () => {
expect(splitDiffHunks('')).toEqual([]);
});
});
describe('splitUnifiedDiffByFile', () => {
it('splits a modified file and counts additions and removals', () => {
const result = splitUnifiedDiffByFile(DIFF);
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({ file: 'src/app.ts', status: 'modified', additions: 3, deletions: 2 });
expect(result[0]!.patch.startsWith('diff --git a/src/app.ts b/src/app.ts')).toBe(true);
});
it('reads an added file from the new side', () => {
const diff = [
'diff --git a/src/new.ts b/src/new.ts',
'new file mode 100644',
'index 0000000..1111111',
'--- /dev/null',
'+++ b/src/new.ts',
'@@ -0,0 +1,2 @@',
'+one',
'+two',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'src/new.ts', status: 'added', additions: 2, deletions: 0 });
});
it('reads a deleted file from the old side', () => {
const diff = [
'diff --git a/src/old.ts b/src/old.ts',
'deleted file mode 100644',
'index 1111111..0000000',
'--- a/src/old.ts',
'+++ /dev/null',
'@@ -1,2 +0,0 @@',
'-one',
'-two',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'src/old.ts', status: 'deleted', additions: 0, deletions: 2 });
});
it('reads a renamed file with no content changes', () => {
const diff = [
'diff --git a/src/old.ts b/src/new.ts',
'similarity index 100%',
'rename from src/old.ts',
'rename to src/new.ts',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'src/new.ts', status: 'modified', additions: 0, deletions: 0 });
});
it('keeps a binary block with no hunks', () => {
const diff = [
'diff --git a/assets/logo.png b/assets/logo.png',
'index 1111111..2222222 100644',
'Binary files a/assets/logo.png and b/assets/logo.png differ',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'assets/logo.png', status: 'modified' });
expect(file!.patch).not.toContain('@@');
});
it('splits multiple files', () => {
const diff = [
DIFF,
'',
'diff --git a/src/other.ts b/src/other.ts',
'--- a/src/other.ts',
'+++ b/src/other.ts',
'@@ -1 +1 @@',
'-a',
'+b',
].join('\n');
expect(splitUnifiedDiffByFile(diff).map((entry) => entry.file)).toEqual(['src/app.ts', 'src/other.ts']);
});
it('parses a path containing a space with the trailing tab git appends', () => {
const diff = [
'diff --git a/my file.txt b/my file.txt',
'index 5626abf..f719efd 100644',
'--- a/my file.txt\t',
'+++ b/my file.txt\t',
'@@ -1 +1 @@',
'-one',
'+two',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'my file.txt', status: 'modified' });
});
it('decodes a C-quoted octal UTF-8 path', () => {
const diff = [
'diff --git "a/\\303\\251.txt" "b/\\303\\251.txt"',
'index 587be6b..ccc9bd6 100644',
'--- "a/\\303\\251.txt"',
'+++ "b/\\303\\251.txt"',
'@@ -1 +1 @@',
'-x',
'+xx',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'é.txt', status: 'modified' });
});
it('decodes a quoted rename target', () => {
const diff = [
'diff --git "a/\\303\\251.txt" "b/\\303\\251 renamed.txt"',
'similarity index 100%',
'rename from "\\303\\251.txt"',
'rename to "\\303\\251 renamed.txt"',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'é renamed.txt', status: 'modified' });
});
it('decodes a quoted path in the header fallback', () => {
const diff = [
'diff --git "a/\\303\\251.bin" "b/\\303\\251.bin"',
'index 1111111..2222222 100644',
'Binary files "a/\\303\\251.bin" and "b/\\303\\251.bin" differ',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'é.bin', status: 'modified' });
});
it('decodes a quoted header path when only the new side is quoted', () => {
const diff = [
'diff --git a/plain.bin "b/\\303\\251.bin"',
'index 1111111..2222222 100644',
'Binary files a/plain.bin and "b/\\303\\251.bin" differ',
].join('\n');
const [file] = splitUnifiedDiffByFile(diff);
expect(file).toMatchObject({ file: 'é.bin', status: 'modified' });
});
it('returns an empty list for an empty diff', () => {
expect(splitUnifiedDiffByFile('')).toEqual([]);
});
});