Skip to content

Commit 754bd61

Browse files
committed
test(objectql): count header reads through the read door, not inside the driver double
The driver double's find carried a read counter backed by a Map, which the objectql-double-limit gate's probe stubs, so the double could not be judged. The control now counts header reads with an engine middleware, which sees every read of the header object under any context. Claude-Session: https://claude.ai/code/session_01Bw3y2DWhT9RPnrmDsNqEVG Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1fa886d commit 754bd61

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

‎packages/objectql/src/validate-preview-parent.test.ts‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,14 @@ const PLAIN_LINE_OBJECT = {
9191
},
9292
};
9393

94-
/** A store-backed driver over several objects, counting the reads of each. */
94+
/** A store-backed driver over several objects: what is stored is what a later read answers. */
9595
function makeStoreDriver() {
9696
const stores = new Map<string, Map<string, Record<string, any>>>();
97-
const reads = new Map<string, number>();
9897
const storeFor = (o: string) => {
9998
let s = stores.get(o);
10099
if (!s) { s = new Map(); stores.set(o, s); }
101100
return s;
102101
};
103-
const read = (o: string) => { reads.set(o, (reads.get(o) ?? 0) + 1); };
104102
const matches = (row: any, where: any): boolean => {
105103
if (!where || typeof where !== 'object') return true;
106104
return Object.entries(where).every(([k, v]: [string, any]) => {
@@ -122,12 +120,10 @@ function makeStoreDriver() {
122120
async connect() {}, async disconnect() {}, async checkHealth() { return true; },
123121
async syncSchema() {},
124122
async find(o: string, ast: any) {
125-
read(o);
126123
const hits = Array.from(storeFor(o).values()).filter((r) => matches(r, ast?.where));
127124
return (typeof ast?.limit === 'number' ? hits.slice(0, ast.limit) : hits).map((r) => ({ ...r }));
128125
},
129126
async findOne(o: string, ast: any) {
130-
read(o);
131127
for (const r of storeFor(o).values()) if (matches(r, ast?.where)) return { ...r };
132128
return null;
133129
},
@@ -148,7 +144,7 @@ function makeStoreDriver() {
148144
return Array.from(storeFor(o).values()).filter((r) => matches(r, ast?.where)).length;
149145
},
150146
};
151-
return { driver, storeFor, reads };
147+
return { driver, storeFor };
152148
}
153149

154150
function makeEngine() {
@@ -198,10 +194,9 @@ const REQUIRED: Verdict = { valid: false, errors: [{ field: 'description', code:
198194
describe('#22474 — the preview binds the master-detail header the write binds', () => {
199195
let engine: ObjectQL;
200196
let storeFor: (o: string) => Map<string, Record<string, any>>;
201-
let reads: Map<string, number>;
202197

203198
beforeEach(() => {
204-
({ engine, storeFor, reads } = makeEngine());
199+
({ engine, storeFor } = makeEngine());
205200
});
206201

207202
describe('(a) insert: admitted under a draft header, refused under a sent one, as the insert', () => {
@@ -256,18 +251,28 @@ describe('#22474 — the preview binds the master-detail header the write binds'
256251
});
257252

258253
describe('(c) control: an object with no parent-scoped rule reads no header', () => {
254+
/** Counts every read of the header object, through the read door, under any context. */
255+
function countHeaderReads(ql: ObjectQL) {
256+
const count = { reads: 0 };
257+
ql.registerMiddleware(async (ctx: any, next: () => Promise<void>) => {
258+
if (ctx.object === HEADER && ['find', 'findOne'].includes(ctx.operation)) count.reads += 1;
259+
await next();
260+
});
261+
return count;
262+
}
263+
259264
it('insert and update previews of a plain line issue no read of the header object', async () => {
260265
storeFor(PLAIN_LINE).set('p1', { id: 'p1', invoice: 'h_sent', quantity: 1 });
261-
reads.clear();
266+
const count = countHeaderReads(engine);
262267
expect(await preview(engine, PLAIN_LINE, { invoice: 'h_sent', quantity: 1 }, 'insert')).toEqual(ADMITTED);
263268
expect(await preview(engine, PLAIN_LINE, { quantity: 2, id: 'p1' }, 'update')).toEqual(ADMITTED);
264-
expect(reads.get(HEADER) ?? 0).toBe(0);
269+
expect(count.reads).toBe(0);
265270
});
266271

267272
it('positive control: the parent-scoped line does read it', async () => {
268-
reads.clear();
273+
const count = countHeaderReads(engine);
269274
await preview(engine, LINE, { invoice: 'h_draft', quantity: 1 }, 'insert');
270-
expect(reads.get(HEADER) ?? 0).toBeGreaterThan(0);
275+
expect(count.reads).toBeGreaterThan(0);
271276
});
272277
});
273278

0 commit comments

Comments
 (0)