Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.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 wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

test(function () {
  assert_equals(Worker.length, 1, "Worker constructor length");
  assert_equals(typeof Worker, "function", "Worker constructor should exist");

  const worker = new Worker(worker_url("postMessage('boot')"));
  try {
    assert_equals(worker.onmessage, null, "Worker.onmessage should start as null");
    assert_equals(typeof worker.postMessage, "function", "postMessage should exist");
    assert_equals(typeof worker.terminate, "function", "terminate should exist");
  } finally {
    worker.terminate();
  }

  const classic = new Worker(worker_url("postMessage('classic')"), { type: "classic" });
  classic.terminate();
}, "Worker exposes the minimal constructor and instance surface");

promise_test(async function () {
  const worker = new Worker(
    worker_url(
      [
        "postMessage({",
        "  kind: 'ready',",
        "  selfEqualsGlobal: self === globalThis,",
        "  importScriptsType: typeof importScripts,",
        "  postMessageType: typeof postMessage",
        "});",
        "onmessage = function (event) {",
        "  postMessage({",
        "    kind: 'reply',",
        "    isMessageEvent: event instanceof MessageEvent,",
        "    type: event.type,",
        "    data: 'pong:' + event.data",
        "  });",
        "};",
      ].join("\n"),
    ),
  );

  try {
    const ready = await next_message(worker);
    assert_equals(ready.data.kind, "ready", "worker should boot and post an initial message");
    assert_true(ready.data.selfEqualsGlobal, "worker self should alias globalThis");
    assert_equals(ready.data.importScriptsType, "function", "importScripts should exist in workers");
    assert_equals(ready.data.postMessageType, "function", "worker postMessage should exist");
    assert_true(ready instanceof MessageEvent, "boot message should arrive as MessageEvent");
    assert_equals(ready.type, "message", "boot message event type");

    worker.postMessage("ping");
    const reply = await next_message(worker);
    assert_equals(reply.data.kind, "reply", "worker should answer postMessage()");
    assert_true(reply.data.isMessageEvent, "worker onmessage should receive MessageEvent");
    assert_equals(reply.data.type, "message", "worker onmessage event type");
    assert_equals(reply.data.data, "pong:ping", "worker reply payload");
  } finally {
    worker.terminate();
  }
}, "Dedicated workers expose a basic global and support postMessage round-trips");

promise_test(async function () {
  const worker = new Worker(
    worker_url(
      [
        "onmessage = function () {",
        "  postMessage('first');",
        "  setTimeout(function () {",
        "    postMessage('late');",
        "  }, 40);",
        "};",
      ].join("\n"),
    ),
  );

  try {
    worker.postMessage("go");
    const first = await next_message(worker);
    assert_equals(first.data, "first", "worker should deliver the first queued message");

    worker.terminate();

    const maybeLate = await Promise.race([
      next_message(worker).then(function (event) {
        return event.data;
      }),
      wait(100).then(function () {
        return null;
      }),
    ]);
    assert_equals(maybeLate, null, "terminate() should stop later worker deliveries");
  } finally {
    worker.terminate();
  }
}, "Worker.terminate stops future message delivery");
</script>