2026-05-10 23:12:26 +02:00
|
|
|
import { describe, expect, it, vi } from 'vitest';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { runKtxCli } from './index.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
function makeIo() {
|
|
|
|
|
let stdout = '';
|
|
|
|
|
let stderr = '';
|
|
|
|
|
return {
|
|
|
|
|
io: {
|
|
|
|
|
stdout: {
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stdout += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stderr: {
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stderr += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stdout: () => stdout,
|
|
|
|
|
stderr: () => stderr,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('dev Commander tree', () => {
|
|
|
|
|
it('prints visible dev help with only supported low-level command groups', async () => {
|
|
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(runKtxCli(['dev', '--help'], testIo.io)).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
expect(testIo.stdout()).toContain('Usage: ktx dev [options] [command]');
|
2026-05-13 12:00:08 +02:00
|
|
|
for (const command of ['init', 'runtime']) {
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(testIo.stdout()).toContain(command);
|
|
|
|
|
}
|
|
|
|
|
for (const removed of [
|
2026-05-12 23:51:46 +02:00
|
|
|
'doctor',
|
2026-05-13 12:00:08 +02:00
|
|
|
'scan',
|
|
|
|
|
'ingest',
|
|
|
|
|
'mapping',
|
2026-05-10 23:12:26 +02:00
|
|
|
'knowledge',
|
|
|
|
|
'model',
|
|
|
|
|
'replay',
|
|
|
|
|
'report',
|
|
|
|
|
'status',
|
|
|
|
|
'artifacts',
|
|
|
|
|
'config',
|
|
|
|
|
'tools',
|
|
|
|
|
'daemon',
|
|
|
|
|
]) {
|
|
|
|
|
expect(testIo.stdout()).not.toContain(`${removed} `);
|
|
|
|
|
}
|
|
|
|
|
expect(testIo.stderr()).toBe('');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
it('lists dev in root command rows', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(runKtxCli(['--help'], testIo.io)).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(testIo.stdout()).not.toContain('Advanced:');
|
|
|
|
|
expect(testIo.stdout()).toContain('dev');
|
|
|
|
|
expect(testIo.stdout()).toMatch(/Low-level project initialization and runtime\s+management/);
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(testIo.stderr()).toBe('');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('keeps project scaffolding under dev init', async () => {
|
|
|
|
|
const { mkdtemp, readFile, rm } = await import('node:fs/promises');
|
|
|
|
|
const { tmpdir } = await import('node:os');
|
|
|
|
|
const { join } = await import('node:path');
|
2026-05-10 23:51:24 +02:00
|
|
|
const tempDir = await mkdtemp(join(tmpdir(), 'ktx-dev-init-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
const projectDir = join(tempDir, 'warehouse');
|
|
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-14 17:39:31 +02:00
|
|
|
await expect(runKtxCli(['dev', 'init', projectDir], testIo.io)).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
expect(testIo.stdout()).toContain(`Initialized KTX project at ${projectDir}`);
|
2026-05-14 17:39:31 +02:00
|
|
|
await expect(readFile(join(projectDir, 'ktx.yaml'), 'utf-8')).resolves.not.toContain('project:');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(testIo.stderr()).toBe('');
|
|
|
|
|
} finally {
|
|
|
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('uses global project-dir for dev init when the positional directory is omitted', async () => {
|
|
|
|
|
const { mkdtemp, rm } = await import('node:fs/promises');
|
|
|
|
|
const { tmpdir } = await import('node:os');
|
|
|
|
|
const { join } = await import('node:path');
|
2026-05-10 23:51:24 +02:00
|
|
|
const tempDir = await mkdtemp(join(tmpdir(), 'ktx-dev-init-global-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
const projectDir = join(tempDir, 'global-init');
|
|
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await expect(
|
2026-05-14 17:39:31 +02:00
|
|
|
runKtxCli(['--project-dir', projectDir, 'dev', 'init'], testIo.io),
|
2026-05-10 23:12:26 +02:00
|
|
|
).resolves.toBe(0);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
expect(testIo.stdout()).toContain(`Initialized KTX project at ${projectDir}`);
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(testIo.stderr()).toBe('');
|
|
|
|
|
} finally {
|
|
|
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects removed dev command groups', async () => {
|
|
|
|
|
for (const argv of [
|
2026-05-12 23:51:46 +02:00
|
|
|
['dev', 'doctor', 'setup'],
|
2026-05-13 12:00:08 +02:00
|
|
|
['dev', 'runtime', 'doctor'],
|
2026-05-13 12:28:24 +02:00
|
|
|
['dev', 'runtime', 'prune', '--dry-run'],
|
2026-05-13 12:00:08 +02:00
|
|
|
['dev', 'scan', 'warehouse'],
|
|
|
|
|
['dev', 'ingest', 'run'],
|
|
|
|
|
['dev', 'mapping', 'list'],
|
|
|
|
|
['dev', 'completion', 'zsh'],
|
|
|
|
|
['dev', '__complete', '--shell', 'zsh', '--position', '2', '--', 'ktx', ''],
|
2026-05-10 23:12:26 +02:00
|
|
|
['dev', 'knowledge', 'list'],
|
|
|
|
|
['dev', 'model', 'list'],
|
|
|
|
|
['dev', 'artifacts'],
|
|
|
|
|
]) {
|
|
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(runKtxCli(argv, testIo.io)).resolves.toBe(1);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
expect(testIo.stderr()).toMatch(/unknown command|error:/);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
{
|
2026-05-12 23:51:46 +02:00
|
|
|
argv: ['dev', 'runtime', '--help'],
|
2026-05-13 12:28:24 +02:00
|
|
|
expected: ['Usage: ktx dev runtime', 'install', 'start', 'stop', 'status'],
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
])('prints generated nested help for $argv', async ({ argv, expected }) => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const doctor = vi.fn(async () => 0);
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
await expect(runKtxCli(argv, io.io, { doctor })).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
for (const text of expected) {
|
|
|
|
|
expect(io.stdout()).toContain(text);
|
|
|
|
|
}
|
2026-05-13 12:28:24 +02:00
|
|
|
if (argv.join(' ') === 'dev runtime --help') {
|
|
|
|
|
expect(io.stdout()).not.toContain('prune');
|
|
|
|
|
expect(io.stdout()).not.toContain('doctor');
|
|
|
|
|
}
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(io.stderr()).toBe('');
|
|
|
|
|
expect(doctor).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
it('rejects old adapter-backed ingest flags through public option parsing and keeps run out of ingest help', async () => {
|
|
|
|
|
const helpIo = makeIo();
|
|
|
|
|
const runIo = makeIo();
|
|
|
|
|
const publicIngest = vi.fn(async () => 0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
await expect(runKtxCli(['ingest', '--help'], helpIo.io, { publicIngest })).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
await expect(
|
2026-05-14 01:43:06 +02:00
|
|
|
runKtxCli(
|
|
|
|
|
['ingest', 'run', '--connection-id', 'warehouse', '--adapter', 'metabase', '--project-dir', '/tmp/project'],
|
|
|
|
|
runIo.io,
|
|
|
|
|
{ publicIngest },
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(1);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(helpIo.stdout()).not.toMatch(/^ run\s/m);
|
|
|
|
|
expect(runIo.stderr()).toMatch(/unknown option '--connection-id'|error:/);
|
|
|
|
|
expect(publicIngest).not.toHaveBeenCalled();
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-13 12:00:08 +02:00
|
|
|
it.each([
|
2026-05-14 01:43:06 +02:00
|
|
|
{ argv: ['scan'] },
|
|
|
|
|
{ argv: ['scan', '--help'] },
|
|
|
|
|
{ argv: ['scan', 'warehouse'] },
|
|
|
|
|
{ argv: ['scan', 'warehouse', '--project-dir', '/tmp/project', '--dry-run'] },
|
|
|
|
|
{ argv: ['scan', 'warehouse', '--project-dir', '/tmp/project', '--mode', 'relationships'] },
|
|
|
|
|
])('rejects removed top-level scan command $argv', async ({ argv }) => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const io = makeIo();
|
2026-05-14 01:43:06 +02:00
|
|
|
const publicIngest = vi.fn(async () => 0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
await expect(runKtxCli(argv, io.io, { publicIngest })).resolves.toBe(1);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(publicIngest).not.toHaveBeenCalled();
|
|
|
|
|
expect(io.stderr()).toMatch(/unknown command|error:/);
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
it('rejects old adapter-backed top-level ingest flags without low-level ingest registration', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const io = makeIo();
|
2026-05-14 01:43:06 +02:00
|
|
|
const publicIngest = vi.fn(async () => 0);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
await expect(
|
2026-05-10 23:51:24 +02:00
|
|
|
runKtxCli(
|
2026-05-10 23:12:26 +02:00
|
|
|
[
|
|
|
|
|
'ingest',
|
|
|
|
|
'run',
|
|
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
|
|
|
|
'--adapter',
|
|
|
|
|
'metabase',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
'/tmp/project',
|
|
|
|
|
'--json',
|
|
|
|
|
],
|
|
|
|
|
io.io,
|
2026-05-14 01:43:06 +02:00
|
|
|
{ publicIngest },
|
2026-05-10 23:12:26 +02:00
|
|
|
),
|
2026-05-14 01:43:06 +02:00
|
|
|
).resolves.toBe(1);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(publicIngest).not.toHaveBeenCalled();
|
|
|
|
|
expect(io.stderr()).toMatch(/unknown option '--connection-id'|error:/);
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
});
|