Skip to content

armgit / moli

Public Imported from GitHub
0

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

4.9 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) {
    worker.addEventListener("message", resolve, { once: true });
    worker.addEventListener("error", function (event) {
      reject(new Error("Worker error: " + event.message));
    }, { once: true });
  });
}

promise_test(async function () {
  const source = [
    "const events = ['start'];",
    "export const answer = 42;",
    "await Promise.resolve().then(function () { events.push('awaited'); });",
    "postMessage({",
    "  events,",
    "  answer,",
    "  topLevelThis: this,",
    "  importScriptsType: typeof importScripts,",
    "  metaUrl: import.meta.url",
    "});",
  ].join("\n");
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    const message = await next_message(worker);
    assert_array_equals(message.data.events, ["start", "awaited"], "top-level await should delay later module body work");
    assert_equals(message.data.answer, 42, "local exported declarations should remain visible after top-level await");
    assert_equals(message.data.topLevelThis, undefined, "module top-level this should be undefined");
    assert_equals(message.data.importScriptsType, "function", "module workers should expose WorkerGlobalScope.importScripts");
    assert_true(message.data.metaUrl.indexOf("data:text/javascript,") === 0, "import.meta.url should remain available");
  } finally {
    worker.terminate();
  }
}, "Dedicated no-import module workers support top-level await");

promise_test(async function () {
  const dependencyUrl = worker_url([
    "export const label = 'dependency';",
    "export default 'default-value';",
  ].join("\n"));
  const source = [
    "import value, { label } from " + JSON.stringify(dependencyUrl) + ";",
    "const events = ['before'];",
    "await Promise.resolve().then(function () { events.push(label); });",
    "postMessage({",
    "  events,",
    "  value,",
    "  label,",
    "  importScriptsType: typeof importScripts",
    "});",
  ].join("\n");
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    const message = await next_message(worker);
    assert_array_equals(message.data.events, ["before", "dependency"], "imported bindings should be visible across top-level await");
    assert_equals(message.data.value, "default-value", "default import should remain visible after top-level await");
    assert_equals(message.data.label, "dependency", "named import should remain visible after top-level await");
    assert_equals(message.data.importScriptsType, "function", "module workers should expose WorkerGlobalScope.importScripts");
  } finally {
    worker.terminate();
  }
}, "Dedicated module workers support top-level await with static imports");

promise_test(async function () {
  const source = [
    "const events = ['start'];",
    "async function record(label) {",
    "  await Promise.resolve();",
    "  events.push(label);",
    "  return label;",
    "}",
    "const templated = `body:${await record('body-template')}`;",
    "export const exportedTemplate = `export:${await record('export-template')}`;",
    "export let exportedLetTemplate = `let:${await record('let-template')}`;",
    "export var exportedVarTemplate = `var:${await record('var-template')}`;",
    "postMessage({",
    "  events,",
    "  templated,",
    "  exportedTemplate,",
    "  exportedLetTemplate,",
    "  exportedVarTemplate,",
    "  importScriptsType: typeof importScripts",
    "});",
  ].join("\n");
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    const message = await next_message(worker);
    assert_array_equals(message.data.events, ["start", "body-template", "export-template", "let-template", "var-template"], "template expression awaits should run in module body order");
    assert_equals(message.data.templated, "body:body-template", "top-level await inside a template expression should evaluate in the module async wrapper");
    assert_equals(message.data.exportedTemplate, "export:export-template", "exported const template expression awaits should also trigger async module evaluation");
    assert_equals(message.data.exportedLetTemplate, "let:let-template", "exported let template expression awaits should also trigger async module evaluation");
    assert_equals(message.data.exportedVarTemplate, "var:var-template", "exported var template expression awaits should also trigger async module evaluation");
    assert_equals(message.data.importScriptsType, "function", "module workers should expose WorkerGlobalScope.importScripts");
  } finally {
    worker.terminate();
  }
}, "Dedicated module workers support top-level await inside template expressions");
</script>