Skip to content

Commit d5b8902

Browse files
committed
test(runtime): the body-read pin's double reads findOne through the engine's own predicate (wip)
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016GiHYRmLSNWTfbX9gVQkpz
1 parent 9c87884 commit d5b8902

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎packages/runtime/src/stored-metadata-body-reads.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
import { describe, it, expect } from 'vitest';
30+
import { assertEngineFindOnePredicate } from '@objectstack/metadata-core';
3031
import { STORED_METADATA_BODY_OBJECTS } from '@objectstack/spec/kernel';
3132
import {
3233
refuseStoredMetadataBodyReads,
@@ -40,6 +41,8 @@ import { QuickJSScriptRunner } from './sandbox/quickjs-runner.js';
4041
const FAMILY = [...STORED_METADATA_BODY_OBJECTS];
4142
const ORDINARY = 'boundary_note';
4243
const READ_VERBS = ['find', 'findOne', 'count', 'aggregate'];
44+
/** The query a control read is called with: `findOne` takes a predicate, as the engine requires. */
45+
const queryFor = (verb: string): Record<string, unknown> => (verb === 'findOne' ? { where: { id: 'n1' } } : {});
4346
const SENTINEL = 'body-read-unit-sentinel-5e02';
4447

4548
/** A stored family row: the body carries a credential, the row a content hash. */
@@ -57,15 +60,20 @@ function storedRow(): Record<string, unknown> {
5760
* A scoped-API double that counts `<object>.<verb>` calls and answers the
5861
* stored form, so a read that reached it would carry the sentinel. Read verbs
5962
* and one write verb only: no `update` / `delete` member, so it carries no
60-
* write dispatch to hold to the engine's.
63+
* write dispatch to hold to the engine's; `findOne` routes through the
64+
* engine's own predicate (check:engine-double-contract).
6165
*/
6266
function countingApi(calls: string[] = []): any {
6367
const repo = (name: string) => {
6468
const record = (verb: string) => calls.push(`${name}.${verb}`);
6569
const family = name.startsWith('sys_metadata');
6670
return {
6771
async find() { record('find'); return family ? [storedRow()] : [{ id: 'n1' }]; },
68-
async findOne() { record('findOne'); return family ? storedRow() : { id: 'n1' }; },
72+
async findOne(query?: any) {
73+
assertEngineFindOnePredicate(name, query);
74+
record('findOne');
75+
return family ? storedRow() : { id: 'n1' };
76+
},
6977
async count() { record('count'); return 1; },
7078
async aggregate() { record('aggregate'); return family ? [{ metadata: storedRow().metadata, count: 1 }] : []; },
7179
async insert() { record('insert'); return { id: 'n1' }; },
@@ -138,7 +146,7 @@ describe('[#21594] refuseStoredMetadataBodyReads — every family read is refuse
138146
it('an ordinary table reads as before (control)', async () => {
139147
const calls: string[] = [];
140148
const api = refuseStoredMetadataBodyReads(countingApi(calls));
141-
for (const verb of READ_VERBS) await api.object(ORDINARY)[verb]({});
149+
for (const verb of READ_VERBS) await api.object(ORDINARY)[verb](queryFor(verb));
142150
expect(calls).toEqual(READ_VERBS.map((verb) => `${ORDINARY}.${verb}`));
143151
});
144152

@@ -182,7 +190,7 @@ describe('[#21594] the API a body holds — the write layer over the read layer'
182190
it('an ordinary table reads and writes as before (control)', async () => {
183191
const calls: string[] = [];
184192
const api = bodyApi(calls);
185-
for (const verb of READ_VERBS) await api.object(ORDINARY)[verb]({});
193+
for (const verb of READ_VERBS) await api.object(ORDINARY)[verb](queryFor(verb));
186194
await api.object(ORDINARY).insert({ label: 'x' });
187195
expect(calls).toEqual([...READ_VERBS, 'insert'].map((verb) => `${ORDINARY}.${verb}`));
188196
});

‎scripts/engine-double-contract.pinned.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,6 +3811,11 @@
38113811
"verb": "update",
38123812
"pinned": 1
38133813
},
3814+
{
3815+
"file": "packages/runtime/src/stored-metadata-body-reads.test.ts",
3816+
"verb": "findOne",
3817+
"pinned": 1
3818+
},
38143819
{
38153820
"file": "packages/runtime/src/stored-metadata-reader-seam.test.ts",
38163821
"verb": "delete",

0 commit comments

Comments
 (0)