ktx/packages/cli/test/commands/wiki-sl-read-commands.test.ts
Andrey Avtomonov d320d54ab2
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

157 lines
4.9 KiB
TypeScript

import { Command } from '@commander-js/extra-typings';
import { describe, expect, it, vi } from 'vitest';
import type { KtxCliCommandContext } from '../../src/cli-program.js';
import { registerWikiCommands } from '../../src/commands/knowledge-commands.js';
import { registerSlCommands } from '../../src/commands/sl-commands.js';
function makeContext(overrides: Partial<KtxCliCommandContext> = {}): KtxCliCommandContext {
let exitCode = 0;
return {
io: {
stdout: { write: vi.fn() },
stderr: { write: vi.fn() },
},
deps: {},
packageInfo: { name: '@kaelio/ktx', version: '0.0.0-test' },
setExitCode: (code) => {
exitCode = code;
},
runInit: vi.fn(),
writeDebug: vi.fn(),
...overrides,
get exitCode() {
return exitCode;
},
} as KtxCliCommandContext;
}
describe('wiki and sl read command routing', () => {
it('routes wiki read through the knowledge runner', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const knowledge = vi.fn(async () => 0);
const context = makeContext({ deps: { knowledge } });
registerWikiCommands(program, context);
await expect(
program.parseAsync(['--project-dir', '/tmp/ktx-project', 'wiki', 'read', 'metrics-revenue'], {
from: 'user',
}),
).resolves.toBe(program);
expect(knowledge).toHaveBeenCalledWith(
{
command: 'read',
projectDir: '/tmp/ktx-project',
key: 'metrics-revenue',
userId: 'local',
},
context.io,
);
});
it('routes wiki read with the parent --user-id option', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const knowledge = vi.fn(async () => 0);
const context = makeContext({ deps: { knowledge } });
registerWikiCommands(program, context);
await expect(
program.parseAsync(
['--project-dir', '/tmp/ktx-project', 'wiki', '--user-id', 'alex', 'read', 'handoff'],
{ from: 'user' },
),
).resolves.toBe(program);
expect(knowledge).toHaveBeenCalledWith(
{
command: 'read',
projectDir: '/tmp/ktx-project',
key: 'handoff',
userId: 'alex',
},
context.io,
);
});
it('routes sl read through the semantic-layer runner', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const sl = vi.fn(async () => 0);
const context = makeContext({ deps: { sl } });
registerSlCommands(program, context);
await expect(
program.parseAsync(
['--project-dir', '/tmp/ktx-project', 'sl', '--connection-id', 'warehouse', 'read', 'orders'],
{ from: 'user' },
),
).resolves.toBe(program);
expect(sl).toHaveBeenCalledWith(
{
command: 'read',
projectDir: '/tmp/ktx-project',
connectionId: 'warehouse',
sourceName: 'orders',
},
context.io,
);
});
it('routes sl read without --connection-id through the semantic-layer runner', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const sl = vi.fn(async () => 0);
const context = makeContext({ deps: { sl } });
registerSlCommands(program, context);
await expect(
program.parseAsync(['--project-dir', '/tmp/ktx-project', 'sl', 'read', 'orders'], { from: 'user' }),
).resolves.toBe(program);
expect(sl).toHaveBeenCalledWith(
{
command: 'read',
projectDir: '/tmp/ktx-project',
connectionId: undefined,
sourceName: 'orders',
},
context.io,
);
});
it('routes sl validate without --connection-id through the semantic-layer runner', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const sl = vi.fn(async () => 0);
const context = makeContext({ deps: { sl } });
registerSlCommands(program, context);
await expect(
program.parseAsync(['--project-dir', '/tmp/ktx-project', 'sl', 'validate', 'orders'], { from: 'user' }),
).resolves.toBe(program);
expect(sl).toHaveBeenCalledWith(
{
command: 'validate',
projectDir: '/tmp/ktx-project',
connectionId: undefined,
sourceName: 'orders',
},
context.io,
);
});
it('keeps sl query requiring --connection-id before invoking the runner', async () => {
const program = new Command().exitOverride().option('--project-dir <path>');
const sl = vi.fn(async () => 0);
const context = makeContext({ deps: { sl } });
registerSlCommands(program, context);
await expect(
program.parseAsync(
['--project-dir', '/tmp/ktx-project', 'sl', 'query', '--measure', 'orders.count'],
{ from: 'user' },
),
).rejects.toThrow("error: required option '--connection-id <id>' not specified");
expect(sl).not.toHaveBeenCalled();
});
});