@@ -99,6 +99,7 @@ import {
9999 compileListViewGroupQuery ,
100100 compileListViewGroupRowsQuery ,
101101 deriveColumnSummary ,
102+ listViewGroupOrderColumn ,
102103} from '@objectstack/spec/ui' ;
103104import type { ListViewGroupHeaderRow , ListViewGroupQuerySource } from '@objectstack/spec/ui' ;
104105import { RestServer } from './rest-server' ;
@@ -215,6 +216,60 @@ const PLAIN_VIEW: ListViewGroupQuerySource = { grouping: { fields: [{ field: 'bu
215216const OBJECT_CONTIGUOUS = 'work_item' ;
216217const OBJECT_INTERLEAVED = 'work_item_interleaved' ;
217218const OBJECT_WITH_EMPTY = 'work_item_unassigned' ;
219+ /** [#25786] The forecast shape §11 orders by another field. */
220+ const OBJECT_FORECAST = 'forecast_period' ;
221+
222+ /**
223+ * [#25786] Periods grouped by their LABEL, which sorts as text, ordered by
224+ * their START. Two labels share a start (the tie), one has none (the null
225+ * group), and the rows are stored interleaved so no group order can fall out
226+ * of the stored order.
227+ */
228+ const FORECAST_PERIODS : ReadonlyArray < readonly [ label : string , start : string | null , closedAt : string | null , rows : number ] > = [
229+ [ 'Q4 2025' , '2025-10-01' , '2025-12-30T09:00:00.000Z' , 2 ] ,
230+ [ 'Sep 2026' , '2026-09-01' , '2026-09-29T17:30:00.000Z' , 1 ] ,
231+ [ 'Oct 2026' , '2026-10-01' , '2026-10-30T08:15:00.000Z' , 3 ] ,
232+ [ 'Q4 2026' , '2026-10-01' , '2026-12-31T23:59:00.000Z' , 1 ] ,
233+ [ 'Nov 2026' , '2026-11-01' , '2026-11-27T12:00:00.000Z' , 2 ] ,
234+ [ 'Q1 2027' , '2027-01-01' , '2027-03-31T10:45:00.000Z' , 2 ] ,
235+ [ 'Unscheduled' , null , null , 1 ] ,
236+ ] ;
237+
238+ const FORECAST_ROWS : Array < Record < string , unknown > > = ( ( ) => {
239+ const queues = FORECAST_PERIODS . map ( ( [ label , start , closedAt , rows ] ) =>
240+ Array . from ( { length : rows } , ( _ , i ) => ( {
241+ id : `${ label } -${ i } ` ,
242+ period_label : label ,
243+ period_start : start ,
244+ // Each row of a period closes an hour after the one before it, so `min`
245+ // and `max` over this column are different instants per group.
246+ closed_at : closedAt === null ? null : new Date ( Date . parse ( closedAt ) + i * 3_600_000 ) . toISOString ( ) ,
247+ amount : i + 1 ,
248+ } ) ) ) ;
249+ const out : Array < Record < string , unknown > > = [ ] ;
250+ while ( queues . some ( ( q ) => q . length > 0 ) ) {
251+ for ( const q of [ ...queues ] . reverse ( ) ) {
252+ const next = q . shift ( ) ;
253+ if ( next ) out . push ( next ) ;
254+ }
255+ }
256+ return out ;
257+ } ) ( ) ;
258+
259+ function forecastSchema ( ) {
260+ return {
261+ name : OBJECT_FORECAST ,
262+ label : OBJECT_FORECAST ,
263+ systemFields : false ,
264+ fields : {
265+ id : { name : 'id' , type : 'text' as const , primaryKey : true } ,
266+ period_label : { name : 'period_label' , type : 'text' as const , label : 'Period' } ,
267+ period_start : { name : 'period_start' , type : 'date' as const , label : 'Period start' } ,
268+ closed_at : { name : 'closed_at' , type : 'datetime' as const , label : 'Closed at' } ,
269+ amount : { name : 'amount' , type : 'number' as const , label : 'Amount' } ,
270+ } ,
271+ } ;
272+ }
218273
219274function objectSchema ( name : string ) {
220275 return {
@@ -318,7 +373,7 @@ const TIERS: readonly Tier[] = ['driver-sql', 'in-memory'];
318373const calls = { nativeAggregate : 0 , driverFind : 0 , dataFind : 0 , findWithAggregations : 0 } ;
319374
320375/** The row stores this file seeds — a `find` on one of these is a DATA read. */
321- const DATA_OBJECTS : readonly string [ ] = [ OBJECT_CONTIGUOUS , OBJECT_INTERLEAVED , OBJECT_WITH_EMPTY ] ;
376+ const DATA_OBJECTS : readonly string [ ] = [ OBJECT_CONTIGUOUS , OBJECT_INTERLEAVED , OBJECT_WITH_EMPTY , OBJECT_FORECAST ] ;
322377
323378/**
324379 * Every `driver.find` the shim saw, in order — object plus the AST's top-level
@@ -381,10 +436,12 @@ beforeAll(async () => {
381436 for ( const name of [ OBJECT_CONTIGUOUS , OBJECT_INTERLEAVED , OBJECT_WITH_EMPTY ] ) {
382437 engine . registry . registerObject ( objectSchema ( name ) as never ) ;
383438 }
439+ engine . registry . registerObject ( forecastSchema ( ) as never ) ;
384440 await engine . syncSchemas ( ) ;
385441 await engine . insert ( OBJECT_CONTIGUOUS , inStoredOrder ( CONTIGUOUS ) ) ;
386442 await engine . insert ( OBJECT_INTERLEAVED , inStoredOrder ( INTERLEAVED ) ) ;
387443 await engine . insert ( OBJECT_WITH_EMPTY , inStoredOrder ( [ ...CONTIGUOUS , ...UNASSIGNED ] ) ) ;
444+ await engine . insert ( OBJECT_FORECAST , FORECAST_ROWS ) ;
388445
389446 const protocol = new ObjectStackProtocolImplementation ( engine as never ) ;
390447 const rest = new RestServer (
@@ -949,3 +1006,117 @@ describe('§10 a grouped query under `search` groups the SEARCHED rows', () => {
9491006 expect ( res . _body ?. code ) . toBe ( 'INVALID_FIELD' ) ;
9501007 } ) ;
9511008} ) ;
1009+
1010+ // ─── §11 A level ordered by another field (#25786) ───────────────────────────
1011+
1012+ /**
1013+ * `GroupingFieldSchema.orderBy`: the consumer sorts the header rows, and the
1014+ * header query carries the value to sort by — one `min` (asc) / `max` (desc)
1015+ * node over `orderBy`. What was unmeasured is whether the two faces answer
1016+ * that node with one value of one TYPE per group over a `date` and a
1017+ * `datetime` column, and whether the door refuses an operand the lint refuses.
1018+ *
1019+ * The consumer's sort is written out here as `GroupingFieldSchema.orderBy`
1020+ * states it — test-local, because the sort is the consumer's: by the column
1021+ * {@link listViewGroupOrderColumn} names, `null` last in both directions,
1022+ * equal values by the group key in the same direction.
1023+ */
1024+ describe ( '§11 a level ordered by another field — the header rows carry the order value' , ( ) => {
1025+ const byStart = ( order : 'asc' | 'desc' = 'asc' ) => ( {
1026+ grouping : { fields : [ { field : 'period_label' , orderBy : 'period_start' , order } ] } ,
1027+ } ) ;
1028+
1029+ const sortHeaders = ( headers : Array < Record < string , unknown > > , column : string , order : 'asc' | 'desc' ) : string [ ] => {
1030+ const sign = order === 'desc' ? - 1 : 1 ;
1031+ const cmp = ( a : unknown , b : unknown ) => ( a === b ? 0 : ( a as string ) < ( b as string ) ? - 1 : 1 ) ;
1032+ return [ ...headers ]
1033+ . sort ( ( a , b ) => {
1034+ const av = a [ column ] ;
1035+ const bv = b [ column ] ;
1036+ if ( av == null || bv == null ) return av == null && bv == null ? 0 : av == null ? 1 : - 1 ;
1037+ return sign * ( cmp ( av , bv ) || cmp ( a . period_label , b . period_label ) ) ;
1038+ } )
1039+ . map ( ( h ) => String ( h . period_label ) ) ;
1040+ } ;
1041+
1042+ it . each ( TIERS ) ( '%s: one header per LABEL, each carrying its period start under min_period_start' , async ( tier ) => {
1043+ const view = byStart ( ) ;
1044+ const query = compileListViewGroupQuery ( view ) ;
1045+ expect ( query . groupBy ) . toEqual ( [ 'period_label' ] ) ;
1046+ const column = listViewGroupOrderColumn ( view . grouping , 0 ) ;
1047+ expect ( column ) . toBe ( 'min_period_start' ) ;
1048+
1049+ const headers = await onTier ( tier , ( ) => records ( OBJECT_FORECAST , query ) ) ;
1050+ expect ( headers ) . toHaveLength ( FORECAST_PERIODS . length ) ;
1051+ for ( const [ label , start , , rows ] of FORECAST_PERIODS ) {
1052+ const header = headers . find ( ( h ) => h . period_label === label ) ! ;
1053+ expect ( header , label ) . toBeDefined ( ) ;
1054+ expect ( header [ LIST_VIEW_GROUP_COUNT_ALIAS ] , label ) . toBe ( rows ) ;
1055+ // The stored calendar-day form, on both faces — a string the consumer
1056+ // orders as it is.
1057+ expect ( header [ column ] , label ) . toBe ( start ) ;
1058+ }
1059+ expect ( sortHeaders ( headers , column , 'asc' ) ) . toEqual ( [
1060+ 'Q4 2025' , 'Sep 2026' , 'Oct 2026' , 'Q4 2026' , 'Nov 2026' , 'Q1 2027' , 'Unscheduled' ,
1061+ ] ) ;
1062+ // CONTROL — the key alone sorts as text, which is what the forecast list showed.
1063+ expect ( sortHeaders ( headers , 'period_label' , 'asc' ) ) . toEqual ( [
1064+ 'Nov 2026' , 'Oct 2026' , 'Q1 2027' , 'Q4 2025' , 'Q4 2026' , 'Sep 2026' , 'Unscheduled' ,
1065+ ] ) ;
1066+ } ) ;
1067+
1068+ it . each ( TIERS ) ( '%s: `desc` carries max_period_start, and the null group is still last' , async ( tier ) => {
1069+ const view = byStart ( 'desc' ) ;
1070+ const column = listViewGroupOrderColumn ( view . grouping , 0 ) ;
1071+ expect ( column ) . toBe ( 'max_period_start' ) ;
1072+ const headers = await onTier ( tier , ( ) => records ( OBJECT_FORECAST , compileListViewGroupQuery ( view ) ) ) ;
1073+ expect ( sortHeaders ( headers , column , 'desc' ) ) . toEqual ( [
1074+ 'Q1 2027' , 'Nov 2026' , 'Q4 2026' , 'Oct 2026' , 'Sep 2026' , 'Q4 2025' , 'Unscheduled' ,
1075+ ] ) ;
1076+ } ) ;
1077+
1078+ it ( 'driver-sql and the in-memory tier answer the same order values, types included — date and datetime' , async ( ) => {
1079+ for ( const grouping of [
1080+ byStart ( ) . grouping ,
1081+ byStart ( 'desc' ) . grouping ,
1082+ { fields : [ { field : 'period_label' , orderBy : 'closed_at' } ] } ,
1083+ { fields : [ { field : 'period_label' , orderBy : 'closed_at' , order : 'desc' as const } ] } ,
1084+ ] ) {
1085+ const query = compileListViewGroupQuery ( { grouping } ) ;
1086+ const pushedDown = await onTier ( 'driver-sql' , ( ) => records ( OBJECT_FORECAST , query ) ) ;
1087+ const inMemory = await onTier ( 'in-memory' , ( ) => records ( OBJECT_FORECAST , query ) ) ;
1088+ const label = JSON . stringify ( grouping . fields [ 0 ] ) ;
1089+ expect ( canonical ( inMemory ) , label ) . toEqual ( canonical ( pushedDown ) ) ;
1090+ expect ( pushedDown , label ) . toHaveLength ( FORECAST_PERIODS . length ) ;
1091+ }
1092+ } ) ;
1093+
1094+ it . each ( TIERS ) ( '%s: min and max over a datetime are different instants per group, each the right row\'s' , async ( tier ) => {
1095+ const asc = { fields : [ { field : 'period_label' , orderBy : 'closed_at' } ] } ;
1096+ const desc = { fields : [ { field : 'period_label' , orderBy : 'closed_at' , order : 'desc' as const } ] } ;
1097+ const first = await onTier ( tier , ( ) => records ( OBJECT_FORECAST , compileListViewGroupQuery ( { grouping : asc } ) ) ) ;
1098+ const last = await onTier ( tier , ( ) => records ( OBJECT_FORECAST , compileListViewGroupQuery ( { grouping : desc } ) ) ) ;
1099+ const own = ( label : string ) => FORECAST_ROWS . filter ( ( r ) => r . period_label === label ) . map ( ( r ) => r . closed_at as string ) . sort ( ) ;
1100+ const instant = ( v : unknown ) => ( v == null ? null : new Date ( v as string ) . getTime ( ) ) ;
1101+ const oct = ( rows : Array < Record < string , unknown > > , column : string ) =>
1102+ instant ( rows . find ( ( h ) => h . period_label === 'Oct 2026' ) ! [ column ] ) ;
1103+ expect ( oct ( first , listViewGroupOrderColumn ( asc , 0 ) ) ) . toBe ( instant ( own ( 'Oct 2026' ) [ 0 ] ) ) ;
1104+ expect ( oct ( last , listViewGroupOrderColumn ( desc , 0 ) ) ) . toBe ( instant ( own ( 'Oct 2026' ) [ 2 ] ) ) ;
1105+ expect ( oct ( first , 'min_closed_at' ) ) . not . toBe ( oct ( last , 'max_closed_at' ) ) ;
1106+ } ) ;
1107+
1108+ it ( 'an `orderBy` the lint refuses is refused by the door: `min` over a text field is 400 / INVALID_FIELD' , async ( ) => {
1109+ const query = compileListViewGroupQuery ( { grouping : { fields : [ { field : 'period_start' , orderBy : 'period_label' } ] } } ) ;
1110+ expect ( query . aggregations ) . toContainEqual ( { function : 'min' , field : 'period_label' , alias : 'min_period_label' } ) ;
1111+ const res = await postQuery ( OBJECT_FORECAST , query ) ;
1112+ expect ( res . _status ) . toBe ( 400 ) ;
1113+ expect ( res . _body ?. code ) . toBe ( 'INVALID_FIELD' ) ;
1114+ } ) ;
1115+
1116+ it ( 'a dotted `orderBy` is refused by the door: 400 / INVALID_FIELD, not a null order value' , async ( ) => {
1117+ const query = compileListViewGroupQuery ( { grouping : { fields : [ { field : 'period_label' , orderBy : 'period_start.year' } ] } } ) ;
1118+ const res = await postQuery ( OBJECT_FORECAST , query ) ;
1119+ expect ( res . _status ) . toBe ( 400 ) ;
1120+ expect ( res . _body ?. code ) . toBe ( 'INVALID_FIELD' ) ;
1121+ } ) ;
1122+ } ) ;
0 commit comments