package com.atlassian.bitbucket.mesh.git.builder.blame;

import com.atlassian.bitbucket.mesh.git.content.Blame;
import org.junit.Test;

import java.util.List;

import static com.atlassian.bitbucket.mesh.util.ProcessStdioHandlerUtils.handleStdout;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class IncrementalBlameStdioHandlerTest {

    @Test
    public void testBlameCanContainMultipleKeysBetweenSpans() {
        IncrementalBlameStdioHandler handler = new IncrementalBlameStdioHandler();
        handleStdout(handler, "git-blame-incremental-2.11.1.out");

        List<Blame> blames = handler.getResult();
        assertNotNull(blames);
        assertBlame("ee2df7e9318483c02e7675ab36c44f24562a1161", 425, 4, blames.get(0));
    }

    @Test
    public void testConfusingCommitMessage() {
        IncrementalBlameStdioHandler handler = new IncrementalBlameStdioHandler();
        handleStdout(handler, "git-blame-incremental-confusing-commit-message.out");

        List<Blame> blames = handler.getResult();
        assertThat(blames, hasSize(1));
        assertNotNull(blames);
        assertBlame("ee2df7e9318483c02e7675ab36c44f24562a1161", 425, 4, blames.get(0));
    }

    @Test
    public void testConsecutiveBlamesAreMerged() {
        IncrementalBlameStdioHandler handler = new IncrementalBlameStdioHandler();
        handleStdout(handler, "git-blame-incremental-with-consecutive-commits-on-same-hash.out");

        List<Blame> blames = handler.getResult();
        assertThat(blames, hasSize(20));

        // Check that this part of the blame:
        // > filename util/src/main/java/com/atlassian/stash/util/ AuditUtils.java
        // > 5b8c81d14e03a3c7b733f9c9f41ba505ac2cc103 8 11 1
        // > filename util/src/main/java/com/atlassian/stash/util/AuditUtils.java
        // > 5b8c81d14e03a3c7b733f9c9f41ba505ac2cc103 9 12 3
        // Gets merged into one Blame object
        assertBlame("5b8c81d14e03a3c7b733f9c9f41ba505ac2cc103", 11, 4, blames.get(8));
    }

    @Test
    public void testExecute() {
        IncrementalBlameStdioHandler handler = new IncrementalBlameStdioHandler();
        handleStdout(handler, "git-blame-incremental-paged.out");

        List<Blame> blames = handler.getResult();
        assertThat(blames, hasSize(8));
        assertBlame("959ef44bec5ec7200dcb8bc40aa62c271e459942", 10, blames.get(0));
        assertBlame("529d5911080d80064a289d030fe60f28e665e3c2", 11, blames.get(1));
        assertBlame("959ef44bec5ec7200dcb8bc40aa62c271e459942", 12, 2, blames.get(2));
        assertBlame("5049e2ee264cd792f51e0bbe04d52871a245136a", 14, blames.get(3));
        assertBlame("e066aace2b950f770872f8ca90f4b760165f3099", 15, blames.get(4));
        assertBlame("6b52185c5cf8da1b0f8df4ef79611d94c52f8a6b", 16, blames.get(5));
        assertBlame("529d5911080d80064a289d030fe60f28e665e3c2", 17, blames.get(6));
        assertBlame("6076ae70d29564dabddb5d6e0430545f7aea954f", 18, 3, blames.get(7));
    }

    private static void assertBlame(String hash, int lineNumber, Blame blame) {
        assertBlame(hash, lineNumber, 1, blame);
    }

    private static void assertBlame(String hash, int lineNumber, int spannedLines, Blame blame) {
        assertEquals(hash, blame.getCommitId());
        assertEquals(lineNumber, blame.getLineNumber());
        assertEquals(spannedLines, blame.getSpannedLines());
    }
}
