import { describe, expect, it } from 'vitest';
import { parseUnifiedDiff } from '../src/lib/diff-parse';

const SAMPLE = `diff --git a/README.md b/README.md
index 1234567..89abcde 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
 # demo
+edited line
 second
-old third
+new third
@@ -10,2 +11,2 @@
 ten
-eleven
+ELEVEN`;

describe('parseUnifiedDiff', () => {
  it('ignore les en-têtes de fichier avant le premier @@', () => {
    const r = parseUnifiedDiff(SAMPLE);
    expect(r.hunks).toHaveLength(2);
    expect(r.hunks[0].header).toBe('@@ -1,3 +1,4 @@');
  });

  it('compte additions et suppressions', () => {
    const r = parseUnifiedDiff(SAMPLE);
    expect(r.additions).toBe(3); // edited line, new third, ELEVEN
    expect(r.deletions).toBe(2); // old third, eleven
  });

  it('numérote correctement les lignes ancien/nouveau', () => {
    const { hunks } = parseUnifiedDiff(SAMPLE);
    const lines = hunks[0].lines;
    expect(lines[0]).toMatchObject({ type: 'context', content: ' # demo'.slice(1), oldLine: 1, newLine: 1 });
    const add = lines.find((l) => l.type === 'add');
    expect(add).toMatchObject({ oldLine: null, newLine: 2, content: 'edited line' });
    const del = lines.find((l) => l.type === 'del');
    expect(del).toMatchObject({ newLine: null, content: 'old third' });
  });

  it('ignore le marqueur « \\ No newline at end of file »', () => {
    const diff = `@@ -1 +1 @@\n-a\n+b\n\\ No newline at end of file`;
    const r = parseUnifiedDiff(diff);
    expect(r.additions).toBe(1);
    expect(r.deletions).toBe(1);
    expect(r.hunks[0].lines).toHaveLength(2);
  });

  it('diff vide → aucun hunk', () => {
    expect(parseUnifiedDiff('').hunks).toHaveLength(0);
  });

  it('gère un fichier untracked (--- /dev/null)', () => {
    const diff = `--- /dev/null\n+++ b/new.txt\n@@ -0,0 +1,2 @@\n+line one\n+line two`;
    const r = parseUnifiedDiff(diff);
    expect(r.additions).toBe(2);
    expect(r.hunks[0].lines.every((l) => l.type === 'add')).toBe(true);
  });
});
