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

describe('parseGitCommitShowOutput', () => {
  it('should parse git show output correctly', () => {
    // Arrange
    const gitShowOutput = `
      commit 9fceb02d0ae598e95dc970b74767e84987207e60 (HEAD -> main, origin/main)
      Author: John Doe <john.doe@example.com>
      Date:   Mon Nov 15 08:37:41 2023 -0800

          Fix a bug and improve documentation

      diff --git a/file1.txt b/file1.txt
      index 7f77f51..9c54642 100644
      --- a/file1.txt
      +++ b/file1.txt
      @@ -1,2 +1,2 @@
      -Hello, world!
      +Hello, Git!

      diff --git a/file2.txt b/file2.txt
      index 8ad1c76..f414f31 100644
      --- a/file2.txt
      +++ b/file2.txt
      @@ -1,3 +1,4 @@
       Hello, world!
      +This is a new line.

      diff --git a/documentation.txt b/documentation.txt
      index 553c207..762f61c 100644
      --- a/documentation.txt
      +++ b/documentation.txt
      @@ -4,6 +4,7 @@ Title: Documentation
       - Section 1
       - Section 2
       - Section 3
      +- Section 4
    `;

    // Act
    const result = parseGitCommitShowOutput(gitShowOutput);

    // Assert
    expect(result).toEqual({
      commitHash: '9fceb02d0ae598e95dc970b74767e84987207e60',
      author: 'John Doe <john.doe@example.com>',
      date: 'Mon Nov 15 08:37:41 2023 -0800',
      title: 'Fix a bug and improve documentation',
      message: '',  // Assuming message is empty if not provided
      diffs: [
        {
          fileName: 'file1.txt',
          lines: [
            {
              lineNum: 1,
              content: '-Hello, world!',
            },
            {
              lineNum: 2,
              content: '+Hello, Git!'
            }
          ]
        },
        {
          fileName: 'file2.txt',
          lines: [
            {
              lineNum: 1,
              content: ' Hello, world!'
            },
            {
              lineNum: 2,
              content: '+This is a new line.'
            },
          ]
        },
        {
          fileName: 'documentation.txt',
          lines: [
            {
              lineNum: 4,
              content: ' - Section 1'
            },
            {
              lineNum: 5,
              content: ' - Section 2'
            },
            {
              lineNum: 6,
              content: ' - Section 3'
            },
            {
              lineNum: 7,
              content: '+- Section 4'
            }
          ]
        },
      ]
    });
  });

  it('should throw an error if git show output is empty', () => {
    // Arrange
    const gitShowOutput = '';

    // Act and Assert
    expect(() => parseGitCommitShowOutput(gitShowOutput)).toThrow('Git show output empty. Exiting.');
  });
});