mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-07 07:55:13 +02:00
* 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
307 lines
10 KiB
TypeScript
307 lines
10 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
pickDatabaseScope,
|
|
type DatabaseScopePromptAdapter,
|
|
type DatabaseTreePickerRenderer,
|
|
type PickDatabaseScopeArgs,
|
|
} from '../src/database-tree-picker.js';
|
|
import type { TreePickerChrome, TreePickerResult } from '../src/tree-picker-tui.js';
|
|
import type { PickerState } from '../src/tree-picker-state.js';
|
|
|
|
function makeIo() {
|
|
let stdout = '';
|
|
let stderr = '';
|
|
return {
|
|
io: {
|
|
stdout: {
|
|
isTTY: true,
|
|
write: (chunk: string) => {
|
|
stdout += chunk;
|
|
},
|
|
},
|
|
stderr: {
|
|
write: (chunk: string) => {
|
|
stderr += chunk;
|
|
},
|
|
},
|
|
},
|
|
stdout: () => stdout,
|
|
stderr: () => stderr,
|
|
};
|
|
}
|
|
|
|
function captureRenderer(): {
|
|
renderer: DatabaseTreePickerRenderer;
|
|
capture: { chrome?: TreePickerChrome; state?: PickerState };
|
|
setResult: (result: TreePickerResult) => void;
|
|
} {
|
|
const capture: { chrome?: TreePickerChrome; state?: PickerState } = {};
|
|
let nextResult: TreePickerResult = { kind: 'quit' };
|
|
const renderer: DatabaseTreePickerRenderer = vi.fn(async (chrome, state) => {
|
|
capture.chrome = chrome;
|
|
capture.state = state;
|
|
return nextResult;
|
|
});
|
|
return {
|
|
renderer,
|
|
capture,
|
|
setResult: (result) => {
|
|
nextResult = result;
|
|
},
|
|
};
|
|
}
|
|
|
|
const discovered = [
|
|
{ catalog: null, schema: 'analytics', name: 'customers', kind: 'table' as const },
|
|
{ catalog: null, schema: 'analytics', name: 'orders', kind: 'table' as const },
|
|
{ catalog: null, schema: 'public', name: 'events', kind: 'view' as const },
|
|
{ catalog: null, schema: 'public', name: 'sessions', kind: 'table' as const },
|
|
];
|
|
|
|
function promptAdapter(overrides: Partial<DatabaseScopePromptAdapter> = {}): DatabaseScopePromptAdapter {
|
|
return {
|
|
autocompleteMultiselect: vi.fn(async () => ['analytics']),
|
|
select: vi.fn(async () => 'refine'),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function baseArgs(overrides: Partial<PickDatabaseScopeArgs> = {}): PickDatabaseScopeArgs {
|
|
return {
|
|
connectionId: 'warehouse',
|
|
schemaNoun: 'schema',
|
|
schemaNounPlural: 'schemas',
|
|
schemas: ['analytics', 'public'],
|
|
schemaSuggestion: { excluded: new Set(), suggested: new Set(['analytics']) },
|
|
existing: { enabledTables: [] },
|
|
supportsSchemaScope: true,
|
|
listTablesForSchemas: vi.fn(async () => discovered),
|
|
prompts: promptAdapter(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('pickDatabaseScope', () => {
|
|
it('starts Stage 1 with no checked schemas and does not enumerate tables before schema selection', async () => {
|
|
const prompts = promptAdapter({
|
|
autocompleteMultiselect: vi.fn(async () => ['analytics']),
|
|
select: vi.fn(async () => 'save'),
|
|
});
|
|
const listTablesForSchemas = vi.fn(async () => [
|
|
{ catalog: null, schema: 'analytics', name: 'orders', kind: 'table' as const },
|
|
]);
|
|
|
|
const result = await pickDatabaseScope(
|
|
baseArgs({
|
|
connectionId: 'warehouse',
|
|
schemaNoun: 'dataset',
|
|
schemaNounPlural: 'datasets',
|
|
schemas: ['analytics', 'raw'],
|
|
schemaSuggestion: { excluded: new Set(['raw']), suggested: new Set(['analytics']) },
|
|
listTablesForSchemas,
|
|
prompts,
|
|
}),
|
|
makeIo().io,
|
|
captureRenderer().renderer,
|
|
);
|
|
|
|
expect(listTablesForSchemas).toHaveBeenCalledTimes(1);
|
|
expect(listTablesForSchemas).toHaveBeenCalledWith(['analytics']);
|
|
expect(result).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics'],
|
|
enabledTables: ['analytics.orders'],
|
|
});
|
|
});
|
|
|
|
it('emits fully-qualified catalog.schema.name ids for catalog-bearing drivers and round-trips existing selection', async () => {
|
|
const promptsSave = promptAdapter({
|
|
autocompleteMultiselect: vi.fn(async () => ['analytics']),
|
|
select: vi.fn(async () => 'save'),
|
|
});
|
|
const listTablesForSchemas = vi.fn(async () => [
|
|
{ catalog: 'project-1', schema: 'analytics', name: 'orders', kind: 'table' as const },
|
|
{ catalog: 'project-1', schema: 'analytics', name: 'customers', kind: 'table' as const },
|
|
]);
|
|
const saveResult = await pickDatabaseScope(
|
|
baseArgs({
|
|
schemas: ['analytics'],
|
|
schemaSuggestion: { excluded: new Set(), suggested: new Set(['analytics']) },
|
|
listTablesForSchemas,
|
|
prompts: promptsSave,
|
|
}),
|
|
makeIo().io,
|
|
captureRenderer().renderer,
|
|
);
|
|
expect(saveResult).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics'],
|
|
enabledTables: ['project-1.analytics.orders', 'project-1.analytics.customers'],
|
|
});
|
|
|
|
const { renderer, capture, setResult } = captureRenderer();
|
|
setResult({
|
|
kind: 'save',
|
|
selectedIds: ['project-1.analytics.orders'],
|
|
});
|
|
const refineResult = await pickDatabaseScope(
|
|
baseArgs({
|
|
schemas: ['analytics'],
|
|
schemaSuggestion: { excluded: new Set(), suggested: new Set(['analytics']) },
|
|
existing: { enabledTables: ['project-1.analytics.orders'] },
|
|
listTablesForSchemas,
|
|
prompts: promptAdapter({
|
|
autocompleteMultiselect: vi.fn(async () => ['analytics']),
|
|
select: vi.fn(async () => 'refine'),
|
|
}),
|
|
}),
|
|
makeIo().io,
|
|
renderer,
|
|
);
|
|
expect(refineResult).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics'],
|
|
enabledTables: ['project-1.analytics.orders'],
|
|
});
|
|
expect([...(capture.state?.checked ?? [])]).toContain('project-1.analytics.orders');
|
|
});
|
|
|
|
it('routes partial existing allowlists through Stage 2 so save preserves table selections', async () => {
|
|
const { renderer, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics.customers'] });
|
|
const prompts = promptAdapter({
|
|
autocompleteMultiselect: vi.fn(async () => ['analytics']),
|
|
select: vi.fn(async () => 'save'),
|
|
});
|
|
const listTablesForSchemas = vi.fn(async () => [
|
|
{ catalog: null, schema: 'analytics', name: 'customers', kind: 'table' as const },
|
|
{ catalog: null, schema: 'analytics', name: 'orders', kind: 'table' as const },
|
|
]);
|
|
|
|
const result = await pickDatabaseScope(
|
|
baseArgs({
|
|
schemas: ['analytics'],
|
|
schemaSuggestion: { excluded: new Set(), suggested: new Set(['analytics']) },
|
|
existing: { enabledTables: ['analytics.customers'] },
|
|
listTablesForSchemas,
|
|
prompts,
|
|
}),
|
|
makeIo().io,
|
|
renderer,
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics'],
|
|
enabledTables: ['analytics.customers'],
|
|
});
|
|
});
|
|
|
|
it('builds a 2-level tree (schemas as parents, tables as children) and uses save-empty action', async () => {
|
|
const { renderer, capture, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics'] });
|
|
|
|
await pickDatabaseScope(baseArgs(), makeIo().io, renderer);
|
|
|
|
expect(capture.state?.skipEmptyAction).toBe('save-empty');
|
|
const schemaIds = capture.state?.tree.filter((n) => n.parentId === null).map((n) => n.id);
|
|
const tableIds = capture.state?.tree.filter((n) => n.parentId !== null).map((n) => n.id);
|
|
expect((schemaIds ?? []).sort()).toEqual(['analytics', 'public']);
|
|
expect((tableIds ?? []).sort()).toEqual([
|
|
'analytics.customers',
|
|
'analytics.orders',
|
|
'public.events',
|
|
'public.sessions',
|
|
]);
|
|
expect([...(capture.state?.expanded ?? [])].sort()).toEqual(['analytics', 'public']);
|
|
expect(capture.state?.byId.get('public.events')?.title).toBe('events (view)');
|
|
});
|
|
|
|
it('pre-checks selected schemas at the parent level when no existing selection reaches Stage 2', async () => {
|
|
const { renderer, capture, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics'] });
|
|
|
|
await pickDatabaseScope(baseArgs(), makeIo().io, renderer);
|
|
|
|
expect([...(capture.state?.checked ?? [])]).toEqual(['analytics']);
|
|
});
|
|
|
|
it('collapses an existing full-schema selection back into the parent check', async () => {
|
|
const { renderer, capture, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics'] });
|
|
|
|
await pickDatabaseScope(
|
|
baseArgs({ existing: { enabledTables: ['analytics.customers', 'analytics.orders'] } }),
|
|
makeIo().io,
|
|
renderer,
|
|
);
|
|
|
|
expect([...(capture.state?.checked ?? [])]).toEqual(['analytics']);
|
|
});
|
|
|
|
it('keeps a partial existing selection at the leaf level', async () => {
|
|
const { renderer, capture, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics.customers'] });
|
|
|
|
await pickDatabaseScope(
|
|
baseArgs({ existing: { enabledTables: ['analytics.customers'] } }),
|
|
makeIo().io,
|
|
renderer,
|
|
);
|
|
|
|
expect([...(capture.state?.checked ?? [])]).toEqual(['analytics.customers']);
|
|
});
|
|
|
|
it('expands a selected schema parent into all its tables and derives activeSchemas', async () => {
|
|
const { renderer, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics'] });
|
|
|
|
const result = await pickDatabaseScope(baseArgs(), makeIo().io, renderer);
|
|
|
|
expect(result).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics'],
|
|
enabledTables: ['analytics.customers', 'analytics.orders'],
|
|
});
|
|
});
|
|
|
|
it('combines parent and individual leaf selections without duplicate tables', async () => {
|
|
const { renderer, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics', 'public.events'] });
|
|
|
|
const result = await pickDatabaseScope(baseArgs(), makeIo().io, renderer);
|
|
|
|
expect(result).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: ['analytics', 'public'],
|
|
enabledTables: ['analytics.customers', 'analytics.orders', 'public.events'],
|
|
});
|
|
});
|
|
|
|
it('omits activeSchemas when the driver does not support a schema scope', async () => {
|
|
const { renderer, setResult } = captureRenderer();
|
|
setResult({ kind: 'save', selectedIds: ['analytics'] });
|
|
|
|
const result = await pickDatabaseScope(
|
|
baseArgs({ supportsSchemaScope: false }),
|
|
makeIo().io,
|
|
renderer,
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
kind: 'selected',
|
|
activeSchemas: [],
|
|
enabledTables: ['analytics.customers', 'analytics.orders'],
|
|
});
|
|
});
|
|
|
|
it('returns back when Stage 1 is cancelled', async () => {
|
|
const prompts = promptAdapter({
|
|
autocompleteMultiselect: vi.fn(async () => ['back']),
|
|
});
|
|
|
|
const result = await pickDatabaseScope(baseArgs({ prompts }), makeIo().io, captureRenderer().renderer);
|
|
|
|
expect(result).toEqual({ kind: 'back' });
|
|
});
|
|
});
|