88 *
99 * Pinned: a top five over more customers than the tool's row cap answers
1010 * exactly five, in order, with the true number of groups; an order by a group
11- * key ascending; an order key naming no column of the aggregated row refused
11+ * key ascending; the declared option order `aggregate_records` sends for a
12+ * `limit` with no `orderBy` (a rank on a select key), taken by the statement's
13+ * CASE before the cut, NULL last, with the true number of groups; an order
14+ * key naming no column of the aggregated row refused
1215 * `INVALID_SORT` with the columns named; `aggregate` with no order answers as
1316 * before; and the statement the driver ran carries the ORDER BY, the LIMIT and
1417 * `count(*) over ()` — so a sort in the application cannot pass. The tool's
@@ -33,8 +36,11 @@ const DEF = {
3336 fields : {
3437 customer : { name : 'customer' , label : 'Customer' , type : 'text' } ,
3538 amount : { name : 'amount' , label : 'Amount' , type : 'number' } ,
39+ stage : { name : 'stage' , label : 'Stage' , type : 'select' , options : [ { value : 'open' } , { value : 'won' } , { value : 'lost' } ] } ,
3640 } ,
3741} ;
42+ /** Each customer's stage, in an order that is neither declared nor alphabetical; every fourth has none. */
43+ const STAGE_OF = ( i : number ) => [ 'lost' , 'won' , null , 'open' ] [ i % 4 ] ;
3844/** More customers than the tool's row cap (50): `cNN` totals `NN * 10 + 5`. */
3945const CUSTOMERS = 70 ;
4046const REVENUE = { groupBy : [ 'customer' ] , aggregations : [ { function : 'sum' as const , field : 'amount' , alias : 'revenue' } ] } ;
@@ -54,8 +60,8 @@ describe('[aggregate order and cut] the HTTP MCP bridge over a real SQLite datab
5460 engine . registry . registerObject ( DEF as any ) ;
5561 for ( let i = 0 ; i < CUSTOMERS ; i ++ ) {
5662 const customer = `c${ String ( i ) . padStart ( 2 , '0' ) } ` ;
57- await driver . create ( OBJECT , { id : `${ customer } -a` , customer, amount : i * 10 } ) ;
58- await driver . create ( OBJECT , { id : `${ customer } -b` , customer, amount : 5 } ) ;
63+ await driver . create ( OBJECT , { id : `${ customer } -a` , customer, amount : i * 10 , stage : STAGE_OF ( i ) } ) ;
64+ await driver . create ( OBJECT , { id : `${ customer } -b` , customer, amount : 5 , stage : STAGE_OF ( i ) } ) ;
5965 }
6066 ( driver as unknown as { knex : { on : Function } } ) . knex . on ( 'query' , ( q : { sql : string } ) => { statements . push ( q . sql ) ; } ) ;
6167 const deps = {
@@ -110,6 +116,22 @@ describe('[aggregate order and cut] the HTTP MCP bridge over a real SQLite datab
110116 expect ( ran [ 0 ] ) . toMatch ( / o r d e r b y c a s e [ ^ ] + w h e n \? t h e n 0 w h e n \? t h e n 1 w h e n \? t h e n 2 e l s e 3 e n d a s c / i) ;
111117 } ) ;
112118
119+ it ( 'the declared option order a limit-only aggregate_records call sends: declared groups first, NULL last, the cut after it, the true total' , async ( ) => {
120+ // The order key `aggregate_records` derives for a `limit` with no
121+ // `orderBy` over a select group key (`@objectstack/mcp`, pinned there).
122+ const declared = { field : 'stage' , order : 'asc' as const , rank : [ 'open' , 'won' , 'lost' ] } ;
123+ const COUNT = { groupBy : [ 'stage' ] , aggregations : [ { function : 'count' as const , alias : 'n' } ] } ;
124+ statements . length = 0 ;
125+ const cut = await bridge . aggregateGroups ( OBJECT , { ...COUNT , orderBy : [ declared ] , limit : 2 } ) ;
126+ expect ( cut ) . toEqual ( { rows : [ { stage : 'open' , n : 34 } , { stage : 'won' , n : 36 } ] , totalGroups : 4 } ) ;
127+ const ran = statements . filter ( ( s ) => / g r o u p b y / i. test ( s ) ) ;
128+ expect ( ran , statements . join ( '\n' ) ) . toHaveLength ( 1 ) ;
129+ expect ( ran [ 0 ] ) . toMatch ( / o r d e r b y c a s e [ ^ ] + w h e n \? t h e n 0 w h e n \? t h e n 1 w h e n \? t h e n 2 e l s e 3 e n d a s c / i) ;
130+ expect ( ran [ 0 ] ) . toMatch ( / l i m i t \? / i) ;
131+ const all = await bridge . aggregateGroups ( OBJECT , { ...COUNT , orderBy : [ declared ] , limit : 10 } ) ;
132+ expect ( all . rows . map ( ( r : any ) => r . stage ) ) . toEqual ( [ 'open' , 'won' , 'lost' , null ] ) ;
133+ } ) ;
134+
113135 it ( 'an order key naming no column of the aggregated row is refused INVALID_SORT, naming the columns' , async ( ) => {
114136 const err : any = await bridge . aggregateGroups ( OBJECT , { ...REVENUE , orderBy : [ { field : 'amount' , order : 'desc' } ] , limit : 5 } )
115137 . then ( ( ) => undefined , ( e : unknown ) => e ) ;
0 commit comments