Skip to content

armgit / moli

Public Imported from GitHub
0

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

4.5 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) {
  try {
    callback();
  } catch (error) {
    assert_equals(
      error && error.name,
      expectedName,
      description || "unexpected exception name"
    );
    return;
  }
  assert_unreached(
    (description || "assert_throws_name") + ": expected an exception"
  );
}

function wait_request(request) {
  return new Promise((resolve, reject) => {
    request.onsuccess = () => resolve(request.result);
    request.onerror = () => reject(request.error);
  });
}

function cursor_keys(request) {
  return new Promise((resolve, reject) => {
    const keys = [];
    request.onerror = () => reject(request.error);
    request.onsuccess = () => {
      const cursor = request.result;
      if (!cursor) {
        resolve(keys);
        return;
      }
      keys.push(cursor.key);
      cursor.continue();
    };
  });
}

function object_store(db, storeName) {
  return db.transaction(storeName).objectStore(storeName);
}

function index_store(db, storeName, indexName) {
  return object_store(db, storeName).index(indexName);
}

promise_test(async () => {
  const dbname = "idb-cursor-query-conversion-" + Math.random();
  const storeName = "store";
  const indexName = "byGroup";

  const openRequest = indexedDB.open(dbname, 1);
  openRequest.onupgradeneeded = () => {
    const store = openRequest.result.createObjectStore(storeName);
    store.createIndex(indexName, "group");
    store.add({ id: 1, group: "a" }, 1);
    store.add({ id: 2, group: "a" }, 2);
    store.add({ id: 3, group: "b" }, 3);
  };

  const db = await wait_request(openRequest);

  assert_array_equals(
    await cursor_keys(object_store(db, storeName).openCursor()),
    [1, 2, 3],
    "objectStore.openCursor() missing query iterates every record"
  );
  assert_array_equals(
    await cursor_keys(object_store(db, storeName).openCursor(undefined)),
    [1, 2, 3],
    "objectStore.openCursor(undefined) iterates every record"
  );
  assert_array_equals(
    await cursor_keys(object_store(db, storeName).openCursor(IDBKeyRange.bound(2, 3))),
    [2, 3],
    "objectStore.openCursor() accepts IDBKeyRange queries"
  );
  assert_array_equals(
    await cursor_keys(object_store(db, storeName).openKeyCursor(2)),
    [2],
    "objectStore.openKeyCursor() accepts key queries"
  );
  assert_array_equals(
    await cursor_keys(index_store(db, storeName, indexName).openCursor("a")),
    ["a", "a"],
    "index.openCursor() accepts key queries"
  );
  assert_array_equals(
    await cursor_keys(index_store(db, storeName, indexName).openKeyCursor("a")),
    ["a", "a"],
    "index.openKeyCursor() accepts key queries"
  );

  assert_throws_name(
    "DataError",
    () => object_store(db, storeName).openCursor({}),
    "objectStore.openCursor() keeps invalid query validation in IndexedDB runtime"
  );
  assert_throws_name(
    "DataError",
    () => object_store(db, storeName).openKeyCursor({}),
    "objectStore.openKeyCursor() keeps invalid query validation in IndexedDB runtime"
  );
  assert_throws_name(
    "DataError",
    () => index_store(db, storeName, indexName).openCursor({}),
    "index.openCursor() keeps invalid query validation in IndexedDB runtime"
  );
  assert_throws_name(
    "DataError",
    () => index_store(db, storeName, indexName).openKeyCursor({}),
    "index.openKeyCursor() keeps invalid query validation in IndexedDB runtime"
  );

  assert_throws_name(
    "DataError",
    () => object_store(db, storeName).openCursor({}, Symbol("direction")),
    "objectStore.openCursor() validates query before converting direction"
  );
  assert_throws_name(
    "DataError",
    () => object_store(db, storeName).openKeyCursor({}, Symbol("direction")),
    "objectStore.openKeyCursor() validates query before converting direction"
  );
  assert_throws_name(
    "DataError",
    () => index_store(db, storeName, indexName).openCursor({}, Symbol("direction")),
    "index.openCursor() validates query before converting direction"
  );
  assert_throws_name(
    "DataError",
    () => index_store(db, storeName, indexName).openKeyCursor({}, Symbol("direction")),
    "index.openKeyCursor() validates query before converting direction"
  );

  db.close();
  await wait_request(indexedDB.deleteDatabase(dbname));
}, "IndexedDB openCursor/openKeyCursor query arguments use WebIDL optional raw any boundaries");
</script>