Skip to content

armgit / moli

Public Imported from GitHub
0

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

2.3 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 src="/wpt/ported/worker/resources/assert-bootstrap-error.js"></script>
<script>
function worker_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

function wait_for(promise, description) {
  return Promise.race([
    promise,
    wait(300).then(function () {
      throw new Error("timed out waiting for " + description);
    }),
  ]);
}

async function assert_module_graph_failure(workerUrl, description) {
  const previousOnerror = window.onerror;
  let messageSeen = false;
  const worker = new Worker(workerUrl, { type: "module" });

  try {
    window.onerror = function () {
      return true;
    };
    worker.onmessage = function () {
      messageSeen = true;
    };

    const event = await wait_for(
      new Promise(function (resolve) {
        worker.onerror = function (event) {
          resolve(event);
          return true;
        };
      }),
      "module worker " + description + " graph failure",
    );

    assert_worker_bootstrap_error(event, worker);
    assert_false(messageSeen, "module evaluation should not continue after the graph failure");
  } finally {
    window.onerror = previousOnerror;
    worker.terminate();
  }
}

promise_test(async function () {
  const missingHttpMainUrl = new URL(
    "/wpt/ported/worker/resources/worker-module-load-error-http-missing-main.js",
    location.href
  ).href;
  await assert_module_graph_failure(
    missingHttpMainUrl,
    "HTTP dependency 404",
  );

  const unsupportedSchemeSource = [
    "import 'ftp://example.test/module-worker-dependency.js';",
    "postMessage('unexpected');",
  ].join("\n");
  await assert_module_graph_failure(
    worker_url(unsupportedSchemeSource),
    "unsupported dependency scheme",
  );

  const invalidSpecifierSource = [
    "import 'http://[::1';",
    "postMessage('unexpected');",
  ].join("\n");
  await assert_module_graph_failure(
    worker_url(invalidSpecifierSource),
    "invalid dependency specifier",
  );
}, "Dedicated module workers report dependency load and resolution failures before evaluation");
</script>