2026-05-10 23:12:26 +02:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import { test } from 'node:test';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { runWorkspaceKtx } from './run-ktx.mjs';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
function freshBuildFs() {
|
|
|
|
|
return {
|
|
|
|
|
stat: async (path) => ({
|
|
|
|
|
mtimeMs: path.endsWith('/packages/cli/dist/bin.js') ? 2000 : 1000,
|
|
|
|
|
isDirectory: () => path.endsWith('/src') || path.endsWith('/packages'),
|
|
|
|
|
}),
|
|
|
|
|
readdir: async (path) => {
|
|
|
|
|
if (path.endsWith('/packages')) {
|
|
|
|
|
return [{ name: 'cli', isDirectory: () => true }];
|
|
|
|
|
}
|
|
|
|
|
if (path.endsWith('/src')) {
|
|
|
|
|
return [{ name: 'bin.ts', isDirectory: () => false }];
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
test('runWorkspaceKtx runs the built CLI when it already exists', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const calls = [];
|
|
|
|
|
const logs = [];
|
|
|
|
|
const fs = freshBuildFs();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const exitCode = await runWorkspaceKtx(['--version'], {
|
|
|
|
|
rootDir: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
access: async () => undefined,
|
|
|
|
|
stat: fs.stat,
|
|
|
|
|
readdir: fs.readdir,
|
|
|
|
|
execFile: async (command, args, options) => {
|
|
|
|
|
calls.push({ command, args, cwd: options.cwd });
|
2026-05-10 23:51:24 +02:00
|
|
|
return { stdout: '@ktx/cli 0.0.0-private\n', stderr: '' };
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
stdout: { write: (chunk) => logs.push(['stdout', chunk]) },
|
|
|
|
|
stderr: { write: (chunk) => logs.push(['stderr', chunk]) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(exitCode, 0);
|
|
|
|
|
assert.deepEqual(calls, [
|
|
|
|
|
{
|
|
|
|
|
command: process.execPath,
|
2026-05-10 23:51:24 +02:00
|
|
|
args: ['/workspace/ktx/packages/cli/dist/bin.js', '--version'],
|
|
|
|
|
cwd: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
assert.deepEqual(logs, [['stdout', '@ktx/cli 0.0.0-private\n']]);
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
test('runWorkspaceKtx forwards a caller-provided environment to buffered commands', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const calls = [];
|
|
|
|
|
const fs = freshBuildFs();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const exitCode = await runWorkspaceKtx(['--version'], {
|
|
|
|
|
rootDir: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
access: async () => undefined,
|
|
|
|
|
stat: fs.stat,
|
|
|
|
|
readdir: fs.readdir,
|
2026-05-10 23:51:24 +02:00
|
|
|
env: { PATH: '/bin', GIT_CEILING_DIRECTORIES: '/workspace/ktx/examples' },
|
2026-05-10 23:12:26 +02:00
|
|
|
execFile: async (command, args, options) => {
|
|
|
|
|
calls.push({ command, args, cwd: options.cwd, env: options.env });
|
2026-05-10 23:51:24 +02:00
|
|
|
return { stdout: '@ktx/cli 0.0.0-private\n', stderr: '' };
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
stdout: { write: () => undefined },
|
|
|
|
|
stderr: { write: () => undefined },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(exitCode, 0);
|
|
|
|
|
assert.deepEqual(calls, [
|
|
|
|
|
{
|
|
|
|
|
command: process.execPath,
|
2026-05-10 23:51:24 +02:00
|
|
|
args: ['/workspace/ktx/packages/cli/dist/bin.js', '--version'],
|
|
|
|
|
cwd: '/workspace/ktx',
|
|
|
|
|
env: { PATH: '/bin', GIT_CEILING_DIRECTORIES: '/workspace/ktx/examples' },
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
test('runWorkspaceKtx drops a leading npm argument separator', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const calls = [];
|
|
|
|
|
const fs = freshBuildFs();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const exitCode = await runWorkspaceKtx(['--', 'connection', 'test', 'warehouse', '--help'], {
|
|
|
|
|
rootDir: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
access: async () => undefined,
|
|
|
|
|
stat: fs.stat,
|
|
|
|
|
readdir: fs.readdir,
|
|
|
|
|
execFile: async (command, args, options) => {
|
|
|
|
|
calls.push({ command, args, cwd: options.cwd });
|
2026-05-10 23:51:24 +02:00
|
|
|
return { stdout: 'Usage: ktx connection test\n', stderr: '' };
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
stdout: { write: () => undefined },
|
|
|
|
|
stderr: { write: () => undefined },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(exitCode, 0);
|
|
|
|
|
assert.deepEqual(calls, [
|
|
|
|
|
{
|
|
|
|
|
command: process.execPath,
|
2026-05-10 23:51:24 +02:00
|
|
|
args: ['/workspace/ktx/packages/cli/dist/bin.js', 'connection', 'test', 'warehouse', '--help'],
|
|
|
|
|
cwd: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
test('runWorkspaceKtx builds the workspace CLI before running it when dist is missing', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const calls = [];
|
|
|
|
|
const logs = [];
|
|
|
|
|
let binExists = false;
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const exitCode = await runWorkspaceKtx(['setup', 'demo', '--mode', 'replay', '--no-input', '--viz'], {
|
|
|
|
|
rootDir: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
access: async () => {
|
|
|
|
|
if (!binExists) {
|
|
|
|
|
throw Object.assign(new Error('missing'), { code: 'ENOENT' });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
execFile: async (command, args, options) => {
|
|
|
|
|
calls.push({ command, args, cwd: options.cwd });
|
|
|
|
|
if (command === 'pnpm') {
|
|
|
|
|
binExists = true;
|
|
|
|
|
return { stdout: 'build ok\n', stderr: '' };
|
|
|
|
|
}
|
|
|
|
|
return { stdout: 'Replay complete\n', stderr: '' };
|
|
|
|
|
},
|
|
|
|
|
stdout: { write: (chunk) => logs.push(['stdout', chunk]) },
|
|
|
|
|
stderr: { write: (chunk) => logs.push(['stderr', chunk]) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(exitCode, 0);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
calls.map((call) => [call.command, call.args]),
|
|
|
|
|
[
|
|
|
|
|
['pnpm', ['run', 'build']],
|
|
|
|
|
[
|
|
|
|
|
process.execPath,
|
2026-05-10 23:51:24 +02:00
|
|
|
['/workspace/ktx/packages/cli/dist/bin.js', 'setup', 'demo', '--mode', 'replay', '--no-input', '--viz'],
|
2026-05-10 23:12:26 +02:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(logs, [
|
2026-05-10 23:51:24 +02:00
|
|
|
['stderr', 'KTX CLI build output is missing. Building it now with `pnpm run build`...\n'],
|
2026-05-10 23:12:26 +02:00
|
|
|
['stdout', 'build ok\n'],
|
|
|
|
|
['stdout', 'Replay complete\n'],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
test('runWorkspaceKtx rebuilds before running when workspace sources are newer than dist', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const calls = [];
|
|
|
|
|
const logs = [];
|
|
|
|
|
let sourceMtimeMs = 3000;
|
|
|
|
|
|
2026-05-13 11:14:16 +02:00
|
|
|
const exitCode = await runWorkspaceKtx(['scan', 'orbit', '--mode', 'relationships'], {
|
2026-05-10 23:51:24 +02:00
|
|
|
rootDir: '/workspace/ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
access: async () => undefined,
|
|
|
|
|
stat: async (path) => ({
|
|
|
|
|
mtimeMs: path.endsWith('/packages/cli/dist/bin.js') ? 2000 : sourceMtimeMs,
|
|
|
|
|
isDirectory: () => path.endsWith('/src') || path.endsWith('/packages'),
|
|
|
|
|
}),
|
|
|
|
|
readdir: async (path) => {
|
|
|
|
|
if (path.endsWith('/packages')) {
|
|
|
|
|
return [{ name: 'context', isDirectory: () => true }];
|
|
|
|
|
}
|
|
|
|
|
if (path.endsWith('/src')) {
|
|
|
|
|
return [{ name: 'scan.ts', isDirectory: () => false }];
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
},
|
|
|
|
|
execFile: async (command, args, options) => {
|
|
|
|
|
calls.push({ command, args, cwd: options.cwd });
|
|
|
|
|
if (command === 'pnpm') {
|
|
|
|
|
sourceMtimeMs = 1000;
|
|
|
|
|
return { stdout: 'build ok\n', stderr: '' };
|
|
|
|
|
}
|
|
|
|
|
return { stdout: 'scan ok\n', stderr: '' };
|
|
|
|
|
},
|
|
|
|
|
stdout: { write: (chunk) => logs.push(['stdout', chunk]) },
|
|
|
|
|
stderr: { write: (chunk) => logs.push(['stderr', chunk]) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.equal(exitCode, 0);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
calls.map((call) => [call.command, call.args]),
|
|
|
|
|
[
|
|
|
|
|
['pnpm', ['run', 'build']],
|
2026-05-13 11:14:16 +02:00
|
|
|
[process.execPath, ['/workspace/ktx/packages/cli/dist/bin.js', 'scan', 'orbit', '--mode', 'relationships']],
|
2026-05-10 23:12:26 +02:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(logs, [
|
2026-05-10 23:51:24 +02:00
|
|
|
['stderr', 'KTX CLI build output is stale. Rebuilding it now with `pnpm run build`...\n'],
|
2026-05-10 23:12:26 +02:00
|
|
|
['stdout', 'build ok\n'],
|
|
|
|
|
['stdout', 'scan ok\n'],
|
|
|
|
|
]);
|
|
|
|
|
});
|