11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
51cb0ef41Sopenharmony_ciconst { setTimeout } = require('timers/promises');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
81cb0ef41Sopenharmony_ci  const test = require('node:test');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  if (process.argv[3] === 'abortSignal') {
111cb0ef41Sopenharmony_ci    assert.throws(() => test({ signal: {} }), {
121cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
131cb0ef41Sopenharmony_ci      name: 'TypeError'
141cb0ef41Sopenharmony_ci    });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    let testSignal;
171cb0ef41Sopenharmony_ci    test({ timeout: 10 }, common.mustCall(async ({ signal }) => {
181cb0ef41Sopenharmony_ci      assert.strictEqual(signal.aborted, false);
191cb0ef41Sopenharmony_ci      testSignal = signal;
201cb0ef41Sopenharmony_ci      await setTimeout(50);
211cb0ef41Sopenharmony_ci    })).finally(common.mustCall(() => {
221cb0ef41Sopenharmony_ci      test(() => assert.strictEqual(testSignal.aborted, true));
231cb0ef41Sopenharmony_ci    }));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    // TODO(benjamingr) add more tests to describe + AbortSignal
261cb0ef41Sopenharmony_ci    // this just tests the parameter is passed
271cb0ef41Sopenharmony_ci    test.describe('Abort Signal in describe', common.mustCall(({ signal }) => {
281cb0ef41Sopenharmony_ci      test.it('Supports AbortSignal', () => {
291cb0ef41Sopenharmony_ci        assert.strictEqual(signal.aborted, false);
301cb0ef41Sopenharmony_ci      });
311cb0ef41Sopenharmony_ci    }));
321cb0ef41Sopenharmony_ci  } else assert.fail('unreachable');
331cb0ef41Sopenharmony_ci} else {
341cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [__filename, 'child', 'abortSignal']);
351cb0ef41Sopenharmony_ci  const stdout = child.stdout.toString();
361cb0ef41Sopenharmony_ci  assert.match(stdout, /^# pass 2$/m);
371cb0ef41Sopenharmony_ci  assert.match(stdout, /^# fail 0$/m);
381cb0ef41Sopenharmony_ci  assert.match(stdout, /^# cancelled 1$/m);
391cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
401cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
411cb0ef41Sopenharmony_ci}
42