from unittest.mock import MagicMock

from g2h.file_page import FilePage, bytes_to_content

def test_init():
    commit = MagicMock()
    commit.hexsha = '123'
    page = FilePage('test/file', 'file test', commit, '.test_file', 'path/test', '')
    assert(page.name == 'file test')
    assert(page.out_file_path == 'test/file/123/t/path/test/_test_file.md')
    assert(page.template.name == 'file.md')
    assert(page.commit == commit)
    assert(page.file_name == '.test_file')
    assert(page.file_path == 'path/test')
    commit.tree.join.assert_called_with('path/test')

def test_read_file():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, 'test_file', '', '')
    page.read_file()
    tree.join.assert_called_with('test_file')
    file.data_stream.read.assert_called()

def test_context():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, 'test_file.txt', '', 'file/content')
    file.data_stream.read.return_value = 'test'.encode()
    context = page.context()
    assert(context['content'] == 'test')
    assert(context['content_subdir'] == 'file/content')
    assert(context['file_extension'] == 'txt')
    assert(context['title'] == 'file test: test_file.txt')
    assert(context['type'] == 'text')
    assert(context['breadcrumbs'] == '[/](/file/content/123/t/)/[test_file.txt](/file/content/123/t/test_file.txt)')

def test_context_dot_file():
    file = MagicMock()
    tree = MagicMock()
    commit = MagicMock()
    commit.hexsha = '123'
    commit.tree =  tree
    commit.tree.join.return_value = file
    page = FilePage('test/file', 'file test', commit, '.file', '', 'file/content')
    file.data_stream.read.return_value = 'test'.encode()
    context = page.context()
    assert(context['content'] == 'test')
    assert(context['content_subdir'] == 'file/content')
    assert(context['file_extension'] == '')
    assert(context['title'] == 'file test: .file')
    assert(context['type'] == 'text')
    assert(context['breadcrumbs'] == '[/](/file/content/123/t/)/[.file](/file/content/123/t/_file)')

def test_bytes_to_content_text():
    content, type = bytes_to_content('test'.encode())
    assert(content == 'test')
    assert(type == 'text')

def test_bytes_to_content_pic():
    webp_1px = bytes([0x52, 0x49, 0x46, 0x46, 0x1a, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4c, 0x0e, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x11, 0xfd, 0x0f, 0x44, 0x44, 0xff, 0x03, 0x0a])
    content, type = bytes_to_content(webp_1px)
    assert(content == 'UklGRhoAAABXRUJQVlA4TA4AAAAvAAAAAAcQEf0PRET/Awo=')
    assert(type == 'image')

def test_bytes_to_content_empty():
    content, type = bytes_to_content(bytes())
    assert(content == None)
    assert(type == 'empty')

def test_bytes_to_content_bin():
    content, type = bytes_to_content(bytes([0xc3, 0x28]))
    assert(content == None)
    assert(type == None)

