11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=../resources/recording-streams.js
31cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js
41cb0ef41Sopenharmony_ci'use strict';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cipromise_test(t => {
71cb0ef41Sopenharmony_ci  const ts = recordingTransformStream({}, undefined, { highWaterMark: 0 });
81cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
91cb0ef41Sopenharmony_ci    start(controller) {
101cb0ef41Sopenharmony_ci      controller.enqueue(0);
111cb0ef41Sopenharmony_ci    }
121cb0ef41Sopenharmony_ci  });
131cb0ef41Sopenharmony_ci  let pipeToRejected = false;
141cb0ef41Sopenharmony_ci  const pipeToPromise = promise_rejects_js(t, TypeError, rs.pipeTo(ts.writable), 'pipeTo should reject').then(() => {
151cb0ef41Sopenharmony_ci    pipeToRejected = true;
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci  return delay(0).then(() => {
181cb0ef41Sopenharmony_ci    assert_array_equals(ts.events, [], 'transform() should have seen no chunks');
191cb0ef41Sopenharmony_ci    assert_false(pipeToRejected, 'pipeTo() should not have rejected yet');
201cb0ef41Sopenharmony_ci    ts.controller.terminate();
211cb0ef41Sopenharmony_ci    return pipeToPromise;
221cb0ef41Sopenharmony_ci  }).then(() => {
231cb0ef41Sopenharmony_ci    assert_array_equals(ts.events, [], 'transform() should still have seen no chunks');
241cb0ef41Sopenharmony_ci    assert_true(pipeToRejected, 'pipeToRejected must be true');
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci}, 'controller.terminate() should error pipeTo()');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cipromise_test(t => {
291cb0ef41Sopenharmony_ci  const ts = recordingTransformStream({}, undefined, { highWaterMark: 1 });
301cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
311cb0ef41Sopenharmony_ci    start(controller) {
321cb0ef41Sopenharmony_ci      controller.enqueue(0);
331cb0ef41Sopenharmony_ci      controller.enqueue(1);
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  const pipeToPromise = rs.pipeTo(ts.writable);
371cb0ef41Sopenharmony_ci  return delay(0).then(() => {
381cb0ef41Sopenharmony_ci    assert_array_equals(ts.events, ['transform', 0], 'transform() should have seen one chunk');
391cb0ef41Sopenharmony_ci    ts.controller.terminate();
401cb0ef41Sopenharmony_ci    return promise_rejects_js(t, TypeError, pipeToPromise, 'pipeTo() should reject');
411cb0ef41Sopenharmony_ci  }).then(() => {
421cb0ef41Sopenharmony_ci    assert_array_equals(ts.events, ['transform', 0], 'transform() should still have seen only one chunk');
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci}, 'controller.terminate() should prevent remaining chunks from being processed');
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_citest(() => {
471cb0ef41Sopenharmony_ci  new TransformStream({
481cb0ef41Sopenharmony_ci    start(controller) {
491cb0ef41Sopenharmony_ci      controller.enqueue(0);
501cb0ef41Sopenharmony_ci      controller.terminate();
511cb0ef41Sopenharmony_ci      assert_throws_js(TypeError, () => controller.enqueue(1), 'enqueue should throw');
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci}, 'controller.enqueue() should throw after controller.terminate()');
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciconst error1 = new Error('error1');
571cb0ef41Sopenharmony_cierror1.name = 'error1';
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cipromise_test(t => {
601cb0ef41Sopenharmony_ci  const ts = new TransformStream({
611cb0ef41Sopenharmony_ci    start(controller) {
621cb0ef41Sopenharmony_ci      controller.enqueue(0);
631cb0ef41Sopenharmony_ci      controller.terminate();
641cb0ef41Sopenharmony_ci      controller.error(error1);
651cb0ef41Sopenharmony_ci    }
661cb0ef41Sopenharmony_ci  });
671cb0ef41Sopenharmony_ci  return Promise.all([
681cb0ef41Sopenharmony_ci    promise_rejects_js(t, TypeError, ts.writable.abort(), 'abort() should reject with a TypeError'),
691cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, ts.readable.cancel(), 'cancel() should reject with error1'),
701cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, ts.readable.getReader().closed, 'closed should reject with error1')
711cb0ef41Sopenharmony_ci  ]);
721cb0ef41Sopenharmony_ci}, 'controller.error() after controller.terminate() with queued chunk should error the readable');
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cipromise_test(t => {
751cb0ef41Sopenharmony_ci  const ts = new TransformStream({
761cb0ef41Sopenharmony_ci    start(controller) {
771cb0ef41Sopenharmony_ci      controller.terminate();
781cb0ef41Sopenharmony_ci      controller.error(error1);
791cb0ef41Sopenharmony_ci    }
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci  return Promise.all([
821cb0ef41Sopenharmony_ci    promise_rejects_js(t, TypeError, ts.writable.abort(), 'abort() should reject with a TypeError'),
831cb0ef41Sopenharmony_ci    ts.readable.cancel(),
841cb0ef41Sopenharmony_ci    ts.readable.getReader().closed
851cb0ef41Sopenharmony_ci  ]);
861cb0ef41Sopenharmony_ci}, 'controller.error() after controller.terminate() without queued chunk should do nothing');
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cipromise_test(() => {
891cb0ef41Sopenharmony_ci  const ts = new TransformStream({
901cb0ef41Sopenharmony_ci    flush(controller) {
911cb0ef41Sopenharmony_ci      controller.terminate();
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci  const writer = ts.writable.getWriter();
951cb0ef41Sopenharmony_ci  return Promise.all([
961cb0ef41Sopenharmony_ci    writer.close(),
971cb0ef41Sopenharmony_ci    writer.closed,
981cb0ef41Sopenharmony_ci    ts.readable.getReader().closed
991cb0ef41Sopenharmony_ci  ]);
1001cb0ef41Sopenharmony_ci}, 'controller.terminate() inside flush() should not prevent writer.close() from succeeding');
101