from g2h.log_page import LogPage
from unittest.mock import MagicMock

def test_init():
    page = LogPage('test/log', 'log test', None, None, None, '')
    assert(page.name == 'log test')
    assert(page.out_file_path == 'test/log/l.md')
    assert(page.template.name == 'log.md')

def test_context_no_commits():
    page = LogPage('test/log', 'log test', None, None, None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [])

def test_context_no_branches_and_tags():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    page = LogPage('test/log', 'log test', [commit], None, None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}])

def test_context_matching_branch_no_tags():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    branch = MagicMock()
    branch.commit.hexsha = commit.hexsha
    branch.name = 'mock branch'
    page = LogPage('test/log', 'log test', [commit], [branch], None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock branch'], 'summary': 'mock commit'}])

def test_context_unmatching_branch_no_tags():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    branch = MagicMock()
    branch.commit.hexsha = 4321
    branch.name = 'mock branch'
    page = LogPage('test/log', 'log test', [commit], [branch], None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}])

def test_context_no_branch_matching_tag():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    tag = MagicMock()
    tag.commit.hexsha = commit.hexsha
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit], None, [tag], '')
    context = page.context()
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock tag'], 'summary': 'mock commit'}])

def test_context_no_branch_no_matching_tag():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    tag = MagicMock()
    tag.commit.hexsha = 4321
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit], None, [tag], '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}])

def test_context_no_matching_branch_no_matching_tag():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    branch = MagicMock()
    branch.commit.hexsha = 321
    branch.name = 'mock branch'
    tag = MagicMock()
    tag.commit.hexsha = 4321
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit], [branch], [tag], '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}])

def test_context_matching_branch_matching_tag():
    commit = MagicMock()
    commit.summary = 'mock commit'
    commit.hexsha = 1234;
    commit.committed_datetime = 0
    branch = MagicMock()
    branch.commit.hexsha = commit.hexsha
    branch.name = 'mock branch'
    tag = MagicMock()
    tag.commit.hexsha = commit.hexsha
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit], [branch], [tag], '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock branch', 'mock tag'], 'summary': 'mock commit'}])

def test_multiple_commits():
    commit1 = MagicMock()
    commit1.summary = 'mock commit 1'
    commit1.hexsha = 1;
    commit1.committed_datetime = 0
    commit2 = MagicMock()
    commit2.summary = 'mock commit 2'
    commit2.hexsha = 2;
    commit2.committed_datetime = 1
    page = LogPage('test/log', 'log test', [commit1, commit2], None, None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [
        {'committed_datetime': 0, 'hexsha': 1, 'refs': [], 'summary': 'mock commit 1'},
        {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'}
    ])

def test_multiple_commits_branch():
    commit1 = MagicMock()
    commit1.summary = 'mock commit 1'
    commit1.hexsha = 1;
    commit1.committed_datetime = 0
    commit2 = MagicMock()
    commit2.summary = 'mock commit 2'
    commit2.hexsha = 2;
    commit2.committed_datetime = 1
    branch = MagicMock()
    branch.commit.hexsha = 1
    branch.name = 'mock branch'
    page = LogPage('test/log', 'log test', [commit1, commit2], [branch], None, '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [
        {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock branch'], 'summary': 'mock commit 1'},
        {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'}
    ])

def test_multiple_commits_tag():
    commit1 = MagicMock()
    commit1.summary = 'mock commit 1'
    commit1.hexsha = 1;
    commit1.committed_datetime = 0
    commit2 = MagicMock()
    commit2.summary = 'mock commit 2'
    commit2.hexsha = 2;
    commit2.committed_datetime = 1
    tag = MagicMock()
    tag.commit.hexsha = 1
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit1, commit2], None, [tag], '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [
        {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock tag'], 'summary': 'mock commit 1'},
        {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'}
    ])

def test_multiple_commits_branch_and_tag():
    commit1 = MagicMock()
    commit1.summary = 'mock commit 1'
    commit1.hexsha = 1;
    commit1.committed_datetime = 0
    commit2 = MagicMock()
    commit2.summary = 'mock commit 2'
    commit2.hexsha = 2;
    commit2.committed_datetime = 1
    branch = MagicMock()
    branch.commit.hexsha = 1
    branch.name = 'mock branch'
    tag = MagicMock()
    tag.commit.hexsha = 2
    tag.name = 'mock tag'
    page = LogPage('test/log', 'log test', [commit1, commit2], [branch], [tag], '')
    context = page.context()
    assert(context['title'] == 'log test: Log')
    assert(context['commits'] == [
        {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock branch'], 'summary': 'mock commit 1'},
        {'committed_datetime': 1, 'hexsha': 2, 'refs': ['mock tag'], 'summary': 'mock commit 2'}
    ])

