import pytest
from pathlib import Path

from ghidrecomp import decompile, get_parser
from ghidrecomp.decompile import get_bin_output_path, gen_proj_bin_name_from_path


def test_decomplie_ls(shared_datadir: Path):

    parser = get_parser()

    bin_path = shared_datadir / 'ls_aarch64'

    args = parser.parse_args([f"{bin_path.absolute()}", "--skip-cache"])

    bin_proj_name = gen_proj_bin_name_from_path(bin_path)
    expected_output_path = get_bin_output_path(args.output_path, bin_proj_name)

    all_funcs, decompilations, output_path, compiler, lang_id, callgraphs, sast_sarifs = decompile(args)

    assert len(all_funcs) == 532
    assert len(decompilations) == 532
    assert output_path == expected_output_path
    assert compiler == 'unknown'
    assert lang_id == 'AARCH64:LE:64:v8A'
    assert len(callgraphs) == 0


def test_decomplie_ls_cached(shared_datadir: Path):

    parser = get_parser()

    bin_path = shared_datadir / 'ls_aarch64'

    args = parser.parse_args([f"{bin_path.absolute()}"])

    bin_proj_name = gen_proj_bin_name_from_path(bin_path)
    expected_output_path = get_bin_output_path(args.output_path, bin_proj_name)

    all_funcs, decompilations, output_path, compiler, lang_id, callgraphs, sast_sarifs = decompile(args)

    assert len(all_funcs) == 532
    assert len(decompilations) == 0
    assert output_path == expected_output_path
    assert compiler == 'unknown'
    assert lang_id == 'AARCH64:LE:64:v8A'
    assert len(callgraphs) == 0


def test_ctype_filter_ls(shared_datadir: Path):

    parser = get_parser()

    bin_path = shared_datadir / 'ls_aarch64'

    args = parser.parse_args([f"{bin_path.absolute()}", "--filter", "ctype", "--skip-cache"])

    all_funcs, decompilations, output_path, compiler, lang_id, callgraphs, sast_sarifs = decompile(args)

    assert len(all_funcs) == 8
    assert len(decompilations) == 8
    assert len(callgraphs) == 0


def test_decomplie_afd(shared_datadir: Path):

    parser = get_parser()

    bin_path = shared_datadir / 'afd.sys.10.0.22621.1415'

    args = parser.parse_args([f"{bin_path.absolute()}", "--skip-cache"])

    bin_proj_name = gen_proj_bin_name_from_path(bin_path)
    expected_output_path = get_bin_output_path(args.output_path, bin_proj_name)

    all_funcs, decompilations, output_path, compiler, lang_id, callgraphs, sast_sarifs = decompile(args)

    assert len(all_funcs) == len(decompilations)
    assert len(all_funcs) in {1275, 1273, 1172, 1615,1277}, f"Unexpected function count: {len(all_funcs)}"
    assert output_path == expected_output_path
    assert compiler == 'visualstudio:unknown'
    assert lang_id == 'x86:LE:64:default'
    assert len(callgraphs) == 0


def test_decomplie_afd_cached(shared_datadir: Path):

    parser = get_parser()

    bin_path = shared_datadir / 'afd.sys.10.0.22621.1415'

    args = parser.parse_args([f"{bin_path.absolute()}"])

    bin_proj_name = gen_proj_bin_name_from_path(bin_path)
    expected_output_path = get_bin_output_path(args.output_path, bin_proj_name)

    all_funcs, decompilations, output_path, compiler, lang_id, callgraphs, sast_sarifs = decompile(args)

    assert len(all_funcs) in {1275, 1273, 1172, 1615,1277}, f"Unexpected function count: {len(all_funcs)}"
    assert len(decompilations) == 0
    assert output_path == expected_output_path
    assert compiler == 'visualstudio:unknown'
    assert lang_id == 'x86:LE:64:default'
    assert len(callgraphs) == 0
