11cb0ef41Sopenharmony_ciimport { spawnPromisified } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport tmpdir from '../common/tmpdir.js';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciimport assert from 'node:assert';
51cb0ef41Sopenharmony_ciimport { mkdir, writeFile } from 'node:fs/promises';
61cb0ef41Sopenharmony_ciimport * as path from 'node:path';
71cb0ef41Sopenharmony_ciimport { execPath } from 'node:process';
81cb0ef41Sopenharmony_ciimport { describe, it, before } from 'node:test';
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cidescribe('ESM in main field', { concurrency: true }, () => {
111cb0ef41Sopenharmony_ci  before(() => tmpdir.refresh());
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  it('should handle fully-specified relative path without any warning', async () => {
141cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
151cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
161cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
171cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
181cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
191cb0ef41Sopenharmony_ci      main: './index.js',
201cb0ef41Sopenharmony_ci      type: 'module',
211cb0ef41Sopenharmony_ci    }));
221cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
231cb0ef41Sopenharmony_ci      '--input-type=module',
241cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
251cb0ef41Sopenharmony_ci    ], { cwd });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
281cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
291cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  it('should handle fully-specified absolute path without any warning', async () => {
321cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
331cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
341cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
351cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
361cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
371cb0ef41Sopenharmony_ci      main: path.join(pkgPath, './index.js'),
381cb0ef41Sopenharmony_ci      type: 'module',
391cb0ef41Sopenharmony_ci    }));
401cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
411cb0ef41Sopenharmony_ci      '--input-type=module',
421cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
431cb0ef41Sopenharmony_ci    ], { cwd });
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
461cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
471cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  it('should emit warning when "main" and "exports" are missing', async () => {
511cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
521cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
531cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
541cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
551cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
561cb0ef41Sopenharmony_ci      type: 'module',
571cb0ef41Sopenharmony_ci    }));
581cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
591cb0ef41Sopenharmony_ci      '--input-type=module',
601cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
611cb0ef41Sopenharmony_ci    ], { cwd });
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    assert.match(stderr, /\[DEP0151\]/);
641cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
651cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
661cb0ef41Sopenharmony_ci  });
671cb0ef41Sopenharmony_ci  it('should emit warning when "main" is falsy', async () => {
681cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
691cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
701cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
711cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
721cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
731cb0ef41Sopenharmony_ci      type: 'module',
741cb0ef41Sopenharmony_ci      main: '',
751cb0ef41Sopenharmony_ci    }));
761cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
771cb0ef41Sopenharmony_ci      '--input-type=module',
781cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
791cb0ef41Sopenharmony_ci    ], { cwd });
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    assert.match(stderr, /\[DEP0151\]/);
821cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
831cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
841cb0ef41Sopenharmony_ci  });
851cb0ef41Sopenharmony_ci  it('should emit warning when "main" is a relative path without extension', async () => {
861cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
871cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
881cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
891cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
901cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
911cb0ef41Sopenharmony_ci      main: 'index',
921cb0ef41Sopenharmony_ci      type: 'module',
931cb0ef41Sopenharmony_ci    }));
941cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
951cb0ef41Sopenharmony_ci      '--input-type=module',
961cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
971cb0ef41Sopenharmony_ci    ], { cwd });
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    assert.match(stderr, /\[DEP0151\]/);
1001cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
1011cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1021cb0ef41Sopenharmony_ci  });
1031cb0ef41Sopenharmony_ci  it('should emit warning when "main" is an absolute path without extension', async () => {
1041cb0ef41Sopenharmony_ci    const cwd = path.join(tmpdir.path, Math.random().toString());
1051cb0ef41Sopenharmony_ci    const pkgPath = path.join(cwd, './node_modules/pkg/');
1061cb0ef41Sopenharmony_ci    await mkdir(pkgPath, { recursive: true });
1071cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
1081cb0ef41Sopenharmony_ci    await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
1091cb0ef41Sopenharmony_ci      main: pkgPath + 'index',
1101cb0ef41Sopenharmony_ci      type: 'module',
1111cb0ef41Sopenharmony_ci    }));
1121cb0ef41Sopenharmony_ci    const { code, stdout, stderr } = await spawnPromisified(execPath, [
1131cb0ef41Sopenharmony_ci      '--input-type=module',
1141cb0ef41Sopenharmony_ci      '--eval', 'import "pkg"',
1151cb0ef41Sopenharmony_ci    ], { cwd });
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci    assert.match(stderr, /\[DEP0151\]/);
1181cb0ef41Sopenharmony_ci    assert.match(stdout, /^Hello World!\r?\n$/);
1191cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
1201cb0ef41Sopenharmony_ci  });
1211cb0ef41Sopenharmony_ci});
122