Files
qinglong/cli/test/internal/esmDependencies.test.cjs
T
whyour 801a71d740 feat(cli): cover OpenAPI with a remote npm CLI and internal panel tools (#3074)
* feat(cli): add unified Commander CLI for QingLong 2.x

* fix(cli): publish via npm and address security review feedback

* ci(cli): package npm artifacts and remove evaluation collateral

* test(cli): use a fixed shell fixture for log retention

* refactor(cli): separate remote npm client from panel tools

* feat(cli): cover active panel OpenAPI resources

* docs(cli): unify authentication and skill guidance

* refactor(cli): isolate internal commands and generate Commander help

* refactor(cli): organize remote and internal modules by responsibility

* ci(cli): publish verified npm archives from master

* fix(cli): publish under the whyour npm scope

* ci: use npm trusted publishing for both packages

* docs: introduce the published CLI on the project homepage

* fix(cli): preserve server log truncation and correct login hints

* fix(cli): accept dashboard record request bodies

* fix(cli): preserve stdin for local task execution

* fix(cli): resolve task executables after changing directory

* fix(cli): preserve shell function tasks and sanitize test failures

* fix(cli): preserve shell hook state and resolve workdir after hooks

* fix(cli): preserve cleanup across shared shell task timeouts

* fix(cli): isolate shell control descriptors and reap timed-out descendants
2026-09-25 23:24:41 +08:00

153 lines
5.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const { createContext } = require('../../dist/internal/runtime/context');
const { executeTask } = require('../../dist/internal/execution/taskRunner');
test('ESM resolves global packages and exported subpaths with native import conditions and local fallback', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-esm-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const global = path.join(root, 'global # percent % space', 'node_modules');
const bin = path.join(root, 'bin');
await fs.mkdir(bin);
await fs.writeFile(
path.join(bin, 'pnpm'),
`#!${process.execPath}\nprocess.stdout.write(${JSON.stringify(global)});`,
{ mode: 0o755 },
);
const context = createContext(
{ root },
{ PATH: `${bin}${path.delimiter}${process.env.PATH}`, no_tee: 'true' },
);
for (const dir of [
context.paths.dir_preload,
context.paths.dir_config,
context.paths.dir_scripts,
])
await fs.mkdir(dir, { recursive: true });
for (const [name, contents] of Object.entries({
'env.sh': '',
'env.js': '',
'client.js': 'module.exports={};',
'__ql_notify__.js': 'exports.sendNotify=()=>{};',
}))
await fs.writeFile(path.join(context.paths.dir_preload, name), contents);
await fs.copyFile(
path.resolve(__dirname, '../../../shell/preload/esm-loader.mjs'),
path.join(context.paths.dir_preload, 'esm-loader.mjs'),
);
await fs.writeFile(context.paths.file_task_before, 'true\n');
await fs.writeFile(context.paths.file_task_before_js, '');
const packages = [
[
global,
'ql-fixture',
{
type: 'module',
exports: {
'.': { import: './entry.mjs', require: './entry.cjs' },
'./feature': './actual/feature.mjs',
},
},
{
'entry.mjs': 'export default "global-import";',
'entry.cjs': 'module.exports="global-require";',
'actual/feature.mjs': 'export default "global-feature";',
'private.mjs': 'export default "private";',
},
],
[
global,
'@ql/fixture',
{
type: 'module',
exports: { '.': './entry.mjs', './feature': './feature.mjs' },
imports: { '#internal': './internal.mjs' },
},
{
'entry.mjs': 'import value from "#internal"; export default value;',
'internal.mjs': 'export default "scoped";',
'feature.mjs': 'export default "scoped-feature";',
},
],
[
context.paths.dir_scripts + '/node_modules',
'ql-fixture',
{ type: 'module', main: 'index.mjs' },
{ 'index.mjs': 'export default "local-shadow";' },
],
[
context.paths.dir_scripts + '/node_modules',
'local-only',
{ type: 'module', main: 'index.mjs' },
{ 'index.mjs': 'export default "local-only";' },
],
[
global,
'common-fixture',
{ main: 'index.cjs' },
{ 'index.cjs': 'module.exports="commonjs";' },
],
];
for (const [base, name, manifest, files] of packages) {
const dir = path.join(base, name);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(
path.join(dir, 'package.json'),
JSON.stringify({ name, ...manifest }),
);
for (const [file, source] of Object.entries(files)) {
await fs.mkdir(path.dirname(path.join(dir, file)), { recursive: true });
await fs.writeFile(path.join(dir, file), source);
}
}
// A pnpm-style global link points into the content store, not a copied package.
const link = path.join(global, '@ql/fixture');
const stored = path.join(
global,
'.pnpm',
'@ql+fixture@1',
'node_modules',
'@ql',
'fixture',
);
await fs.mkdir(path.dirname(stored), { recursive: true });
await fs.rename(link, stored);
await fs.symlink(stored, link);
await fs.writeFile(
path.join(context.paths.dir_scripts, 'relative.mjs'),
'export default "relative";',
);
const expected = {
'ql-fixture': 'global-import',
'ql-fixture/feature': 'global-feature',
'@ql/fixture': 'scoped',
'@ql/fixture/feature': 'scoped-feature',
'local-only': 'local-only',
'common-fixture': 'commonjs',
'./relative.mjs': 'relative',
'data:text/javascript,export default "data"': 'data',
'ql-fixture/private.mjs': 'ERR_PACKAGE_PATH_NOT_EXPORTED',
};
await fs.writeFile(
path.join(context.paths.dir_scripts, 'fixture.mjs'),
`import fs from 'node:fs'; import path from 'path'; const result={}; for (const name of ${JSON.stringify(
Object.keys(expected),
)}) { try { result[name]=(await import(name)).default; } catch(error) { result[name]=error.code; } } console.log('RESULT:'+JSON.stringify(result));`,
);
const result = await executeTask(context, {
argv: ['fixture.mjs'],
mode: 'now',
});
const log = await fs.readFile(
path.join(context.paths.dir_log, result.logPath),
'utf8',
);
assert.equal(result.exitCode, 0, log);
const line = log.split('\n').find((line) => line.startsWith('RESULT:'));
assert.ok(line, log);
assert.deepEqual(JSON.parse(line.slice(7)), expected);
});