from g2h.commit_page import CommitPage, redact_email
from unittest.mock import MagicMock

def test_init():
    commit = MagicMock()
    commit.hexsha = '123'
    page = CommitPage('test/commit', 'commit test', commit, '')
    assert(page.name == 'commit test')
    assert(page.out_file_path == 'test/commit/123/index.md')
    assert(page.template.name == 'commit.md')

def test_context():
    commit = MagicMock()
    commit.hexsha = '123'
    commit.author.email = 'unit@te.st'
    page = CommitPage('test/commit', 'commit test', commit, '')
    context = page.context()
    assert(context['title'] == 'commit test: 123')
    assert(context['commit'] == commit)
    assert(context['redacted_email'] == 'u...t@te...st')

def test_redacting_email():
    assert(redact_email(None) == '')
    assert(redact_email('') == '')
    assert(redact_email('a') == 'a...a')
    assert(redact_email('az') == 'a...z')
    assert(redact_email('root') == 'r...t')
    assert(redact_email('root@localhost') == 'r...t@localhost')
    assert(redact_email('test@example.com') == 't...t@example...com')
    assert(redact_email('test@sub.example.com') == 't...t@sub...example...com')
    assert(redact_email('az@example.com') == 'a...z@example...com')
    assert(redact_email('a@example.com') == 'a...a@example...com')
    assert(redact_email('@example.com') == '@example...com')
    assert(redact_email('test@') == 't...t@')
    assert(redact_email('az@') == 'a...z@')
    assert(redact_email('@') == '@')
