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 { finished } = require('stream/promises');
141cb0ef41Sopenharmony_ciconst { buffer } = require('stream/consumers');
151cb0ef41Sopenharmony_ciconst tmpDir = tmpdir.path;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_citmpdir.refresh();
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciasync function validateWrite() {
201cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-write.txt');
211cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle, 'w');
221cb0ef41Sopenharmony_ci  const buffer = Buffer.from('Hello world'.repeat(100), 'utf8');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  const stream = fileHandle.createWriteStream();
251cb0ef41Sopenharmony_ci  stream.end(buffer);
261cb0ef41Sopenharmony_ci  await finished(stream);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const readFileData = fs.readFileSync(filePathForHandle);
291cb0ef41Sopenharmony_ci  assert.deepStrictEqual(buffer, readFileData);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciasync function validateRead() {
331cb0ef41Sopenharmony_ci  const filePathForHandle = path.resolve(tmpDir, 'tmp-read.txt');
341cb0ef41Sopenharmony_ci  const buf = Buffer.from('Hello world'.repeat(100), 'utf8');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  fs.writeFileSync(filePathForHandle, buf);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const fileHandle = await open(filePathForHandle);
391cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
401cb0ef41Sopenharmony_ci    await buffer(fileHandle.createReadStream()),
411cb0ef41Sopenharmony_ci    buf
421cb0ef41Sopenharmony_ci  );
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciPromise.all([
461cb0ef41Sopenharmony_ci  validateWrite(),
471cb0ef41Sopenharmony_ci  validateRead(),
481cb0ef41Sopenharmony_ci]).then(common.mustCall());
49