11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst error1 = new Error('a unique string');
51cb0ef41Sopenharmony_cierror1.name = 'error1';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_citest(() => {
81cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => {
91cb0ef41Sopenharmony_ci    new WritableStream({}, {
101cb0ef41Sopenharmony_ci      get size() {
111cb0ef41Sopenharmony_ci        throw error1;
121cb0ef41Sopenharmony_ci      },
131cb0ef41Sopenharmony_ci      highWaterMark: 5
141cb0ef41Sopenharmony_ci    });
151cb0ef41Sopenharmony_ci  }, 'construction should re-throw the error');
161cb0ef41Sopenharmony_ci}, 'Writable stream: throwing strategy.size getter');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_citest(() => {
191cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => {
201cb0ef41Sopenharmony_ci    new WritableStream({}, { size: 'a string' });
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci}, 'reject any non-function value for strategy.size');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_citest(() => {
251cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => {
261cb0ef41Sopenharmony_ci    new WritableStream({}, {
271cb0ef41Sopenharmony_ci      size() {
281cb0ef41Sopenharmony_ci        return 1;
291cb0ef41Sopenharmony_ci      },
301cb0ef41Sopenharmony_ci      get highWaterMark() {
311cb0ef41Sopenharmony_ci        throw error1;
321cb0ef41Sopenharmony_ci      }
331cb0ef41Sopenharmony_ci    });
341cb0ef41Sopenharmony_ci  }, 'construction should re-throw the error');
351cb0ef41Sopenharmony_ci}, 'Writable stream: throwing strategy.highWaterMark getter');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_citest(() => {
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  for (const highWaterMark of [-1, -Infinity, NaN, 'foo', {}]) {
401cb0ef41Sopenharmony_ci    assert_throws_js(RangeError, () => {
411cb0ef41Sopenharmony_ci      new WritableStream({}, {
421cb0ef41Sopenharmony_ci        size() {
431cb0ef41Sopenharmony_ci          return 1;
441cb0ef41Sopenharmony_ci        },
451cb0ef41Sopenharmony_ci        highWaterMark
461cb0ef41Sopenharmony_ci      });
471cb0ef41Sopenharmony_ci    }, `construction should throw a RangeError for ${highWaterMark}`);
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci}, 'Writable stream: invalid strategy.highWaterMark');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cipromise_test(t => {
521cb0ef41Sopenharmony_ci  const ws = new WritableStream({}, {
531cb0ef41Sopenharmony_ci    size() {
541cb0ef41Sopenharmony_ci      throw error1;
551cb0ef41Sopenharmony_ci    },
561cb0ef41Sopenharmony_ci    highWaterMark: 5
571cb0ef41Sopenharmony_ci  });
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  const p1 = promise_rejects_exactly(t, error1, writer.write('a'), 'write should reject with the thrown error');
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const p2 = promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with the thrown error');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  return Promise.all([p1, p2]);
661cb0ef41Sopenharmony_ci}, 'Writable stream: throwing strategy.size method');
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cipromise_test(() => {
691cb0ef41Sopenharmony_ci  const sizes = [NaN, -Infinity, Infinity, -1];
701cb0ef41Sopenharmony_ci  return Promise.all(sizes.map(size => {
711cb0ef41Sopenharmony_ci    const ws = new WritableStream({}, {
721cb0ef41Sopenharmony_ci      size() {
731cb0ef41Sopenharmony_ci        return size;
741cb0ef41Sopenharmony_ci      },
751cb0ef41Sopenharmony_ci      highWaterMark: 5
761cb0ef41Sopenharmony_ci    });
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    return writer.write('a').then(() => assert_unreached('write must reject'), writeE => {
811cb0ef41Sopenharmony_ci      assert_equals(writeE.name, 'RangeError', `write must reject with a RangeError for ${size}`);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci      return writer.closed.then(() => assert_unreached('write must reject'), closedE => {
841cb0ef41Sopenharmony_ci        assert_equals(closedE, writeE, `closed should reject with the same error as write`);
851cb0ef41Sopenharmony_ci      });
861cb0ef41Sopenharmony_ci    });
871cb0ef41Sopenharmony_ci  }));
881cb0ef41Sopenharmony_ci}, 'Writable stream: invalid strategy.size return value');
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_citest(() => {
911cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new WritableStream(undefined, {
921cb0ef41Sopenharmony_ci    size: 'not a function',
931cb0ef41Sopenharmony_ci    highWaterMark: NaN
941cb0ef41Sopenharmony_ci  }), 'WritableStream constructor should throw a TypeError');
951cb0ef41Sopenharmony_ci}, 'Writable stream: invalid size beats invalid highWaterMark');
96