# tests/test_artifacts.py
from pathlib import Path
from ctf.forensics import get_metadata
from ctf.analysis import run_analysis

ARTIFACTS_DIR = Path("tests/artifacts")

# {{{ test_flag_txt
def test_flag_txt():
    """Asserts flag.txt holds the direct plaintext flag."""
    flag_file = ARTIFACTS_DIR / "flag.txt"
    content = flag_file.read_text().strip()
    assert content == "picoCTF{h1dd3n_1n_1m4g3_5d4cba73}"
# }}}

# {{{ test_cat_jpg
def test_cat_jpg():
    """Asserts cat.jpg contains the base64-encoded license flag."""
    file_path = ARTIFACTS_DIR / "cat.jpg"
    meta = get_metadata(file_path)
    # Check that license metadata has the base64 encoded hint
    assert "EXIF:cc:license" in meta.decoded_hints
    assert meta.decoded_hints["EXIF:cc:license"].get("base64") == "picoCTF{the_m3tadata_1s_modified}"
    
    # Run analysis
    res = run_analysis(file_path)
    assert res.success
    assert "picoCTF{the_m3tadata_1s_modified}" in res.routes[0].extracted_flags
# }}}

# {{{ test_easy_cat_jpg
def test_easy_cat_jpg():
    """Asserts easy-cat.jpg contains comment flag and license flag."""
    file_path = ARTIFACTS_DIR / "easy-cat.jpg"
    meta = get_metadata(file_path)
    assert meta.comment == "picoCTF{easypeasy}"
    
    # Run analysis
    res = run_analysis(file_path)
    assert res.success
    flags = res.routes[0].extracted_flags
    assert "picoCTF{easypeasy}" in flags
    assert "picoCTF{the_m3tadata_1s_modified}" in flags
# }}}

# {{{ test_easy_cat_jpg_original
def test_easy_cat_jpg_original():
    """Asserts easy-cat.jpg_original contains only the license flag."""
    file_path = ARTIFACTS_DIR / "easy-cat.jpg_original"
    meta = get_metadata(file_path)
    assert not meta.comment
    
    res = run_analysis(file_path)
    assert res.success
    assert "picoCTF{the_m3tadata_1s_modified}" in res.routes[0].extracted_flags
# }}}

# {{{ test_garden_jpg
def test_garden_jpg():
    """Asserts garden.jpg contains no metadata flags."""
    file_path = ARTIFACTS_DIR / "garden.jpg"
    meta = get_metadata(file_path)
    assert not meta.comment
    
    res = run_analysis(file_path)
    assert not res.success
    assert not res.routes[0].extracted_flags
# }}}

# {{{ test_hips_jpg
def test_hips_jpg():
    """Asserts hips.jpg contains comment hint but no actual flags."""
    file_path = ARTIFACTS_DIR / "hips.jpg"
    meta = get_metadata(file_path)
    assert "Comment" in meta.decoded_hints
    # Steghide password hint decodes to pAzzword, not a flag
    assert meta.decoded_hints["Comment"].get("base64->base64") == "pAzzword"
    
    res = run_analysis(file_path)
    assert not res.success
    assert not res.routes[0].extracted_flags
# }}}

# {{{ test_red_png
def test_red_png():
    """Asserts red.png contains comment hint but no actual flags."""
    file_path = ARTIFACTS_DIR / "red.png"
    meta = get_metadata(file_path)
    assert "Comment" in meta.decoded_hints
    assert meta.decoded_hints["Comment"].get("base64->base64") == "pAzzword"
    
    res = run_analysis(file_path)
    assert not res.success
    assert not res.routes[0].extracted_flags
# }}}
