Skip to content

armgit / moli

Public Imported from GitHub
0

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

6.1 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>
<iframe id="child" src="resources/a-page.html"></iframe>
<script>
function wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

function wait_for(promise, description) {
  return Promise.race([
    promise,
    wait(1000).then(function () {
      throw new Error("timed out waiting for " + description);
    }),
  ]);
}

function wait_for_hashchange(childWindow, expectedHash, action) {
  return new Promise(function (resolve) {
    function onHashchange() {
      if (childWindow.location.hash !== expectedHash) {
        return;
      }
      childWindow.removeEventListener("hashchange", onHashchange);
      resolve();
    }
    childWindow.addEventListener("hashchange", onHashchange);
    action();
  });
}

promise_test(async function () {
  const iframe = document.getElementById("child");
  const topOrder = [];
  const childBaseUrl = new URL("resources/a-page.html", location.href).href;

  window.addEventListener("popstate", function () {
    topOrder.push("top-popstate");
  });
  window.addEventListener("hashchange", function () {
    topOrder.push("top-hashchange");
  });

  await wait_for(
    new Promise(function (resolve) {
      function poll() {
        const childWindow = iframe.contentWindow;
        if (
          childWindow &&
          childWindow.location.href === childBaseUrl &&
          childWindow.navigation.currentEntry?.url === childBaseUrl &&
          childWindow.document.readyState === "complete"
        ) {
          resolve();
          return;
        }
        setTimeout(poll, 10);
      }
      poll();
    }),
    "child iframe initial navigation commit",
  );

  const childWindow = iframe.contentWindow;
  const childOrder = [];

  childWindow.history.pushState({ step: 1 }, "", "#one");
  childWindow.history.pushState({ step: 2 }, "", "#two");
  await wait_for_hashchange(
    childWindow,
    "#one",
    function () {
      childWindow.history.back();
    },
  );

  let committedSettled = false;
  let finishedSettled = false;
  let committedValue = null;
  let finishedValue = null;

  const result = childWindow.navigation.forward();
  result.committed.then(function (entry) {
    committedSettled = true;
    committedValue = entry;
    childOrder.push("child-committed:" + childWindow.location.hash);
  });
  result.finished.then(function (entry) {
    finishedSettled = true;
    finishedValue = entry;
    childOrder.push("child-finished:" + childWindow.location.hash);
  });

  const syncHash = childWindow.location.hash;
  const syncState = String(childWindow.history.state?.step ?? "null");
  const syncCommitted = String(committedSettled);
  const syncFinished = String(finishedSettled);
  const syncKeys = Object.keys(result).join(",");
  const syncProps = Object.getOwnPropertyNames(result).join(",");

  await Promise.all([result.committed, result.finished]);
  const snapshot = {
    syncHash,
    syncState,
    syncCommitted,
    syncFinished,
    syncKeys,
    syncProps,
    timeoutHash: childWindow.location.hash,
    timeoutState: String(childWindow.history.state?.step ?? "null"),
    timeoutCommitted: String(committedSettled),
    timeoutFinished: String(finishedSettled),
    timeoutCommittedType: Object.prototype.toString.call(committedValue),
    timeoutFinishedType: Object.prototype.toString.call(finishedValue),
    timeoutCommittedUrl: committedValue ? committedValue.url : "",
    timeoutFinishedUrl: finishedValue ? finishedValue.url : "",
    timeoutCurrentSameCommitted: String(
      committedValue === childWindow.navigation.currentEntry,
    ),
    timeoutCurrentSameFinished: String(
      finishedValue === childWindow.navigation.currentEntry,
    ),
    order: childOrder.join(","),
  };

  assert_array_equals(topOrder, [], "top-level traversal events should stay window-local to the child");
  assert_equals(snapshot.syncHash, "#one", "child should be back on the first fragment synchronously");
  assert_equals(snapshot.syncState, "1", "child history.state should match the traversed-back entry");
  assert_equals(snapshot.syncCommitted, "false", "committed should still be pending synchronously");
  assert_equals(snapshot.syncFinished, "false", "finished should still be pending synchronously");
  assert_equals(snapshot.syncKeys, "committed,finished", "result should expose committed and finished");
  assert_equals(
    snapshot.syncProps,
    "committed,finished",
    "result own properties should stay committed/finished",
  );
  assert_equals(snapshot.timeoutHash, "#two", "forward traversal should eventually restore the second fragment");
  assert_equals(snapshot.timeoutState, "2", "forward traversal should eventually restore the second state");
  assert_equals(snapshot.timeoutCommitted, "true", "committed should settle after traversal");
  assert_equals(snapshot.timeoutFinished, "true", "finished should settle after traversal");
  assert_equals(
    snapshot.order,
    "child-committed:#two,child-finished:#two",
    "child committed/finished callbacks should settle in order",
  );
  assert_equals(
    snapshot.timeoutCommittedType,
    "[object NavigationHistoryEntry]",
    "committed should resolve to a NavigationHistoryEntry",
  );
  assert_equals(
    snapshot.timeoutFinishedType,
    "[object NavigationHistoryEntry]",
    "finished should resolve to a NavigationHistoryEntry",
  );
  assert_equals(
    snapshot.timeoutCommittedUrl,
    childBaseUrl + "#two",
    "committed should resolve with the child current entry",
  );
  assert_equals(
    snapshot.timeoutFinishedUrl,
    childBaseUrl + "#two",
    "finished should resolve with the child current entry",
  );
  assert_equals(
    snapshot.timeoutCurrentSameCommitted,
    "true",
    "committed should resolve to the child currentEntry",
  );
  assert_equals(
    snapshot.timeoutCurrentSameFinished,
    "true",
    "finished should resolve to the child currentEntry",
  );
}, "Child browsing context navigation.forward() result promises stay local to the child window");
</script>