|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import type { components } from '$control/schema.d.ts'; |
3 | | -import { toCatalogNodes } from './http-catalog'; |
| 3 | +import { findNode, nodeDetailOf, toCatalogNodes } from './http-catalog'; |
| 4 | +import { toCheck } from './http-checks'; |
4 | 5 |
|
5 | 6 | type W = components['schemas']; |
6 | 7 |
|
@@ -53,4 +54,46 @@ describe('catalog node mapping', () => { |
53 | 54 | expect([orders.description, orders.tags, orders.columnCount, orders.relation]).toEqual(['One row per order', ['marts'], 4, { database: null, schema: 'main', name: 'orders' }]); |
54 | 55 | expect([orders.stale, orders.checks, orders.lastOutcome, orders.group, orders.owner]).toEqual([null, null, null, null, []]); |
55 | 56 | }); |
| 57 | + |
| 58 | + it('finds a node by key, or by bare name preferring the model', () => { |
| 59 | + const nodes = toCatalogNodes([node('source:orders'), node('source:customers'), node('model:orders', { materialization: 'view' })]); |
| 60 | + expect([findNode(nodes, 'source:orders')?.key, findNode(nodes, 'orders')?.key, findNode(nodes, 'customers')?.key, findNode(nodes, 'order')]).toEqual(['source:orders', 'model:orders', 'source:customers', null]); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +function check(id: string, target: string, outcome: string): W['CheckResult'] { |
| 65 | + return { event_id: `e-${id}`, binding_key: id, audit_name: id, audit_definition_name: 'not_null', attachment_kind: 'model', attached_target_name: target, severity: 'error', evaluation_mode: 'violations', run_scope_phase: 'final', occurred_at: '2026-11-07T06:00:00Z', run_id: 'r', invocation_id: 'i', outcome, reused: 'false' }; |
| 66 | +} |
| 67 | + |
| 68 | +function status(definition: W['NodeStatus']['definition'], physical: W['NodeStatus']['physical']): W['NodeStatus'] { |
| 69 | + const build = { definition_hash: 'd', event_id: 'e', observed_at: '2026-11-07T06:00:00Z', run_id: 'sqb-run-1', schema_fingerprint: 's', source_event_id: 'x', version_hash: 'v1' }; |
| 70 | + return { node_key: 'model:orders', snapshot_id: 'snap', build, definition, physical, source_data: { status: 'unknown', reason: 'not tracked' } }; |
| 71 | +} |
| 72 | + |
| 73 | +describe('catalog node detail', () => { |
| 74 | + const nodes = toCatalogNodes([node('model:orders', { materialization: 'table' }), node('model:order_totals', { materialization: 'view', dependencies: ['model:orders'] }), node('model:orders_by_day', { materialization: 'view' })]); |
| 75 | + const checks = [check('a', 'orders', 'pass'), check('b', 'model:orders', 'fail'), check('c', 'orders', 'warn'), check('d', 'orders_by_day', 'fail'), { ...check('e', 'orders', 'fail'), attachment_kind: 'source' }].map(toCheck); |
| 76 | + const observed: W['NodeStatus']['physical'] = { status: 'observed', observation: { event_id: 'o', observed_at: '2026-11-07T06:00:00Z', record: { name: 'orders', relation_type: 'table', state_schema: 'main', columns: [{ name: 'order_id', data_type: 'INTEGER' }] } } }; |
| 77 | + |
| 78 | + it('keeps only its own checks, matched by key or by bare name of the same kind, and counts them by newest outcome', () => { |
| 79 | + const d = nodeDetailOf(nodes, nodes[0], null, null, checks); |
| 80 | + expect(d.checkResults.map((c) => c.id)).toEqual(['a', 'b', 'c']); |
| 81 | + expect(d.checks).toEqual({ total: 3, passing: 1, warning: 1, failing: 1 }); |
| 82 | + expect(d.downstream).toEqual(['model:order_totals']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('reports a changed definition as stale and maps the warehouse columns', () => { |
| 86 | + const changed = nodeDetailOf(nodes, nodes[0], status({ status: 'changed', current_local_hash: 'b', recorded_local_hash: 'a' }, observed), null, []); |
| 87 | + expect(changed.stale.map((r) => [r.reason, r.since])).toEqual([['changed', '2026-11-07T06:00:00Z']]); |
| 88 | + expect(changed.columns).toEqual([{ name: 'order_id', type: 'INTEGER', description: '', audits: [] }]); |
| 89 | + const matched = nodeDetailOf(nodes, nodes[0], status({ status: 'matched', local_hash: 'a' }, { status: 'unavailable', reason: 'no catalog' }), null, []); |
| 90 | + expect([matched.stale, matched.columns]).toEqual([[], []]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('leaves refused SQL out and unreported history unknown rather than empty', () => { |
| 94 | + const refused = nodeDetailOf(nodes, nodes[0], null, null, []); |
| 95 | + expect([refused.sql, refused.runs, refused.jobs, refused.history]).toEqual([null, null, null, null]); |
| 96 | + const withSql = nodeDetailOf(nodes, nodes[0], null, { node_key: 'model:orders', snapshot_id: 'snap', raw_sql: 'select {{ x }}', compiled_sql: null }, []); |
| 97 | + expect(withSql.sql).toEqual({ raw: 'select {{ x }}', compiled: 'select {{ x }}' }); |
| 98 | + }); |
56 | 99 | }); |
0 commit comments