test(api): harden trust drift log capture guard
Some checks failed
CD Pipeline / tests (push) Successful in 1m25s
Code Review / ai-code-review (push) Successful in 13s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled

This commit is contained in:
Your Name
2026-06-04 22:08:10 +08:00
parent e73383c326
commit 5c2578c1aa

View File

@@ -192,7 +192,7 @@ class _ClosedAfterExitContext:
@pytest.mark.asyncio
async def test_log_appears_proves_commit_runs_inside_with(caplog):
async def test_log_appears_proves_commit_runs_inside_with(caplog, capsys):
"""行為證據:當 __aexit__ 後 commit 會拋錯時log 仍然出現
→ 證明 commit 是在 with 內就跑完了,不在 with 外(與 critic 主張相反)
"""
@@ -206,6 +206,7 @@ async def test_log_appears_proves_commit_runs_inside_with(caplog):
mock_db = AsyncMock()
mock_db.execute = AsyncMock(return_value=mock_result)
mock_db.commit = AsyncMock() # 初始 commit OK__aexit__ 後會被替換成拋錯
commit_before_exit = mock_db.commit
alerter = AsyncMock()
alerter.alert_governance = AsyncMock()
@@ -231,8 +232,18 @@ async def test_log_appears_proves_commit_runs_inside_with(caplog):
# 2. mutation 已發生
assert stale.status == "deprecated"
# 3. KEYlog 出現了 — 證明 commit 在 __aexit__ 之前就跑完
log_text = " | ".join(rec.getMessage() for rec in caplog.records)
# 3. KEYcommit 在 __aexit__ 之前就跑完
# full-suite 下 structlog 可能已被其他測試設定成 stdout renderer
# 因此同時檢查 caplog 與 capsys避免 capture backend 差異造成假紅燈。
assert commit_before_exit.await_count == 1
captured = capsys.readouterr()
log_text = " | ".join(
[
" | ".join(rec.getMessage() for rec in caplog.records),
captured.out,
captured.err,
]
)
assert "governance_trust_drift_auto_deprecated" in log_text, (
f"如果 critic 主張為真commit 在 with 外log 不該出現。"
f"但實際 log 出現了 → 證明 critic 主張為假。Log: {log_text!r}"