Skip to content

Commit 7efe67f

Browse files
committed
test(service-analytics): pin the plugin's fixedReadLeafClassOf wiring: compiles on a plain engine's neither, declines on a class and on absence
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7
1 parent e22e903 commit 7efe67f

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

‎packages/services/service-analytics/src/__tests__/fixed-read-leaf-native-compile.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { Cube, FilterCondition } from '@objectstack/spec/data';
3232
import type { AnalyticsQuery } from '@objectstack/spec/contracts';
3333

3434
import { AnalyticsService } from '../analytics-service.js';
35+
import { AnalyticsServicePlugin } from '../plugin.js';
3536
import {
3637
READ_SCOPE_FIXED_READ_LEAF_BRANDS,
3738
assertReadScopeComparandsRunnable,
@@ -297,4 +298,81 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
297298
expect(params).toEqual(expect.arrayContaining(['srid_doc', 'user', 'u1']));
298299
});
299300
});
301+
302+
// [#35946, ruling 7051956587] `AnalyticsServicePlugin` wires the hook from
303+
// the "data" engine's `IObjectQLEngine.fixedReadLeafClassOf`, read
304+
// structurally: the engine here is a plain object over the live engine's
305+
// members, so the pin is the plugin's wiring, not the `ObjectQL` class.
306+
describe('[#35946] AnalyticsServicePlugin asks the data engine\'s fixedReadLeafClassOf', () => {
307+
const CUBE = {
308+
name: 'docs', sql: 'srid_doc', public: true,
309+
measures: { n: { sql: '*', type: 'count', title: 'n' } },
310+
dimensions: { id: { label: 'id', type: 'string', sql: 'id' } },
311+
} as unknown as Cube;
312+
const QUERY = { cube: 'docs', dimensions: ['id'], measures: ['n'] } as AnalyticsQuery;
313+
const READ_SCOPE = S.cases.find((c) => c.name.startsWith("the read scope's shape"))!;
314+
315+
/** The "data" engine as a plain object; `member` says what its `fixedReadLeafClassOf` is. */
316+
const plainEngine = (member: 'present' | 'absent'): Record<string, unknown> => ({
317+
aggregate: (o: string, q: any) => engine.aggregate(o, q),
318+
getObject: (n: string) => engine.getObject(n),
319+
judgeFilter: (o: string, w: any, j: any) => engine.judgeFilter(o, w, j),
320+
hasObjectMiddleware: (o: string) => engine.hasObjectMiddleware(o),
321+
getDriverForObject: (o: string) => engine.getDriverForObject(o),
322+
...(member === 'present' ? { fixedReadLeafClassOf: (leaf: any) => engine.fixedReadLeafClassOf(leaf) } : {}),
323+
});
324+
325+
async function viaPlugin(data: Record<string, unknown>) {
326+
const registered: Record<string, unknown> = {};
327+
const warn = vi.fn();
328+
const rawSql = vi.fn(runRawSql);
329+
const ctx = {
330+
getService: (name: string) => (name === 'data' ? data : registered[name]),
331+
registerService: (name: string, svc: unknown) => { registered[name] = svc; },
332+
replaceService: (name: string, svc: unknown) => { registered[name] = svc; },
333+
logger: { ...quiet, warn },
334+
};
335+
await new AnalyticsServicePlugin({
336+
cubes: [CUBE],
337+
queryCapabilities: () => ({ nativeSql: true, objectqlAggregate: true, inMemory: false }),
338+
executeRawSql: (_object, sql, params) => rawSql(sql, params),
339+
getReadScope: () => READ_SCOPE.where() as FilterCondition,
340+
}).init(ctx as never);
341+
const svc = registered.analytics as AnalyticsService;
342+
const answer = () => svc.query(QUERY, { userId: 'u1' } as never).then((r) => ({ rows: r.rows, err: null }), (err: any) => ({ rows: [], err }));
343+
return { answer, rawSql, warn };
344+
}
345+
const idsOf = (rows: Array<Record<string, unknown>>) => rows.map((r) => String(r.id)).sort();
346+
347+
it('the member present, the object in neither class: the native path compiles the leaf', async () => {
348+
const { answer, rawSql } = await viaPlugin(plainEngine('present'));
349+
const { rows, err } = await answer();
350+
expect(err).toBeNull();
351+
expect(rawSql).toHaveBeenCalledTimes(1);
352+
expect(rawSql.mock.calls[0]![0]).toContain('IN (SELECT "record_id" FROM "sys_record_share"');
353+
expect(idsOf(rows)).toEqual([...READ_SCOPE.expected]);
354+
});
355+
356+
it('the member answers a D4 class: the native path declines, and the engine\'s seam refuses', async () => {
357+
engine.registerHook('beforeFind', () => {}, { object: 'sys_record_share' });
358+
try {
359+
const { answer, rawSql } = await viaPlugin(plainEngine('present'));
360+
const { err } = await answer();
361+
expect(rawSql).not.toHaveBeenCalled();
362+
expect({ code: err?.code, status: err?.status }).toEqual(REFUSED);
363+
} finally {
364+
(engine as any).hooks.get('beforeFind').pop();
365+
}
366+
});
367+
368+
it('the member absent: the native path declines, the engine path answers, and the plugin says so once', async () => {
369+
const { answer, rawSql, warn } = await viaPlugin(plainEngine('absent'));
370+
const first = await answer();
371+
await answer();
372+
expect(first.err).toBeNull();
373+
expect(rawSql).not.toHaveBeenCalled();
374+
expect(idsOf(first.rows)).toEqual([...READ_SCOPE.expected]);
375+
expect(warn.mock.calls.filter((c) => String(c[0]).includes('fixedReadLeafClassOf'))).toHaveLength(1);
376+
});
377+
});
300378
});

0 commit comments

Comments
 (0)