diff --git a/apps/api/tests/test_check_trust_drift_commit_outside_context_poc.py b/apps/api/tests/test_check_trust_drift_commit_outside_context_poc.py index 1e4aad8a..41e4efd3 100644 --- a/apps/api/tests/test_check_trust_drift_commit_outside_context_poc.py +++ b/apps/api/tests/test_check_trust_drift_commit_outside_context_poc.py @@ -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. KEY:log 出現了 — 證明 commit 在 __aexit__ 之前就跑完 - log_text = " | ".join(rec.getMessage() for rec in caplog.records) + # 3. KEY:commit 在 __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}"