2026-05-10 23:12:26 +02:00
|
|
|
import { describe, expect, it } from 'vitest';
|
test: split cli tests from source tree (#216)
* feat(cli): define full warehouse dialect contract
* test(cli): keep dialect edge tests focused
* fix(cli): stabilize dialect contract foundation
* refactor(connectors): own read-only query preparation
* refactor(connectors): resolve dialects through registry
* refactor(connectors): keep concrete dialect classes internal
* chore(workspace): enforce dialect import boundary
* refactor(cli): resolve relationship dialect at scan boundary
* refactor(cli): use dialect display parsing for entity details
* refactor(cli): use dialect display parsing for warehouse catalog
* refactor(cli): use dialect SQL in relationship workflows
* test(cli): verify solid dialect scan workflow closure
* test: split cli tests from source tree
* refactor(cli): standardize BigQuery scope listing
* feat(sqlite): implement connector scope listing
* test(connectors): cover required table listing
* feat(cli): add warehouse driver registry
* refactor(setup): route scope discovery through driver registry
* refactor(cli): route local query execution through driver registry
* refactor(historic-sql): route dialect support through driver registry
* refactor(cli): test warehouse connections through driver registry
* fix(cli): close driver registry type export gaps
* Improve setup daemon diagnostics
* refactor(setup): centralize rail-prefixed diagnostics + query-history fallback
Extract errorMessage, writePrefixedLines, and flushPrefixedBufferedCommandOutput
into clack.ts so the setup wizard, managed daemons, and embedding/agent steps
share one rail-formatted writer. setup-databases.ts also adds a
"disable query history and retry" option when the schema-context build fails
and query history is the likely culprit, surfaced via a new
failed-query-history-unavailable status.
* fix(cli): carry catalog through the picker so BigQuery/Snowflake/SQL Server scope filters match
The setup picker's KtxTableListEntry was a 2-level { schema, name }, so
qualifiedTableId always wrote db.name into enabled_tables. When BigQuery,
Snowflake, or SQL Server later ran fast ingest, their introspect step filtered
the scope set with scopedTableNames(scope, { catalog: projectId|database, db })
— catalog was non-null on the introspect side but null in the scope refs, so
every entry was rejected, the live-database adapter staged zero table files,
and detect() failed with 'Adapter "live-database" did not recognize fetched
source output'.
Align the picker boundary with the canonical 3-level KtxTableRef:
- Add catalog: string | null to KtxTableListEntry.
- BigQuery/Snowflake/SQL Server listTables populate catalog from the
resolved projectId / database; Postgres/MySQL/ClickHouse/SQLite set null.
- qualifiedTableId emits catalog.schema.name when catalog is non-null
(resolveEnabledTables already accepts the 3-part shape) and
schemasFromEnabledTables now goes through parseDottedTableEntry so it
recovers the schema correctly from both 2-part and 3-part entries.
- Export parseDottedTableEntry from enabled-tables.ts (@internal) for picker
reuse.
Update listTables expectations in all seven connector tests and the setup /
picker test fixtures. Add a picker regression test that covers the
catalog-bearing round-trip (save + refine).
* fix(cli): allow debug telemetry under opt-out env
2026-05-26 08:49:05 +02:00
|
|
|
import type { KtxCliIo } from '../../src/cli-runtime.js';
|
|
|
|
|
import { createRankBadgeFormatter, printList, type PrintListColumn } from '../../src/io/print-list.js';
|
|
|
|
|
import { SYMBOLS } from '../../src/io/symbols.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
function recorder(): { io: KtxCliIo; out: () => string; err: () => string } {
|
2026-05-10 23:12:26 +02:00
|
|
|
let stdout = '';
|
|
|
|
|
let stderr = '';
|
|
|
|
|
return {
|
|
|
|
|
io: {
|
|
|
|
|
stdout: { write: (chunk: string) => { stdout += chunk; } },
|
|
|
|
|
stderr: { write: (chunk: string) => { stderr += chunk; } },
|
|
|
|
|
},
|
|
|
|
|
out: () => stdout,
|
|
|
|
|
err: () => stderr,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SlRow {
|
|
|
|
|
connectionId: string;
|
|
|
|
|
name: string;
|
|
|
|
|
columnCount: number;
|
|
|
|
|
measureCount: number;
|
|
|
|
|
joinCount: number;
|
|
|
|
|
description?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SL_COLUMNS: ReadonlyArray<PrintListColumn<SlRow>> = [
|
|
|
|
|
{ key: 'connectionId', label: 'CONNECTION', plain: '' },
|
|
|
|
|
{ key: 'name', label: 'NAME', plain: '' },
|
|
|
|
|
{ key: 'columnCount', label: 'COLS', plain: 'columns=', dim: true },
|
|
|
|
|
{ key: 'measureCount', label: 'MEASURES', plain: 'measures=', dim: true },
|
|
|
|
|
{ key: 'joinCount', label: 'JOINS', plain: 'joins=', dim: true },
|
|
|
|
|
{ key: 'description', label: 'DESCRIPTION', plain: false, optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const ORDERS: SlRow = { connectionId: 'warehouse', name: 'orders', columnCount: 5, measureCount: 3, joinCount: 1 };
|
|
|
|
|
const USERS: SlRow = { connectionId: 'warehouse', name: 'users', columnCount: 8, measureCount: 2, joinCount: 2, description: 'User profile + auth' };
|
|
|
|
|
|
|
|
|
|
describe('printList — plain mode', () => {
|
|
|
|
|
it('emits one tab-separated row per item, skipping plain:false columns', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [ORDERS, USERS],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
mode: 'plain',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
expect(r.out()).toBe(
|
|
|
|
|
'warehouse\torders\tcolumns=5\tmeasures=3\tjoins=1\n' +
|
|
|
|
|
'warehouse\tusers\tcolumns=8\tmeasures=2\tjoins=2\n',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('emits nothing on empty list (preserves current sl list zero-row behavior)', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
mode: 'plain',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
expect(r.out()).toBe('');
|
2026-05-14 15:15:20 +02:00
|
|
|
expect(r.err()).toBe('');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('routes emptyMessage + emptyHint to stderr when no rows and hint is provided', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
mode: 'plain',
|
|
|
|
|
command: 'sl search',
|
|
|
|
|
emptyMessage: 'No sources matched "foo"',
|
2026-05-20 01:52:37 +02:00
|
|
|
emptyHint: 'Run `ktx sl` to see available sources.',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
|
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
expect(r.out()).toBe('');
|
|
|
|
|
expect(r.err()).toBe(
|
|
|
|
|
'No sources matched "foo"\n' +
|
2026-05-20 01:52:37 +02:00
|
|
|
'Run `ktx sl` to see available sources.\n',
|
2026-05-14 15:15:20 +02:00
|
|
|
);
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('printList — json mode', () => {
|
|
|
|
|
it('emits the envelope with kind=list, data.items, and meta.command', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [ORDERS, USERS],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
mode: 'json',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const written = r.out();
|
|
|
|
|
expect(written.endsWith('\n')).toBe(true);
|
|
|
|
|
const parsed = JSON.parse(written);
|
|
|
|
|
expect(parsed).toEqual({
|
|
|
|
|
kind: 'list',
|
|
|
|
|
data: { items: [ORDERS, USERS] },
|
|
|
|
|
meta: { command: 'sl list' },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('emits an empty items array when no rows', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
mode: 'json',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
emptyHint: 'ignored in json mode',
|
|
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
expect(JSON.parse(r.out())).toEqual({
|
|
|
|
|
kind: 'list',
|
|
|
|
|
data: { items: [] },
|
|
|
|
|
meta: { command: 'sl list' },
|
|
|
|
|
});
|
2026-05-14 15:15:20 +02:00
|
|
|
expect(r.err()).toBe('');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function stripAnsi(s: string): string {
|
|
|
|
|
// Matches ESC [ ... m sequences emitted by node:util.styleText.
|
|
|
|
|
return s.replace(/\[[0-9;]*m/g, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('printList — pretty mode', () => {
|
2026-05-15 08:54:36 -04:00
|
|
|
it('renders a bold header, grouped rows, and footer', () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [ORDERS, USERS],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
groupBy: 'connectionId',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('sl list');
|
|
|
|
|
expect(out).toContain('warehouse');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(out).toContain('(2 sources)');
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toMatch(/orders\s+5 cols/);
|
|
|
|
|
expect(out).toMatch(new RegExp(`3 measures ${escapeRegExp(SYMBOLS.middot)} 1 join\\b`));
|
|
|
|
|
expect(out).toMatch(new RegExp(`2 measures ${escapeRegExp(SYMBOLS.middot)} 2 joins\\b`));
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(out).toContain(`${SYMBOLS.emDash} User profile + auth`);
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('2 sources');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders an empty-state message when no rows', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
groupBy: 'connectionId',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No semantic-layer sources found in /tmp/proj',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('sl list');
|
|
|
|
|
expect(out).toContain('No semantic-layer sources found in /tmp/proj');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-15 08:54:36 -04:00
|
|
|
it('renders empty-state with hint when emptyHint is provided', () => {
|
2026-05-14 15:15:20 +02:00
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
groupBy: 'connectionId',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'sl search',
|
|
|
|
|
emptyMessage: 'No sources matched "foo"',
|
2026-05-20 01:52:37 +02:00
|
|
|
emptyHint: 'Run `ktx sl` to see available sources.',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
|
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('No sources matched "foo"');
|
2026-05-20 01:52:37 +02:00
|
|
|
expect(out).toContain('Run `ktx sl` to see available sources.');
|
2026-05-14 15:15:20 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
it('singularizes the footer when there is one row', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
printList<SlRow>({
|
|
|
|
|
rows: [ORDERS],
|
|
|
|
|
columns: SL_COLUMNS,
|
|
|
|
|
groupBy: 'connectionId',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
emptyMessage: 'No sources',
|
2026-05-14 15:15:20 +02:00
|
|
|
unit: 'source',
|
2026-05-10 23:12:26 +02:00
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('1 source');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
2026-05-14 15:15:20 +02:00
|
|
|
|
|
|
|
|
it('uses the provided unit in pluralization and group counts', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
interface PageRow { scope: string; key: string; summary: string }
|
|
|
|
|
const PAGE_COLUMNS: ReadonlyArray<PrintListColumn<PageRow>> = [
|
|
|
|
|
{ key: 'scope', label: 'SCOPE', plain: '' },
|
|
|
|
|
{ key: 'key', label: 'KEY', plain: '' },
|
|
|
|
|
{ key: 'summary', label: 'SUMMARY', plain: '', optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
printList<PageRow>({
|
|
|
|
|
rows: [
|
|
|
|
|
{ scope: 'GLOBAL', key: 'a', summary: 'x' },
|
|
|
|
|
{ scope: 'GLOBAL', key: 'b', summary: '' },
|
|
|
|
|
],
|
|
|
|
|
columns: PAGE_COLUMNS,
|
|
|
|
|
groupBy: 'scope',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'wiki list',
|
|
|
|
|
emptyMessage: 'No pages',
|
|
|
|
|
unit: 'page',
|
|
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
|
|
|
|
expect(out).toContain('(2 pages)');
|
2026-05-15 08:54:36 -04:00
|
|
|
expect(out).toContain('2 pages');
|
2026-05-14 15:15:20 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-17 02:32:41 +02:00
|
|
|
it('renders a leading rank badge column in pretty mode', () => {
|
2026-05-14 15:15:20 +02:00
|
|
|
const r = recorder();
|
|
|
|
|
interface SearchRow { score: number; scope: string; key: string; summary: string }
|
2026-05-17 02:32:41 +02:00
|
|
|
const rows: SearchRow[] = [
|
|
|
|
|
{ score: 0.87, scope: 'GLOBAL', key: 'alpha', summary: 'first' },
|
|
|
|
|
{ score: 0.04, scope: 'GLOBAL', key: 'beta', summary: 'second' },
|
|
|
|
|
];
|
2026-05-14 15:15:20 +02:00
|
|
|
const SEARCH_COLUMNS: ReadonlyArray<PrintListColumn<SearchRow>> = [
|
|
|
|
|
{
|
|
|
|
|
key: 'score',
|
|
|
|
|
label: 'SCORE',
|
|
|
|
|
plain: 'score=',
|
|
|
|
|
role: 'badge',
|
2026-05-17 02:32:41 +02:00
|
|
|
prettyFormat: createRankBadgeFormatter(rows),
|
2026-05-14 15:15:20 +02:00
|
|
|
dim: true,
|
|
|
|
|
},
|
|
|
|
|
{ key: 'scope', label: 'SCOPE', plain: '' },
|
|
|
|
|
{ key: 'key', label: 'KEY', plain: '' },
|
|
|
|
|
{ key: 'summary', label: 'SUMMARY', plain: '', optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
printList<SearchRow>({
|
|
|
|
|
rows,
|
|
|
|
|
columns: SEARCH_COLUMNS,
|
|
|
|
|
groupBy: 'scope',
|
|
|
|
|
mode: 'pretty',
|
|
|
|
|
command: 'wiki search',
|
|
|
|
|
emptyMessage: 'No matches',
|
|
|
|
|
unit: 'page',
|
|
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
const out = stripAnsi(r.out());
|
2026-05-17 02:32:41 +02:00
|
|
|
expect(out).toMatch(/#1\s+alpha\s+/);
|
|
|
|
|
expect(out).toMatch(/#2\s+beta\s+/);
|
|
|
|
|
expect(out).not.toContain('%');
|
2026-05-14 15:15:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('emits the badge column in plain mode using its plain prefix', () => {
|
|
|
|
|
const r = recorder();
|
|
|
|
|
interface SearchRow { score: number; scope: string; key: string; summary: string }
|
2026-05-17 02:32:41 +02:00
|
|
|
const rows: SearchRow[] = [{ score: 0.87, scope: 'GLOBAL', key: 'alpha', summary: 'first' }];
|
2026-05-14 15:15:20 +02:00
|
|
|
const SEARCH_COLUMNS: ReadonlyArray<PrintListColumn<SearchRow>> = [
|
|
|
|
|
{
|
|
|
|
|
key: 'score',
|
|
|
|
|
label: 'SCORE',
|
|
|
|
|
plain: 'score=',
|
|
|
|
|
role: 'badge',
|
2026-05-17 02:32:41 +02:00
|
|
|
prettyFormat: createRankBadgeFormatter(rows),
|
2026-05-14 15:15:20 +02:00
|
|
|
dim: true,
|
|
|
|
|
},
|
|
|
|
|
{ key: 'scope', label: 'SCOPE', plain: '' },
|
|
|
|
|
{ key: 'key', label: 'KEY', plain: '' },
|
|
|
|
|
{ key: 'summary', label: 'SUMMARY', plain: '', optional: true, dim: true },
|
|
|
|
|
];
|
|
|
|
|
printList<SearchRow>({
|
2026-05-17 02:32:41 +02:00
|
|
|
rows,
|
2026-05-14 15:15:20 +02:00
|
|
|
columns: SEARCH_COLUMNS,
|
|
|
|
|
groupBy: 'scope',
|
|
|
|
|
mode: 'plain',
|
|
|
|
|
command: 'wiki search',
|
|
|
|
|
emptyMessage: 'No matches',
|
|
|
|
|
unit: 'page',
|
|
|
|
|
io: r.io,
|
|
|
|
|
});
|
|
|
|
|
expect(r.out()).toBe('score=0.87\tGLOBAL\talpha\tfirst\n');
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function escapeRegExp(s: string): string {
|
|
|
|
|
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
|
}
|