Files
aigc-home/docs/hel-892/singleloop-document-evidence/collect_browser.py
T
2026-11-01 17:00:01 +08:00

84 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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/singleloop-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})
with socket.socket() as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("127.0.0.1", port))
ports[-1]["independently_bindable"] = True
data = json.loads((LOCAL / "browser-results.json").read_bytes())
assert not data["errors"] and data["viewports"] == [1440, 390]
backend = collections.Counter(
re.findall('"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
polling_delta = {p: backend[p] - expected[p] for p in ["/api/jobs", "/api/events"]}
assert polling_delta["/api/events"] == 0
assert polling_delta["/api/jobs"] in [0, 1]
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,
"polling_acceptance": (
"仅允许后端比浏览末次快照多0或1次/api/jobs;" + "/api/events与所有固定路径零差额"
),
}
(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()