11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs';
31cb0ef41Sopenharmony_ciimport assert from 'node:assert';
41cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
51cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cidescribe('Worker threads do not spawn infinitely', { concurrency: true }, () => {
81cb0ef41Sopenharmony_ci  it('should not trigger an infinite loop when using a loader exports no recognized hooks', async () => {
91cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
101cb0ef41Sopenharmony_ci      '--no-warnings',
111cb0ef41Sopenharmony_ci      '--experimental-loader',
121cb0ef41Sopenharmony_ci      fixtures.fileURL('empty.js'),
131cb0ef41Sopenharmony_ci      '--eval',
141cb0ef41Sopenharmony_ci      'setTimeout(() => console.log("hello"),99)',
151cb0ef41Sopenharmony_ci    ]);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
181cb0ef41Sopenharmony_ci    assert.match(stdout, /^hello\r?\n$/);
191cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
201cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  it('should support a CommonJS entry point and a loader that imports a CommonJS module', async () => {
241cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
251cb0ef41Sopenharmony_ci      '--no-warnings',
261cb0ef41Sopenharmony_ci      '--experimental-loader',
271cb0ef41Sopenharmony_ci      fixtures.fileURL('es-module-loaders/loader-with-dep.mjs'),
281cb0ef41Sopenharmony_ci      fixtures.path('print-delayed.js'),
291cb0ef41Sopenharmony_ci    ]);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
321cb0ef41Sopenharmony_ci    assert.match(stdout, /^delayed\r?\n$/);
331cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
341cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  it('should support --require and --import along with using a loader written in CJS and CJS entry point', async () => {
381cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
391cb0ef41Sopenharmony_ci      '--no-warnings',
401cb0ef41Sopenharmony_ci      '--eval',
411cb0ef41Sopenharmony_ci      'setTimeout(() => console.log("D"),99)',
421cb0ef41Sopenharmony_ci      '--import',
431cb0ef41Sopenharmony_ci      fixtures.fileURL('printC.js'),
441cb0ef41Sopenharmony_ci      '--experimental-loader',
451cb0ef41Sopenharmony_ci      fixtures.fileURL('printB.js'),
461cb0ef41Sopenharmony_ci      '--require',
471cb0ef41Sopenharmony_ci      fixtures.path('printA.js'),
481cb0ef41Sopenharmony_ci    ]);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
511cb0ef41Sopenharmony_ci    // We are validating that:
521cb0ef41Sopenharmony_ci    // 1. the `--require` flag is run first from the main thread (and A is printed).
531cb0ef41Sopenharmony_ci    // 2. the `--require` flag is then run on the loader thread (and A is printed).
541cb0ef41Sopenharmony_ci    // 3. the `--loader` module is executed (and B is printed).
551cb0ef41Sopenharmony_ci    // 4. the `--import` module is evaluated once, on the main thread (and C is printed).
561cb0ef41Sopenharmony_ci    // 5. the user code is finally executed (and D is printed).
571cb0ef41Sopenharmony_ci    // The worker code should always run before the --import, but the console.log might arrive late.
581cb0ef41Sopenharmony_ci    assert.match(stdout, /^A\r?\n(A\r?\nB\r?\nC|A\r?\nC\r?\nB|C\r?\nA\r?\nB)\r?\nD\r?\n$/);
591cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
601cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
611cb0ef41Sopenharmony_ci  });
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  it('should support --require and --import along with using a loader written in ESM and ESM entry point', async () => {
641cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
651cb0ef41Sopenharmony_ci      '--no-warnings',
661cb0ef41Sopenharmony_ci      '--require',
671cb0ef41Sopenharmony_ci      fixtures.path('printA.js'),
681cb0ef41Sopenharmony_ci      '--experimental-loader',
691cb0ef41Sopenharmony_ci      'data:text/javascript,console.log("B")',
701cb0ef41Sopenharmony_ci      '--import',
711cb0ef41Sopenharmony_ci      fixtures.fileURL('printC.js'),
721cb0ef41Sopenharmony_ci      '--input-type=module',
731cb0ef41Sopenharmony_ci      '--eval',
741cb0ef41Sopenharmony_ci      'setTimeout(() => console.log("D"),99)',
751cb0ef41Sopenharmony_ci    ]);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
781cb0ef41Sopenharmony_ci    // The worker code should always run before the --import, but the console.log might arrive late.
791cb0ef41Sopenharmony_ci    assert.match(stdout, /^A\r?\nA\r?\n(B\r?\nC|C\r?\nB)\r?\nD\r?\n$/);
801cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
811cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci});
84