-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild-probes.test.ts
More file actions
212 lines (193 loc) · 8.46 KB
/
Copy pathbuild-probes.test.ts
File metadata and controls
212 lines (193 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { runBuildProbes, type ProbeEngine } from '@objectstack/metadata-protocol';
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
/**
* ADR-0038 L3 — runtime probes. Each probe is one real read; findings are
* BuildIssue-shaped (layer 'runtime'); a probe must report, never throw.
*/
const ITEMS: Record<string, unknown> = {
'seed expense_sample': { object: 'expense', records: [{ name: 'a' }] },
'view expense.all': { name: 'expense.all', object: 'expense', viewKind: 'list', config: {} },
'dashboard spending': {
name: 'spending',
widgets: [{ id: 'w1', dataset: 'expense_ds', values: ['amount'] }],
},
'dataset expense_ds': {
name: 'expense_ds',
object: 'expense',
measures: [{ name: 'count', aggregate: 'count' }, { name: 'amount', aggregate: 'sum', field: 'amount' }],
dimensions: [],
},
};
const getItem = async (type: string, name: string) => ITEMS[`${type} ${name}`];
function engineWithRows(rowsByObject: Record<string, number>): ProbeEngine {
return {
find: async (object: string) =>
Array.from({ length: Math.min(rowsByObject[object] ?? 0, 1) }, (_, i) => ({ id: String(i) })),
};
}
describe('runBuildProbes — seeds', () => {
it('flags seed_not_applied when the seeded object has no rows', async () => {
const report = await runBuildProbes({
engine: engineWithRows({ expense: 0 }),
getItem,
published: [{ type: 'seed', name: 'expense_sample' }],
});
expect(report.checked.seeds).toBe(1);
expect(report.issues).toHaveLength(1);
expect(report.issues[0]).toMatchObject({
layer: 'runtime',
severity: 'error',
code: 'seed_not_applied',
artifact: { type: 'seed', name: 'expense_sample' },
ref: { type: 'object', name: 'expense' },
});
});
it('passes when rows exist', async () => {
const report = await runBuildProbes({
engine: engineWithRows({ expense: 3 }),
getItem,
published: [{ type: 'seed', name: 'expense_sample' }],
});
expect(report.issues).toHaveLength(0);
expect(report.checked.seeds).toBe(1);
});
});
describe('runBuildProbes — views', () => {
it('flags view_read_failed when the read throws; empty tables are fine', async () => {
const engine: ProbeEngine = {
find: async (object: string) => {
if (object === 'expense') throw new Error('no such table: expense');
return [];
},
};
const report = await runBuildProbes({
engine,
getItem,
published: [{ type: 'view', name: 'expense.all' }],
});
expect(report.checked.views).toBe(1);
expect(report.issues).toHaveLength(1);
expect(report.issues[0]).toMatchObject({
code: 'view_read_failed',
severity: 'error',
artifact: { type: 'view', name: 'expense.all' },
});
expect(report.issues[0].message).toContain('no such table');
const ok = await runBuildProbes({
engine: engineWithRows({}), // empty result, no throw
getItem,
published: [{ type: 'view', name: 'expense.all' }],
});
expect(ok.issues).toHaveLength(0);
});
});
describe('runBuildProbes — dashboard widgets', () => {
it('flags empty_query when the dataset returns nothing on a populated object (incident #4)', async () => {
const analytics = { queryDataset: vi.fn(async () => ({ rows: [] })) };
const report = await runBuildProbes({
engine: engineWithRows({ expense: 5 }),
getItem,
analytics,
published: [{ type: 'dashboard', name: 'spending' }],
});
expect(analytics.queryDataset).toHaveBeenCalledOnce();
// The widget's own values are used as the probe selection.
expect((analytics.queryDataset.mock.calls[0][1] as any).measures).toEqual(['amount']);
expect(report.checked.widgets).toBe(1);
expect(report.issues).toHaveLength(1);
expect(report.issues[0]).toMatchObject({
code: 'empty_query',
severity: 'error',
artifact: { type: 'dashboard', name: 'spending' },
ref: { type: 'dataset', name: 'expense_ds', member: 'w1' },
});
});
it('passes when the query returns data; empty-on-empty-object is fine', async () => {
const withData = await runBuildProbes({
engine: engineWithRows({ expense: 5 }),
getItem,
analytics: { queryDataset: async () => ({ rows: [{ amount: 42 }] }) },
published: [{ type: 'dashboard', name: 'spending' }],
});
expect(withData.issues).toHaveLength(0);
const emptyObject = await runBuildProbes({
engine: engineWithRows({ expense: 0 }),
getItem,
analytics: { queryDataset: async () => ({ rows: [] }) },
published: [{ type: 'dashboard', name: 'spending' }],
});
expect(emptyObject.issues).toHaveLength(0); // no rows promised, none missing
});
it('flags widget_query_failed when the query throws', async () => {
const report = await runBuildProbes({
engine: engineWithRows({ expense: 5 }),
getItem,
analytics: { queryDataset: async () => { throw new Error('RAW_SQL_UNSUPPORTED'); } },
published: [{ type: 'dashboard', name: 'spending' }],
});
expect(report.issues).toHaveLength(1);
expect(report.issues[0].code).toBe('widget_query_failed');
expect(report.issues[0].message).toContain('RAW_SQL_UNSUPPORTED');
});
it('emits ONE probes_unavailable warning when widgets exist but no analytics service does', async () => {
const report = await runBuildProbes({
engine: engineWithRows({ expense: 5 }),
getItem,
published: [{ type: 'dashboard', name: 'spending' }],
});
expect(report.checked.widgets).toBe(0);
expect(report.issues).toHaveLength(1);
expect(report.issues[0]).toMatchObject({ code: 'probes_unavailable', severity: 'warning' });
});
it('never throws — unreadable items and engine crashes degrade to findings/skips', async () => {
const report = await runBuildProbes({
engine: { find: async () => { throw new Error('engine down'); } },
getItem: async () => { throw new Error('metadata down'); },
published: [
{ type: 'seed', name: 'expense_sample' },
{ type: 'view', name: 'expense.all' },
{ type: 'dashboard', name: 'spending' },
],
});
// getItem failures mean no object bindings resolve — nothing probed, nothing thrown.
expect(report.checked).toEqual({ seeds: 0, views: 0, widgets: 0 });
});
});
describe('publishPackageDrafts — probes ride the response (ADR-0038 L3)', () => {
it('runs probes over the published set and reports seed_not_applied', async () => {
const protocol = new ObjectStackProtocolImplementation({} as never);
(protocol as any).ensureOverlayIndex = async () => {};
(protocol as any).getOverlayRepo = () => ({
listDrafts: async () => [
{ type: 'object', name: 'expense' },
{ type: 'seed', name: 'expense_sample' },
],
get: async (_ref: any, opts: any) =>
opts?.state === 'draft' ? { body: ITEMS['seed expense_sample'], hash: 'h' } : null,
});
// ADR-0067 D2 — the batch promotes via the phase-1 seam; side effects are phase 2.
vi.spyOn(protocol as any, 'promoteDraftForPublish').mockImplementation(async (req: any) => ({
singularType: req.type,
orgId: null,
result: { version: 'h', seq: 1, item: { body: ITEMS[`${req.type} ${req.name}`] ?? { name: req.name } }, packageId: null },
}));
vi.spyOn(protocol as any, 'runPublishSideEffects').mockResolvedValue({});
vi.spyOn(protocol as any, 'applySeedBodies').mockResolvedValue({ success: false, inserted: 0, updated: 0, error: 'boom' });
// Probe reads: active items + an engine whose table stayed empty.
(protocol as any).getMetaItem = async ({ type, name }: any) => ({ item: ITEMS[`${type} ${name}`] });
(protocol as any).engine = { find: async () => [] };
const res = await protocol.publishPackageDrafts({ packageId: 'app.exp' });
expect(res.probes).toBeDefined();
expect(res.probes!.checked.seeds).toBe(1);
expect(res.probes!.issues.map((i) => i.code)).toEqual(['seed_not_applied']);
});
it('omits probes when nothing was published', async () => {
const protocol = new ObjectStackProtocolImplementation({} as never);
(protocol as any).ensureOverlayIndex = async () => {};
(protocol as any).getOverlayRepo = () => ({ listDrafts: async () => [] });
const res = await protocol.publishPackageDrafts({ packageId: 'app.empty' });
expect(res.probes).toBeUndefined();
});
});