Files
aigc-home/docs/hel-892/history-attribution-correction/test_contract.py
T

171 lines
6.6 KiB
Python

"""Offline provenance contracts, including the two actual rejected attributions."""
import json
import unittest
from pathlib import Path
from attribution import Attributor, candidate, references, review_language
ROOT = Path(__file__).resolve().parents[3]
PRIOR = ROOT / "docs/hel-892/batch175-closure-evidence"
A = "a" * 40
B = "b" * 40
PATH = "example_ComfyUI_workflows/a.json"
OTHER = "example_ComfyUI_workflows/b.json"
PIN = "1" * 64
class FakeGit:
def __init__(self, multiple=False):
self.mapping = {PATH: PIN, **({OTHER: "2" * 64} if multiple else {})}
self.code = {A: dict(sha256="own-code-bytes", definitions=self.mapping)}
def resolve(self, token):
return (
dict(status="ambiguous", commit=None)
if token == "aaaaaaa"
else dict(status="unique_commit", commit=token)
)
def definitions(self, commit):
return self.mapping
def changes(self, commit):
return self.mapping if commit == A else {OTHER: "2" * 64}
def blob(self, commit, path):
return "original-source-blob"
def ancestor(self, earlier, later):
return earlier == A and later == B
def check(content, git=None):
message = dict(id="synthetic", content=content)
git = git or FakeGit()
return candidate(PATH, PIN, message, references(message, git), git, B)
class Contracts(unittest.TestCase):
@classmethod
def setUpClass(cls):
messages = json.loads((PRIOR / "historical-comment-texts.json").read_bytes())
cls.ids = [
"01a1986f-3ac6-721d-bb47-0d97792f86a9",
"01a1987c-f9aa-7781-85d3-5a93331f1627",
"01a198cf-78a6-7591-94db-1d129c11c87a",
"01a198d9-f8a3-710b-b14d-037a92405368",
]
own = [m for m in messages if m["id"] in cls.ids]
assert len(own) == 4
cls.actual = Attributor(ROOT, own, "0e3e7b071f4c293642900849db8b1ead997417b8")
cls.ledger = json.loads((PRIOR / "ledger.json").read_bytes())
def actual_case(self, name, good, bad):
row = next(r for r in self.ledger if r["source_repo_path"].endswith(name))
result = self.actual.select(row["source_repo_path"], row["pin"], bad)
self.assertEqual(result["delivery_comment_id"], good)
rejected = next(c for c in result["candidates"] if c["comment_id"] == bad)
self.assertFalse(rejected["candidate_accepted"])
self.assertTrue(any(r["role"] == "baseline" for r in rejected["references"]))
def test_actual_krea_165_delivery_rejects_166_baseline(self):
self.actual_case("Krea2_t2i_20260818_API.json", self.ids[0], self.ids[1])
def test_actual_four_refs_172_rejects_173_baseline(self):
self.actual_case("video_minimax_h3_r2v_4ref_API.json", self.ids[2], self.ids[3])
def test_baseline_only(self):
self.assertFalse(check("基线`" + A + "`")["candidate_accepted"])
def test_dependency_only(self):
self.assertFalse(check("依赖提交`" + A + "`")["candidate_accepted"])
def test_old_only(self):
self.assertFalse(check("旧提交`" + A + "`")["candidate_accepted"])
def test_tested_baseline_cannot_supply_implementation_closure(self):
self.assertFalse(
check("受测旧代码基线`" + A + "`;最终head`" + B + "`")["candidate_accepted"]
)
def test_ambiguous_prefix(self):
self.assertFalse(check("提交`aaaaaaa`")["candidate_accepted"])
def test_shared_record_not_target_delta(self):
self.assertFalse(check("提交`" + B + "`,改共享pins.json")["candidate_accepted"])
def test_unscoped_multi_is_unknown(self):
self.assertFalse(check("提交`" + A + "`", FakeGit(True))["candidate_accepted"])
def test_explicit_multi_scope(self):
self.assertTrue(
check("提交`" + A + "`,本批2份固定说明", FakeGit(True))["candidate_accepted"]
)
def test_multi_scope_with_descriptive_noun(self):
self.assertTrue(
check("提交`" + A + "`,本批两张宫格图", FakeGit(True))["candidate_accepted"]
)
def test_bold_markdown_head(self):
result = check("提交 **`" + A + "`**")
self.assertTrue(result["candidate_accepted"])
self.assertEqual(result["head_proofs"][0]["explicit_tested_implementation"], "unknown")
def test_joint_tested_and_final_head(self):
self.assertTrue(check("受测实现与最终head均为 **`" + A + "`**")["candidate_accepted"])
def test_final_abbreviation_after_explicit_implementation(self):
self.assertTrue(check("实现`" + A + "`,最终 **`" + B + "`**")["candidate_accepted"])
def test_shared_multi_record_with_only_other_file_scope(self):
self.assertFalse(
check("提交`" + A + "`,只交付" + OTHER, FakeGit(True))["candidate_accepted"]
)
def test_tested_implementation_and_successor_head_separate(self):
result = check("受测实现`" + A + "`;最终head`" + B + "`")
self.assertTrue(result["candidate_accepted"])
self.assertEqual(result["head_proofs"][0]["delivery_head"], B)
self.assertEqual(
result["head_proofs"][0]["implementation_proof"]["implementation_commit"], A
)
def test_negative_audit_not_accepted(self):
for text in ["未审核通过", "审核未通过", "暂未审核通过", "若审核通过再处理", "待审核通过"]:
with self.subTest(text=text):
self.assertEqual(review_language(text)["status"], "unknown")
def test_progress_or_accept_word_not_certification(self):
for text in [
"总工已开始审核",
"已交总工审核,正在运行",
"接受",
"上一批审核通过",
"目前153份参考通过审核",
"据说审核通过",
"审核通过?",
]:
with self.subTest(text=text):
self.assertEqual(review_language(text)["status"], "unknown")
def test_affirmative_with_global_work_remaining(self):
self.assertEqual(
review_language("本批参考说明审核通过。全量未完成,不部署。")["status"],
"affirmative_audit_relay",
)
def test_explicit_passed_independent_audit(self):
self.assertEqual(
review_language("第172批已通过独立审核。全量未完成。")["status"],
"affirmative_audit_relay",
)
def test_conflicting_negative_does_not_become_positive(self):
self.assertEqual(review_language("本批审核通过。复核未通过。")["status"], "unknown")
if __name__ == "__main__":
unittest.main(verbosity=2)