Skip to content

armgit / moli

Public Imported from GitHub
0

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

4.6 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 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, predicate) {
  port.start();
  return new Promise(function (resolve) {
    function onmessage(event) {
      if (predicate && !predicate(event.data)) {
        return;
      }
      port.removeEventListener("message", onmessage);
      resolve(event);
    }

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

test(function () {
  const channel = new MessageChannel();
  try {
    assert_throws_name("TypeError", function () {
      channel.port1.postMessage("payload", 1);
    }, "non-object, non-iterable second argument should throw");

    assert_throws_name("TypeError", function () {
      channel.port1.postMessage("payload", { transfer: 1 });
    }, "non-iterable options.transfer should throw");
  } finally {
    channel.port1.close();
    channel.port2.close();
  }
}, "MessagePort.postMessage validates the second argument shape before cloning");

promise_test(async function () {
  const channel = new MessageChannel();
  try {
    const plain = next_port_message(channel.port2);
    channel.port1.postMessage("plain", {});
    assert_equals((await plain).ports.length, 0, "empty options dictionary should not create transferred ports");

    const buffer = new Uint8Array([7, 8, 9]).buffer;
    const cloned = next_port_message(channel.port2, function (data) {
      return data && data.kind === "clone";
    });
    channel.port1.postMessage({ kind: "clone", buffer: buffer }, { transfer: null });

    assert_equals(buffer.byteLength, 3, "transfer:null should leave the sender ArrayBuffer attached");

    const event = await cloned;
    assert_true(event.data.buffer instanceof ArrayBuffer, "receiver should get an ArrayBuffer clone");
    assert_equals(event.data.buffer.byteLength, 3, "receiver clone should preserve byte length");
    assert_array_equals(Array.from(new Uint8Array(event.data.buffer)), [7, 8, 9]);
    assert_equals(event.ports.length, 0, "transfer:null should not populate event.ports");
  } finally {
    channel.port1.close();
    channel.port2.close();
  }
}, "MessagePort.postMessage treats empty options and transfer:null as clone-only paths");

promise_test(async function () {
  const channel = new MessageChannel();
  const probe = new MessageChannel();
  try {
    assert_throws_name("DataCloneError", function () {
      channel.port1.postMessage({ port: probe.port1 });
    }, "nested MessagePort without transfer list should throw");

    const delivered = next_port_message(probe.port2);
    probe.port1.postMessage("still-live");
    assert_equals((await delivered).data, "still-live", "failed postMessage should not detach the nested port");
  } finally {
    channel.port1.close();
    channel.port2.close();
    probe.port1.close();
    probe.port2.close();
  }
}, "MessagePort.postMessage rejects nested MessagePorts that are not transferred");

test(function () {
  const channel = new MessageChannel();
  try {
    const buffer = new ArrayBuffer(4);
    assert_throws_name("DataCloneError", function () {
      channel.port1.postMessage("payload", [buffer, buffer]);
    }, "duplicate ArrayBuffer transfer entries should throw");
    assert_equals(buffer.byteLength, 4, "duplicate transfer failure should not detach the buffer");

    assert_throws_name("DataCloneError", function () {
      channel.port1.postMessage("payload", [new Uint8Array([1])]);
    }, "non-transferable transfer entries should throw");
  } finally {
    channel.port1.close();
    channel.port2.close();
  }
}, "MessagePort.postMessage rejects duplicate and non-transferable transfer entries");

test(function () {
  const channel = new MessageChannel();
  try {
    const buffer = new ArrayBuffer(4);
    channel.port1.postMessage(buffer, [buffer]);
    assert_equals(buffer.byteLength, 0, "first transfer should detach the sender buffer");

    assert_throws_name("DataCloneError", function () {
      channel.port1.postMessage(buffer, [buffer]);
    }, "detached ArrayBuffer transfer should throw");
  } finally {
    channel.port1.close();
    channel.port2.close();
  }
}, "MessagePort.postMessage rejects detached ArrayBuffers in transfer lists");
</script>