ktx/packages/cli/test/completion/dynamic-candidates.test.ts

104 lines
4.6 KiB
TypeScript
Raw Permalink Normal View History

feat(cli): shell completion for commands, flags, and entity names (#244) * feat(completion): complete known argument values * fix(completion): hide Commander-hidden subcommands from completions Replace the `__`-prefix name heuristic with Commander's `_hidden` flag so internal subcommands registered with { hidden: true } (e.g. `mcp serve-internal`) are excluded from completions, mirroring `ktx --help`. * test: cover wiki and sl read command routing * test: cover raw wiki and sl reads * feat: add wiki read command * feat: add sl read command * feat: complete read command entity names * docs: document wiki and sl read commands * test: include read commands in command tree * feat(sl): read and validate unique sources by name * feat(sl): make read and validate connection id optional * fix(completion): dedupe semantic source names * docs(sl): document connection-optional read and validate * fix(sl): require connection id for query command * docs(sl): clarify query connection requirement * fix(completion): don't resolve option values as subcommands resolveCommand skipped flag tokens but not the value consumed by a value-taking option in the `--flag value` form, so a connection id like `query` was matched as the `sl query` subcommand and yielded no `sl` completions. Track value-taking options and skip their consumed value before matching subcommands. * test(telemetry): assert first-run notice via TELEMETRY_NOTICE constant CI (which tests this branch merged with main) failed because #243 changed the first-run notice wording in identity.ts (dropped "anonymous") but left this test grepping for the old literal 'ktx collects anonymous usage data', so indexOf returned -1. Assert against the exported TELEMETRY_NOTICE constant instead so the test tracks the source of truth and cannot drift when the notice text changes again.
2026-05-31 23:44:33 +02:00
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { createProjectCompletionProviders } from '../../src/completion/dynamic-candidates.js';
const KTX_YAML = ['connections:', ' warehouse:', ' driver: sqlite', ' analytics:', ' driver: sqlite', ''].join(
'\n',
);
describe('createProjectCompletionProviders', () => {
let projectDir: string;
beforeEach(async () => {
projectDir = await mkdtemp(join(tmpdir(), 'ktx-completion-'));
await writeFile(join(projectDir, 'ktx.yaml'), KTX_YAML, 'utf-8');
});
afterEach(async () => {
await rm(projectDir, { recursive: true, force: true });
});
async function seedProjectEntities(): Promise<void> {
await mkdir(join(projectDir, 'semantic-layer', 'warehouse'), { recursive: true });
await writeFile(
join(projectDir, 'semantic-layer', 'warehouse', 'orders.yaml'),
['name: orders', 'table: public.orders', 'grain: [order_id]', 'columns: []', ''].join('\n'),
'utf-8',
);
await mkdir(join(projectDir, 'semantic-layer', 'analytics'), { recursive: true });
await writeFile(
join(projectDir, 'semantic-layer', 'analytics', 'orders.yaml'),
['name: orders', 'table: public.analytics_orders', 'grain: [order_id]', 'columns: []', ''].join('\n'),
'utf-8',
);
await writeFile(
join(projectDir, 'semantic-layer', 'analytics', 'tickets.yaml'),
['name: tickets', 'table: public.tickets', 'grain: [ticket_id]', 'columns: []', ''].join('\n'),
'utf-8',
);
await mkdir(join(projectDir, 'wiki', 'global'), { recursive: true });
await writeFile(
join(projectDir, 'wiki', 'global', 'revenue.md'),
['---', 'summary: Revenue', 'tags: []', 'refs: []', 'sl_refs: []', '---', '', 'Revenue rules.', ''].join('\n'),
'utf-8',
);
}
it('completes connection ids for the `connection test` positional', async () => {
const providers = createProjectCompletionProviders();
const result = await providers.positionalCandidates(['connection', 'test'], ['--project-dir', projectDir]);
expect(result).toEqual(['analytics', 'warehouse']);
});
it('completes connection ids for the `ingest` positional', async () => {
const providers = createProjectCompletionProviders();
const result = await providers.positionalCandidates(['ingest'], ['--project-dir', projectDir]);
expect(result).toEqual(['analytics', 'warehouse']);
});
it('completes entity names only for read and validate subcommands', async () => {
await seedProjectEntities();
const providers = createProjectCompletionProviders();
await expect(providers.positionalCandidates(['sl'], ['--project-dir', projectDir])).resolves.toEqual([]);
await expect(providers.positionalCandidates(['sl', 'read'], ['--project-dir', projectDir])).resolves.toEqual([
'orders',
'tickets',
]);
await expect(providers.positionalCandidates(['sl', 'validate'], ['--project-dir', projectDir])).resolves.toEqual([
'orders',
'tickets',
]);
await expect(
providers.positionalCandidates(['sl', 'read'], ['--project-dir', projectDir, '--connection-id', 'warehouse']),
).resolves.toEqual(['orders']);
await expect(
providers.positionalCandidates(['sl', 'validate'], ['--project-dir', projectDir, '--connection-id', 'analytics']),
).resolves.toEqual(['orders', 'tickets']);
await expect(providers.positionalCandidates(['wiki'], ['--project-dir', projectDir])).resolves.toEqual([]);
await expect(providers.positionalCandidates(['wiki', 'read'], ['--project-dir', projectDir])).resolves.toEqual([
'revenue',
]);
});
it('returns no positional candidates outside a project', async () => {
const providers = createProjectCompletionProviders();
const result = await providers.positionalCandidates(['connection', 'test'], ['--project-dir', join(projectDir, 'nope')]);
expect(result).toEqual([]);
});
it('completes connection ids for the sql --connection option', async () => {
const providers = createProjectCompletionProviders();
const result = await providers.optionValueCandidates(['sql'], '--connection', ['--project-dir', projectDir]);
expect(result).toEqual(['analytics', 'warehouse']);
});
it('still completes connection ids for the --connection-id option', async () => {
const providers = createProjectCompletionProviders();
const result = await providers.optionValueCandidates(['ingest'], '--connection-id', ['--project-dir', projectDir]);
expect(result).toEqual(['analytics', 'warehouse']);
});
});