结算失败关键节点落调试快照(HTML+截图),方便排查 needs_human 场景

enter_checkout/submit_order/pay 抛错前尽力落一份页面快照到
evidence_dir/{task_id}/debug-*,与已有编号步骤证据同目录;快照方法自身
失败只记日志,不会掩盖原始异常。之前失败时只有日志文字,要复现现场只能
靠临时探针脚本重新触发一遍真实流程。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-14 02:07:12 +08:00
co-authored by Claude Sonnet 5
parent e5f7da09a9
commit 295fc7ad69
2 changed files with 103 additions and 0 deletions
+56
View File
@@ -629,4 +629,60 @@ async def test_add_to_cart_without_start_raises():
await site.add_to_cart(_make_task())
# ---- _dump_debug_snapshot:失败时落调试快照,本身出错不能污染原始异常 ----
class _FakePage:
"""不依赖 Playwright 的最小 page 替身,只实现 _dump_debug_snapshot 用到的接口"""
def __init__(self, *, html: str = "<html></html>", url: str = "https://example.test/x"):
self.url = url
self._html = html
self.screenshot_calls: list[str] = []
async def content(self) -> str:
return self._html
async def screenshot(self, *, path: str, full_page: bool = True) -> None:
self.screenshot_calls.append(path)
Path(path).write_bytes(b"fake-png")
class _BrokenPage(_FakePage):
"""content() 抛错,模拟页面已关闭等场景"""
async def content(self) -> str:
raise RuntimeError("page already closed")
async def test_dump_debug_snapshot_writes_html_and_png(tmp_path):
from app.shared.config import Settings
settings = Settings(evidence_dir=str(tmp_path))
site = SiteInteractor(auth_session=None, settings=settings) # type: ignore[arg-type]
page = _FakePage(html="<html>boom</html>")
await site._dump_debug_snapshot(page, task_id="t1", label="enter_checkout-error")
written = list((tmp_path / "t1").glob("debug-*"))
htmls = [p for p in written if p.suffix == ".html"]
pngs = [p for p in written if p.suffix == ".png"]
assert len(htmls) == 1
assert len(pngs) == 1
assert htmls[0].read_text(encoding="utf-8") == "<html>boom</html>"
async def test_dump_debug_snapshot_swallows_its_own_errors(tmp_path):
"""page 已经关闭、content() 抛错时,快照方法本身不能再抛,否则会掩盖原始异常"""
from app.shared.config import Settings
settings = Settings(evidence_dir=str(tmp_path))
site = SiteInteractor(auth_session=None, settings=settings) # type: ignore[arg-type]
page = _BrokenPage()
await site._dump_debug_snapshot(page, task_id="t1", label="pay-blocked")
assert list((tmp_path / "t1").glob("debug-*")) == []
# ---- helper ----