11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// The following tests validate base functionality for the fs.promises
61cb0ef41Sopenharmony_ci// FileHandle.write method.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst { open } = fs.promises;
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_ciconst assert = require('assert');
131cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citmpdir.refresh();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciasync function validateWrite() {
181cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
191cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle, 'w+');
201cb0ef41Sopenharmony_ci  const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  await fileHandle.write(buffer, 0, buffer.length);
231cb0ef41Sopenharmony_ci  const readFileData = fs.readFileSync(filePathForHandle);
241cb0ef41Sopenharmony_ci  assert.deepStrictEqual(buffer, readFileData);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  await fileHandle.close();
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciasync function validateEmptyWrite() {
301cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-empty-write.txt');
311cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle, 'w+');
321cb0ef41Sopenharmony_ci  const buffer = Buffer.from(''); // empty buffer
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  await fileHandle.write(buffer, 0, buffer.length);
351cb0ef41Sopenharmony_ci  const readFileData = fs.readFileSync(filePathForHandle);
361cb0ef41Sopenharmony_ci  assert.deepStrictEqual(buffer, readFileData);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  await fileHandle.close();
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciasync function validateNonUint8ArrayWrite() {
421cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-data-write.txt');
431cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle, 'w+');
441cb0ef41Sopenharmony_ci  const buffer = Buffer.from('Hello world', 'utf8').toString('base64');
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  await fileHandle.write(buffer, 0, buffer.length);
471cb0ef41Sopenharmony_ci  const readFileData = fs.readFileSync(filePathForHandle);
481cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Buffer.from(buffer, 'utf8'), readFileData);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  await fileHandle.close();
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciasync function validateNonStringValuesWrite() {
541cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-non-string-write.txt');
551cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle, 'w+');
561cb0ef41Sopenharmony_ci  const nonStringValues = [
571cb0ef41Sopenharmony_ci    123, {}, new Map(), null, undefined, 0n, () => {}, Symbol(), true,
581cb0ef41Sopenharmony_ci    new String('notPrimitive'),
591cb0ef41Sopenharmony_ci    { toString() { return 'amObject'; } },
601cb0ef41Sopenharmony_ci    { [Symbol.toPrimitive]: (hint) => 'amObject' },
611cb0ef41Sopenharmony_ci  ];
621cb0ef41Sopenharmony_ci  for (const nonStringValue of nonStringValues) {
631cb0ef41Sopenharmony_ci    await assert.rejects(
641cb0ef41Sopenharmony_ci      fileHandle.write(nonStringValue),
651cb0ef41Sopenharmony_ci      { message: /"buffer"/, code: 'ERR_INVALID_ARG_TYPE' }
661cb0ef41Sopenharmony_ci    );
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  await fileHandle.close();
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciPromise.all([
731cb0ef41Sopenharmony_ci  validateWrite(),
741cb0ef41Sopenharmony_ci  validateEmptyWrite(),
751cb0ef41Sopenharmony_ci  validateNonUint8ArrayWrite(),
761cb0ef41Sopenharmony_ci  validateNonStringValuesWrite(),
771cb0ef41Sopenharmony_ci]).then(common.mustCall());
78