@microsoft/rayfin-semantic-model-pack 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,488 @@
1
+ //-----------------------------------------------------------------------
2
+ // <copyright company="Microsoft Corporation">
3
+ // Copyright (c) Microsoft Corporation. All rights reserved.
4
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
5
+ // </copyright>
6
+ //-----------------------------------------------------------------------
7
+
8
+ // End-to-end coverage of the `schema:sql` entry point.
9
+ //
10
+ // These spawn the real script rather than importing it, so argument wiring,
11
+ // stdout payloads and exit statuses are covered. The runtime scripts are copied
12
+ // into a temporary app with a fake `@microsoft/rayfin-cli` beside them, so
13
+ // ordinary module resolution finds the fake by construction and no request can
14
+ // reach a real tenant.
15
+
16
+ import assert from 'node:assert/strict';
17
+ import { spawn } from 'node:child_process';
18
+ import { once } from 'node:events';
19
+ import {
20
+ copyFileSync,
21
+ existsSync,
22
+ mkdirSync,
23
+ mkdtempSync,
24
+ readFileSync,
25
+ rmSync,
26
+ writeFileSync,
27
+ } from 'node:fs';
28
+ import { tmpdir } from 'node:os';
29
+ import { dirname, join } from 'node:path';
30
+ import test from 'node:test';
31
+ import { fileURLToPath } from 'node:url';
32
+
33
+ const KIT = dirname(fileURLToPath(import.meta.url));
34
+ const RUNTIME_SCRIPTS = [
35
+ 'schema-sql.mjs',
36
+ 'schema.mjs',
37
+ 'rayfin-connector.mjs',
38
+ ];
39
+ const DATASET_ID = '11111111-2222-3333-4444-555555555555';
40
+ const CALL_LOG = 'cli-calls.log';
41
+
42
+ const SOURCE_TABLES = {
43
+ 'INFO.VIEW.TABLES()': {
44
+ columns: ['Name', 'Description'],
45
+ rows: [
46
+ ['Sales', 'Sales facts'],
47
+ ['Date', 'Date dimension'],
48
+ ],
49
+ },
50
+ 'INFO.VIEW.COLUMNS()': {
51
+ columns: ['Table', 'Name', 'DataType'],
52
+ rows: [['Sales', 'Amount', 'Decimal']],
53
+ },
54
+ 'INFO.VIEW.MEASURES()': {
55
+ columns: ['Table', 'Name', 'Expression', 'Description', 'DisplayFolder'],
56
+ rows: [
57
+ ['Sales', 'Revenue', "SUM('Sales'[Amount])", 'Total revenue', 'Finance'],
58
+ ],
59
+ },
60
+ 'INFO.VIEW.RELATIONSHIPS()': {
61
+ columns: ['FromTable', 'FromColumn', 'ToTable', 'ToColumn'],
62
+ rows: [['Sales', 'DateKey', 'Date', 'DateKey']],
63
+ },
64
+ COLUMNSTATISTICS: {
65
+ columns: [
66
+ 'Table Name',
67
+ 'Column Name',
68
+ 'Min',
69
+ 'Max',
70
+ 'Cardinality',
71
+ 'Max Length',
72
+ ],
73
+ rows: [['Sales', 'Amount', '1', '99', 42, 0]],
74
+ },
75
+ };
76
+
77
+ // `deny` names a source whose DAX should fail. Every invocation is logged so a
78
+ // test can prove the fake, not a real CLI, was what answered.
79
+ function fakeCliSource(deny) {
80
+ return `
81
+ import { appendFileSync, readFileSync, writeFileSync } from 'node:fs';
82
+ import { join } from 'node:path';
83
+
84
+ const argv = process.argv.slice(2);
85
+ const tables = ${JSON.stringify(SOURCE_TABLES)};
86
+ const deny = ${JSON.stringify(deny)};
87
+ const log = (entry) => appendFileSync(
88
+ join(process.cwd(), ${JSON.stringify(CALL_LOG)}),
89
+ JSON.stringify(entry) + '\\n',
90
+ 'utf8'
91
+ );
92
+
93
+ if (argv[0] === 'connector' && argv[1] === 'list') {
94
+ log({ command: 'list' });
95
+ process.stdout.write(JSON.stringify([
96
+ {
97
+ name: 'model',
98
+ type: 'fabric-semanticmodel',
99
+ config: { itemId: ${JSON.stringify(DATASET_ID)}, workspaceId: 'ws' },
100
+ },
101
+ ]));
102
+ process.exit(0);
103
+ }
104
+
105
+ if (argv[0] === 'connector' && argv[1] === 'invoke') {
106
+ const payload = JSON.parse(readFileSync(argv[argv.indexOf('--file') + 1], 'utf8'));
107
+ const outputPath = argv[argv.indexOf('--output-file') + 1];
108
+ const key = Object.keys(tables).find((name) => payload.query.includes(name));
109
+ log({ command: 'invoke', source: key });
110
+ if (deny && payload.query.includes(deny)) {
111
+ writeFileSync(outputPath, JSON.stringify({
112
+ status: 'error',
113
+ error: { message: 'denied: caller lacks read permission' },
114
+ }), 'utf8');
115
+ process.stdout.write(JSON.stringify({ status: 'ok', outputFile: outputPath }));
116
+ process.exit(0);
117
+ }
118
+ const table = tables[key];
119
+ writeFileSync(outputPath, JSON.stringify({
120
+ status: 'ok',
121
+ table: {
122
+ columns: table.columns.map((name) => ({ name, dataType: 'String' })),
123
+ rows: table.rows,
124
+ },
125
+ }), 'utf8');
126
+ // The real CLI spills large results and leaves a preview on stdout, so the
127
+ // envelope is all the caller may trust.
128
+ process.stdout.write(JSON.stringify({
129
+ status: 'ok',
130
+ outputFile: outputPath,
131
+ outputBytes: 1234,
132
+ }));
133
+ process.exit(0);
134
+ }
135
+
136
+ process.stderr.write('unexpected argv: ' + JSON.stringify(argv));
137
+ process.exit(1);
138
+ `;
139
+ }
140
+
141
+ function writeFakeCli(app, deny) {
142
+ const cliDir = join(app, 'node_modules', '@microsoft', 'rayfin-cli');
143
+ mkdirSync(cliDir, { recursive: true });
144
+ writeFileSync(
145
+ join(cliDir, 'package.json'),
146
+ JSON.stringify({
147
+ name: '@microsoft/rayfin-cli',
148
+ version: '0.0.0-test',
149
+ bin: { rayfin: './rayfin.mjs' },
150
+ }),
151
+ 'utf8'
152
+ );
153
+ writeFileSync(join(cliDir, 'rayfin.mjs'), fakeCliSource(deny), 'utf8');
154
+ }
155
+
156
+ // The scripts are copied into the app the way `pack:add` installs them, so the
157
+ // fake CLI is found by ordinary parent-directory lookup rather than a NODE_PATH
158
+ // fallback that a really installed package would pre-empt.
159
+ function makeApp(deny) {
160
+ const app = mkdtempSync(join(tmpdir(), 'schema-sql-cli-'));
161
+ mkdirSync(join(app, 'scripts'), { recursive: true });
162
+ for (const name of RUNTIME_SCRIPTS) {
163
+ copyFileSync(join(KIT, name), join(app, 'scripts', name));
164
+ }
165
+ writeFakeCli(app, deny);
166
+ return app;
167
+ }
168
+
169
+ function cliCalls(app) {
170
+ const path = join(app, CALL_LOG);
171
+ if (!existsSync(path)) {
172
+ return [];
173
+ }
174
+ return readFileSync(path, 'utf8')
175
+ .split('\n')
176
+ .filter(Boolean)
177
+ .map((line) => JSON.parse(line));
178
+ }
179
+
180
+ function invokeCount(app) {
181
+ return cliCalls(app).filter((call) => call.command === 'invoke').length;
182
+ }
183
+
184
+ function runCli(app, args, environment = {}) {
185
+ const child = spawn(
186
+ process.execPath,
187
+ [join(app, 'scripts', 'schema-sql.mjs'), ...args],
188
+ {
189
+ cwd: app,
190
+ shell: false,
191
+ windowsHide: true,
192
+ env: { ...process.env, ...environment },
193
+ }
194
+ );
195
+ let stdout = '';
196
+ let stderr = '';
197
+ child.stdout.on('data', (chunk) => {
198
+ stdout += chunk.toString();
199
+ });
200
+ child.stderr.on('data', (chunk) => {
201
+ stderr += chunk.toString();
202
+ });
203
+ return once(child, 'close').then(([code]) => ({ code, stdout, stderr }));
204
+ }
205
+
206
+ test('the CLI builds a catalog on a cold cache and answers SQL', async (t) => {
207
+ const app = makeApp(null);
208
+ t.after(() => rmSync(app, { recursive: true, force: true }));
209
+
210
+ const cold = await runCli(app, [
211
+ 'SELECT [Table], [Name] FROM info_view_measures ORDER BY [Name]',
212
+ ]);
213
+ assert.equal(cold.code, 0, `stderr: ${cold.stderr}`);
214
+ assert.match(cold.stderr, /Building the model catalog/u);
215
+ assert.match(cold.stdout, /^# \{"ok":true/u);
216
+ assert.match(cold.stdout, /Table\tName\nSales\tRevenue\n$/u);
217
+
218
+ // Were the fake bypassed these counters would stay at zero and the run would
219
+ // have reached a real tenant, so this is what proves the test is offline.
220
+ assert.equal(invokeCount(app), 5, 'the five hardcoded sources were fetched');
221
+ // Exactly one: resolveAlias hands its connector record to resolveIdentity, so
222
+ // a single run must not spawn the connector list twice.
223
+ assert.equal(
224
+ cliCalls(app).filter((call) => call.command === 'list').length,
225
+ 1,
226
+ 'one run resolves the connector once'
227
+ );
228
+
229
+ const warm = await runCli(app, [
230
+ 'SELECT [Table], [Name] FROM info_view_measures ORDER BY [Name]',
231
+ ]);
232
+ assert.equal(warm.code, 0, `stderr: ${warm.stderr}`);
233
+ assert.ok(
234
+ !/Building the model catalog/u.test(warm.stderr),
235
+ 'a fresh catalog must not be rebuilt'
236
+ );
237
+ assert.equal(warm.stdout, cold.stdout, 'the payload is stable');
238
+ assert.equal(invokeCount(app), 5, 'a warm cache fetches nothing');
239
+ });
240
+
241
+ test('the CLI applies owned-measure enrichment end to end', async (t) => {
242
+ const app = makeApp(null);
243
+ t.after(() => rmSync(app, { recursive: true, force: true }));
244
+
245
+ // info_view_tables carries an ownedMeasures array, so this payload is JSON
246
+ // rather than TSV.
247
+ const result = await runCli(app, [
248
+ 'SELECT [Name] FROM info_view_tables ORDER BY [Name]',
249
+ ]);
250
+ assert.equal(result.code, 0, `stderr: ${result.stderr}`);
251
+ const byName = Object.fromEntries(
252
+ JSON.parse(result.stdout).rows.map((row) => [row.Name, row])
253
+ );
254
+
255
+ assert.equal(byName.Sales.ownedMeasureCount, 1);
256
+ assert.equal(byName.Sales.ownedMeasuresNeedDetail, true);
257
+ assert.deepEqual(byName.Sales.ownedMeasures, [
258
+ { name: 'Revenue', displayFolder: 'Finance' },
259
+ ]);
260
+ assert.equal(byName.Date.ownedMeasureCount, 0);
261
+ assert.equal(byName.Date.ownedMeasuresNeedDetail, false);
262
+ });
263
+
264
+ test('the CLI answers a real batch in one process', async (t) => {
265
+ const app = makeApp(null);
266
+ t.after(() => rmSync(app, { recursive: true, force: true }));
267
+
268
+ const result = await runCli(app, [
269
+ '--search',
270
+ 'revenue',
271
+ '--sql',
272
+ 'SELECT [Name] FROM info_view_tables ORDER BY [Name]',
273
+ '--sql',
274
+ 'SELEKT broken',
275
+ ]);
276
+
277
+ const lines = result.stdout.split('\n');
278
+ assert.deepEqual(JSON.parse(lines[0].slice(2)), {
279
+ ok: false,
280
+ batch: true,
281
+ statements: 3,
282
+ });
283
+ const labels = lines
284
+ .filter((line) => line.startsWith('## '))
285
+ .map((line) => JSON.parse(line.slice(3)));
286
+ assert.deepEqual(
287
+ labels.map((label) => label.kind),
288
+ ['search', 'sql', 'sql']
289
+ );
290
+ assert.match(result.stdout, /Revenue/u);
291
+ assert.match(result.stdout, /syntax error/u);
292
+ assert.equal(result.code, 1, 'one failed statement fails the invocation');
293
+ assert.equal(invokeCount(app), 5, 'one process, one build, three answers');
294
+ });
295
+
296
+ test('the CLI rejects an argument mistake before building anything', async (t) => {
297
+ const app = makeApp(null);
298
+ t.after(() => rmSync(app, { recursive: true, force: true }));
299
+
300
+ const result = await runCli(app, ['SELECT 1', '--sql', 'SELECT 2']);
301
+ assert.equal(result.code, 1);
302
+ assert.match(result.stderr, /not both/u);
303
+ assert.equal(result.stdout, '');
304
+ assert.deepEqual(cliCalls(app), [], 'planning happens before any fetch');
305
+ });
306
+
307
+ test('--help prints usage and exits successfully', async (t) => {
308
+ const app = makeApp(null);
309
+ t.after(() => rmSync(app, { recursive: true, force: true }));
310
+
311
+ const result = await runCli(app, ['--help']);
312
+ assert.equal(result.code, 0);
313
+ assert.match(result.stdout, /Usage:/u);
314
+ assert.match(result.stdout, /--sql <text>/u);
315
+ assert.match(result.stdout, /shares one byte budget/u);
316
+ // Exit 2 covers two conditions; the help must not describe only one.
317
+ assert.match(result.stdout, /truncated OR an explicitly requested/u);
318
+ assert.deepEqual(cliCalls(app), []);
319
+ });
320
+
321
+ test('exit 2 covers truncation and an unsatisfied refresh alike', async (t) => {
322
+ const app = makeApp(null);
323
+ t.after(() => rmSync(app, { recursive: true, force: true }));
324
+
325
+ const truncated = await runCli(app, [
326
+ '--limit',
327
+ '1',
328
+ 'SELECT [Name] FROM info_view_tables ORDER BY [Name]',
329
+ ]);
330
+ assert.equal(truncated.code, 2, 'a truncated preview exits 2');
331
+ assert.match(truncated.stdout, /"reason":"row_limit"/u);
332
+
333
+ const tolerated = await runCli(app, [
334
+ '--limit',
335
+ '1',
336
+ '--allow-truncated',
337
+ 'SELECT [Name] FROM info_view_tables ORDER BY [Name]',
338
+ ]);
339
+ assert.equal(tolerated.code, 0, '--allow-truncated accepts a preview');
340
+ });
341
+
342
+ test('an explicit --refresh that could not refresh says so and fails', async (t) => {
343
+ const app = makeApp(null);
344
+ t.after(() => rmSync(app, { recursive: true, force: true }));
345
+
346
+ const built = await runCli(app, ['--refresh']);
347
+ assert.equal(built.code, 0, `stderr: ${built.stderr}`);
348
+
349
+ writeFakeCli(app, 'COLUMNSTATISTICS');
350
+ const denied = await runCli(app, ['--refresh']);
351
+
352
+ assert.match(
353
+ denied.stderr,
354
+ /COLUMNSTATISTICS/u,
355
+ 'the failing source is named'
356
+ );
357
+ assert.match(denied.stderr, /denied/u, 'the underlying reason survives');
358
+ assert.match(denied.stderr, /Refresh not completed/u);
359
+ assert.equal(
360
+ denied.code,
361
+ 2,
362
+ 'an explicitly requested refresh that did not happen cannot exit 0'
363
+ );
364
+
365
+ // The retained catalog and its status row describe the earlier generation
366
+ // accurately, and must not be rewritten to look degraded.
367
+ const status = await runCli(app, [
368
+ 'SELECT status, incomplete FROM catalog_status',
369
+ ]);
370
+ assert.match(status.stdout, /complete\t0/u);
371
+ });
372
+
373
+ test('a failed automatic refresh does not re-fetch on the next command', async (t) => {
374
+ const app = makeApp(null);
375
+ t.after(() => rmSync(app, { recursive: true, force: true }));
376
+
377
+ const stale = { RAYFIN_SCHEMA_TTL_MINUTES: '0' };
378
+ const built = await runCli(app, ['--refresh']);
379
+ assert.equal(built.code, 0, `stderr: ${built.stderr}`);
380
+
381
+ writeFakeCli(app, 'COLUMNSTATISTICS');
382
+ const first = await runCli(
383
+ app,
384
+ ['SELECT [Table], [Name] FROM info_view_measures'],
385
+ stale
386
+ );
387
+ assert.equal(first.code, 0, 'nobody asked for freshness, so this succeeds');
388
+ assert.match(first.stderr, /COLUMNSTATISTICS/u, 'the reason is reported');
389
+ assert.ok(
390
+ !/Refresh not completed/u.test(first.stderr),
391
+ 'an automatic rebuild is not a failed request'
392
+ );
393
+ const afterFirst = invokeCount(app);
394
+
395
+ // Throttling is the usual cause, so retrying on the very next command would
396
+ // extend the block it is reacting to.
397
+ const second = await runCli(
398
+ app,
399
+ ['SELECT [Table], [Name] FROM info_view_measures'],
400
+ stale
401
+ );
402
+ assert.equal(second.code, 0);
403
+ assert.match(second.stdout, /Sales\tRevenue/u, 'it still answers locally');
404
+ assert.equal(
405
+ invokeCount(app),
406
+ afterFirst,
407
+ 'the failed refresh is not retried on every subsequent command'
408
+ );
409
+ assert.match(second.stderr, /not retrying until/u);
410
+ });
411
+
412
+ test('a cooldown cannot suppress a rebuild when no catalog exists', async (t) => {
413
+ const app = makeApp(null);
414
+ t.after(() => rmSync(app, { recursive: true, force: true }));
415
+
416
+ // A marker left behind from an earlier failure, with the catalog since gone
417
+ // or never built. Honouring it would answer from a database that is not
418
+ // there, for the whole window.
419
+ const cacheDir = join(app, '.schema', 'models', DATASET_ID);
420
+ mkdirSync(cacheDir, { recursive: true });
421
+ writeFileSync(
422
+ join(cacheDir, '.refresh-cooldown.json'),
423
+ JSON.stringify({
424
+ retryAfter: new Date(Date.now() + 60 * 60_000).toISOString(),
425
+ reason: 'an earlier failure',
426
+ }),
427
+ 'utf8'
428
+ );
429
+
430
+ const result = await runCli(app, [
431
+ 'SELECT [Table], [Name] FROM info_view_measures',
432
+ ]);
433
+ assert.equal(result.code, 0, `stderr: ${result.stderr}`);
434
+ assert.match(result.stderr, /Building the model catalog/u);
435
+ assert.match(result.stdout, /Sales\tRevenue/u);
436
+ assert.ok(
437
+ !/not retrying until/u.test(result.stderr),
438
+ 'with nothing to serve, the cooldown must not apply'
439
+ );
440
+ });
441
+
442
+ test('a cold start that failed can recover without --refresh', async (t) => {
443
+ const app = makeApp('INFO.VIEW.TABLES');
444
+ t.after(() => rmSync(app, { recursive: true, force: true }));
445
+
446
+ // Table discovery is fatal, so nothing is ever written: there is no catalog
447
+ // to fall back to.
448
+ const failed = await runCli(app, [
449
+ 'SELECT [Table], [Name] FROM info_view_measures',
450
+ ]);
451
+ assert.equal(failed.code, 1);
452
+ assert.equal(invokeCount(app), 1, 'the fatal source stopped discovery');
453
+
454
+ // A cooldown here would skip the rebuild and then fail to open a database
455
+ // that was never created, locking the user out for the whole window.
456
+ writeFakeCli(app, null);
457
+ const recovered = await runCli(app, [
458
+ 'SELECT [Table], [Name] FROM info_view_measures',
459
+ ]);
460
+ assert.equal(
461
+ recovered.code,
462
+ 0,
463
+ `an ordinary command must recover once the cause clears: ${recovered.stderr}`
464
+ );
465
+ assert.match(recovered.stderr, /Building the model catalog/u);
466
+ assert.match(recovered.stdout, /Sales\tRevenue/u);
467
+ assert.ok(
468
+ !/not retrying until/u.test(recovered.stderr),
469
+ 'there was no catalog to serve, so nothing may be suppressed'
470
+ );
471
+ });
472
+
473
+ test('an explicit --refresh retries even during a cooldown', async (t) => {
474
+ const app = makeApp(null);
475
+ t.after(() => rmSync(app, { recursive: true, force: true }));
476
+
477
+ await runCli(app, ['--refresh']);
478
+ writeFakeCli(app, 'COLUMNSTATISTICS');
479
+ await runCli(app, ['--refresh']);
480
+ const afterFirst = invokeCount(app);
481
+
482
+ const second = await runCli(app, ['--refresh']);
483
+ assert.equal(second.code, 2);
484
+ assert.ok(
485
+ invokeCount(app) > afterFirst,
486
+ 'an explicit refresh is never suppressed by the cooldown'
487
+ );
488
+ });