Skip to content

armgit / moli

Public Imported from GitHub
0

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

4.2 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, reject) {
    function cleanup() {
      worker.removeEventListener("message", onmessage);
      worker.removeEventListener("error", onerror);
    }

    function onmessage(event) {
      cleanup();
      resolve(event);
    }

    function onerror(event) {
      cleanup();
      reject(new Error("unexpected worker error: " + event.message));
    }

    worker.addEventListener("message", onmessage);
    worker.addEventListener("error", onerror);
  });
}

promise_test(async function () {
  const source = [
    "export const answer = 42;",
    "let base64Error = null;",
    "try {",
    "  btoa('\\u2713');",
    "} catch (error) {",
    "  base64Error = {",
    "    name: error.name,",
    "    code: error.code,",
    "    isDomException: error instanceof DOMException",
    "  };",
    "}",
    "postMessage({",
    "  kind: 'ready',",
    "  answer,",
    "  importScriptsType: typeof importScripts,",
    "  metaUrl: import.meta.url,",
    "  atobType: typeof atob,",
    "  btoaType: typeof btoa,",
    "  atobLength: atob.length,",
    "  btoaLength: btoa.length,",
    "  encoded: btoa('module'),",
    "  binaryEncoded: btoa('\\x00\\xff'),",
    "  decoded: atob('bW9kdWxl'),",
    "  whitespaceDecoded: atob(' Y\\tQ\\n==\\r'),",
    "  omittedPaddingDecoded: atob('YQ'),",
    "  domExceptionConstant: DOMException.INVALID_CHARACTER_ERR,",
    "  domExceptionPrototypeConstant: DOMException.prototype.INVALID_CHARACTER_ERR,",
    "  base64Error",
    "});",
    "onmessage = function (event) {",
    "  postMessage('echo:' + event.data);",
    "};",
  ].join("\n");
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    const ready = await next_message(worker);
    assert_equals(ready.data.kind, "ready", "module worker should run its initial script");
    assert_equals(ready.data.answer, 42, "exported module declarations should be available locally");
    assert_equals(ready.data.importScriptsType, "function", "module workers should expose WorkerGlobalScope.importScripts");
    assert_true(ready.data.metaUrl.indexOf("data:text/javascript,") === 0, "import.meta.url should expose the worker script URL");
    assert_equals(ready.data.atobType, "function", "module workers should expose atob");
    assert_equals(ready.data.btoaType, "function", "module workers should expose btoa");
    assert_equals(ready.data.atobLength, 1, "atob.length");
    assert_equals(ready.data.btoaLength, 1, "btoa.length");
    assert_equals(ready.data.encoded, "bW9kdWxl", "btoa should encode ASCII strings");
    assert_equals(ready.data.binaryEncoded, "AP8=", "btoa should preserve Latin-1 bytes");
    assert_equals(ready.data.decoded, "module", "atob should decode base64 strings");
    assert_equals(ready.data.whitespaceDecoded, "a", "atob should ignore ASCII whitespace");
    assert_equals(ready.data.omittedPaddingDecoded, "a", "atob should accept omitted padding");
    assert_equals(
      ready.data.domExceptionConstant,
      5,
      "module workers should expose DOMException legacy constants",
    );
    assert_equals(
      ready.data.domExceptionPrototypeConstant,
      5,
      "DOMException prototype should expose legacy constants in module workers",
    );
    assert_equals(
      ready.data.base64Error.name,
      "InvalidCharacterError",
      "btoa should reject non-Latin-1 input with InvalidCharacterError",
    );
    assert_equals(ready.data.base64Error.code, 5, "InvalidCharacterError legacy code");
    assert_equals(
      ready.data.base64Error.isDomException,
      true,
      "base64 errors should be DOMException instances in the module worker realm",
    );

    worker.postMessage("ping");
    const reply = await next_message(worker);
    assert_equals(reply.data, "echo:ping", "module worker should still receive window messages");
  } finally {
    worker.terminate();
  }
}, "Dedicated module workers accept type module and run simple no-import module source");
</script>