Skip to content

Commit c2a5fd1

Browse files
committed
test(objectql): pin the multi-row insert preview's batch overlay on per-caller header columns through a parent. predicate
The carol and erin personas read the header through a field-level read rule. A requiredWhen on parent. reads the header the preview binds per caller; a record.REF rule reads the related binding, a system read. Claude-Session: https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3b3cd84 commit c2a5fd1

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

‎packages/objectql/src/summary-rollup.test.ts‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,8 @@ describe('[#34821] a multi-row insert judges a rule reading a parent field calcu
18991899
bud: { type: 'master_detail', reference: 'bud' },
19001900
// A field-level parent. predicate reading the header's calculated field.
19011901
note: { type: 'text', requiredWhen: 'parent.headroom < 50' },
1902+
// Reads the header's formula on `parent`, guarded against a column the reader is not served (`null`).
1903+
memo: { type: 'text', requiredWhen: "record.tag == 'remg' && parent.remaining != null && parent.remaining < 50" },
19021904
},
19031905
validations: [
19041906
{ name: 'remaining_cover', type: 'script', severity: 'error', message: 'line over the remaining budget', condition: "record.tag == 'rem' && record.amount > record.bud.remaining" },
@@ -2022,26 +2024,41 @@ describe('[#34821] a multi-row insert judges a rule reading a parent field calcu
20222024

20232025
const noteless = (budId: string) => [{ bud: budId, amount: 40, tag: 'x' }, { bud: budId, amount: 40, tag: 'x' }, { bud: budId, amount: 1, tag: 'x' }];
20242026

2027+
const guarded = (budId: string, amounts: number[]) => amounts.map((amount) => ({ bud: budId, amount, tag: 'remg', note: 'n' }));
2028+
const validOf = async (batch: Record<string, unknown>[], context?: Record<string, unknown>) =>
2029+
(await engine.validate('bud_line', batch, { mode: 'insert', ...(context ? { context: context as any } : {}) })).results!.map((r) => r.valid);
2030+
20252031
it('multi-row insert preview, a caller not served the calculated header field: every row judges it as empty, as the first row does, whatever the header holds', async () => {
2026-
withholdBudColumns('carol', ['budget', 'headroom', 'remaining']);
2032+
withholdBudColumns('carol', ['remaining']);
20272033
const carol = { userId: 'carol' };
20282034
const verdicts: boolean[][] = [];
20292035
for (const budget of [100, 1000]) {
20302036
const p = await engine.insert('bud', { name: 'B', budget });
2031-
// Precondition: the read door serves carol the header, without the withheld columns.
2037+
// Precondition: the read door serves carol the header and its inputs, without the withheld column.
20322038
const served = await engine.findOne('bud', { where: { id: p.id }, context: carol } as any);
2033-
expect(served).toMatchObject({ id: p.id });
2034-
for (const c of ['budget', 'headroom', 'remaining']) expect(served).not.toHaveProperty(c);
2035-
for (const batch of [noteless(p.id), [1, 2, 3].map(() => ({ bud: p.id, amount: 40, tag: 'rem', note: 'n' }))]) {
2036-
const preview = await engine.validate('bud_line', batch, { mode: 'insert', context: carol as any });
2037-
const rows = preview.results!.map((r) => r.valid);
2038-
// No row reads a value the first row could not: the earlier rows' amounts steer no withheld column.
2039-
expect(rows).toEqual(rows.map(() => rows[0]));
2040-
verdicts.push(rows);
2041-
}
2039+
expect(served).toMatchObject({ id: p.id, budget, total: 0 });
2040+
expect(served).not.toHaveProperty('remaining');
2041+
verdicts.push(await validOf(guarded(p.id, [40, 40, 1]), carol));
20422042
}
2043-
// Identical whatever the withheld budget holds.
2044-
expect(verdicts.slice(2)).toEqual(verdicts.slice(0, 2));
2043+
// CONTROL: a reader served the column needs the memo on the third line (remaining 20), as the write does.
2044+
const p = await engine.insert('bud', { name: 'B', budget: 100 });
2045+
expect(await validOf(guarded(p.id, [40, 40, 1]))).toEqual([true, true, false]);
2046+
expect((await refusalOf(engine.insert('bud_line', guarded(p.id, [40, 40, 1]))) as ValidationError).fields.map((f) => f.field)).toContain('memo');
2047+
// No row reads a value the first row could not, whatever the withheld column holds:
2048+
// the earlier rows' amounts steer no withheld column.
2049+
expect(verdicts).toEqual([[true, true, true], [true, true, true]]);
2050+
});
2051+
2052+
it('multi-row insert preview, a caller not served the roll-up itself: it moves from the stored value, not from the empty column, so the served calculated field reads as one by one serves it', async () => {
2053+
withholdBudColumns('erin', ['total']);
2054+
const p = await engine.insert('bud', { name: 'B', budget: 100 });
2055+
await engine.insert('bud_line', { bud: p.id, amount: 40, tag: 'x', note: 'n' });
2056+
// One by one: the first line reads remaining 60, the second 40 and needs the memo.
2057+
// Moved from the empty column instead, the second would read 80.
2058+
const erin = await engine.validate('bud_line', guarded(p.id, [20, 1]), { mode: 'insert', context: { userId: 'erin' } as any });
2059+
expect(erin.results!.map((r) => r.valid)).toEqual([true, false]);
2060+
expect(erin.results![1]!.errors.map((e) => e.field)).toContain('memo');
2061+
expect(await validOf(guarded(p.id, [20, 1]))).toEqual([true, false]);
20452062
});
20462063

20472064
it('multi-row insert preview, a caller served the calculated field but not its input: the served field moves as one by one serves it, and the third line needs the note', async () => {

0 commit comments

Comments
 (0)