11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { Blob, File } = require('buffer');
61cb0ef41Sopenharmony_ciconst { inspect } = require('util');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  // ensure File extends Blob
101cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.getPrototypeOf(File.prototype), Blob.prototype);
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  assert.throws(() => new File(), TypeError);
151cb0ef41Sopenharmony_ci  assert.throws(() => new File([]), TypeError);
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci{
191cb0ef41Sopenharmony_ci  const properties = ['name', 'lastModified'];
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  for (const prop of properties) {
221cb0ef41Sopenharmony_ci    const desc = Object.getOwnPropertyDescriptor(File.prototype, prop);
231cb0ef41Sopenharmony_ci    assert.notStrictEqual(desc, undefined);
241cb0ef41Sopenharmony_ci    // Ensure these properties are getters.
251cb0ef41Sopenharmony_ci    assert.strictEqual(desc.get?.name, `get ${prop}`);
261cb0ef41Sopenharmony_ci    assert.strictEqual(desc.set, undefined);
271cb0ef41Sopenharmony_ci    assert.strictEqual(desc.enumerable, true);
281cb0ef41Sopenharmony_ci    assert.strictEqual(desc.configurable, true);
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci{
331cb0ef41Sopenharmony_ci  const file = new File([], '');
341cb0ef41Sopenharmony_ci  assert.strictEqual(file[Symbol.toStringTag], 'File');
351cb0ef41Sopenharmony_ci  assert.strictEqual(File.prototype[Symbol.toStringTag], 'File');
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  assert.throws(() => File.prototype.name, TypeError);
401cb0ef41Sopenharmony_ci  assert.throws(() => File.prototype.lastModified, TypeError);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci{
441cb0ef41Sopenharmony_ci  const keys = Object.keys(File.prototype).sort();
451cb0ef41Sopenharmony_ci  assert.deepStrictEqual(keys, ['lastModified', 'name']);
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci  const file = new File([], 'dummy.txt.exe');
501cb0ef41Sopenharmony_ci  assert.strictEqual(file.name, 'dummy.txt.exe');
511cb0ef41Sopenharmony_ci  assert.strictEqual(file.size, 0);
521cb0ef41Sopenharmony_ci  assert.strictEqual(typeof file.lastModified, 'number');
531cb0ef41Sopenharmony_ci  assert(file.lastModified <= Date.now());
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci{
571cb0ef41Sopenharmony_ci  const emptyFile = new File([], 'empty.txt');
581cb0ef41Sopenharmony_ci  const blob = new Blob(['hello world']);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  emptyFile.text.call(blob).then(common.mustCall((text) => {
611cb0ef41Sopenharmony_ci    assert.strictEqual(text, 'hello world');
621cb0ef41Sopenharmony_ci  }));
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  const toPrimitive = {
671cb0ef41Sopenharmony_ci    [Symbol.toPrimitive]() {
681cb0ef41Sopenharmony_ci      return 'NaN';
691cb0ef41Sopenharmony_ci    }
701cb0ef41Sopenharmony_ci  };
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  const invalidLastModified = [
731cb0ef41Sopenharmony_ci    null,
741cb0ef41Sopenharmony_ci    'string',
751cb0ef41Sopenharmony_ci    false,
761cb0ef41Sopenharmony_ci    toPrimitive,
771cb0ef41Sopenharmony_ci  ];
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  for (const lastModified of invalidLastModified) {
801cb0ef41Sopenharmony_ci    const file = new File([], '', { lastModified });
811cb0ef41Sopenharmony_ci    assert.strictEqual(file.lastModified, 0);
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci{
861cb0ef41Sopenharmony_ci  const file = new File([], '', { lastModified: undefined });
871cb0ef41Sopenharmony_ci  assert.notStrictEqual(file.lastModified, 0);
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci  const toPrimitive = {
921cb0ef41Sopenharmony_ci    [Symbol.toPrimitive]() {
931cb0ef41Sopenharmony_ci      throw new TypeError('boom');
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci  };
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  const throwValues = [
981cb0ef41Sopenharmony_ci    BigInt(3n),
991cb0ef41Sopenharmony_ci    toPrimitive,
1001cb0ef41Sopenharmony_ci  ];
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  for (const lastModified of throwValues) {
1031cb0ef41Sopenharmony_ci    assert.throws(() => new File([], '', { lastModified }), TypeError);
1041cb0ef41Sopenharmony_ci  }
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  const valid = [
1091cb0ef41Sopenharmony_ci    {
1101cb0ef41Sopenharmony_ci      [Symbol.toPrimitive]() {
1111cb0ef41Sopenharmony_ci        return 10;
1121cb0ef41Sopenharmony_ci      }
1131cb0ef41Sopenharmony_ci    },
1141cb0ef41Sopenharmony_ci    new Number(10),
1151cb0ef41Sopenharmony_ci    10,
1161cb0ef41Sopenharmony_ci  ];
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  for (const lastModified of valid) {
1191cb0ef41Sopenharmony_ci    assert.strictEqual(new File([], '', { lastModified }).lastModified, 10);
1201cb0ef41Sopenharmony_ci  }
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci{
1241cb0ef41Sopenharmony_ci  const file = new File([], '');
1251cb0ef41Sopenharmony_ci  assert(inspect(file).startsWith('File { size: 0, type: \'\', name: \'\', lastModified:'));
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci{
1291cb0ef41Sopenharmony_ci  function MyClass() {}
1301cb0ef41Sopenharmony_ci  MyClass.prototype.lastModified = 10;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  const file = new File([], '', new MyClass());
1331cb0ef41Sopenharmony_ci  assert.strictEqual(file.lastModified, 10);
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci{
1371cb0ef41Sopenharmony_ci  let counter = 0;
1381cb0ef41Sopenharmony_ci  new File([], '', {
1391cb0ef41Sopenharmony_ci    get lastModified() {
1401cb0ef41Sopenharmony_ci      counter++;
1411cb0ef41Sopenharmony_ci      return 10;
1421cb0ef41Sopenharmony_ci    }
1431cb0ef41Sopenharmony_ci  });
1441cb0ef41Sopenharmony_ci  assert.strictEqual(counter, 1);
1451cb0ef41Sopenharmony_ci}
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci{
1481cb0ef41Sopenharmony_ci  const getter = Object.getOwnPropertyDescriptor(File.prototype, 'name').get;
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  [
1511cb0ef41Sopenharmony_ci    undefined,
1521cb0ef41Sopenharmony_ci    null,
1531cb0ef41Sopenharmony_ci    true,
1541cb0ef41Sopenharmony_ci  ].forEach((invalidThis) => {
1551cb0ef41Sopenharmony_ci    assert.throws(
1561cb0ef41Sopenharmony_ci      () => getter.call(invalidThis),
1571cb0ef41Sopenharmony_ci      TypeError
1581cb0ef41Sopenharmony_ci    );
1591cb0ef41Sopenharmony_ci  });
1601cb0ef41Sopenharmony_ci}
161