from __future__ import annotations import unittest from moli_benchmark.process import ProcessResult from moli_benchmark.public_web import ( POST_DCL_SETTLE_MILLISECONDS, POST_DCL_WAIT_SCRIPT, PUBLIC_WEB_SNAPSHOT_CONTRACT, TOP_SITES_ARTIFACT_POLICY, TOP_SITES_CLASSIFIER, WILD_WEB_CLASSIFIER, PublicWebAttempt, PublicWebResult, PublicWebScheduler, resolve_main_document_status, schedule_public_web_cases, ) class PublicWebTests(unittest.TestCase): def test_post_dcl_wait_script_encodes_the_reported_snapshot_contract(self) -> None: self.assertEqual(POST_DCL_SETTLE_MILLISECONDS, 50) self.assertIn("setTimeout", POST_DCL_WAIT_SCRIPT) self.assertIn("}, 50)", POST_DCL_WAIT_SCRIPT) self.assertIn("PostDclReady", POST_DCL_WAIT_SCRIPT) self.assertIn("50 ms", PUBLIC_WEB_SNAPSHOT_CONTRACT) def test_typed_cli_fetch_readiness_timeout_is_shared_by_both_suites(self) -> None: stderr = b""" Error: failed to fetch `https://example.test/` Reason: fetch readiness timed out after 30000 ms while waiting for DOMContentLoaded """ for classifier in (TOP_SITES_CLASSIFIER, WILD_WEB_CLASSIFIER): with self.subTest(policy=classifier.policy): self.assertEqual( classifier.classify_output( stdout=b"", stderr=stderr, returncode=1, timed_out=False, ), "timeout", ) def test_protocol_status_is_preferred(self) -> None: self.assertEqual( resolve_main_document_status( 200, b"lifecycle target document `https://example.test/` returned 502 Bad Gateway", 1, ), (200, "protocol"), ) def test_nonzero_cli_diagnostic_supplies_main_document_status(self) -> None: self.assertEqual( resolve_main_document_status( None, b"Reason: lifecycle target document `https://example.test/` " b"returned 429 Too Many Requests", 1, ), (429, "cli-diagnostic"), ) self.assertEqual( resolve_main_document_status( None, b"HTTP request `https://example.test/` returned 404 Not Found", 1, ), (404, "cli-diagnostic"), ) def test_success_and_subresource_diagnostics_are_not_promoted(self) -> None: diagnostic = b"script subresource `https://cdn.example.test/a.js` returned 500" self.assertEqual(resolve_main_document_status(None, diagnostic, 1), (None, None)) self.assertEqual( resolve_main_document_status( None, b"lifecycle target document `https://example.test/` returned 500", 0, ), (None, None), ) def test_attempt_result_owns_common_evidence_fields(self) -> None: attempt = PublicWebAttempt.start( target="chrome", metadata={"engine": "chrome", "driver": "cdp-dcl"}, target_info={"available": True, "path": "/bin/chromium"}, run=2, case_fields={"seed": "example"}, url="https://example.test/", schedule_index=3, target_order_index=2, artifact_stem="chrome-run-2-example", ) process = ProcessResult( command=["chromium"], returncode=0, elapsed_ms=12.5, stdout=b"