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_cifunction interceptThen() {
71cb0ef41Sopenharmony_ci  const intercepted = [];
81cb0ef41Sopenharmony_ci  let callCount = 0;
91cb0ef41Sopenharmony_ci  Object.prototype.then = function(resolver) {
101cb0ef41Sopenharmony_ci    if (!this.done) {
111cb0ef41Sopenharmony_ci      intercepted.push(this.value);
121cb0ef41Sopenharmony_ci    }
131cb0ef41Sopenharmony_ci    const retval = Object.create(null);
141cb0ef41Sopenharmony_ci    retval.done = ++callCount === 3;
151cb0ef41Sopenharmony_ci    retval.value = callCount;
161cb0ef41Sopenharmony_ci    resolver(retval);
171cb0ef41Sopenharmony_ci    if (retval.done) {
181cb0ef41Sopenharmony_ci      delete Object.prototype.then;
191cb0ef41Sopenharmony_ci    }
201cb0ef41Sopenharmony_ci  }
211cb0ef41Sopenharmony_ci  return intercepted;
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cipromise_test(async t => {
251cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
261cb0ef41Sopenharmony_ci    start(controller) {
271cb0ef41Sopenharmony_ci      controller.enqueue('a');
281cb0ef41Sopenharmony_ci      controller.close();
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const intercepted = interceptThen();
341cb0ef41Sopenharmony_ci  t.add_cleanup(() => {
351cb0ef41Sopenharmony_ci    delete Object.prototype.then;
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  await rs.pipeTo(ws);
391cb0ef41Sopenharmony_ci  delete Object.prototype.then;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  assert_array_equals(intercepted, [], 'nothing should have been intercepted');
431cb0ef41Sopenharmony_ci  assert_array_equals(ws.events, ['write', 'a', 'close'], 'written chunk should be "a"');
441cb0ef41Sopenharmony_ci}, 'piping should not be observable');
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cipromise_test(async t => {
471cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
481cb0ef41Sopenharmony_ci    start(controller) {
491cb0ef41Sopenharmony_ci      controller.enqueue('a');
501cb0ef41Sopenharmony_ci      controller.close();
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const [ branch1, branch2 ] = rs.tee();
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const intercepted = interceptThen();
581cb0ef41Sopenharmony_ci  t.add_cleanup(() => {
591cb0ef41Sopenharmony_ci    delete Object.prototype.then;
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  await branch1.pipeTo(ws);
631cb0ef41Sopenharmony_ci  delete Object.prototype.then;
641cb0ef41Sopenharmony_ci  branch2.cancel();
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  assert_array_equals(intercepted, [], 'nothing should have been intercepted');
671cb0ef41Sopenharmony_ci  assert_array_equals(ws.events, ['write', 'a', 'close'], 'written chunk should be "a"');
681cb0ef41Sopenharmony_ci}, 'tee should not be observable');
69