Skip to content

armgit / moli

Public Imported from GitHub
0

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

6.0 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.name, expectedName, description);
    return;
  }
  throw new Error(description || "expected exception");
}

function file_list(files) {
  const transfer = new DataTransfer();
  for (const file of files) {
    transfer.items.add(file);
  }
  return transfer.files;
}

promise_test(async function () {
  const file = new File(["hello ", new Uint8Array([0xe2, 0x99, 0xa5])], "note.txt", {
    type: "TEXT/PLAIN",
    lastModified: 12345,
  });

  assert_true(file instanceof File);
  assert_true(file instanceof Blob);
  assert_equals(file.name, "note.txt");
  assert_equals(file.lastModified, 12345);
  assert_equals(file.type, "text/plain");
  assert_equals(file.size, 9);
  assert_equals(await file.text(), "hello \u2665");
}, "File constructor stores name, lastModified, type, size, and text");

test(function () {
  const file = new File(["hello"], "note.txt");
  const second = new File(["second"], "second.txt");
  const third = new File(["third"], "third.txt");
  assert_throws_name("TypeError", () => new FileList(), "FileList is not constructible");
  const list = file_list([file, second, third]);

  assert_true(list instanceof FileList, "FileList instance");
  assert_equals(list.length, 3, "FileList length");
  assert_equals(list[0], file, "FileList indexed file");
  assert_equals(list.item(0), file, "FileList.item(0)");
  assert_equals(list.item(3), null, "FileList.item() returns null for missing indexes");
  assert_equals(list.item(-1), null, "wrapped negative index should return null when out of range");
  assert_throws_name("TypeError", () => list.item(), "item() should require its index argument");
  assert_equals(list.item(undefined), file, "explicit undefined should convert to zero");
  assert_equals(list.item(null), file, "null should convert to zero");
  assert_equals(list.item("1.9"), second, "string decimals should truncate through unsigned long conversion");
  assert_equals(list.item(NaN), file, "NaN should convert to zero");
  assert_equals(list.item(Infinity), file, "Infinity should convert to zero");
  assert_equals(list.item(-4294967295), second, "negative values should wrap through unsigned long conversion");
  assert_equals(list.item(4294967297), second, "large values should wrap through unsigned long conversion");
  assert_equals(
    list.item({
      valueOf() { return {}; },
      toString() { return "2"; },
    }),
    third,
    "object values should fall back to toString during unsigned long conversion",
  );
  assert_throws_name("Error", () => {
    list.item({ valueOf() { throw new Error("item conversion failed"); } });
  }, "item() should propagate object conversion exceptions");
  assert_throws_name("TypeError", () => list.item(Symbol("index")), "item() should reject Symbol conversion");
  assert_throws_name("TypeError", () => list.item(1n), "item() should reject BigInt conversion");
  assert_equals(
    Object.getOwnPropertyDescriptor(list, "length"),
    undefined,
    "FileList.length should not be installed as an own data property",
  );

  const descriptor = Object.getOwnPropertyDescriptor(FileList.prototype, "length");
  assert_equals(typeof descriptor.get, "function", "FileList.length prototype getter");
  assert_equals(descriptor.set, undefined, "FileList.length should be readonly");
  assert_true(descriptor.enumerable, "FileList.length prototype descriptor should be enumerable");
  assert_true(descriptor.configurable, "FileList.length prototype descriptor should be configurable");
}, "FileList length is a prototype accessor, not an own data property");

promise_test(async function () {
  const file = new File([new Uint8Array([65, 66, 67])], "bytes.bin");
  const bytes = new Uint8Array(await file.arrayBuffer());

  assert_equals(file.name, "bytes.bin");
  assert_equals(file.type, "");
  assert_array_equals(Array.from(bytes), [65, 66, 67]);
}, "File inherits Blob byte reading behavior");

test(function () {
  assert_equals(new File(["x"], "a.txt", undefined).type, "");
  assert_equals(new File(["x"], "a.txt", null).type, "");
  assert_equals(new File(["x"], "a.txt", {}).type, "");
  assert_equals(new File(["x"], "a.txt", { type: undefined }).type, "");
  assert_equals(new File(["x"], "a.txt", { type: null }).type, "null");
  assert_equals(new File(["x"], "a.txt", { type: "TEXT/PLAIN" }).type, "text/plain");
  assert_equals(new File(["x"], "a.txt", { type: "text/plain\u0001bad" }).type, "");
  assert_throws_name("TypeError", function () {
    new File(["x"], "a.txt", { type: Symbol() });
  }, "Symbol type member must fail DOMString conversion");
  assert_throws_name("TypeError", function () {
    new File(["x"], "a.txt", Symbol());
  }, "Symbol options must fail dictionary conversion");
}, "FilePropertyBag.type follows BlobPropertyBag DOMString conversion before MIME normalization");

test(function () {
  assert_throws_name("TypeError", function () {
    new File();
  }, "File constructor should require fileBits");
  assert_throws_name("TypeError", function () {
    new File([]);
  }, "File constructor should require fileName");
  assert_throws_name("TypeError", function () {
    new File([], Symbol("name"));
  }, "File constructor should reject Symbol fileName");

  const file = new File([], null, {
    get lastModified() {
      return "42.9";
    },
  });
  assert_equals(file.name, "null", "fileName uses USVString conversion");
  assert_equals(file.lastModified, 42, "lastModified uses unrestricted double then truncates");

  assert_throws_name("RangeError", function () {
    new File([], "x.txt", {
      get lastModified() {
        throw new RangeError("lastModified conversion");
      },
    });
  }, "File constructor should propagate lastModified getter errors");
}, "File constructor required arguments and FilePropertyBag members follow WebIDL conversion");
</script>