Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.7 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 assert_id_array(actual, expected, description) {
  assert_array_equals(
    actual.map((value) => value.id),
    expected,
    description
  );
}

promise_test(async () => {
  const dbname = "idb-collection-count-conversion-" + Math.random();
  const storeName = "store";
  const indexName = "byGroup";
  const throwingCount = {
    valueOf() {
      throw new RangeError("collection count conversion");
    },
  };

  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);
  const tx = db.transaction(storeName);
  const store = tx.objectStore(storeName);
  const index = store.index(indexName);

  assert_throws_name(
    "TypeError",
    () => store.getAll(undefined, Symbol("count")),
    "objectStore.getAll rejects Symbol count"
  );
  assert_throws_name(
    "TypeError",
    () => store.getAll(undefined, NaN),
    "objectStore.getAll rejects NaN under EnforceRange"
  );
  assert_throws_name(
    "TypeError",
    () => store.getAll(undefined, Infinity),
    "objectStore.getAll rejects Infinity under EnforceRange"
  );
  assert_throws_name(
    "TypeError",
    () => store.getAll(undefined, -1),
    "objectStore.getAll rejects negative count under EnforceRange"
  );
  assert_throws_name(
    "TypeError",
    () => store.getAll(undefined, 4294967296),
    "objectStore.getAll rejects count outside unsigned long range"
  );
  assert_throws_name(
    "RangeError",
    () => store.getAll(undefined, throwingCount),
    "objectStore.getAll propagates count conversion errors"
  );

  let conversions = 0;
  const countedObject = {
    valueOf() {
      ++conversions;
      return 2.9;
    },
  };
  const deferredRequest = store.getAll(undefined, countedObject);
  assert_equals(conversions, 1, "count converts synchronously once");
  assert_id_array(
    await wait_request(deferredRequest),
    [1, 2],
    "objectStore.getAll truncates fractional counts"
  );
  assert_equals(conversions, 1, "deferred operation reuses normalized count");

  assert_array_equals(
    await wait_request(store.getAllKeys(undefined, "2.9")),
    [1, 2],
    "objectStore.getAllKeys converts numeric string counts"
  );
  assert_array_equals(
    await wait_request(store.getAllKeys(undefined, null)),
    [],
    "objectStore.getAllKeys converts null count to zero"
  );
  assert_id_array(
    await wait_request(index.getAll(undefined, { valueOf() { return 1.9; } })),
    [1],
    "index.getAll truncates object count"
  );
  assert_array_equals(
    await wait_request(index.getAllKeys(undefined, 2.9)),
    [1, 2],
    "index.getAllKeys truncates numeric count"
  );

  db.close();
  await wait_request(indexedDB.deleteDatabase(dbname));
}, "IDB getAll/getAllKeys count follows WebIDL EnforceRange unsigned long conversion");
</script>