Files
aigc-home/calliope-backend/tests/test_delivery_scope.py
T

177 lines
6.5 KiB
Python

"""Current delivery excludes one exact path, preserving historical sources and rows."""
import json
from scripts.compare_frontend_exports import compare_case
from calliope.comfyui import delivery_scope
from calliope.comfyui.companions import load_companions
from calliope.comfyui.library import import_library, rollback_batch
from calliope.db import get_db, migrate_db
from tests.test_supplied_companions import MANIFEST, REPO, ROOT
EXCLUDED = next(
c["source_path"] for c in json.loads(MANIFEST.read_text())["cases"] if c["id"] == "06"
)
def test_exclusion_is_exact_and_comparison_never_requests_materials(tmp_path):
case = next(c for c in json.loads(MANIFEST.read_text())["cases"] if c["id"] == "06")
result = compare_case(tmp_path, tmp_path, case)
assert result["status"] == "user_excluded"
assert not result["executable"] and not result["execution_verified"]
assert result["exclusion"]["decision"]["comment_id"] == "01a187e6-5df9-7c98-8afe-05fc9baa5b55"
assert delivery_scope.exclusion_for(EXCLUDED)
assert delivery_scope.exclusion_for(EXCLUDED.replace("KJ版", "其他版本")) is None
assert delivery_scope.exclusion_for(EXCLUDED.replace("5-数字人", "其他目录")) is None
assert (REPO / EXCLUDED).is_file()
assert (MANIFEST.parent / "06.api.json").is_file()
async def test_new_library_scope_and_old_user_row_preserved(tmp_path, monkeypatch):
await migrate_db(tmp_path / "local.db")
conn = get_db(tmp_path / "local.db")
# Reconstruct the previous historical scope without mutating its recorded files.
with monkeypatch.context() as patch:
patch.setattr("calliope.comfyui.library.exclusion_for", lambda path: None)
prior = import_library(conn, ROOT, source_commit="historical")
assert prior["counts"] == {"imported": 156}
conn.execute(
"UPDATE workflows SET description='用户旧数据' WHERE source_repo_path=?", (EXCLUDED,)
)
conn.commit()
before = dict(
conn.execute("SELECT * FROM workflows WHERE source_repo_path=?", (EXCLUDED,)).fetchone()
)
report = import_library(
conn, ROOT, source_commit="current", companions=load_companions(MANIFEST)
)
assert (report["source_total"], report["target_total"], report["excluded_total"]) == (
156,
155,
1,
)
assert report["counts"] == {"updated": 6, "skipped": 149, "user_excluded": 1}
record = next(r for r in report["files"] if r["path"] == EXCLUDED)
assert record["historical_row_preserved"] and record["result"] == "user_excluded"
assert (
dict(
conn.execute("SELECT * FROM workflows WHERE source_repo_path=?", (EXCLUDED,)).fetchone()
)
== before
)
assert all(
r["workflow_id"] != before["id"] for r in rollback_batch(conn, report["batch_id"])["files"]
)
assert (
dict(
conn.execute("SELECT * FROM workflows WHERE source_repo_path=?", (EXCLUDED,)).fetchone()
)
== before
)
conn.close()
def test_excluded_missing_material_does_not_block_six_active_exports(tmp_path):
manifest = json.loads(MANIFEST.read_text())
for case in manifest["cases"]:
if case["id"] != "06":
(tmp_path / case["export_file"]).write_bytes(
(MANIFEST.parent / case["export_file"]).read_bytes()
)
path = tmp_path / "manifest.json"
path.write_text(json.dumps(manifest))
records = load_companions(path)
assert records[EXCLUDED]["raw"] is None
assert sum(r["raw"] is not None for r in records.values()) == 6
def test_comparison_only_excluded_is_skipped_successfully(tmp_path, monkeypatch):
from scripts.compare_frontend_exports import main
case = next(c for c in json.loads(MANIFEST.read_text())["cases"] if c["id"] == "06")
cases = tmp_path / "cases.json"
cases.write_text(json.dumps({"cases": [case]}))
output = tmp_path / "report.json"
monkeypatch.setattr(
"sys.argv",
[
"compare",
"--repo",
str(tmp_path),
"--cases",
str(cases),
"--exports",
str(tmp_path),
"--output",
str(output),
],
)
assert main() == 0
report = json.loads(output.read_text())
assert report["status"] == "user_excluded"
assert report["active_cases"] == 0 and report["user_excluded"] == 1
assert not report["execution_verified"]
def test_cli_delivery_manifest_filters_preserved_history(tmp_path, monkeypatch):
import asyncio
from calliope.comfyui.library_cli import main
db = tmp_path / "history.db"
asyncio.run(migrate_db(db))
conn = get_db(db)
with monkeypatch.context() as patch:
patch.setattr("calliope.comfyui.library.exclusion_for", lambda path: None)
import_library(conn, ROOT, source_commit="historical")
before = dict(
conn.execute("SELECT * FROM workflows WHERE source_repo_path=?", (EXCLUDED,)).fetchone()
)
conn.close()
output = tmp_path / "delivery"
monkeypatch.setattr(
"sys.argv",
[
"library_cli",
"import",
"--root",
str(ROOT),
"--db",
str(db),
"--source-commit",
"current",
"--companions",
str(MANIFEST),
"--output-dir",
str(output),
],
)
main()
manifest = json.loads((output / "manifest.json").read_text())
assert len(manifest) == 155
assert EXCLUDED not in {row["path"] for row in manifest}
conn = get_db(db)
assert conn.execute("SELECT count(*) FROM workflows").fetchone()[0] == 156
assert (
dict(
conn.execute("SELECT * FROM workflows WHERE source_repo_path=?", (EXCLUDED,)).fetchone()
)
== before
)
conn.close()
def test_prerequisite_inventory_separates_historical_and_current_scope():
from scripts.audit_conversion_prerequisites import inventory
report = inventory(REPO)
assert report["total_ui_files"] == 146
assert report["remaining_reliable_conversions"] == report["target_ui_files"] == 145
assert report["source_files"] == 156 and report["target_files"] == 155
assert report["historical_exact_golden_candidates"] == 7
assert report["exact_golden_candidates"] == 6 and report["user_excluded"] == 1
excluded = next(f for f in report["files"] if f["path"] == EXCLUDED)
assert excluded["delivery_status"] == "user_excluded"
assert not excluded["converted_reliably"] and not excluded["execution_verified"]