11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst { Writable } = require('stream');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction expectError(w, args, code, sync) {
81cb0ef41Sopenharmony_ci  if (sync) {
91cb0ef41Sopenharmony_ci    if (code) {
101cb0ef41Sopenharmony_ci      assert.throws(() => w.write(...args), { code });
111cb0ef41Sopenharmony_ci    } else {
121cb0ef41Sopenharmony_ci      w.write(...args);
131cb0ef41Sopenharmony_ci    }
141cb0ef41Sopenharmony_ci  } else {
151cb0ef41Sopenharmony_ci    let errorCalled = false;
161cb0ef41Sopenharmony_ci    let ticked = false;
171cb0ef41Sopenharmony_ci    w.write(...args, common.mustCall((err) => {
181cb0ef41Sopenharmony_ci      assert.strictEqual(ticked, true);
191cb0ef41Sopenharmony_ci      assert.strictEqual(errorCalled, false);
201cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, code);
211cb0ef41Sopenharmony_ci    }));
221cb0ef41Sopenharmony_ci    ticked = true;
231cb0ef41Sopenharmony_ci    w.on('error', common.mustCall((err) => {
241cb0ef41Sopenharmony_ci      errorCalled = true;
251cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, code);
261cb0ef41Sopenharmony_ci    }));
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction test(autoDestroy) {
311cb0ef41Sopenharmony_ci  {
321cb0ef41Sopenharmony_ci    const w = new Writable({
331cb0ef41Sopenharmony_ci      autoDestroy,
341cb0ef41Sopenharmony_ci      _write() {}
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci    w.end();
371cb0ef41Sopenharmony_ci    expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END');
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  {
411cb0ef41Sopenharmony_ci    const w = new Writable({
421cb0ef41Sopenharmony_ci      autoDestroy,
431cb0ef41Sopenharmony_ci      _write() {}
441cb0ef41Sopenharmony_ci    });
451cb0ef41Sopenharmony_ci    w.destroy();
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  {
491cb0ef41Sopenharmony_ci    const w = new Writable({
501cb0ef41Sopenharmony_ci      autoDestroy,
511cb0ef41Sopenharmony_ci      _write() {}
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci    expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true);
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  {
571cb0ef41Sopenharmony_ci    const w = new Writable({
581cb0ef41Sopenharmony_ci      autoDestroy,
591cb0ef41Sopenharmony_ci      _write() {}
601cb0ef41Sopenharmony_ci    });
611cb0ef41Sopenharmony_ci    expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true);
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  {
651cb0ef41Sopenharmony_ci    const w = new Writable({
661cb0ef41Sopenharmony_ci      decodeStrings: false,
671cb0ef41Sopenharmony_ci      autoDestroy,
681cb0ef41Sopenharmony_ci      _write() {}
691cb0ef41Sopenharmony_ci    });
701cb0ef41Sopenharmony_ci    expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_citest(false);
751cb0ef41Sopenharmony_citest(true);
76