Files
aigc-home/docs/hel-892/zimage-reverse-document-evidence/collect_browser.py
T
2026-10-31 09:44:19 +08:00

84 lines
3.2 KiB
Python

"Collect final completed browser evidence and independently verify port release."
import collections
import json
import re
import shutil
import socket
from pathlib import Path
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[3]
OUT = Path(__file__).resolve().parent
LOCAL = ROOT / (".local-evidence/zimage-reverse-document/browser")
def main():
stopped = json.loads((LOCAL / "service-stop.json").read_bytes())
assert stopped["ports_released"] == [8247, 5173]
ports = []
for port in [8247, 5173]:
with socket.socket() as s:
s.settimeout(1)
result = s.connect_ex(("127.0.0.1", port))
assert result != 0
ports.append({"port": port, "connect_ex": result, "connection_refused": True})
data = json.loads((LOCAL / "browser-results.json").read_bytes())
assert not data["errors"] and data["viewports"] == [1440, 390]
backend = collections.Counter(
re.findall(r'"GET ([^ ]+) HTTP/1.1"', (LOCAL / "service-0.log").read_text())
)
polling = {p: n for p, n in backend.items() if p in ["/api/events", "/api/jobs"]}
fixed = {p: n for p, n in backend.items() if p not in polling}
browser = collections.Counter(urlsplit(r["url"]).path for r in data["requests"])
intercepted = collections.Counter(urlsplit(r["url"]).path for r in data["interceptions"])
expected = browser.copy()
for p, n in intercepted.items():
expected[p] -= n
expected["/api/workflows"] += 1 # Foreground readiness probe, explicitly separate.
# Browser result is flushed just before browser.close; a final timer GET can
# reach the server after that snapshot. Keep its exact count separately.
polling_delta = {p: backend[p] - expected[p] for p in ["/api/jobs", "/api/events"]}
assert all(abs(n) <= 1 for n in polling_delta.values())
assert {p: n for p, n in expected.items() if p not in polling_delta} == fixed
counts = {
"fixed_count": sum(fixed.values()),
"fixed_paths": fixed,
"polling_count": sum(polling.values()),
"polling_paths": polling,
"readiness_gets": 1,
"detail_fixture_intercepts": len(data["interceptions"]),
("fixed_paths_browser_minus_fixtures_plus_readiness_matches_backend"): True,
"polling_snapshot_delta": polling_delta,
"polling_snapshot_tolerance": 1,
}
(OUT / "backend-request-counts.json").write_text(
json.dumps(counts, ensure_ascii=False, indent=2) + "\n"
)
(OUT / "ports-independent.json").write_text(json.dumps(ports, indent=2) + "\n")
for name in [
"browser-results.json",
"service-stop.json",
"service-0.log",
"service-1.log",
]:
shutil.copyfile(LOCAL / name, OUT / name.replace(".log", ".txt"))
print(
"PASS browser",
data["assertion_count"],
data["assertionGroups"],
"fixed/polling/fixture",
data["request_accounting"]["fixed_count"],
data["request_accounting"]["polling_count"],
len(data["interceptions"]),
"backend",
counts["fixed_count"],
counts["polling_count"],
("readiness1; independently refused8247/5173"),
)
if __name__ == "__main__":
main()