Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.8 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
function worker_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function next_message(worker) {
  return new Promise(function (resolve) {
    worker.addEventListener("message", resolve, { once: true });
  });
}

function assert_immutability_payload(data, label) {
  assert_equals(data.namedAssignment, "TypeError", label + ": assigning to a named import should throw");
  assert_equals(data.defaultAssignment, "TypeError", label + ": assigning to a default import should throw");
  assert_equals(data.counter, 5, label + ": imported named binding should remain live after assignment failures");
  assert_equals(data.current, 5, label + ": imported default binding should remain live after assignment failures");
  assert_equals(data.importScriptsType, "function", label + ": module workers should expose WorkerGlobalScope.importScripts");
}

function module_source(depUrl) {
  return [
    "import current, { counter, setCounter } from " + JSON.stringify(depUrl) + ";",
    "function assignmentName(callback) {",
    "  try {",
    "    callback();",
    "    return 'none';",
    "  } catch (error) {",
    "    return error && error.name;",
    "  }",
    "}",
    "const namedAssignment = assignmentName(function () { counter = 7; });",
    "const defaultAssignment = assignmentName(function () { current = 8; });",
    "setCounter(5);",
    "postMessage({",
    "  namedAssignment,",
    "  defaultAssignment,",
    "  counter,",
    "  current,",
    "  importScriptsType: typeof importScripts",
    "});",
  ].join("\n");
}

promise_test(async function () {
  const depUrl = worker_url([
    "export let counter = 1;",
    "export { counter as default };",
    "export function setCounter(value) { counter = value; }",
  ].join("\n"));
  const worker = new Worker(worker_url(module_source(depUrl)), { type: "module" });

  try {
    const message = await next_message(worker);
    assert_immutability_payload(message.data, "data URL dependency");
  } finally {
    worker.terminate();
  }
}, "Dedicated module worker imported bindings reject assignment for data URL dependencies");

promise_test(async function () {
  const workerUrl = new URL(
    "/wpt/ported/worker/resources/worker-module-import-immutability-main.js",
    location.href
  ).href;
  const worker = new Worker(workerUrl, { type: "module" });

  try {
    const message = await next_message(worker);
    assert_immutability_payload(message.data, "HTTP dependency");
  } finally {
    worker.terminate();
  }
}, "Dedicated module worker imported bindings reject assignment for HTTP dependencies");
</script>