3030import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
3131import { ObjectQL } from '@objectstack/objectql' ;
3232import { SqlDriver } from '@objectstack/driver-sql' ;
33+ import type { EngineCountOptions , EngineQueryOptions } from '@objectstack/spec/data' ;
3334import { SysApprovalDelegation } from './sys-approval-delegation.object.js' ;
3435import { bindDelegationWriteGuard } from './lifecycle-hooks.js' ;
3536
@@ -63,18 +64,23 @@ const delegatorField = (): Record<string, any> =>
6364 ( SysApprovalDelegation . fields as Record < string , any > ) . delegator_id ;
6465
6566/**
66- * The picker's candidate `where` , from the declared `lookupFilters`. The
67+ * The picker's candidate query , from the declared `lookupFilters`. The
6768 * console lowers each `eq` entry to `{ [field]: value }` and sends it as the
6869 * `$filter` of its `sys_user` query (objectui `lookupFiltersToRecord`); the
6970 * declaration test holds that every entry here is `eq`.
7071 */
71- function pickerWhere ( ) : Record < string , unknown > {
72+ function pickerQuery ( ) : EngineQueryOptions {
7273 const filters = ( delegatorField ( ) . lookupFilters ?? [ ] ) as LookupFilter [ ] ;
7374 const where : Record < string , unknown > = { } ;
7475 for ( const f of filters ) where [ f . field ] = f . value ;
75- return where ;
76+ return { where } ;
7677}
7778
79+ /** The delegations that name Priya as delegator. */
80+ const PRIYA_AS_DELEGATOR : EngineCountOptions = { where : { delegator_id : 'u_priya' } } ;
81+
82+ const byId = ( id : string ) : EngineQueryOptions => ( { where : { id } } ) ;
83+
7884describe ( 'sys_approval_delegation — the Delegator picker offers only what the write guard admits' , ( ) => {
7985 let engine : ObjectQL ;
8086
@@ -112,12 +118,12 @@ describe('sys_approval_delegation — the Delegator picker offers only what the
112118 } ) ;
113119
114120 it . each ( USERS . map ( ( u ) => [ u . id ] ) ) ( 'the picker offers %s themselves and nobody else' , async ( caller ) => {
115- const candidates = await engine . find ( 'sys_user' , { where : pickerWhere ( ) } as any , asUser ( caller ) ) ;
121+ const candidates = await engine . find ( 'sys_user' , pickerQuery ( ) , asUser ( caller ) ) ;
116122 expect ( candidates . map ( ( r : any ) => r . id ) ) . toEqual ( [ caller ] ) ;
117123 } ) ;
118124
119125 it ( 'every candidate the picker offers is admitted by the guard' , async ( ) => {
120- const candidates = await engine . find ( 'sys_user' , { where : pickerWhere ( ) } as any , asUser ( 'u_admin' ) ) ;
126+ const candidates = await engine . find ( 'sys_user' , pickerQuery ( ) , asUser ( 'u_admin' ) ) ;
121127 expect ( candidates . length ) . toBeGreaterThan ( 0 ) ;
122128 for ( const candidate of candidates ) {
123129 const created = await engine . insert (
@@ -128,19 +134,19 @@ describe('sys_approval_delegation — the Delegator picker offers only what the
128134 } ) ;
129135
130136 it ( 'the guard is not widened: a create naming anyone else is refused FORBIDDEN / 403 and stores nothing' , async ( ) => {
131- const before = await engine . count ( OBJECT , { where : { delegator_id : 'u_priya' } } as any , SYSTEM ) ;
137+ const before = await engine . count ( OBJECT , PRIYA_AS_DELEGATOR , SYSTEM ) ;
132138 await expect (
133139 engine . insert ( OBJECT , { delegator_id : 'u_priya' , delegate_id : 'u_tomas' } , asUser ( 'u_admin' ) ) ,
134140 ) . rejects . toMatchObject ( { code : 'FORBIDDEN' , statusCode : 403 } ) ;
135- expect ( await engine . count ( OBJECT , { where : { delegator_id : 'u_priya' } } as any , SYSTEM ) ) . toBe ( before ) ;
141+ expect ( await engine . count ( OBJECT , PRIYA_AS_DELEGATOR , SYSTEM ) ) . toBe ( before ) ;
136142 } ) ;
137143
138144 it ( 'the guard is not widened: an update relabelling the delegator is refused FORBIDDEN / 403' , async ( ) => {
139145 const own = await engine . insert ( OBJECT , { delegator_id : 'u_admin' , delegate_id : 'u_tomas' } , asUser ( 'u_admin' ) ) ;
140146 await expect (
141147 engine . update ( OBJECT , { id : own . id , delegator_id : 'u_priya' } , asUser ( 'u_admin' ) ) ,
142148 ) . rejects . toMatchObject ( { code : 'FORBIDDEN' , statusCode : 403 } ) ;
143- const row = await engine . findOne ( OBJECT , { where : { id : own . id } } as any , SYSTEM ) ;
149+ const row = await engine . findOne ( OBJECT , byId ( own . id ) , SYSTEM ) ;
144150 expect ( row ?. delegator_id ) . toBe ( 'u_admin' ) ;
145151 } ) ;
146152
0 commit comments