import os
import shutil
import tempfile
from pyucc.core.differ import BaselineManager, Differ


def test_baseline_and_diff(tmp_path):
    # create a fake project dir
    proj = tmp_path / "proj"
    proj.mkdir()
    f1 = proj / "a.txt"
    f1.write_text("line1\nline2\n")
    sub = proj / "sub"
    sub.mkdir()
    f2 = sub / "b.txt"
    f2.write_text("x\ny\n")

    # workspace root is tmp_path; store baselines in a separate temp folder
    baselines_dir = str(tmp_path / "baselines")
    bm = BaselineManager(str(tmp_path), baselines_root=baselines_dir)
    baseline_id = bm.create_baseline_from_dir(str(proj), baseline_id="testbase", snapshot=False, compute_sha1=True, ignore_patterns=None, profile_name="testprofile", max_keep=5)
    assert baseline_id == "testbase"

    metadata = bm.load_metadata(baseline_id)
    assert metadata.baseline_id == "testbase"
    # modify project
    f1.write_text("line1\nline2 changed\nline3\n")
    f3 = proj / "c.txt"
    f3.write_text("newfile\n")

    d = Differ(metadata, str(proj), max_workers=2)
    result = d.diff()
    assert "total" in result
    # we expect some added lines (c.txt) and modified lines in a.txt
    assert result["total"]["added"] >= 1


if __name__ == '__main__':
    import pytest
    pytest.main([__file__])
