spb/spbgit Public MIT
SPB Git — the platform hosting itself
JavaScript 73.9%
CSS 11.7%
Nunjucks 11.6%
Shell 2.7%
1/**2 * ─────────────────────────────────────────────3 * SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 * Author : Simon-Pierre Boucher6 * Contact : contact@spboucher.ai7 * File : test/unit/diff.test.mjs8 * Purpose : Unit tests — unified diff parsing + git path unquoting9 * License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { describe, it, expect } from 'vitest';14import { parseUnifiedDiff, unquoteGitPath } from '../../src/git/repo.mjs';1516const SAMPLE = `diff --git a/src/app.js b/src/app.js17index 1111111..2222222 10064418--- a/src/app.js19+++ b/src/app.js20@@ -1,4 +1,5 @@21 const a = 1;22-const b = 2;23+const b = 3;24+const c = 4;25 export { a, b };26diff --git a/new.txt b/new.txt27new file mode 10064428index 0000000..333333329--- /dev/null30+++ b/new.txt31@@ -0,0 +1,2 @@32+hello33+world34diff --git a/img.png b/img.png35Binary files a/img.png and b/img.png differ36`;3738describe('parseUnifiedDiff', () => {39 it('parses files, hunks, line numbers, and stats', () => {40 const files = parseUnifiedDiff(SAMPLE);41 expect(files).toHaveLength(3);4243 const [modified, added, binary] = files;44 expect(modified.newPath).toBe('src/app.js');45 expect(modified.status).toBe('modified');46 expect(modified.additions).toBe(2);47 expect(modified.deletions).toBe(1);48 expect(modified.hunks).toHaveLength(1);49 const lines = modified.hunks[0].lines;50 expect(lines[0]).toMatchObject({ type: 'ctx', old: 1, new: 1 });51 expect(lines[1]).toMatchObject({ type: 'del', old: 2, new: null });52 expect(lines[2]).toMatchObject({ type: 'add', old: null, new: 2 });53 expect(lines[3]).toMatchObject({ type: 'add', old: null, new: 3 });54 expect(lines[4]).toMatchObject({ type: 'ctx', old: 3, new: 4 });5556 expect(added.status).toBe('added');57 expect(added.additions).toBe(2);5859 expect(binary.binary).toBe(true);60 });6162 it('handles empty input', () => {63 expect(parseUnifiedDiff('')).toEqual([]);64 });65});6667describe('unquoteGitPath', () => {68 it('passes through plain paths', () => {69 expect(unquoteGitPath('src/app.js')).toBe('src/app.js');70 });71 it('decodes octal utf-8 escapes', () => {72 expect(unquoteGitPath('"docs/\\303\\251t\\303\\251.txt"')).toBe('docs/été.txt');73 });74 it('decodes simple escapes', () => {75 expect(unquoteGitPath('"a\\"b\\\\c"')).toBe('a"b\\c');76 });77});78