11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js
31cb0ef41Sopenharmony_ci// META: script=../resources/recording-streams.js
41cb0ef41Sopenharmony_ci'use strict';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst error1 = new Error('error1');
71cb0ef41Sopenharmony_cierror1.name = 'error1';
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_citest(() => {
101cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => {
111cb0ef41Sopenharmony_ci    new WritableStream({
121cb0ef41Sopenharmony_ci      get start() {
131cb0ef41Sopenharmony_ci        throw error1;
141cb0ef41Sopenharmony_ci      }
151cb0ef41Sopenharmony_ci    });
161cb0ef41Sopenharmony_ci  }, 'constructor should throw same error as throwing start getter');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => {
191cb0ef41Sopenharmony_ci    new WritableStream({
201cb0ef41Sopenharmony_ci      start() {
211cb0ef41Sopenharmony_ci        throw error1;
221cb0ef41Sopenharmony_ci      }
231cb0ef41Sopenharmony_ci    });
241cb0ef41Sopenharmony_ci  }, 'constructor should throw same error as throwing start method');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => {
271cb0ef41Sopenharmony_ci    new WritableStream({
281cb0ef41Sopenharmony_ci      start: 'not a function or undefined'
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci  }, 'constructor should throw TypeError when passed a non-function start property');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => {
331cb0ef41Sopenharmony_ci    new WritableStream({
341cb0ef41Sopenharmony_ci      start: { apply() {} }
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci  }, 'constructor should throw TypeError when passed a non-function start property with an .apply method');
371cb0ef41Sopenharmony_ci}, 'start: errors in start cause WritableStream constructor to throw');
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cipromise_test(t => {
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
421cb0ef41Sopenharmony_ci    close() {
431cb0ef41Sopenharmony_ci      throw error1;
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.close(), 'close() promise must reject with the thrown error')
501cb0ef41Sopenharmony_ci  .then(() => promise_rejects_exactly(t, error1, writer.ready, 'ready promise must reject with the thrown error'))
511cb0ef41Sopenharmony_ci  .then(() => promise_rejects_exactly(t, error1, writer.closed, 'closed promise must reject with the thrown error'))
521cb0ef41Sopenharmony_ci  .then(() => {
531cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['close']);
541cb0ef41Sopenharmony_ci  });
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci}, 'close: throwing method should cause writer close() and ready to reject');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cipromise_test(t => {
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
611cb0ef41Sopenharmony_ci    close() {
621cb0ef41Sopenharmony_ci      return Promise.reject(error1);
631cb0ef41Sopenharmony_ci    }
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.close(), 'close() promise must reject with the same error')
691cb0ef41Sopenharmony_ci  .then(() => promise_rejects_exactly(t, error1, writer.ready, 'ready promise must reject with the same error'))
701cb0ef41Sopenharmony_ci  .then(() => assert_array_equals(ws.events, ['close']));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci}, 'close: returning a rejected promise should cause writer close() and ready to reject');
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_citest(() => {
751cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => new WritableStream({
761cb0ef41Sopenharmony_ci    get close() {
771cb0ef41Sopenharmony_ci      throw error1;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci  }), 'constructor should throw');
801cb0ef41Sopenharmony_ci}, 'close: throwing getter should cause constructor to throw');
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_citest(() => {
831cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => new WritableStream({
841cb0ef41Sopenharmony_ci    get write() {
851cb0ef41Sopenharmony_ci      throw error1;
861cb0ef41Sopenharmony_ci    }
871cb0ef41Sopenharmony_ci  }), 'constructor should throw');
881cb0ef41Sopenharmony_ci}, 'write: throwing getter should cause write() and closed to reject');
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_cipromise_test(t => {
911cb0ef41Sopenharmony_ci  const ws = new WritableStream({
921cb0ef41Sopenharmony_ci    write() {
931cb0ef41Sopenharmony_ci      throw error1;
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci  });
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.write('a'), 'write should reject with the thrown error')
1001cb0ef41Sopenharmony_ci  .then(() => promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with the thrown error'));
1011cb0ef41Sopenharmony_ci}, 'write: throwing method should cause write() and closed to reject');
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_cipromise_test(t => {
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  let rejectSinkWritePromise;
1061cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
1071cb0ef41Sopenharmony_ci    write() {
1081cb0ef41Sopenharmony_ci      return new Promise((r, reject) => {
1091cb0ef41Sopenharmony_ci        rejectSinkWritePromise = reject;
1101cb0ef41Sopenharmony_ci      });
1111cb0ef41Sopenharmony_ci    }
1121cb0ef41Sopenharmony_ci  });
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
1151cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
1161cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
1171cb0ef41Sopenharmony_ci    rejectSinkWritePromise(error1);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    return Promise.all([
1201cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise, 'writer write must reject with the same error'),
1211cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready, 'ready promise must reject with the same error')
1221cb0ef41Sopenharmony_ci    ]);
1231cb0ef41Sopenharmony_ci  })
1241cb0ef41Sopenharmony_ci  .then(() => {
1251cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['write', 'a']);
1261cb0ef41Sopenharmony_ci  });
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci}, 'write: returning a promise that becomes rejected after the writer write() should cause writer write() and ready ' +
1291cb0ef41Sopenharmony_ci   'to reject');
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_cipromise_test(t => {
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
1341cb0ef41Sopenharmony_ci    write() {
1351cb0ef41Sopenharmony_ci      if (ws.events.length === 2) {
1361cb0ef41Sopenharmony_ci        return delay(0);
1371cb0ef41Sopenharmony_ci      }
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci      return Promise.reject(error1);
1401cb0ef41Sopenharmony_ci    }
1411cb0ef41Sopenharmony_ci  });
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  // Do not wait for this; we want to test the ready promise when the stream is "full" (desiredSize = 0), but if we wait
1461cb0ef41Sopenharmony_ci  // then the stream will transition back to "empty" (desiredSize = 1)
1471cb0ef41Sopenharmony_ci  writer.write('a');
1481cb0ef41Sopenharmony_ci  const readyPromise = writer.ready;
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.write('b'), 'second write must reject with the same error').then(() => {
1511cb0ef41Sopenharmony_ci    assert_equals(writer.ready, readyPromise,
1521cb0ef41Sopenharmony_ci      'the ready promise must not change, since the queue was full after the first write, so the pending one simply ' +
1531cb0ef41Sopenharmony_ci      'transitioned');
1541cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, writer.ready, 'ready promise must reject with the same error');
1551cb0ef41Sopenharmony_ci  })
1561cb0ef41Sopenharmony_ci  .then(() => assert_array_equals(ws.events, ['write', 'a', 'write', 'b']));
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci}, 'write: returning a rejected promise (second write) should cause writer write() and ready to reject');
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_citest(() => {
1611cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new WritableStream({
1621cb0ef41Sopenharmony_ci    start: 'test'
1631cb0ef41Sopenharmony_ci  }), 'constructor should throw');
1641cb0ef41Sopenharmony_ci}, 'start: non-function start method');
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_citest(() => {
1671cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new WritableStream({
1681cb0ef41Sopenharmony_ci    write: 'test'
1691cb0ef41Sopenharmony_ci  }), 'constructor should throw');
1701cb0ef41Sopenharmony_ci}, 'write: non-function write method');
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_citest(() => {
1731cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new WritableStream({
1741cb0ef41Sopenharmony_ci    close: 'test'
1751cb0ef41Sopenharmony_ci  }), 'constructor should throw');
1761cb0ef41Sopenharmony_ci}, 'close: non-function close method');
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_citest(() => {
1791cb0ef41Sopenharmony_ci  assert_throws_js(TypeError, () => new WritableStream({
1801cb0ef41Sopenharmony_ci    abort: { apply() {} }
1811cb0ef41Sopenharmony_ci  }), 'constructor should throw');
1821cb0ef41Sopenharmony_ci}, 'abort: non-function abort method with .apply');
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_citest(() => {
1851cb0ef41Sopenharmony_ci  assert_throws_exactly(error1, () => new WritableStream({
1861cb0ef41Sopenharmony_ci    get abort() {
1871cb0ef41Sopenharmony_ci      throw error1;
1881cb0ef41Sopenharmony_ci    }
1891cb0ef41Sopenharmony_ci  }), 'constructor should throw');
1901cb0ef41Sopenharmony_ci}, 'abort: throwing getter should cause abort() and closed to reject');
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_cipromise_test(t => {
1931cb0ef41Sopenharmony_ci  const abortReason = new Error('different string');
1941cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1951cb0ef41Sopenharmony_ci    abort() {
1961cb0ef41Sopenharmony_ci      throw error1;
1971cb0ef41Sopenharmony_ci    }
1981cb0ef41Sopenharmony_ci  });
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.abort(abortReason), 'abort should reject with the thrown error')
2031cb0ef41Sopenharmony_ci  .then(() => promise_rejects_exactly(t, abortReason, writer.closed, 'closed should reject with abortReason'));
2041cb0ef41Sopenharmony_ci}, 'abort: throwing method should cause abort() and closed to reject');
205