Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.0 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 assert_throws_name(expectedName, callback, description) {
  let error = null;
  try {
    callback();
  } catch (thrown) {
    error = thrown;
  }
  assert_true(error !== null, (description || "assert_throws_name") + ": expected an exception");
  assert_equals(error.name, expectedName, (description || "assert_throws_name") + ": unexpected exception name");
}

function next_port_message(port) {
  port.start();
  return new Promise(function (resolve) {
    function onmessage(event) {
      port.removeEventListener("message", onmessage);
      resolve(event);
    }

    port.addEventListener("message", onmessage);
  });
}

test(function () {
  const worker = new Worker(worker_url("postMessage('ready');"));

  try {
    const buffer = new Uint8Array([1, 2, 3]).buffer;
    assert_throws_name(
      "DataCloneError",
      function () {
        worker.postMessage(buffer, { transfer: [buffer, buffer] });
      },
      "duplicate ArrayBuffer in Worker options.transfer should throw",
    );
    assert_equals(
      buffer.byteLength,
      3,
      "duplicate Worker options.transfer failure should not detach the ArrayBuffer",
    );
  } finally {
    worker.terminate();
  }
}, "Worker.postMessage options.transfer rejects duplicate ArrayBuffers without detaching");

test(function () {
  const worker = new Worker(worker_url("postMessage('ready');"));

  try {
    const typedArray = new Uint8Array([9]);
    assert_throws_name(
      "DataCloneError",
      function () {
        worker.postMessage("payload", { transfer: [typedArray] });
      },
      "non-transferable Worker options.transfer entry should throw",
    );
    assert_equals(
      typedArray.byteLength,
      1,
      "failed Worker options.transfer validation should not mutate the typed array view",
    );
  } finally {
    worker.terminate();
  }
}, "Worker.postMessage options.transfer rejects non-transferable entries");

promise_test(async function () {
  const worker = new Worker(worker_url("postMessage('ready');"));
  const channel = new MessageChannel();

  try {
    assert_throws_name(
      "DataCloneError",
      function () {
        worker.postMessage("payload", { transfer: [channel.port1, channel.port1] });
      },
      "duplicate MessagePort in Worker options.transfer should throw",
    );

    const delivered = next_port_message(channel.port2);
    channel.port1.postMessage("still-live");
    assert_equals(
      (await delivered).data,
      "still-live",
      "failed duplicate Worker options.transfer should not detach the MessagePort",
    );
  } finally {
    channel.port1.close();
    channel.port2.close();
    worker.terminate();
  }
}, "Worker.postMessage options.transfer rejects duplicate MessagePorts without detaching");
</script>