Skip to content

Commit d2b3757

Browse files
committed
test(objectql): a header the write names answers the reference check first
With the reference check before the record's validation rules, a non-system write that names a master-detail header it cannot read, or one that exists nowhere, answers the field's reference_not_found rather than the rule's unevaluable refusal; a header the row already stores is still bound by the rules, and a system write keeps the rules first. The header-read counts add the reference check's read door, and the stored-metadata suites count only reads that ask for more than id. Claude-Session: https://claude.ai/code/session_01Utp6CYQjMkN514R78sCrTT Co-authored-by: Claude <noreply@anthropic.com>
1 parent 48a24f8 commit d2b3757

5 files changed

Lines changed: 75 additions & 30 deletions

‎packages/objectql/src/engine-formula-hop-stored-metadata-body.test.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ for (const family of STORED_METADATA_BODY_OBJECTS) {
119119
let d: ReturnType<typeof makeDriver>;
120120
let warns: ReturnType<typeof makeRecordingLogger>['warns'];
121121
const familyReads = () => d.calls.filter((c) => c.object === family).length;
122+
/**
123+
* [#29029] Reads of the family that ask for more than `id`: a member's
124+
* write that names a family row has it checked through its read door,
125+
* projected to `id` alone, and that read serves no body.
126+
*/
127+
const familyBodyReads = () => d.calls.filter((c) => c.object === family
128+
&& !(Array.isArray(c.ast?.fields) && c.ast.fields.length === 1 && c.ast.fields[0] === 'id')).length;
122129
const faultsOn = (field: string) => warns.filter((w) => w.meta?.object === 'probe_carrier' && w.meta?.field === field);
123130
const servesNothing = (value: unknown) => {
124131
const text = JSON.stringify(value);
@@ -200,13 +207,20 @@ for (const family of STORED_METADATA_BODY_OBJECTS) {
200207
} as any, 'test-package');
201208
for (const context of [MEMBER, SYSTEM]) {
202209
const err = await engine.insert('probe_ruled', { meta: 'm1' }, { context }).then(() => undefined, (e) => e);
203-
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', fields: [expect.objectContaining({ field: '_record', code: 'rule_violation' })] });
204-
// No verdict over the body: the refusal is the unevaluable one, never the rule's own.
210+
// No verdict over the body: never the rule's own refusal.
205211
expect(String(err.message)).not.toBe('probe hit');
206-
expect(String(err.message)).toContain(`could not read '${family}'`);
207212
servesNothing(err.message);
213+
if (context === MEMBER) {
214+
// [#29029] A member's write names a record its read door does not
215+
// return: the reference check answers, before the record's rules.
216+
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', fields: [expect.objectContaining({ field: 'meta', code: 'reference_not_found' })] });
217+
continue;
218+
}
219+
// A system write keeps the rules first: the refusal is the unevaluable one.
220+
expect(err).toMatchObject({ code: 'VALIDATION_FAILED', fields: [expect.objectContaining({ field: '_record', code: 'rule_violation' })] });
221+
expect(String(err.message)).toContain(`could not read '${family}'`);
208222
}
209-
expect(familyReads()).toBe(0);
223+
expect(familyBodyReads()).toBe(0);
210224
});
211225
});
212226
}

‎packages/objectql/src/engine-hydration-stored-metadata-body.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,17 @@ for (const family of STORED_METADATA_BODY_OBJECTS) {
187187
const holding = await failure(engine.insert('probe_detail', { hdr: 'm1' }, { context }));
188188
const control = await failure(engine.insert('probe_detail', { hdr: 'm2' }, { context }));
189189
expect(holding).toMatchObject({ code: 'VALIDATION_FAILED' });
190-
expect(holding?.message).toContain("reads 'parent'");
191-
// The verdict does not depend on the row: the bit the body would decide is not served.
192-
expect(control).toEqual(holding);
190+
if (context === MEMBER) {
191+
// [#29029] A member's write names a header its read door does not
192+
// return: the reference check answers, before the record's rules,
193+
// alike for either header.
194+
expect(holding?.message).toBe(`hdr: no ${family} record has id "m1"`);
195+
expect(control).toEqual({ ...holding, message: `hdr: no ${family} record has id "m2"` });
196+
} else {
197+
expect(holding?.message).toContain("reads 'parent'");
198+
// The verdict does not depend on the row: the bit the body would decide is not served.
199+
expect(control).toEqual(holding);
200+
}
193201
servesNothing([holding, control]);
194202
expect(familyBodyReads()).toHaveLength(0);
195203
});

‎packages/objectql/src/engine-predicate-related-master-detail-header.test.ts‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ async function refusalOf(run: () => Promise<unknown>): Promise<unknown> {
233233

234234
/** ADR-0137 D2: the rule has no verdict over a header that is not there. */
235235
const UNEVALUABLE = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: '_record', code: 'rule_violation' }] };
236+
/**
237+
* [#29029] A header the write itself names is judged by the reference check
238+
* before the record's rules: the answer is the master-detail field's.
239+
*/
240+
const NOT_FOUND = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'invoice', code: 'reference_not_found' }] };
236241

237242
/** The rule's own verdict: it read the header's stored state and refused (the bulk path adds the row id). */
238243
function expectRuleVerdict(answer: Answer): void {
@@ -251,6 +256,8 @@ describe('[#22892] a predicate rule reading its master through the master-detail
251256

252257
type Door = 'insert' | 'batch insert' | 'update by id' | 'repoint by id' | 'bulk update';
253258
const DOORS: Door[] = ['insert', 'batch insert', 'update by id', 'repoint by id', 'bulk update'];
259+
/** [#29029] The doors whose write names the header; the other two carry a stored one. */
260+
const namesHeader = (door: Door) => door === 'insert' || door === 'batch insert' || door === 'repoint by id';
254261

255262
/** One write through `door` that lands the line under `header`. */
256263
const write = (door: Door, header: string, context: Record<string, unknown> = ALICE) => {
@@ -275,8 +282,12 @@ describe('[#22892] a predicate rule reading its master through the master-detail
275282
const before = [...storeFor(LINE).values()];
276283
const nowhere = await refusalOf(() => write(door, NOWHERE));
277284
expect(nowhere).toBeInstanceOf(ValidationError);
278-
expect(answerOf(nowhere, NOWHERE)).toMatchObject(UNEVALUABLE);
279-
expect(answerOf(nowhere, NOWHERE).message).toContain('the related record was not found');
285+
if (namesHeader(door)) {
286+
expect(answerOf(nowhere, NOWHERE)).toMatchObject(NOT_FOUND);
287+
} else {
288+
expect(answerOf(nowhere, NOWHERE)).toMatchObject(UNEVALUABLE);
289+
expect(answerOf(nowhere, NOWHERE).message).toContain('the related record was not found');
290+
}
280291
for (const header of UNREADABLE) {
281292
expect(answerOf(await refusalOf(() => write(door, header)), header)).toEqual(answerOf(nowhere, NOWHERE));
282293
}
@@ -289,9 +300,13 @@ describe('[#22892] a predicate rule reading its master through the master-detail
289300

290301
it.each(DOORS)('%s — a refused read of the header object answers as a header that exists nowhere', async (door) => {
291302
const nowhere = answerOf(await refusalOf(() => write(door, NOWHERE, CAROL)), NOWHERE);
292-
expect(nowhere).toMatchObject(UNEVALUABLE);
303+
expect(nowhere).toMatchObject(namesHeader(door) ? NOT_FOUND : UNEVALUABLE);
293304
// Alice's headers exist; Carol's read door refuses the object, whatever they hold.
294-
for (const header of ['h_mine_open', 'h_mine_locked']) {
305+
// [#29029] The batch's first row and the repoint's stored line are
306+
// `h_mine_open` itself, the door's own fixture: those doors take the
307+
// locked header only.
308+
const headers = door === 'batch insert' || door === 'repoint by id' ? ['h_mine_locked'] : ['h_mine_open', 'h_mine_locked'];
309+
for (const header of headers) {
295310
expect(answerOf(await refusalOf(() => write(door, header, CAROL)), header)).toEqual(nowhere);
296311
}
297312
});
@@ -302,7 +317,7 @@ describe('[#22892] a predicate rule reading its master through the master-detail
302317
return (out.results?.[0]?.errors ?? []).map((e: any) => ({ field: e.field, code: e.code, message: String(e.message).split(header).join('ID') }));
303318
};
304319
const nowhere = await preview(NOWHERE);
305-
expect(nowhere).toEqual([expect.objectContaining({ field: '_record', code: 'rule_violation' })]);
320+
expect(nowhere).toEqual([expect.objectContaining({ field: 'invoice', code: 'reference_not_found' })]);
306321
for (const header of UNREADABLE) expect(await preview(header)).toEqual(nowhere);
307322
// CONTROL: a readable header is judged on its stored state.
308323
expect(await preview('h_mine_locked')).toEqual([{ field: '_record', code: 'rule_violation', message: RULE_MESSAGE }]);
@@ -320,7 +335,7 @@ describe('[#22892] a predicate rule reading its master through the master-detail
320335
expect(out.map((o) => o.ok)).toEqual([false, false, false, false, true]);
321336
const errors = out.map((o) => (o.ok ? undefined : o.error));
322337
const nowhere = answerOf(errors[2], NOWHERE);
323-
expect(nowhere).toMatchObject(UNEVALUABLE);
338+
expect(nowhere).toMatchObject(NOT_FOUND);
324339
expect(answerOf(errors[0], 'h_other_open')).toEqual(nowhere);
325340
expect(answerOf(errors[1], 'h_other_locked')).toEqual(nowhere);
326341
expectRuleVerdict(answerOf(errors[3], 'h_mine_locked'));
@@ -329,8 +344,10 @@ describe('[#22892] a predicate rule reading its master through the master-detail
329344

330345
it('the mechanism: the caller\'s read door first, and no elevated read of a header it did not return', async () => {
331346
await refusalOf(() => engine.insert(LINE, { invoice: 'h_other_locked', quantity: 1 }, { context: ALICE } as any));
347+
// The rule's binding read and the reference check's [#29029], each through the read door.
332348
expect(headerReads).toEqual([
333349
{ operation: 'find', elevated: false, where: { id: 'h_other_locked' } },
350+
{ operation: 'find', elevated: false, where: { id: 'h_other_locked' } },
334351
]);
335352
headerReads.length = 0;
336353
await engine.insert(LINE, { invoice: 'h_mine_open', quantity: 1 }, { context: ALICE } as any);

‎packages/objectql/src/engine-reference-tenant-scope.test.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,10 @@ describe('[#19837] the master-detail parent binding is scoped to the caller\'s o
546546
const open = await refusalOf(() => insertLine('hy_open'));
547547
const nowhere = await refusalOf(() => insertLine('h_nowhere'));
548548

549-
// ADR-0137 D2: the unbound header leaves `note`'s requirement without a
550-
// verdict, and that refusal precedes the reference guard's.
551-
const expected = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'note', code: 'rule_violation' }] };
549+
// [#29029] The header the write names is judged by the reference check
550+
// before the record's rules: an unreadable header answers as one that
551+
// exists nowhere.
552+
const expected = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'header', code: 'reference_not_found' }] };
552553
expect(locked).toBeInstanceOf(ValidationError);
553554
expect(envelopeOf(locked)).toEqual(expected);
554555
expect(envelopeOf(open)).toEqual(expected);
@@ -566,8 +567,8 @@ describe('[#19837] the master-detail parent binding is scoped to the caller\'s o
566567
const locked = await refusalOf(() => repoint('hy_locked'));
567568
const open = await refusalOf(() => repoint('hy_open'));
568569

569-
// ADR-0137 D2: see the describe's docblock — alike, and the rule's refusal.
570-
const expected = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'note', code: 'rule_violation' }] };
570+
// [#29029] Alike, and the reference check's refusal: the repoint names the header.
571+
const expected = { status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'header', code: 'reference_not_found' }] };
571572
expect(envelopeOf(locked)).toEqual(expected);
572573
expect(envelopeOf(open)).toEqual(expected);
573574
expect(storeFor('pb_line').get('ln_x')?.header).toBe('hx_open');
@@ -662,10 +663,11 @@ describe('[#19837] the master-detail parent binding is scoped to the caller\'s o
662663
const global = await refusalOf(() => insertLine('gh_locked', 'pb_gline'));
663664
expect(envelopeOf(global)).toEqual({ status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'note', code: 'required' }] });
664665
// Bound because the engine withheld the tenant for this master (ADR-0066),
665-
// on both of the write's header reads (#22519: the caller's read door, then
666-
// the elevated read).
666+
// on each of the write's header reads: the binding's two (#22519: the
667+
// caller's read door, then the elevated read) and the reference check's
668+
// read door, which runs before the record's rules (#29029).
667669
const read = observed.filter((c) => c.object === 'pb_gheader' && c.method === 'find');
668-
expect(read).toHaveLength(2);
670+
expect(read).toHaveLength(3);
669671
for (const r of read) expect(r.options?.tenantId).toBeUndefined();
670672
});
671673

@@ -681,20 +683,21 @@ describe('[#19837] the master-detail parent binding is scoped to the caller\'s o
681683
engine.insert('pb_line', { id: 'ln_grp', header: 'hy_locked' }, { context: GROUP_MEMBER } as any));
682684

683685
expect(envelopeOf(inside)).toEqual({ status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'note', code: 'required' }] });
684-
// Both of the write's header reads (#22519: the caller's read door, then
685-
// the elevated read) reach the union.
686+
// Each of the write's header reads reaches the union: the binding's two
687+
// (#22519: the caller's read door, then the elevated read) and the
688+
// reference check's read door (#29029).
686689
const read = observed.filter((c) => c.object === 'pb_header' && c.method === 'find');
687-
expect(read).toHaveLength(2);
690+
expect(read).toHaveLength(3);
688691
for (const r of read) expect([...(r.options?.tenantIds ?? [])].sort()).toEqual([ORG_X, ORG_Y].sort());
689692

690693
// Lit control: a member whose set is org X alone cannot see the org-Y
691-
// header, so it binds absent and the write answers as an absent header does
692-
// (ADR-0137 D2: `note`'s requirement has no verdict, and is refused).
694+
// header, so the write answers as an absent header does (#29029: the
695+
// reference check, before the record's rules).
693696
const ONLY_X = { ...MEMBER_X, accessible_org_ids: [ORG_X] } as unknown as ExecutionContext;
694697
const outside = await refusalOf(() =>
695698
engine.insert('pb_line', { id: 'ln_x_only', header: 'hy_locked' }, { context: ONLY_X } as any));
696699

697-
expect(envelopeOf(outside)).toEqual({ status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'note', code: 'rule_violation' }] });
700+
expect(envelopeOf(outside)).toEqual({ status: 400, code: 'VALIDATION_FAILED', fields: [{ field: 'header', code: 'reference_not_found' }] });
698701
});
699702

700703
it('the header read asks the caller\'s read door first, then runs ELEVATED and TENANT-SCOPED — the two halves of `{ ...context, isSystem: true }`', async () => {

‎packages/objectql/src/engine-unreadable-master-detail-header.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ describe('[#22519] an unreadable master-detail header answers what a nonexistent
353353
describe('(b) requiredWhen over parent', () => {
354354
it('insert — an unreadable header in either state answers what a nonexistent one does', async () => {
355355
const nowhere = await refusalOf(() => engine.insert(REQ_LINE, { invoice: NOWHERE, quantity: 1 }, { context: ALICE } as any));
356-
expect(envelopeOf(nowhere)).toEqual(UNEVALUABLE);
356+
// [#29029] The header the write names is judged before the record's rules.
357+
expect(envelopeOf(nowhere)).toEqual(NOT_FOUND);
357358
for (const header of UNREADABLE) {
358359
const err = await refusalOf(() => engine.insert(REQ_LINE, { invoice: header, quantity: 1 }, { context: ALICE } as any));
359360
expect(envelopeOf(err)).toEqual(envelopeOf(nowhere));
@@ -386,7 +387,8 @@ describe('[#22519] an unreadable master-detail header answers what a nonexistent
386387
};
387388
for (const bulk of [false, true]) {
388389
const nowhere = await repoint(NOWHERE, bulk);
389-
expect(envelopeOf(nowhere)).toEqual(UNEVALUABLE);
390+
// [#29029] The header the repoint names is judged before the record's rules.
391+
expect(envelopeOf(nowhere)).toEqual(NOT_FOUND);
390392
for (const header of UNREADABLE) {
391393
const err = await repoint(header, bulk);
392394
expect(envelopeOf(err)).toEqual(envelopeOf(nowhere));
@@ -449,7 +451,8 @@ describe('[#22519] an unreadable master-detail header answers what a nonexistent
449451

450452
it('a controlled_by_parent detail takes the same rule under an unreadable header', async () => {
451453
const nowhere = envelopeOf(await refusalOf(() => engine.insert(CBP_LINE, { invoice: NOWHERE, quantity: 1 }, { context: ALICE } as any)));
452-
expect(nowhere).toEqual(UNEVALUABLE);
454+
// [#29029] The header the write names is judged before the record's rules.
455+
expect(nowhere).toEqual(NOT_FOUND);
453456
for (const header of UNREADABLE) {
454457
expect(envelopeOf(await refusalOf(() => engine.insert(CBP_LINE, { invoice: header, quantity: 1 }, { context: ALICE } as any)))).toEqual(nowhere);
455458
}

0 commit comments

Comments
 (0)