Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(wpt): serve JSON module lifecycle fixtures
ldm0005751612cc2
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/case_set.py b/moli-benchmark/moli_benchmark/wpt_cross/case_set.pyindex 79b15e491..b5293962d 100644--- a/moli-benchmark/moli_benchmark/wpt_cross/case_set.py+++ b/moli-benchmark/moli_benchmark/wpt_cross/case_set.py@@ -728,10 +728,30 @@ def _script_content_type_handler_reference_patterns(directory: str) -> tuple[re.)+@lru_cache(maxsize=None)+def _json_module_handler_reference_patterns(directory: str) -> tuple[re.Pattern[str], ...]:+ references = []+ for name in (+ "json-module/load-error-events.py", "serve-json-then-js.py",+ ):+ resource = "html/semantics/scripting-1/the-script-element/" + name+ relative = posixpath.relpath(resource, directory)+ references.extend(("/" + resource, relative, "./" + relative))+ return tuple(+ re.compile(+ rf"(?<![A-Za-z0-9_./-]){re.escape(reference)}"+ rf"{WPTSERVE_HANDLER_TRAILING_BOUNDARY}"+ )+ for reference in references+ )++def _supported_wptserve_handler_references(rel: str | None,) -> tuple[re.Pattern[str], ...]:supported: tuple[re.Pattern[str], ...] = ()+ if rel is not None:+ supported += _json_module_handler_reference_patterns(posixpath.dirname(rel) or ".")if rel is not None:supported += _script_content_type_handler_reference_patterns(posixpath.dirname(rel) or ".")if rel is not None and rel.rsplit("/", 1)[0] == "fetch/api/abort":diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.pyindex adfc3d82f..e1609ffb4 100644--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py@@ -85,6 +85,11 @@ FETCH_ABORT_RESOURCE_PATHS = {LINK_STYLESHEET_COUNTER_PATH = ("/html/semantics/document-metadata/the-link-element/stylesheet.py")+JSON_THEN_JS_PATH = "/html/semantics/scripting-1/the-script-element/serve-json-then-js.py"+JSON_LOAD_ERROR_PATH = (+ "/html/semantics/scripting-1/the-script-element/json-module/load-error-events.py"+)+BENCH_TIMEOUT_MULTIPLIER_QUERY = "__moli_bench_timeout_multiplier"BENCH_REPORT_BRIDGE_SRC_RE = re.compile(rb"(?P<prefix>\bsrc\s*=\s*)(?P<quote>['\"])"@@ -1574,6 +1579,7 @@ class FetchStash:def __init__(self) -> None:self._lock = threading.Lock()self._values: dict[uuid.UUID, str] = {}+ self._counts: dict[tuple[str, uuid.UUID], int] = {}def put(self, key: str, value: str, *, overwrite: bool = False) -> None:parsed_key = uuid.UUID(key)@@ -1582,6 +1588,14 @@ class FetchStash:raise ValueError("Tried to overwrite existing shared stash value")self._values[parsed_key] = value+ def increment(self, key: str, *, path: str) -> int:+ parsed_key = (path, uuid.UUID(key))+ with self._lock:+ value = int(self._counts.get(parsed_key, 0)) + 1+ self._counts[parsed_key] = value+ return value++def take(self, key: str) -> str | None:parsed_key = uuid.UUID(key)with self._lock:@@ -1869,6 +1883,14 @@ def _make_handler(if path == "/resources/testdriver-vendor.js":self._send_bytes("application/javascript; charset=utf-8", BENCH_TESTDRIVER_VENDOR_BRIDGE, emit_body=emit_body)return+ if path == JSON_LOAD_ERROR_PATH:+ self._serve_script_load_error_events(+ parsed.query, emit_body=emit_body, json_module=True,+ )+ return+ if path == JSON_THEN_JS_PATH:+ self._serve_json_then_js(parsed.query, emit_body=emit_body)+ returnif path == "/xhr/resources/delay.py":self._serve_xhr_delay(parsed.query, emit_body=emit_body)return@@ -2577,19 +2599,39 @@ def _make_handler(except (BrokenPipeError, ConnectionResetError, OSError):return- def _serve_script_load_error_events(self, query: str, *, emit_body: bool) -> None:+ def _serve_json_then_js(self, query: str, *, emit_body: bool) -> None:+ params = parse_qs(query, keep_blank_values=True, encoding="latin-1")+ try:+ count = fetch_stash.increment(params["key"][0], path=JSON_THEN_JS_PATH)+ except (KeyError, ValueError):+ self.send_error(400, "Not enough parameters")+ return+ if count == 1:+ content_type, body = "text/json", b'{"hello": "world"}'+ else:+ content_type, body = "application/javascript", b"export default 'hello';"+ self._send_bytes(content_type, body, emit_body=emit_body, cache_control=None)+++ def _serve_script_load_error_events(+ self, query: str, *, emit_body: bool, json_module: bool = False,+ ) -> None:params = parse_qs(query, keep_blank_values=True)test = params.get("test", [""])[0]if re.fullmatch(r"[a-zA-Z0-9_]+", test) is None:- self.send_error(400)+ self.send_error(500 if json_module else 400)returnif "_load" in test:status = 200- body = f'"use strict"; {test}.executed = true;'+ prefix = 'import "./module.json" with { type: "json"};' if json_module else '"use strict";'+ body = f'{prefix} {test}.executed = true;'else:- status = 404+ # The JSON fixture serves a valid module whose dependency+ # fetch fails; the classic fixture itself returns 404.+ status = 200 if json_module else 404+ prefix = 'import "./not_found.json" with { type: "json"};' if json_module else '"use strict";'body = (- f'"use strict"; {test}.test.step(function() {{ '+ f'{prefix} {test}.test.step(function() {{ ''assert_unreached("404 script should not be executed"); });')self._send_bytes(diff --git a/moli-benchmark/tests/test_wpt_json_module_server.py b/moli-benchmark/tests/test_wpt_json_module_server.pynew file mode 100644index 000000000..0b68d70b2--- /dev/null+++ b/moli-benchmark/tests/test_wpt_json_module_server.py@@ -0,0 +1,126 @@+from __future__ import annotations++import tempfile+import unittest+import uuid+from contextlib import ExitStack+from http.client import HTTPConnection+from pathlib import Path+from unittest.mock import patch++from moli_benchmark.wpt_cross.case_set import enumerate_cases+from moli_benchmark.wpt_cross.server import (+ JSON_LOAD_ERROR_PATH, JSON_THEN_JS_PATH, WptFixtureServer,+)+++SCRIPT_DIRECTORY = "html/semantics/scripting-1/the-script-element"+++class JsonModuleFixtureTests(unittest.TestCase):+ def setUp(self) -> None:+ self.stack = ExitStack()+ self.addCleanup(self.stack.close)+ self.root = Path(self.stack.enter_context(tempfile.TemporaryDirectory()))+ (self.root / "resources").mkdir()+ (self.root / "resources/testharness.js").write_text("// testharness")+ for path in (JSON_LOAD_ERROR_PATH, JSON_THEN_JS_PATH):+ resource = self.root / path.lstrip("/")+ resource.parent.mkdir(parents=True, exist_ok=True)+ resource.write_text("# Python source must not be served")+ self.stack.enter_context(patch(+ "moli_benchmark.wpt_cross.server._global_ipv6_address", return_value=None,+ ))+ self.server = self.stack.enter_context(WptFixtureServer(self.root))++ def request(self, path: str, query: str, *, method: str = "GET", port: int | None = None):+ connection = HTTPConnection("127.0.0.1", port or self.server.port, timeout=2)+ try:+ connection.request(method, path + "?" + query)+ response = connection.getresponse()+ return response.status, response.headers, response.read()+ finally:+ connection.close()++ def test_json_load_error_fixture_fails_at_dependency_fetch(self) -> None:+ cases = (+ ("test1_load", b'import "./module.json" with { type: "json"}; test1_load.executed = true;'),+ ("test1_error", b'import "./not_found.json" with { type: "json"}; test1_error.test.step(function() { assert_unreached("404 script should not be executed"); });'),+ )+ for name, expected in cases:+ for method in ("GET", "HEAD"):+ with self.subTest(name=name, method=method):+ status, headers, body = self.request(+ JSON_LOAD_ERROR_PATH, f"test={name}&test=ignored", method=method,+ )+ self.assertEqual(status, 200)+ self.assertEqual(headers["Content-Type"], "text/javascript")+ self.assertEqual(headers["Content-Length"], str(len(expected)))+ self.assertEqual(body, b"" if method == "HEAD" else expected)++ def test_json_then_js_counts_requests_per_uuid_and_across_origins(self) -> None:+ key = uuid.uuid4()+ first, headers, body = self.request(JSON_THEN_JS_PATH, f"key={key}&key=ignored")+ self.assertEqual((first, headers["Content-Type"], body),+ (200, "text/json", b'{"hello": "world"}'))+ self.assertIsNone(headers["Cache-Control"])+ second, headers, body = self.request(+ JSON_THEN_JS_PATH, f"key={str(key).upper()}",+ method="HEAD", port=self.server.alternate_port,+ )+ self.assertEqual((second, headers["Content-Type"], body),+ (200, "application/javascript", b""))+ self.assertEqual(headers["Content-Length"], str(len(b"export default 'hello';")))+ third, headers, body = self.request(JSON_THEN_JS_PATH, f"key={key.hex}")+ self.assertEqual((third, headers["Content-Type"], body),+ (200, "application/javascript", b"export default 'hello';"))+ _, headers, body = self.request(JSON_THEN_JS_PATH, f"key={uuid.uuid4()}")+ self.assertEqual((headers["Content-Type"], body), ("text/json", b'{"hello": "world"}'))++ def test_head_consumes_first_response_and_servers_do_not_share_state(self) -> None:+ key = uuid.uuid4()+ _, headers, body = self.request(JSON_THEN_JS_PATH, f"key={key}", method="HEAD")+ self.assertEqual((headers["Content-Type"], body), ("text/json", b""))+ _, headers, body = self.request(JSON_THEN_JS_PATH, f"key={key}")+ self.assertEqual((headers["Content-Type"], body),+ ("application/javascript", b"export default 'hello';"))+ other = self.stack.enter_context(WptFixtureServer(self.root))+ _, headers, body = self.request(JSON_THEN_JS_PATH, f"key={key}", port=other.port)+ self.assertEqual((headers["Content-Type"], body), ("text/json", b'{"hello": "world"}'))++ def test_invalid_parameters_return_errors(self) -> None:+ for path, queries, expected_status in (+ (JSON_THEN_JS_PATH, ("", "key=", "key=not-a-uuid", "key=%FF"), 400),+ (JSON_LOAD_ERROR_PATH, ("", "test=", "test=a.b_load", "test=a%0A_load"), 500),+ ):+ for query in queries:+ with self.subTest(path=path, query=query):+ self.assertEqual(self.request(path, query)[0], expected_status)++ def test_discovery_accepts_only_supported_resource_locations(self) -> None:+ cases = {+ "json-module/switch.html": ("../serve-json-then-js.py", True),+ "json-module/switch-dot.html": ("./../serve-json-then-js.py", True),+ "json-module/switch-absolute.html": (JSON_THEN_JS_PATH, True),+ "json-module/load.html": ("./load-error-events.py", True),+ "json-module/load-absolute.html": (JSON_LOAD_ERROR_PATH, True),+ "json-module/wrong-switch.html": ("serve-json-then-js.py", False),+ "json-module/wrong-load.html": ("../load-error-events.py", False),+ "json-module/suffix.html": ("./load-error-events.py.extra", False),+ }+ for name, (reference, _) in cases.items():+ path = self.root / SCRIPT_DIRECTORY / name+ path.write_text(+ '<script src="/resources/testharness.js"></script>'+ '<script src="/resources/testharnessreport.js"></script>'+ f'<script src="{reference}?test=load"></script>'+ )+ discovered = enumerate_cases(self.root, dir_prefixes=(SCRIPT_DIRECTORY,))+ self.assertEqual(+ sorted(case.case_path for case in discovered),+ sorted(SCRIPT_DIRECTORY + "/" + name for name, (_, allowed) in cases.items() if allowed),+ )+++if __name__ == "__main__":+ unittest.main()