2727 */
2828
2929import { describe , it , expect } from 'vitest' ;
30+ import { assertEngineFindOnePredicate } from '@objectstack/metadata-core' ;
3031import { STORED_METADATA_BODY_OBJECTS } from '@objectstack/spec/kernel' ;
3132import {
3233 refuseStoredMetadataBodyReads ,
@@ -40,6 +41,8 @@ import { QuickJSScriptRunner } from './sandbox/quickjs-runner.js';
4041const FAMILY = [ ...STORED_METADATA_BODY_OBJECTS ] ;
4142const ORDINARY = 'boundary_note' ;
4243const 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' } } : { } ) ;
4346const 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 */
6266function 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 } ) ;
0 commit comments