11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs';
31cb0ef41Sopenharmony_ciimport assert from 'node:assert';
41cb0ef41Sopenharmony_ciimport os from 'node:os';
51cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
61cb0ef41Sopenharmony_ciimport { describe, it } from 'node:test';
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cidescribe('globalPreload hook', () => {
91cb0ef41Sopenharmony_ci  it('should not emit deprecation warning when initialize is supplied', async () => {
101cb0ef41Sopenharmony_ci    const { stderr } = await spawnPromisified(execPath, [
111cb0ef41Sopenharmony_ci      '--experimental-loader',
121cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){}export function initialize(){}',
131cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
141cb0ef41Sopenharmony_ci    ]);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    assert.doesNotMatch(stderr, /`globalPreload` is an experimental feature/);
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  it('should handle globalPreload returning undefined', async () => {
201cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
211cb0ef41Sopenharmony_ci      '--no-warnings',
221cb0ef41Sopenharmony_ci      '--experimental-loader',
231cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){}',
241cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
251cb0ef41Sopenharmony_ci    ]);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
281cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
291cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
301cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  it('should handle loading node:test', async () => {
341cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
351cb0ef41Sopenharmony_ci      '--no-warnings',
361cb0ef41Sopenharmony_ci      '--experimental-loader',
371cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}',
381cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
391cb0ef41Sopenharmony_ci    ]);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
421cb0ef41Sopenharmony_ci    assert.match(stdout, /\n# pass 1\r?\n/);
431cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
441cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  it('should handle loading node:os with node: prefix', async () => {
481cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
491cb0ef41Sopenharmony_ci      '--no-warnings',
501cb0ef41Sopenharmony_ci      '--experimental-loader',
511cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}',
521cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
531cb0ef41Sopenharmony_ci    ]);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
561cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), os.arch());
571cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
581cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  // `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter).
621cb0ef41Sopenharmony_ci  it('should handle loading builtin module without node: prefix', async () => {
631cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
641cb0ef41Sopenharmony_ci      '--no-warnings',
651cb0ef41Sopenharmony_ci      '--experimental-loader',
661cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}',
671cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
681cb0ef41Sopenharmony_ci    ]);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
711cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), os.arch());
721cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
731cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
741cb0ef41Sopenharmony_ci  });
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  it('should throw when loading node:test without node: prefix', async () => {
771cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
781cb0ef41Sopenharmony_ci      '--no-warnings',
791cb0ef41Sopenharmony_ci      '--experimental-loader',
801cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}',
811cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
821cb0ef41Sopenharmony_ci    ]);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/);
851cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
861cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
871cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
881cb0ef41Sopenharmony_ci  });
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  it('should register globals set from globalPreload', async () => {
911cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
921cb0ef41Sopenharmony_ci      '--no-warnings',
931cb0ef41Sopenharmony_ci      '--experimental-loader',
941cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
951cb0ef41Sopenharmony_ci      '--print', 'myGlobal',
961cb0ef41Sopenharmony_ci    ]);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
991cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), '4');
1001cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1011cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1021cb0ef41Sopenharmony_ci  });
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  it('should log console.log calls returned from globalPreload', async () => {
1051cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
1061cb0ef41Sopenharmony_ci      '--no-warnings',
1071cb0ef41Sopenharmony_ci      '--experimental-loader',
1081cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
1091cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
1101cb0ef41Sopenharmony_ci    ]);
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1131cb0ef41Sopenharmony_ci    assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
1141cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1151cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1161cb0ef41Sopenharmony_ci  });
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  it('should crash if globalPreload returns code that throws', async () => {
1191cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
1201cb0ef41Sopenharmony_ci      '--no-warnings',
1211cb0ef41Sopenharmony_ci      '--experimental-loader',
1221cb0ef41Sopenharmony_ci      'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
1231cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
1241cb0ef41Sopenharmony_ci    ]);
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci    assert.match(stderr, /error from globalPreload/);
1271cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
1281cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
1291cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1301cb0ef41Sopenharmony_ci  });
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  it('should have a `this` value that is not bound to the loader instance', async () => {
1331cb0ef41Sopenharmony_ci    const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
1341cb0ef41Sopenharmony_ci      '--no-warnings',
1351cb0ef41Sopenharmony_ci      '--experimental-loader',
1361cb0ef41Sopenharmony_ci      `data:text/javascript,export ${function globalPreload() {
1371cb0ef41Sopenharmony_ci        if (this != null) {
1381cb0ef41Sopenharmony_ci          throw new Error('hook function must not be bound to ESMLoader instance');
1391cb0ef41Sopenharmony_ci        }
1401cb0ef41Sopenharmony_ci      }}`,
1411cb0ef41Sopenharmony_ci      fixtures.path('empty.js'),
1421cb0ef41Sopenharmony_ci    ]);
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
1451cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
1461cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1471cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
1481cb0ef41Sopenharmony_ci  });
1491cb0ef41Sopenharmony_ci});
150