pr-check / tests / parsers / parseDiffChunk.test.ts
parseDiffChunk.test.ts
Raw
import { expect, test, describe, it } from "bun:test";
import { FileDiff } from "../../src/interfaces/FileDiff";
import { parseDiffChunk } from "../../src/parsers/parseGitShowOutput";

describe('parseDiffChunk', () => {
  it('should parse a valid diff chunk', () => {
    const diffChunk = `
diff --git a/file.txt b/file.txt
index e69de29..fcf3b0e 100644
--- a/file.txt
+++ b/file.txt
@@ -0,0 +1,3 @@
+This is a new line.
+This is another new line.
+And yet another new line.`;

    const expected: FileDiff = {
      fileName: 'file.txt',
      lines: [
        {
          lineNum: 1,
          content: '+This is a new line.'
        },
        {
          lineNum: 2,
          content: '+This is another new line.'
        },
        {
          lineNum: 3,
          content: '+And yet another new line.'
        },
      ]
    };

    const actual = parseDiffChunk(diffChunk);
    expect(actual).toEqual(expected);
  });

  it('should throw an error if no file name found', () => {
    const diffChunk = `
diff --git a/file.txt b/file.txt
index e69de29..fcf3b0e 100644
--- a/file.txt
`;

  expect(() => parseDiffChunk(diffChunk)).toThrow('Invalid diff chunk. No file name found.');
  });

  it('should throw an error if no hunk header found', () => {
    const diffChunk = `
diff --git a/file.txt b/file.txt
index e69de29..fcf3b0e 100644
--- a/file.txt
+++ b/file.txt
+This is a new line.
+This is another new line.
`;

    expect(() => parseDiffChunk(diffChunk)).toThrow('Invalid diff chunk. No hunk header found.');
  });
});