65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
||
"""review-knowledge-cards 校准 stamp 门离线测试(不连库、不调模型)。"""
|
||
import json
|
||
import sys
|
||
import tempfile
|
||
from pathlib import Path
|
||
|
||
import click
|
||
|
||
PROJECT_ROOT = Path(__file__).resolve().parents[3]
|
||
SCRIPTS = PROJECT_ROOT / "muse" / "content" / "entity" / "skills" / "review" / "review-knowledge-cards" / "scripts"
|
||
sys.path.insert(0, str(SCRIPTS))
|
||
|
||
from review_cards import ( # noqa: E402
|
||
criteria_fingerprint,
|
||
evaluate_calibrate_diffs,
|
||
is_calibration_batch,
|
||
require_calibrate_stamp,
|
||
write_calibrate_stamp,
|
||
)
|
||
|
||
|
||
def expect_exc(fn) -> None:
|
||
try:
|
||
fn()
|
||
except click.ClickException:
|
||
return
|
||
raise AssertionError("应抛 ClickException")
|
||
|
||
|
||
def main() -> None:
|
||
assert is_calibration_batch("cal-002")
|
||
assert not is_calibration_batch("prod-1")
|
||
fp = criteria_fingerprint()
|
||
assert len(fp) == 64
|
||
assert criteria_fingerprint() == fp
|
||
|
||
ok = evaluate_calibrate_diffs([0.1, -0.2, 0.0])
|
||
assert ok["passed"] and ok["n"] == 3
|
||
bad_mae = evaluate_calibrate_diffs([0.9, -0.8, 0.7])
|
||
assert not bad_mae["passed"]
|
||
bad_max = evaluate_calibrate_diffs([0.0, 1.6])
|
||
assert not bad_max["passed"]
|
||
empty = evaluate_calibrate_diffs([])
|
||
assert empty["passed"] is False and empty["n"] == 0
|
||
|
||
with tempfile.TemporaryDirectory() as tmp:
|
||
path = Path(tmp) / "stamp.json"
|
||
expect_exc(lambda: require_calibrate_stamp(path=path, fingerprint=fp))
|
||
stamp = write_calibrate_stamp(ok, batch="cal-002", path=path)
|
||
loaded = require_calibrate_stamp(path=path, fingerprint=fp)
|
||
assert loaded["batch"] == "cal-002"
|
||
stale = json.loads(path.read_text())
|
||
stale["criteria_sha256"] = "0" * 64
|
||
path.write_text(json.dumps(stale))
|
||
expect_exc(lambda: require_calibrate_stamp(path=path, fingerprint=fp))
|
||
write_calibrate_stamp(bad_mae, batch="cal-bad", path=path)
|
||
expect_exc(lambda: require_calibrate_stamp(path=path, fingerprint=fp))
|
||
assert stamp["criteria_sha256"] == fp
|
||
print("PASS:校准 stamp 门全部通过")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|