Skip to content

armgit / moli

Public Imported from GitHub
0

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

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

function connect_worker_url(body) {
  return shared_worker_url(
    [
      "let connections = 0;",
      "onconnect = function (event) {",
      "  connections++;",
      "  const port = event.ports[0];",
      "  port.onmessage = function (message) {",
      body,
      "  };",
      "};",
    ].join("\n"),
  );
}

function next_port_message(port) {
  return new Promise(function (resolve) {
    port.addEventListener("message", resolve, { once: true });
    port.start();
  });
}

function next_port_close(port) {
  return new Promise(function (resolve) {
    port.addEventListener("close", resolve, { once: true });
    port.start();
  });
}

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

function request(port, data) {
  const next = next_port_message(port);
  port.postMessage(data);
  return next;
}

test(function () {
  assert_equals(typeof SharedWorker, "function", "SharedWorker constructor exists");
  assert_equals(SharedWorker.length, 1, "SharedWorker constructor length");
}, "SharedWorker exposes constructor surface");

test(function () {
  assert_throws_js(TypeError, function () {
    new SharedWorker();
  });
}, "SharedWorker requires a script URL");

test(function () {
  assert_throws_js(Error, function () {
    new SharedWorker(
      {
        toString() {
          throw new Error("scriptURL toString failed");
        },
      },
      "name",
    );
  });
}, "SharedWorker propagates scriptURL stringification exceptions");

test(function () {
  const worker = new SharedWorker(connect_worker_url("port.postMessage('ready');"));
  assert_true(worker instanceof SharedWorker, "constructed object is SharedWorker");
  assert_equals(typeof worker.port, "object", "port exists");
  assert_true(worker.port instanceof MessagePort, "port is a MessagePort");
  assert_equals(worker.onerror, null, "onerror starts as null");
}, "SharedWorker object exposes port and basic EventTarget surface");

test(function () {
  const worker = new SharedWorker(connect_worker_url("port.postMessage('ready');"));
  const original = worker.port;
  worker.port = null;
  assert_equals(worker.port, original, "port is readonly");
}, "SharedWorker.port is readonly");

test(function () {
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), null);
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), undefined);
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), "string-name");
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), 1);
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), NaN);
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), Infinity);
  new SharedWorker(connect_worker_url("port.postMessage('ready');"), { name: "object-name" });
}, "SharedWorker accepts omitted, null, undefined, primitive stringifiable, string, and object names");

promise_test(async function () {
  const cases = [
    [1, "1"],
    [NaN, "NaN"],
    [Infinity, "Infinity"],
  ];
  for (const [input, expected] of cases) {
    const worker = new SharedWorker(
      shared_worker_url(
        [
          "onconnect = function (event) {",
          "  event.ports[0].postMessage(self.name);",
          "};",
        ].join("\n"),
      ),
      input,
    );
    assert_equals((await next_port_message(worker.port)).data, expected);
  }
}, "SharedWorker stringifies primitive name arguments");

promise_test(async function () {
  const worker = new SharedWorker(
    connect_worker_url(
      [
        "port.postMessage({",
        "  data: message.data,",
        "  connectCount: connections,",
        "  scope: typeof SharedWorkerGlobalScope,",
        "  instance: self instanceof SharedWorkerGlobalScope,",
        "  selfEqualsGlobal: self === globalThis,",
        "  hasPostMessage: typeof postMessage,",
        "  name: self.name",
        "});",
      ].join("\n"),
    ),
    "named-shared-worker",
  );
  const event = await request(worker.port, "ping");
  assert_equals(event.data.data, "ping", "worker receives port message");
  assert_equals(event.data.connectCount, 1, "first connection count");
  assert_equals(event.data.scope, "function", "SharedWorkerGlobalScope constructor exists");
  assert_true(event.data.instance, "self is a SharedWorkerGlobalScope");
  assert_true(event.data.selfEqualsGlobal, "self aliases globalThis");
  assert_equals(event.data.hasPostMessage, "undefined", "shared worker global has no postMessage");
  assert_equals(event.data.name, "named-shared-worker", "self.name reflects constructor name");
  assert_true(event instanceof MessageEvent, "client receives MessageEvent");
  assert_equals(event.type, "message", "client message event type");
}, "SharedWorker connect event installs a usable worker-side MessagePort");

promise_test(async function () {
  const worker = new SharedWorker(
    shared_worker_url(
      [
        "onconnect = function (event) {",
        "  const port = event.ports[0];",
        "  port.postMessage({",
        "    isMessageEvent: event instanceof MessageEvent,",
        "    type: event.type,",
        "    data: event.data,",
        "    portsLength: event.ports.length,",
        "    firstPort: event.ports[0] instanceof MessagePort",
        "  });",
        "};",
      ].join("\n"),
    ),
  );
  const event = await next_port_message(worker.port);
  assert_true(event.data.isMessageEvent, "connect event is MessageEvent");
  assert_equals(event.data.type, "connect", "connect event type");
  assert_equals(event.data.data, "", "connect event data is empty string");
  assert_equals(event.data.portsLength, 1, "connect event has one port");
  assert_true(event.data.firstPort, "connect event port is MessagePort");
}, "SharedWorkerGlobalScope receives a MessageEvent connect event");

promise_test(async function () {
  const url = connect_worker_url(
    [
      "if (message.data === 'count') {",
      "  port.postMessage(connections);",
      "} else {",
      "  port.postMessage('echo:' + message.data);",
      "}",
    ].join("\n"),
  );
  const first = new SharedWorker(url, "reuse");
  const second = new SharedWorker(url, "reuse");
  assert_equals((await request(first.port, "count")).data, 2, "first port sees two connections");
  assert_equals((await request(second.port, "count")).data, 2, "second port shares worker");
  assert_equals((await request(first.port, "a")).data, "echo:a", "first port remains usable");
  assert_equals((await request(second.port, "b")).data, "echo:b", "second port remains usable");
}, "SharedWorker reuses the same instance for the same URL and name");

promise_test(async function () {
  const url = shared_worker_url(
    [
      "let connections = 0;",
      "onconnect = function (event) {",
      "  connections++;",
      "  event.ports[0].postMessage(connections);",
      "};",
    ].join("\n"),
  );
  const first = new SharedWorker(url, "left-name");
  const second = new SharedWorker(url, "right-name");
  assert_equals((await next_port_message(first.port)).data, 1, "left name has its own worker");
  assert_equals((await next_port_message(second.port)).data, 1, "right name has its own worker");
}, "SharedWorker name participates in the matching key");

promise_test(async function () {
  const url = shared_worker_url(
    [
      "let connections = 0;",
      "onconnect = function (event) {",
      "  connections++;",
      "  event.ports[0].postMessage(self.name + ':' + connections);",
      "};",
    ].join("\n"),
  );
  const first = new SharedWorker(url, "option-name");
  const second = new SharedWorker(url, { name: "option-name" });
  assert_equals((await next_port_message(first.port)).data, "option-name:1");
  assert_equals((await next_port_message(second.port)).data, "option-name:2");
}, "String name and options.name address the same SharedWorker");

promise_test(async function () {
  const worker = new SharedWorker(
    shared_worker_url(
      [
        "addEventListener('connect', function (event) {",
        "  event.ports[0].postMessage('listener:' + event.type);",
        "});",
      ].join("\n"),
    ),
  );
  assert_equals((await next_port_message(worker.port)).data, "listener:connect");
}, "SharedWorkerGlobalScope supports connect event listeners");

promise_test(async function () {
  const worker = new SharedWorker(
    shared_worker_url(
      [
        "onconnect = function (event) {",
        "  event.ports[0].postMessage('first');",
        "};",
        "onconnect = function (event) {",
        "  event.ports[0].postMessage('second');",
        "};",
      ].join("\n"),
    ),
  );
  assert_equals((await next_port_message(worker.port)).data, "second");
}, "SharedWorkerGlobalScope onconnect replacement uses the latest handler");

promise_test(async function () {
  const worker = new SharedWorker(
    shared_worker_url(
      [
        "onconnect = function (event) {",
        "  event.ports[0].postMessage('before-close');",
        "  close();",
        "};",
      ].join("\n"),
    ),
    "close-notify",
  );
  const closed = next_port_close(worker.port);
  assert_equals((await next_port_message(worker.port)).data, "before-close");
  const event = await closed;
  assert_equals(event.type, "close", "client port is notified when the worker closes");
}, "SharedWorker close() notifies the connected client port");

promise_test(async function () {
  const url = shared_worker_url(
    [
      "let connections = 0;",
      "onconnect = function (event) {",
      "  connections++;",
      "  event.ports[0].postMessage(connections);",
      "  close();",
      "};",
    ].join("\n"),
  );
  const first = new SharedWorker(url, "fresh-after-close");
  const firstClosed = next_port_close(first.port);
  assert_equals((await next_port_message(first.port)).data, 1);
  await firstClosed;

  const second = new SharedWorker(url, "fresh-after-close");
  assert_equals((await next_port_message(second.port)).data, 1);
}, "SharedWorker close() removes the instance before the same key is reused");

promise_test(async function () {
  const url = connect_worker_url("port.postMessage('ready');");
  new SharedWorker(url, { name: "type-check", type: "classic" });
  const worker = new SharedWorker(url, { name: "type-check", type: "module" });
  const event = await next_worker_error(worker);
  assert_worker_bootstrap_error(event, worker);
}, "SharedWorker type mismatch for an existing key dispatches error");

promise_test(async function () {
  const url = connect_worker_url("port.postMessage('ready');");
  new SharedWorker(url, { name: "credentials-check", credentials: "same-origin" });
  const worker = new SharedWorker(url, { name: "credentials-check", credentials: "include" });
  const event = await next_worker_error(worker);
  assert_worker_bootstrap_error(event, worker);
}, "SharedWorker credentials mismatch for an existing key dispatches error");

promise_test(async function () {
  const worker = new SharedWorker("/wpt/missing-shared-worker-script.js");
  let onerrorCalled = false;
  worker.onerror = function (event) {
    onerrorCalled = true;
    assert_worker_bootstrap_error(event, worker);
  };
  await next_worker_error(worker);
  assert_true(onerrorCalled, "onerror handler is invoked for script load failure");
}, "SharedWorker script load failure dispatches an async error event");

promise_test(async function () {
  const worker = new SharedWorker(
    "data:text/javascript;base64,%%%",
    "invalid-data-url-script",
  );
  const event = await next_worker_error(worker);
  assert_worker_bootstrap_error(event, worker);
}, "SharedWorker invalid data URL script dispatches an async error event");

test(function () {
  const url = connect_worker_url("port.postMessage('ready');");
  assert_throws_js(TypeError, function () {
    new SharedWorker(url, { type: "invalid" });
  });
  assert_throws_js(TypeError, function () {
    new SharedWorker(url, { credentials: "invalid" });
  });
}, "SharedWorker validates type and credentials options");
</script>