mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-22 08:38:08 +02:00
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
This commit is contained in:
parent
924868841d
commit
56985b7e09
548 changed files with 5048 additions and 2228 deletions
|
|
@ -1,65 +0,0 @@
|
|||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { createCliOperationalLogger, createNoopOperationalLogger } from './logger.js';
|
||||
|
||||
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('createCliOperationalLogger', () => {
|
||||
it('routes operational messages to stderr outside JSON mode', () => {
|
||||
const io = makeIo();
|
||||
const logger = createCliOperationalLogger(io.io, 'plain');
|
||||
|
||||
logger.log('progress');
|
||||
logger.warn('warning');
|
||||
logger.error('failure');
|
||||
logger.debug?.('debug');
|
||||
|
||||
expect(io.stdout()).toBe('');
|
||||
expect(io.stderr()).toBe('progress\nwarning\nfailure\ndebug\n');
|
||||
});
|
||||
|
||||
it('suppresses operational messages in JSON mode by default', () => {
|
||||
const io = makeIo();
|
||||
const logger = createCliOperationalLogger(io.io, 'json');
|
||||
|
||||
logger.log('progress');
|
||||
logger.warn('warning');
|
||||
logger.error('failure');
|
||||
logger.debug?.('debug');
|
||||
|
||||
expect(io.stdout()).toBe('');
|
||||
expect(io.stderr()).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createNoopOperationalLogger', () => {
|
||||
it('never writes', () => {
|
||||
const logger = createNoopOperationalLogger();
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
|
||||
logger.log('progress');
|
||||
logger.warn('warning');
|
||||
logger.error('failure');
|
||||
logger.debug?.('debug');
|
||||
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import type { KtxCliIo } from '../cli-runtime.js';
|
||||
import { resolveOutputMode } from './mode.js';
|
||||
|
||||
function ioWith(isTTY: boolean | undefined): KtxCliIo {
|
||||
return {
|
||||
stdout: { isTTY, write: () => {} },
|
||||
stderr: { write: () => {} },
|
||||
};
|
||||
}
|
||||
|
||||
describe('resolveOutputMode', () => {
|
||||
it('uses explicit value when provided', () => {
|
||||
expect(resolveOutputMode({ explicit: 'pretty', io: ioWith(false), env: {} })).toBe('pretty');
|
||||
expect(resolveOutputMode({ explicit: 'plain', io: ioWith(true), env: {} })).toBe('plain');
|
||||
expect(resolveOutputMode({ explicit: 'json', io: ioWith(true), env: {} })).toBe('json');
|
||||
});
|
||||
|
||||
it('json:true takes precedence over explicit value', () => {
|
||||
expect(resolveOutputMode({ explicit: 'pretty', json: true, io: ioWith(true), env: {} })).toBe('json');
|
||||
});
|
||||
|
||||
it('throws on unknown explicit value', () => {
|
||||
expect(() => resolveOutputMode({ explicit: 'fancy', io: ioWith(true), env: {} })).toThrow(/Invalid --output/);
|
||||
});
|
||||
|
||||
it('honors KTX_OUTPUT env var when no explicit value', () => {
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { KTX_OUTPUT: 'plain' } })).toBe('plain');
|
||||
expect(resolveOutputMode({ io: ioWith(false), env: { KTX_OUTPUT: 'pretty' } })).toBe('pretty');
|
||||
expect(resolveOutputMode({ io: ioWith(false), env: { KTX_OUTPUT: 'json' } })).toBe('json');
|
||||
});
|
||||
|
||||
it('throws on unknown KTX_OUTPUT', () => {
|
||||
expect(() => resolveOutputMode({ io: ioWith(true), env: { KTX_OUTPUT: 'fancy' } })).toThrow(/Invalid KTX_OUTPUT/);
|
||||
});
|
||||
|
||||
it('returns plain when CI is set to a truthy value', () => {
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { CI: 'true' } })).toBe('plain');
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '1' } })).toBe('plain');
|
||||
});
|
||||
|
||||
it('ignores CI when set to a falsy value', () => {
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '' } })).toBe('pretty');
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { CI: '0' } })).toBe('pretty');
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: { CI: 'false' } })).toBe('pretty');
|
||||
});
|
||||
|
||||
it('returns pretty when stdout is a TTY and CI is not set', () => {
|
||||
expect(resolveOutputMode({ io: ioWith(true), env: {} })).toBe('pretty');
|
||||
});
|
||||
|
||||
it('returns plain when stdout is not a TTY', () => {
|
||||
expect(resolveOutputMode({ io: ioWith(false), env: {} })).toBe('plain');
|
||||
expect(resolveOutputMode({ io: ioWith(undefined), env: {} })).toBe('plain');
|
||||
});
|
||||
|
||||
it('explicit value beats KTX_OUTPUT env var', () => {
|
||||
expect(resolveOutputMode({ explicit: 'json', io: ioWith(true), env: { KTX_OUTPUT: 'plain' } })).toBe('json');
|
||||
});
|
||||
});
|
||||
|
|
@ -1,311 +0,0 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import type { KtxCliIo } from '../cli-runtime.js';
|
||||
import { createRankBadgeFormatter, printList, type PrintListColumn } from './print-list.js';
|
||||
import { SYMBOLS } from './symbols.js';
|
||||
|
||||
function recorder(): { io: KtxCliIo; out: () => string; err: () => string } {
|
||||
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',
|
||||
unit: 'source',
|
||||
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',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
expect(r.out()).toBe('');
|
||||
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"',
|
||||
emptyHint: 'Run `ktx sl` to see available sources.',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
expect(r.out()).toBe('');
|
||||
expect(r.err()).toBe(
|
||||
'No sources matched "foo"\n' +
|
||||
'Run `ktx sl` to see available sources.\n',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
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',
|
||||
unit: 'source',
|
||||
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',
|
||||
emptyHint: 'ignored in json mode',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
expect(JSON.parse(r.out())).toEqual({
|
||||
kind: 'list',
|
||||
data: { items: [] },
|
||||
meta: { command: 'sl list' },
|
||||
});
|
||||
expect(r.err()).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
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', () => {
|
||||
it('renders a bold header, grouped rows, and footer', () => {
|
||||
const r = recorder();
|
||||
printList<SlRow>({
|
||||
rows: [ORDERS, USERS],
|
||||
columns: SL_COLUMNS,
|
||||
groupBy: 'connectionId',
|
||||
mode: 'pretty',
|
||||
command: 'sl list',
|
||||
emptyMessage: 'No sources',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
const out = stripAnsi(r.out());
|
||||
expect(out).toContain('sl list');
|
||||
expect(out).toContain('warehouse');
|
||||
expect(out).toContain('(2 sources)');
|
||||
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`));
|
||||
expect(out).toContain(`${SYMBOLS.emDash} User profile + auth`);
|
||||
expect(out).toContain('2 sources');
|
||||
});
|
||||
|
||||
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',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
const out = stripAnsi(r.out());
|
||||
expect(out).toContain('sl list');
|
||||
expect(out).toContain('No semantic-layer sources found in /tmp/proj');
|
||||
});
|
||||
|
||||
it('renders empty-state with hint when emptyHint is provided', () => {
|
||||
const r = recorder();
|
||||
printList<SlRow>({
|
||||
rows: [],
|
||||
columns: SL_COLUMNS,
|
||||
groupBy: 'connectionId',
|
||||
mode: 'pretty',
|
||||
command: 'sl search',
|
||||
emptyMessage: 'No sources matched "foo"',
|
||||
emptyHint: 'Run `ktx sl` to see available sources.',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
const out = stripAnsi(r.out());
|
||||
expect(out).toContain('No sources matched "foo"');
|
||||
expect(out).toContain('Run `ktx sl` to see available sources.');
|
||||
});
|
||||
|
||||
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',
|
||||
unit: 'source',
|
||||
io: r.io,
|
||||
});
|
||||
const out = stripAnsi(r.out());
|
||||
expect(out).toContain('1 source');
|
||||
});
|
||||
|
||||
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)');
|
||||
expect(out).toContain('2 pages');
|
||||
});
|
||||
|
||||
it('renders a leading rank badge column in pretty mode', () => {
|
||||
const r = recorder();
|
||||
interface SearchRow { score: number; scope: string; key: string; summary: string }
|
||||
const rows: SearchRow[] = [
|
||||
{ score: 0.87, scope: 'GLOBAL', key: 'alpha', summary: 'first' },
|
||||
{ score: 0.04, scope: 'GLOBAL', key: 'beta', summary: 'second' },
|
||||
];
|
||||
const SEARCH_COLUMNS: ReadonlyArray<PrintListColumn<SearchRow>> = [
|
||||
{
|
||||
key: 'score',
|
||||
label: 'SCORE',
|
||||
plain: 'score=',
|
||||
role: 'badge',
|
||||
prettyFormat: createRankBadgeFormatter(rows),
|
||||
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());
|
||||
expect(out).toMatch(/#1\s+alpha\s+/);
|
||||
expect(out).toMatch(/#2\s+beta\s+/);
|
||||
expect(out).not.toContain('%');
|
||||
});
|
||||
|
||||
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 }
|
||||
const rows: SearchRow[] = [{ score: 0.87, scope: 'GLOBAL', key: 'alpha', summary: 'first' }];
|
||||
const SEARCH_COLUMNS: ReadonlyArray<PrintListColumn<SearchRow>> = [
|
||||
{
|
||||
key: 'score',
|
||||
label: 'SCORE',
|
||||
plain: 'score=',
|
||||
role: 'badge',
|
||||
prettyFormat: createRankBadgeFormatter(rows),
|
||||
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: 'plain',
|
||||
command: 'wiki search',
|
||||
emptyMessage: 'No matches',
|
||||
unit: 'page',
|
||||
io: r.io,
|
||||
});
|
||||
expect(r.out()).toBe('score=0.87\tGLOBAL\talpha\tfirst\n');
|
||||
});
|
||||
});
|
||||
|
||||
function escapeRegExp(s: string): string {
|
||||
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue