ktx/scripts/build-benchmark-snapshot.test.mjs
Andrey Avtomonov 56985b7e09
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

263 lines
8.6 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { describe, it } from 'node:test';
import { buildBenchmarkSnapshot } from './build-benchmark-snapshot.mjs';
const require = createRequire(new URL('../packages/cli/package.json', import.meta.url));
const Database = require('better-sqlite3');
describe('buildBenchmarkSnapshot', () => {
it('emits a KtxSchemaSnapshot-shaped object plus expected-links from declared FKs', () => {
const db = new Database(':memory:');
db.exec(`
PRAGMA foreign_keys = ON;
CREATE TABLE accounts (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
account_id INTEGER NOT NULL REFERENCES accounts(id),
total REAL,
created_at TEXT
);
INSERT INTO accounts (id, name) VALUES (1, 'a'), (2, 'b');
INSERT INTO orders (id, account_id, total, created_at) VALUES
(1, 1, 10.0, '2024-01-01'), (2, 1, 20.0, '2024-01-02'), (3, 2, 30.0, '2024-01-03');
`);
const result = buildBenchmarkSnapshot({ db, fixtureId: 'fixture_x' });
db.close();
assert.equal(result.snapshot.connectionId, 'fixture_x');
assert.equal(result.snapshot.driver, 'sqlite');
assert.equal(result.snapshot.tables.length, 2);
const accounts = result.snapshot.tables.find((t) => t.name === 'accounts');
assert.ok(accounts);
assert.equal(accounts.estimatedRows, 2);
assert.deepEqual(accounts.foreignKeys, []);
const idCol = accounts.columns.find((c) => c.name === 'id');
assert.equal(idCol.primaryKey, true);
assert.equal(idCol.normalizedType, 'integer');
assert.equal(idCol.dimensionType, 'number');
const orders = result.snapshot.tables.find((t) => t.name === 'orders');
assert.equal(orders.foreignKeys.length, 1);
assert.equal(orders.foreignKeys[0].fromColumn, 'account_id');
assert.equal(orders.foreignKeys[0].toTable, 'accounts');
assert.equal(orders.foreignKeys[0].toColumn, 'id');
const createdAt = orders.columns.find((c) => c.name === 'created_at');
assert.equal(createdAt.dimensionType, 'time');
const total = orders.columns.find((c) => c.name === 'total');
assert.equal(total.dimensionType, 'number');
assert.equal(total.nullable, true);
assert.deepEqual(
result.expected.expectedPks.sort((a, b) => a.table.localeCompare(b.table)),
[
{ table: 'accounts', columns: ['id'] },
{ table: 'orders', columns: ['id'] },
],
);
assert.deepEqual(result.expected.expectedLinks, [
{
fromTable: 'orders',
fromColumns: ['account_id'],
toTable: 'accounts',
toColumns: ['id'],
relationship: 'many_to_one',
},
]);
});
it('skips internal SQLite tables (sqlite_*) and views', () => {
const db = new Database(':memory:');
db.exec(`
CREATE TABLE keep_me (id INTEGER PRIMARY KEY);
CREATE VIEW keep_me_view AS SELECT id FROM keep_me;
INSERT INTO keep_me (id) VALUES (1);
`);
const result = buildBenchmarkSnapshot({ db, fixtureId: 'fx' });
db.close();
assert.equal(result.snapshot.tables.length, 1);
assert.equal(result.snapshot.tables[0].name, 'keep_me');
});
it('groups composite foreign keys into a single ordered link', () => {
const db = new Database(':memory:');
db.exec(`
PRAGMA foreign_keys = ON;
CREATE TABLE order_lines (
order_id INTEGER NOT NULL,
line_number INTEGER NOT NULL,
sku TEXT NOT NULL,
PRIMARY KEY (order_id, line_number)
);
CREATE TABLE allocations (
id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL,
line_number INTEGER NOT NULL,
FOREIGN KEY (order_id, line_number) REFERENCES order_lines(order_id, line_number)
);
`);
const result = buildBenchmarkSnapshot({ db, fixtureId: 'fx' });
db.close();
const composite = result.expected.expectedLinks.find((l) => l.fromTable === 'allocations');
assert.deepEqual(composite, {
fromTable: 'allocations',
fromColumns: ['order_id', 'line_number'],
toTable: 'order_lines',
toColumns: ['order_id', 'line_number'],
relationship: 'many_to_one',
});
const compositePk = result.expected.expectedPks.find((p) => p.table === 'order_lines');
assert.deepEqual(compositePk.columns, ['order_id', 'line_number']);
});
it('derives expected PKs and grouped FKs from an existing snapshot', async () => {
const { expectedLinksFromSnapshot } = await import('./build-benchmark-snapshot.mjs');
const expected = expectedLinksFromSnapshot({
connectionId: 'fixture',
driver: 'sqlite',
extractedAt: '2026-05-07T00:00:00.000Z',
scope: {},
metadata: {},
tables: [
{
catalog: null,
db: 'main',
name: 'Sales.SalesOrderHeader',
kind: 'table',
comment: null,
estimatedRows: 3,
columns: [
{
name: 'SalesOrderID',
nativeType: 'int',
normalizedType: 'integer',
dimensionType: 'number',
nullable: false,
primaryKey: true,
comment: null,
},
{
name: 'CustomerID',
nativeType: 'int',
normalizedType: 'integer',
dimensionType: 'number',
nullable: false,
primaryKey: false,
comment: null,
},
],
foreignKeys: [
{
fromColumn: 'CustomerID',
toCatalog: null,
toDb: 'main',
toTable: 'Sales.Customer',
toColumn: 'CustomerID',
constraintName: 'FK_SalesOrderHeader_Customer_CustomerID',
},
],
},
{
catalog: null,
db: 'main',
name: 'Sales.Customer',
kind: 'table',
comment: null,
estimatedRows: 2,
columns: [
{
name: 'CustomerID',
nativeType: 'int',
normalizedType: 'integer',
dimensionType: 'number',
nullable: false,
primaryKey: true,
comment: null,
},
],
foreignKeys: [],
},
{
catalog: null,
db: 'main',
name: 'Sales.SalesOrderDetail',
kind: 'table',
comment: null,
estimatedRows: 6,
columns: [
{
name: 'SalesOrderID',
nativeType: 'int',
normalizedType: 'integer',
dimensionType: 'number',
nullable: false,
primaryKey: true,
comment: null,
},
{
name: 'SalesOrderDetailID',
nativeType: 'int',
normalizedType: 'integer',
dimensionType: 'number',
nullable: false,
primaryKey: true,
comment: null,
},
],
foreignKeys: [
{
fromColumn: 'SalesOrderID',
toCatalog: null,
toDb: 'main',
toTable: 'Sales.SalesOrderHeader',
toColumn: 'SalesOrderID',
constraintName: 'FK_SalesOrderDetail_SalesOrderHeader_SalesOrderID',
},
],
},
],
});
assert.deepEqual(expected.expectedPks, [
{ table: 'Sales.Customer', columns: ['CustomerID'] },
{ table: 'Sales.SalesOrderDetail', columns: ['SalesOrderID', 'SalesOrderDetailID'] },
{ table: 'Sales.SalesOrderHeader', columns: ['SalesOrderID'] },
]);
assert.deepEqual(expected.expectedLinks, [
{
fromTable: 'Sales.SalesOrderDetail',
fromColumns: ['SalesOrderID'],
toTable: 'Sales.SalesOrderHeader',
toColumns: ['SalesOrderID'],
relationship: 'many_to_one',
},
{
fromTable: 'Sales.SalesOrderHeader',
fromColumns: ['CustomerID'],
toTable: 'Sales.Customer',
toColumns: ['CustomerID'],
relationship: 'many_to_one',
},
]);
});
it('exposes relationship benchmarks as an explicit CLI package script', async () => {
const packageJson = JSON.parse(await readFile(new URL('../packages/cli/package.json', import.meta.url), 'utf8'));
assert.equal(
packageJson.scripts['relationships:benchmarks:test'],
'KTX_RUN_RELATIONSHIP_BENCHMARKS=1 vitest run test/context/scan/relationship-benchmarks.test.ts',
);
});
});