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

import org.junit.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;

public class BatchHeaderTest {

    private static final String TEST_HASH = "01234567893fc899bdd99fe361187581b15c980a";

    @Test
    public void testParseBlob() {
        BatchHeader output = BatchHeader.parse(TEST_HASH + " blob 123");
        assertFalse(output.isMissing());
        assertEquals(123, output.getSize());
        assertEquals(TEST_HASH, output.getObjectId());
        assertEquals("blob", output.getType());
    }

    @Test
    public void testParseByteBuffer() {
        BatchHeader output = BatchHeader.parse(StandardCharsets.UTF_8.encode(TEST_HASH + " blob 123"));
        assertFalse(output.isMissing());
        assertEquals(123, output.getSize());
        assertEquals(TEST_HASH, output.getObjectId());
        assertEquals("blob", output.getType());
    }

    @Test
    public void testParseCommit() {
        BatchHeader output = BatchHeader.parse(TEST_HASH + " commit 123");
        assertFalse(output.isMissing());
        assertEquals(123, output.getSize());
        assertEquals(TEST_HASH, output.getObjectId());
        assertEquals("commit", output.getType());
    }

    @Test
    public void testParseMissingFile() {
        BatchHeader output = BatchHeader.parse("refs/heads/master:missing.txt missing");
        assertTrue(output.isMissing());
        assertEquals("refs/heads/master:missing.txt", output.getObjectId());
        assertEquals(0, output.getSize());
    }

    @Test
    public void testParseMissingFileWithSpacesInPath() {
        BatchHeader output = BatchHeader.parse("refs/heads/master:path with spaces/a missing file.txt missing");
        assertTrue(output.isMissing());
        assertEquals("refs/heads/master:path with spaces/a missing file.txt", output.getObjectId());
        assertEquals(0, output.getSize());
    }

    @Test
    public void testParseMissingObject() {
        BatchHeader output = BatchHeader.parse(TEST_HASH + " missing");
        assertTrue(output.isMissing());
    }

    @Test
    public void testParseMissingRef() {
        BatchHeader output = BatchHeader.parse("refs/heads/ref_doesnt_exist missing");
        assertTrue(output.isMissing());
    }

    @Test
    public void testParseTree() {
        BatchHeader output = BatchHeader.parse(TEST_HASH + " tree 123");
        assertFalse(output.isMissing());
        assertEquals(123, output.getSize());
        assertEquals(TEST_HASH, output.getObjectId());
        assertEquals("tree", output.getType());
    }
}