165 lines
6.9 KiB
Python
165 lines
6.9 KiB
Python
"""Source API preservation and synthetic direct-link differential evidence.
|
|
|
|
The synthetic pair is not a substitute for the seven real frontend exports.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from calliope.comfyui import source_reviews
|
|
from calliope.comfyui.library import import_library
|
|
from calliope.comfyui.ui2api import convert
|
|
from calliope.db import get_db, migrate_db
|
|
from tests.test_workflow_library import ROOT
|
|
|
|
API_FILES = sorted(ROOT.glob("*_API.json"))
|
|
|
|
|
|
@pytest.mark.parametrize("path", API_FILES, ids=lambda path: path.name)
|
|
def test_ten_existing_api_exports_preserve_every_field_and_link(path):
|
|
source = json.loads(path.read_text())
|
|
before = copy.deepcopy(source)
|
|
result = convert(source)
|
|
assert result.issues == []
|
|
assert result.prompt == before
|
|
assert source == before
|
|
# Verify references independently of the converter, including colon/dotted IDs.
|
|
for node in result.prompt.values():
|
|
for value in node["inputs"].values():
|
|
if isinstance(value, list):
|
|
assert len(value) == 2 and value[0] in result.prompt
|
|
assert type(value[1]) is int and value[1] >= 0
|
|
|
|
|
|
def test_named_direct_link_pair_preserves_nonzero_slot_and_named_values():
|
|
info = {
|
|
"Source": {"input": {"required": {"text": ["STRING", {}]}}, "output": ["IMAGE", "STRING"]},
|
|
"Sink": {"input": {"required": {"value": ["STRING", {}]}}, "output": []},
|
|
}
|
|
ui = {
|
|
"nodes": [
|
|
{"id": "1:7", "type": "Source", "widgets_values": {"text": "逐字保留\n中文"}},
|
|
{"id": 2, "type": "Sink", "inputs": [{"name": "value", "link": 9}]},
|
|
{"id": 3, "type": "Note", "widgets_values": ["not executable"]},
|
|
],
|
|
"links": [[9, "1:7", 1, 2, 0, "STRING"]],
|
|
}
|
|
before = copy.deepcopy(ui)
|
|
expected = {
|
|
"1:7": {
|
|
"class_type": "Source",
|
|
"inputs": {"text": "逐字保留\n中文"},
|
|
"_meta": {"title": "Source"},
|
|
},
|
|
"2": {"class_type": "Sink", "inputs": {"value": ["1:7", 1]}, "_meta": {"title": "Sink"}},
|
|
}
|
|
assert convert(ui, info).prompt == expected
|
|
assert ui == before
|
|
ui["links"][0][2] = 0 # valid slot, incompatible IMAGE -> STRING
|
|
result = convert(ui, info)
|
|
assert result.prompt is None
|
|
assert "link_type_unverified" in {i["code"] for i in result.issues}
|
|
|
|
|
|
@pytest.mark.parametrize("value", [["missing", 0], ["1", -1], ["1", True], ["1"], []])
|
|
def test_invalid_api_links_fail_closed(value):
|
|
source = {"1": {"class_type": "Sink", "inputs": {"value": value}}}
|
|
result = convert(source)
|
|
assert result.prompt is None
|
|
assert result.issues[0]["code"] == "invalid_api_link"
|
|
|
|
|
|
def test_real_ui_sources_stay_closed_without_frontend_evidence():
|
|
paths = [p for p in ROOT.rglob("*") if p.is_file() and p not in API_FILES]
|
|
assert len(API_FILES) == 10 and len(paths) == 146
|
|
for path in paths:
|
|
source = json.loads(path.read_text())
|
|
result = convert(source)
|
|
assert result.prompt is None, path
|
|
assert result.issues, path
|
|
|
|
|
|
async def test_review_revision_refreshes_docs_preserving_user_edits_and_sha_binding(
|
|
tmp_path,
|
|
monkeypatch,
|
|
):
|
|
root = tmp_path / "examples"
|
|
root.mkdir()
|
|
source = root / "a.json"
|
|
source.write_text(json.dumps({"1": {"class_type": "SaveImage", "inputs": {}}}))
|
|
sha = hashlib.sha1(source.read_bytes()).hexdigest()
|
|
notes = {}
|
|
monkeypatch.setattr(source_reviews, "_reviews", lambda: notes)
|
|
await migrate_db(tmp_path / "local.db")
|
|
conn = get_db(tmp_path / "local.db")
|
|
try:
|
|
assert import_library(conn, root, source_commit="fixture")["counts"] == {"imported": 1}
|
|
notes["examples/a.json"] = {
|
|
"source_sha": sha,
|
|
"purpose": "图像保存结构",
|
|
"instruction": "核对 SaveImage",
|
|
}
|
|
assert import_library(conn, root, source_commit="fixture")["counts"] == {"updated": 1}
|
|
doc = json.loads(conn.execute("SELECT doc_json FROM workflows").fetchone()[0])
|
|
assert doc["purpose"] == "图像保存结构"
|
|
assert not doc["source_review"]["complete"]
|
|
assert doc["status"] == {"imported": True, "doc_done": False, "exec_verified": False}
|
|
assert import_library(conn, root, source_commit="fixture")["counts"] == {"skipped": 1}
|
|
# A changed source must not inherit notes for different source bytes.
|
|
source.write_text(json.dumps({"2": {"class_type": "SaveAudio", "inputs": {}}}))
|
|
assert import_library(conn, root, source_commit="fixture")["counts"] == {"updated": 1}
|
|
doc = json.loads(conn.execute("SELECT doc_json FROM workflows").fetchone()[0])
|
|
assert "source_review" not in doc
|
|
conn.execute("UPDATE workflows SET description='用户版'")
|
|
conn.commit()
|
|
notes["examples/a.json"]["instruction"] = "changed review"
|
|
assert import_library(conn, root, source_commit="fixture")["counts"] == {"conflict": 1}
|
|
assert conn.execute("SELECT description FROM workflows").fetchone()[0] == "用户版"
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
async def test_reviewed_catalogue_has_real_named_values_and_api_edges(tmp_path):
|
|
await migrate_db(tmp_path / "local.db")
|
|
conn = get_db(tmp_path / "local.db")
|
|
try:
|
|
report = import_library(conn, ROOT, source_commit="fixture")
|
|
assert report["states"] == {"needs_conversion": 145, "pending_check": 10}
|
|
for row in conn.execute("SELECT * FROM workflows"):
|
|
doc = json.loads(row["doc_json"])
|
|
assert doc["source_review"]["source_sha"] == row["source_sha"]
|
|
review = doc["source_review"]
|
|
assert review["scope"][0] == "结构及用途局部审校"
|
|
if review.get("semantic_review"):
|
|
assert review["semantic_review"]["operation_check"] in doc["steps"]
|
|
assert len(review["scope"]) == 2
|
|
assert not doc["source_review"]["complete"]
|
|
assert doc["status"]["doc_done"] is False
|
|
source = json.loads(row["raw_source_json"])
|
|
if "nodes" not in source:
|
|
links = [
|
|
(nid, name, value)
|
|
for nid, node in source.items()
|
|
for name, value in node["inputs"].items()
|
|
if isinstance(value, list)
|
|
]
|
|
assert len(doc["source_links"]) == len(links)
|
|
for nid, name, value in links:
|
|
assert {
|
|
"scope": "",
|
|
"node_id": nid,
|
|
"field": name,
|
|
"upstream_node_id": value[0],
|
|
"output_slot": value[1],
|
|
} in doc["source_links"]
|
|
for param in doc["params"]:
|
|
assert param["unit"] is None and param["range_source"] == "unverified"
|
|
assert param["field"] != "videopreview"
|
|
finally:
|
|
conn.close()
|