"""Actual catalogue review refresh, user protection and source-evidence audit.""" import copy import json from pathlib import Path from scripts.audit_library_documents import audit, audit_partition from calliope.comfyui import source_reviews from calliope.comfyui.library import import_library from calliope.db import get_db, migrate_db from tests.test_workflow_library import ROOT def test_actual_review_partition_and_evidence_match_original_sources(): repo = ROOT.parent report = audit(repo, repo / "docs/hel-892/offline") partition = audit_partition( repo, report, repo / "docs/hel-892/third-evidence/review-partition.json" ) assert partition["new_named_fields"] == 500 assert partition["new_without_named_fields"] == 114 assert report["named_values_preserved"] == 1227 assert report["selected_named_fields_reviewed"] == 1227 assert report["full_documents_completed"] == report["real_executions"] == 0 async def test_current_scope_reviews_refresh_but_user_modified_row_is_preserved( tmp_path, monkeypatch ): current = json.loads(Path(source_reviews.__file__).with_suffix(".json").read_text()) previous = copy.deepcopy(current) revised = {path for path, review in current.items() if review.get("semantic_review")} for path in revised: previous[path] = { key: previous[path][key] for key in ("source_sha", "purpose", "instruction") } monkeypatch.setattr(source_reviews, "_reviews", lambda: previous) await migrate_db(tmp_path / "catalogue.db") conn = get_db(tmp_path / "catalogue.db") try: assert import_library(conn, ROOT, source_commit="fixture")["counts"] == { "imported": 155, "user_excluded": 1, } prior = { row["source_repo_path"]: dict(row) for row in conn.execute("SELECT * FROM workflows") if row["source_repo_path"] not in revised } protected_path = sorted(revised)[0] conn.execute( "UPDATE workflows SET description=? WHERE source_repo_path=?", ("用户整行版本", protected_path), ) conn.commit() protected = dict( conn.execute( "SELECT * FROM workflows WHERE source_repo_path=?", (protected_path,) ).fetchone() ) monkeypatch.setattr(source_reviews, "_reviews", lambda: current) result = import_library(conn, ROOT, source_commit="fixture") assert result["counts"] == { "updated": 138, "skipped": 16, "conflict": 1, "user_excluded": 1, } assert import_library(conn, ROOT, source_commit="fixture")["counts"] == { "skipped": 154, "user_excluded": 1, "conflict": 1, } for row in conn.execute("SELECT * FROM workflows"): path = row["source_repo_path"] if path == protected_path: assert dict(row) == protected elif path in prior: assert dict(row) == prior[path] else: doc = json.loads(row["doc_json"]) assert doc["source_review"]["semantic_review"] == current[path]["semantic_review"] assert current[path]["semantic_review"]["operation_check"] in doc["steps"] assert row["is_enabled"] == 0 assert row["doc_completed_at"] is None and row["exec_verified_at"] is None finally: conn.close()