@@ -42,7 +42,20 @@ function makeFakeEngine(schemas: Record<string, any>) {
4242 async find ( object : string , options ?: any ) {
4343 const table = ensure ( object ) ;
4444 const filter = options ?. filter ?? options ?. where ;
45- return table . filter ( r => matches ( r , filter ) ) . slice ( 0 , options ?. limit ?? 1000 ) ;
45+ let out = table . filter ( r => matches ( r , filter ) ) ;
46+ if ( options ?. orderBy ?. [ 0 ] ) {
47+ // Canonical SortNode key only (spec/data/query.zod.ts): the real
48+ // engine strips an unknown `direction:` key and defaults to asc, so
49+ // the mock must too — honoring both keys masks wrong-key sorts.
50+ const { field, order } = options . orderBy [ 0 ] ;
51+ out = [ ...out ] . sort ( ( a , b ) => {
52+ const av = a [ field ] ; const bv = b [ field ] ;
53+ if ( av === bv ) return 0 ;
54+ const cmp = av > bv ? 1 : - 1 ;
55+ return order === 'desc' ? - cmp : cmp ;
56+ } ) ;
57+ }
58+ return out . slice ( 0 , options ?. limit ?? 1000 ) ;
4659 } ,
4760 async insert ( object : string , data : any ) {
4861 const row = { ...data } ;
@@ -236,6 +249,17 @@ describe('SharingService.grant / listShares / revoke', () => {
236249 expect ( rows . length ) . toBe ( 2 ) ;
237250 } ) ;
238251
252+ it ( 'listShares returns the newest grant first' , async ( ) => {
253+ // Regression: the query sorted with the non-canonical `direction: 'desc'`
254+ // key, which SortNode strips — so it sorted ascending (oldest first).
255+ engine . _tables . sys_record_share = [
256+ { id : 'shr_old' , object_name : 'account' , record_id : 'a1' , recipient_id : 'bob' , created_at : '2026-01-01T00:00:00Z' } ,
257+ { id : 'shr_new' , object_name : 'account' , record_id : 'a1' , recipient_id : 'carol' , created_at : '2026-02-01T00:00:00Z' } ,
258+ ] ;
259+ const rows = await svc . listShares ( 'account' , 'a1' , { userId : 'admin' } ) ;
260+ expect ( rows . map ( r => r . id ) ) . toEqual ( [ 'shr_new' , 'shr_old' ] ) ;
261+ } ) ;
262+
239263 it ( 'revoke removes the row' , async ( ) => {
240264 const r = await svc . grant ( { object : 'account' , recordId : 'a1' , recipientId : 'bob' } , { userId : 'admin' } ) ;
241265 await svc . revoke ( r . id , { userId : 'admin' } ) ;
0 commit comments