Skip to content

armgit / moli

Public Imported from GitHub
0

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

6.3 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);
  });
}

promise_test(async () => {
  const dbname = "idb-index-conversion-" + Math.random();
  const storeName = "store";
  const indexName = "index-" + Math.random();
  const keyPath = "value";
  const stringifiedIndexName = {
    toString() {
      return indexName;
    },
  };
  const stringifiedKeyPath = {
    toString() {
      return keyPath;
    },
  };
  const throwingName = {
    toString() {
      throw new RangeError("index name conversion");
    },
  };
  const throwingKeyPath = {
    toString() {
      throw new RangeError("keyPath conversion");
    },
  };
  const throwingOptions = {
    get unique() {
      throw new RangeError("index options conversion");
    },
  };

  const request = indexedDB.open(dbname, 1);
  request.onupgradeneeded = () => {
    const db = request.result;
    const store = db.createObjectStore(storeName);

    assert_throws_name(
      "TypeError",
      () => store.createIndex(),
      "createIndex name is required"
    );
    assert_throws_name(
      "TypeError",
      () => store.createIndex("missing-keypath"),
      "createIndex keyPath is required"
    );
    assert_throws_name(
      "TypeError",
      () => store.deleteIndex(),
      "deleteIndex name is required"
    );
    assert_throws_name(
      "TypeError",
      () => store.index(),
      "index name is required"
    );
    assert_throws_name(
      "TypeError",
      () => store.createIndex(Symbol("index"), keyPath),
      "createIndex rejects Symbol name"
    );
    assert_throws_name(
      "TypeError",
      () => store.createIndex("bad-keypath", Symbol("keyPath")),
      "createIndex rejects Symbol keyPath"
    );
    assert_throws_name(
      "TypeError",
      () => store.deleteIndex(Symbol("index")),
      "deleteIndex rejects Symbol name"
    );
    assert_throws_name(
      "TypeError",
      () => store.index(Symbol("index")),
      "index rejects Symbol name"
    );
    assert_throws_name(
      "RangeError",
      () => store.createIndex(throwingName, keyPath),
      "createIndex propagates name stringification errors"
    );
    assert_throws_name(
      "RangeError",
      () => store.createIndex("bad-keypath", throwingKeyPath),
      "createIndex propagates keyPath stringification errors"
    );
    assert_throws_name(
      "RangeError",
      () => store.deleteIndex(throwingName),
      "deleteIndex propagates name stringification errors"
    );
    assert_throws_name(
      "RangeError",
      () => store.index(throwingName),
      "index propagates name stringification errors"
    );
    assert_throws_name(
      "TypeError",
      () => store.createIndex("bad-options", keyPath, Symbol("options")),
      "createIndex rejects non-object options dictionaries"
    );
    assert_throws_name(
      "RangeError",
      () => store.createIndex("throwing-options", keyPath, throwingOptions),
      "createIndex propagates options getter errors"
    );
    assert_throws_name(
      "InvalidAccessError",
      () => store.createIndex("compound-multientry", ["value", "group"], {
        multiEntry: true,
      }),
      "createIndex rejects sequence keyPath with multiEntry"
    );

    const index = store.createIndex(stringifiedIndexName, stringifiedKeyPath);
    assert_equals(index.name, indexName, "index name stringifies through DOMString");
    assert_equals(index.keyPath, keyPath, "string keyPath stringifies through DOMString");
    assert_true(store.indexNames.contains(indexName), "indexNames reflects creation");

    const lookup = store.index(stringifiedIndexName);
    assert_equals(lookup.name, indexName, "index() stringifies lookup name");

    const compoundIndex = store.createIndex("compound-index", ["value", "group"]);
    assert_array_equals(
      compoundIndex.keyPath,
      ["value", "group"],
      "createIndex keyPath accepts sequence conversion"
    );
    store.add({ value: "compound value", group: "a" }, "compound primary");

    const optionIndex = store.createIndex("options-index", "group", {
      unique: { valueOf() { throw new RangeError("unused"); } },
      multiEntry: "yes",
    });
    assert_true(optionIndex.unique, "unique option uses boolean conversion");
    assert_true(optionIndex.multiEntry, "multiEntry option uses boolean conversion");
    assert_true(store.indexNames.contains("options-index"), "options index is visible");

    const undefinedIndex = store.createIndex(undefined, undefined);
    assert_equals(
      undefinedIndex.name,
      "undefined",
      "present undefined converts to a DOMString index name"
    );
    assert_equals(
      undefinedIndex.keyPath,
      "undefined",
      "present undefined converts to a DOMString keyPath"
    );
    assert_true(store.indexNames.contains("undefined"), "undefined index is visible");

    store.deleteIndex(undefined);
    assert_false(
      store.indexNames.contains("undefined"),
      "deleteIndex converts present undefined before deletion"
    );

    store.deleteIndex(stringifiedIndexName);
    assert_false(
      store.indexNames.contains(indexName),
      "deleteIndex converts object name before deletion"
    );

    store.deleteIndex("options-index");
  };

  const db = await wait_request(request);
  const tx = db.transaction(storeName);
  const compoundValue = await wait_request(
    tx.objectStore(storeName).index("compound-index").get(["compound value", "a"])
  );
  assert_equals(
    compoundValue.value,
    "compound value",
    "sequence index keyPath derives compound index keys"
  );
  db.close();
  await wait_request(indexedDB.deleteDatabase(dbname));
}, "IDBObjectStore index schema arguments follow WebIDL conversion");
</script>