2026-05-10 23:12:26 +02:00
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { runKtxCli, type KtxCliDeps } 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('project directory defaults', () => {
|
|
|
|
|
afterEach(() => {
|
2026-05-10 23:51:24 +02:00
|
|
|
delete process.env.KTX_PROJECT_DIR;
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
it('uses KTX_PROJECT_DIR when Commander-dispatched commands omit --project-dir', async () => {
|
|
|
|
|
process.env.KTX_PROJECT_DIR = '/tmp/ktx-env-project';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const connection = vi.fn(async () => 0);
|
|
|
|
|
const doctor = vi.fn(async () => 0);
|
2026-05-14 01:43:06 +02:00
|
|
|
const publicIngest = vi.fn(async () => 0);
|
2026-05-10 23:12:26 +02:00
|
|
|
const setup = vi.fn(async () => 0);
|
2026-05-14 01:43:06 +02:00
|
|
|
const deps: KtxCliDeps = { connection, doctor, publicIngest, setup };
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const cases: Array<{
|
|
|
|
|
argv: string[];
|
|
|
|
|
spy: ReturnType<typeof vi.fn>;
|
|
|
|
|
expected: Record<string, unknown>;
|
2026-05-12 15:04:01 +02:00
|
|
|
expectedStderr: string;
|
2026-05-10 23:12:26 +02:00
|
|
|
}> = [
|
|
|
|
|
{
|
|
|
|
|
argv: ['connection', 'list'],
|
|
|
|
|
spy: connection,
|
2026-05-10 23:51:24 +02:00
|
|
|
expected: { command: 'list', projectDir: '/tmp/ktx-env-project' },
|
2026-05-12 15:04:01 +02:00
|
|
|
expectedStderr: 'Project: /tmp/ktx-env-project\n',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
{
|
2026-05-12 23:51:46 +02:00
|
|
|
argv: ['status', '--no-input'],
|
2026-05-10 23:12:26 +02:00
|
|
|
spy: doctor,
|
2026-05-10 23:51:24 +02:00
|
|
|
expected: { command: 'project', projectDir: '/tmp/ktx-env-project' },
|
2026-05-12 15:04:01 +02:00
|
|
|
expectedStderr: 'Project: /tmp/ktx-env-project\n',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
{
|
2026-05-13 00:38:26 +02:00
|
|
|
argv: ['setup', '--no-input'],
|
2026-05-10 23:12:26 +02:00
|
|
|
spy: setup,
|
2026-05-13 00:38:26 +02:00
|
|
|
expected: { command: 'run', projectDir: '/tmp/ktx-env-project' },
|
2026-05-13 09:16:35 -07:00
|
|
|
expectedStderr: '',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
{
|
2026-05-14 01:43:06 +02:00
|
|
|
argv: ['ingest', 'warehouse', '--no-input'],
|
|
|
|
|
spy: publicIngest,
|
|
|
|
|
expected: { command: 'run', projectDir: '/tmp/ktx-env-project', targetConnectionId: 'warehouse' },
|
2026-05-12 15:04:01 +02:00
|
|
|
expectedStderr: 'Project: /tmp/ktx-env-project\n',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const item of cases) {
|
|
|
|
|
const testIo = makeIo();
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(runKtxCli(item.argv, testIo.io, deps)).resolves.toBe(0);
|
2026-05-12 23:51:46 +02:00
|
|
|
expect(item.spy).toHaveBeenLastCalledWith(expect.objectContaining(item.expected), testIo.io);
|
2026-05-12 15:04:01 +02:00
|
|
|
expect(testIo.stderr()).toBe(item.expectedStderr);
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
it('lets explicit global --project-dir override KTX_PROJECT_DIR before and after nested commands', async () => {
|
|
|
|
|
process.env.KTX_PROJECT_DIR = '/tmp/ktx-env-project';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
const publicIngest = vi.fn(async () => 0);
|
|
|
|
|
const beforeCommandIo = makeIo();
|
|
|
|
|
const afterCommandIo = makeIo();
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
await expect(
|
2026-05-14 01:43:06 +02:00
|
|
|
runKtxCli(['--project-dir', '/tmp/ktx-explicit-project', 'ingest', 'warehouse', '--no-input'], beforeCommandIo.io, {
|
|
|
|
|
publicIngest,
|
|
|
|
|
}),
|
2026-05-10 23:12:26 +02:00
|
|
|
).resolves.toBe(0);
|
|
|
|
|
await expect(
|
2026-05-14 01:43:06 +02:00
|
|
|
runKtxCli(['ingest', 'warehouse', '--project-dir=/tmp/ktx-explicit-project', '--no-input'], afterCommandIo.io, {
|
|
|
|
|
publicIngest,
|
2026-05-10 23:12:26 +02:00
|
|
|
}),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(publicIngest).toHaveBeenNthCalledWith(
|
|
|
|
|
1,
|
2026-05-10 23:51:24 +02:00
|
|
|
expect.objectContaining({ command: 'run', projectDir: '/tmp/ktx-explicit-project' }),
|
2026-05-14 01:43:06 +02:00
|
|
|
beforeCommandIo.io,
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(publicIngest).toHaveBeenNthCalledWith(
|
|
|
|
|
2,
|
|
|
|
|
expect.objectContaining({ command: 'run', projectDir: '/tmp/ktx-explicit-project' }),
|
|
|
|
|
afterCommandIo.io,
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(beforeCommandIo.stderr()).toBe('Project: /tmp/ktx-explicit-project\n');
|
|
|
|
|
expect(afterCommandIo.stderr()).toBe('Project: /tmp/ktx-explicit-project\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
it('uses nearest ancestor containing ktx.yaml when no explicit or environment project-dir exists', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const { mkdir, realpath, writeFile } = await import('node:fs/promises');
|
|
|
|
|
const { mkdtemp, rm } = await import('node:fs/promises');
|
|
|
|
|
const { tmpdir } = await import('node:os');
|
|
|
|
|
const { join } = await import('node:path');
|
|
|
|
|
|
|
|
|
|
const originalCwd = process.cwd();
|
2026-05-10 23:51:24 +02:00
|
|
|
const root = await mkdtemp(join(tmpdir(), 'ktx-cli-nearest-project-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
const projectDir = join(root, 'warehouse');
|
|
|
|
|
const nestedDir = join(projectDir, 'nested', 'deeper');
|
|
|
|
|
await mkdir(nestedDir, { recursive: true });
|
2026-05-10 23:51:24 +02:00
|
|
|
await writeFile(join(projectDir, 'ktx.yaml'), 'project: warehouse\n', 'utf-8');
|
2026-05-10 23:12:26 +02:00
|
|
|
const expectedProjectDir = await realpath(projectDir);
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
const publicIngest = vi.fn(async () => 0);
|
2026-05-10 23:12:26 +02:00
|
|
|
const testIo = makeIo();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
process.chdir(nestedDir);
|
2026-05-14 01:43:06 +02:00
|
|
|
await expect(runKtxCli(['ingest', 'warehouse', '--no-input'], testIo.io, { publicIngest })).resolves.toBe(0);
|
2026-05-10 23:12:26 +02:00
|
|
|
} finally {
|
|
|
|
|
process.chdir(originalCwd);
|
|
|
|
|
await rm(root, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(publicIngest).toHaveBeenCalledWith(
|
2026-05-10 23:12:26 +02:00
|
|
|
expect.objectContaining({ command: 'run', projectDir: expectedProjectDir }),
|
|
|
|
|
testIo.io,
|
|
|
|
|
);
|
2026-05-12 15:04:01 +02:00
|
|
|
expect(testIo.stderr()).toBe(`Project: ${expectedProjectDir}\n`);
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
});
|