@@ -89,21 +89,29 @@ const deals = (stage: string, n: number): Row[] =>
8989// 20 open deals (amounts 0 … 19, sum 190), 4 won (sum 6).
9090const ROWS = [ ...deals ( 'open' , 20 ) , ...deals ( 'won' , 4 ) ] ;
9191
92- /** A grouped aggregate over `rows` as the platform's door answers it. */
93- function groupHeadersOver ( rows : Row [ ] ) {
92+ /**
93+ * A grouped aggregate over `rows` as the platform's door answers it: one group
94+ * per distinct value of every `groupBy` key (objectstack#37844 asks two).
95+ * `cut` answers the first two groups and `hasMore`, as the door cuts a grouped
96+ * read to its default page size.
97+ */
98+ function groupHeadersOver ( rows : Row [ ] , cut = false ) {
9499 const store = new ValueDataSource < Row > ( { items : rows } ) ;
95100 return vi . fn ( async ( _object : string , query : any ) => {
96101 const kept = extractRecords (
97102 await store . find ( OBJECT , { ...( query . where ? { $filter : query . where } : { } ) , $top : 100000 } as any ) ,
98103 ) as Row [ ] ;
99- const key = query . groupBy [ 0 ] ;
100- const groups = new Map < unknown , Row [ ] > ( ) ;
104+ const keys : string [ ] = query . groupBy ;
105+ const groups = new Map < string , { values : unknown [ ] ; members : Row [ ] } > ( ) ;
101106 for ( const row of kept ) {
102- const k = row [ key ] ?? null ;
103- groups . set ( k , [ ...( groups . get ( k ) ?? [ ] ) , row ] ) ;
107+ const values = keys . map ( ( key ) => row [ key ] ?? null ) ;
108+ const id = JSON . stringify ( values ) ;
109+ const group = groups . get ( id ) ?? { values, members : [ ] } ;
110+ group . members . push ( row ) ;
111+ groups . set ( id , group ) ;
104112 }
105- return [ ...groups ] . map ( ( [ k , members ] ) => {
106- const out : Row = { [ key ] : k } ;
113+ const answer : Row [ ] & { hasMore ?: boolean } = [ ...groups . values ( ) ] . map ( ( { values : k , members } ) => {
114+ const out : Row = Object . fromEntries ( keys . map ( ( key , i ) => [ key , k [ i ] ] ) ) ;
107115 for ( const agg of query . aggregations ) {
108116 const values = members . map ( ( m ) => m [ agg . field ] ) as number [ ] ;
109117 out [ agg . alias ] = agg . function === 'count'
@@ -118,10 +126,14 @@ function groupHeadersOver(rows: Row[]) {
118126 }
119127 return out ;
120128 } ) ;
129+ if ( ! cut ) return answer ;
130+ const partial : Row [ ] & { hasMore ?: boolean } = answer . slice ( 0 , 2 ) ;
131+ partial . hasMore = true ;
132+ return partial ;
121133 } ) ;
122134}
123135
124- function adapter ( rows : Row [ ] , door : 'answers' | 'refuses' = 'answers' ) {
136+ function adapter ( rows : Row [ ] , door : 'answers' | 'refuses' | 'cuts' | 'none' = 'answers' ) {
125137 const store = new ValueDataSource < Row > ( { items : rows } ) ;
126138 return {
127139 find : vi . fn ( async ( object : string , params : any ) => store . find ( object , params ) ) ,
@@ -130,19 +142,23 @@ function adapter(rows: Row[], door: 'answers' | 'refuses' = 'answers') {
130142 update : vi . fn ( ) ,
131143 delete : vi . fn ( ) ,
132144 getObjectSchema : vi . fn ( async ( ) => DEF ) ,
133- queryGroupHeaders : door === 'answers'
134- ? groupHeadersOver ( rows )
135- : vi . fn ( async ( ) => { throw Object . assign ( new Error ( 'forbidden' ) , { status : 403 } ) ; } ) ,
145+ ...( door === 'none'
146+ ? { }
147+ : {
148+ queryGroupHeaders : door === 'refuses'
149+ ? vi . fn ( async ( ) => { throw Object . assign ( new Error ( 'forbidden' ) , { status : 403 } ) ; } )
150+ : groupHeadersOver ( rows , door === 'cuts' ) ,
151+ } ) ,
136152 } as any ;
137153}
138154
139155const SWIMLANES = { swimlaneField : 'region' } ;
140156
141- function mount ( source : any , summary : unknown , handed ?: { rows : Row [ ] } ) {
157+ function mount ( source : any , summary : unknown , handed ?: { rows : Row [ ] } , extra : Record < string , unknown > = { } ) {
142158 return render (
143159 < SchemaRendererProvider dataSource = { source } >
144160 < ObjectKanban
145- schema = { { type : 'object-kanban' , objectName : OBJECT , groupBy : 'stage' , cardFields : [ 'amount' ] , summary, ...SWIMLANES } as any }
161+ schema = { { type : 'object-kanban' , objectName : OBJECT , groupBy : 'stage' , cardFields : [ 'amount' ] , summary, ...SWIMLANES , ... extra } as any }
146162 dataSource = { source }
147163 { ...( handed ? { data : handed . rows , laneStatisticQuery : { search : '' , reloadKey : '0' } } : { } ) }
148164 />
@@ -189,22 +205,30 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
189205 expect ( footRow ( container ) ! . textContent ) . toContain ( 'Count' ) ;
190206 } ) ;
191207
192- it ( '(c) on a page handed in with its query, a column counts the whole lane and a swimlane row is marked a floor ' , async ( ) => {
208+ it ( '(c) on a page handed in with its query, a column counts the whole lane and a swimlane row its whole swimlane (objectstack#37844) ' , async ( ) => {
193209 const source = adapter ( ROWS ) ;
194- // The page: 3 open deals, 1 won; the query holds 20 and 4.
210+ // The page: 3 open deals, 1 won; the query holds 20 and 4, 12 north and 12 south .
195211 const { container } = mount ( source , { type : 'sum' , field : 'amount' } , { rows : [ ...deals ( 'open' , 3 ) , ...deals ( 'won' , 1 ) ] } ) ;
196212 await waitFor ( ( ) => expect ( columnCounts ( container ) ) . toEqual ( [ '20' , '4' ] ) ) ;
197213 expect ( footFigures ( container ) ) . toEqual ( [ '190' , '6' ] ) ;
198- // north holds open-000, open-002, won-000 on the page; south open-001.
199- expect ( swimlaneCounts ( container ) ) . toEqual ( [ '3+' , '1+' ] ) ;
200- expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
214+ // The page holds 3 north and 1 south; the swimlane read counts 12 and 12.
215+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '12' , '12' ] ) ) ;
216+ // The lane read, then ONE read on both keys, counting each group.
217+ expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 2 ) ;
218+ expect ( source . queryGroupHeaders . mock . calls [ 1 ] [ 1 ] ) . toEqual ( {
219+ groupBy : [ 'stage' , 'region' ] ,
220+ aggregations : [ { function : 'count' , alias : 'count' } ] ,
221+ } ) ;
201222 expect ( source . find ) . not . toHaveBeenCalled ( ) ;
202223 } ) ;
203224
204- it ( '(d) CONTROL: handed every record of its query, every count is bare' , async ( ) => {
205- const { container } = mount ( adapter ( ROWS ) , undefined , { rows : ROWS } ) ;
225+ it ( '(d) CONTROL: handed every record of its query, every count is bare, and no swimlane read is asked' , async ( ) => {
226+ const source = adapter ( ROWS ) ;
227+ const { container } = mount ( source , undefined , { rows : ROWS } ) ;
206228 await waitFor ( ( ) => expect ( columnCounts ( container ) ) . toEqual ( [ '20' , '4' ] ) ) ;
207229 expect ( swimlaneCounts ( container ) ) . toEqual ( [ '12' , '12' ] ) ;
230+ expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
231+ expect ( source . queryGroupHeaders . mock . calls [ 0 ] [ 1 ] . groupBy ) . toEqual ( [ 'stage' ] ) ;
208232 } ) ;
209233
210234 it ( '(e) a refused read marks the page\'s column counts `+`, and draws no figure' , async ( ) => {
@@ -216,10 +240,13 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
216240 expect ( footFigures ( container ) ) . toEqual ( [ null , null ] ) ;
217241 } ) ;
218242
219- it ( '(f) CONTROL: with no statistic the board draws no foot row' , async ( ) => {
220- const { container } = mount ( adapter ( ROWS ) , undefined ) ;
243+ it ( '(f) CONTROL: with no statistic the board draws no foot row, and a board holding every record asks no read' , async ( ) => {
244+ const source = adapter ( ROWS ) ;
245+ const { container } = mount ( source , undefined ) ;
221246 await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '12' , '12' ] ) ) ;
222247 expect ( footRow ( container ) ) . toBeNull ( ) ;
248+ expect ( columnCounts ( container ) ) . toEqual ( [ '20' , '4' ] ) ;
249+ expect ( source . queryGroupHeaders ) . not . toHaveBeenCalled ( ) ;
223250 } ) ;
224251
225252 // 20 open deals (amounts 0 … 19), 4 won (0 … 3).
@@ -237,6 +264,68 @@ describe('a swimlane board draws the view\'s lane statistic (objectstack#37326)'
237264 } ) ;
238265} ) ;
239266
267+ /**
268+ * objectstack#37844 — a board drawn in swimlanes that queries its own cards
269+ * through a window (`limit` 10 over the query's 24 records: 10 open, all
270+ * north and south alternating, 5 + 5) reads every swimlane row's count and
271+ * every column's count whole from ONE grouped read on both keys.
272+ */
273+ describe ( 'a windowed swimlane board counts each whole swimlane and column from one two-key read (objectstack#37844)' , ( ) => {
274+ const WINDOW = { limit : 10 } ;
275+
276+ it ( 'a board that queries itself through a full window counts 12 and 12 swimlanes, 20 and 4 columns' , async ( ) => {
277+ const source = adapter ( ROWS ) ;
278+ const { container } = mount ( source , undefined , undefined , WINDOW ) ;
279+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '12' , '12' ] ) ) ;
280+ expect ( columnCounts ( container ) ) . toEqual ( [ '20' , '4' ] ) ;
281+ // The window: 10 cards, all open.
282+ expect ( container . querySelectorAll ( '[role="listitem"]' ) ) . toHaveLength ( 10 ) ;
283+ expect ( source . find ) . toHaveBeenCalledTimes ( 1 ) ;
284+ expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
285+ expect ( source . queryGroupHeaders . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
286+ groupBy : [ 'stage' , 'region' ] ,
287+ aggregations : [ { function : 'count' , alias : 'count' } ] ,
288+ } ) ;
289+ } ) ;
290+
291+ it ( 'reads under the board\'s own filter' , async ( ) => {
292+ const source = adapter ( ROWS ) ;
293+ // amount >= 2: open 2 … 19 (18: 9 north, 9 south), won 2, 3 (north, south).
294+ const { container } = mount ( source , undefined , undefined , { ...WINDOW , filter : [ [ 'amount' , '>=' , 2 ] ] } ) ;
295+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '10' , '10' ] ) ) ;
296+ expect ( columnCounts ( container ) ) . toEqual ( [ '18' , '2' ] ) ;
297+ expect ( source . queryGroupHeaders . mock . calls [ 0 ] [ 1 ] . where ) . toBeDefined ( ) ;
298+ } ) ;
299+
300+ it . each ( [
301+ [ 'has no grouped door' , 'none' ] ,
302+ [ 'refuses the read' , 'refuses' ] ,
303+ [ 'cuts the read (hasMore)' , 'cuts' ] ,
304+ ] as const ) ( 'CONTROL: when the source %s, the window\'s counts stay marked `+`' , async ( _ , door ) => {
305+ const source = adapter ( ROWS , door ) ;
306+ const { container } = mount ( source , undefined , undefined , WINDOW ) ;
307+ if ( door !== 'none' ) await waitFor ( ( ) => expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ) ;
308+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '5+' , '5+' ] ) ) ;
309+ expect ( columnCounts ( container ) ) . toEqual ( [ '10+' , '0+' ] ) ;
310+ } ) ;
311+
312+ it ( 'CONTROL: a window the query does not fill asks no read and writes bare counts' , async ( ) => {
313+ const source = adapter ( ROWS ) ;
314+ const { container } = mount ( source , undefined , undefined , { limit : 50 } ) ;
315+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '12' , '12' ] ) ) ;
316+ expect ( columnCounts ( container ) ) . toEqual ( [ '20' , '4' ] ) ;
317+ expect ( source . queryGroupHeaders ) . not . toHaveBeenCalled ( ) ;
318+ } ) ;
319+
320+ it ( 'CONTROL: a page handed in whose lane read is cut asks no swimlane read; its rows stay marked `+`' , async ( ) => {
321+ const source = adapter ( ROWS , 'cuts' ) ;
322+ const { container } = mount ( source , undefined , { rows : [ ...deals ( 'open' , 3 ) , ...deals ( 'won' , 1 ) ] } ) ;
323+ await waitFor ( ( ) => expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ) ;
324+ await waitFor ( ( ) => expect ( swimlaneCounts ( container ) ) . toEqual ( [ '3+' , '1+' ] ) ) ;
325+ expect ( source . queryGroupHeaders ) . toHaveBeenCalledTimes ( 1 ) ;
326+ } ) ;
327+ } ) ;
328+
240329describe ( 'a swimlane board handed one page draws no column past the page (objectstack#37718)' , ( ) => {
241330 const ACCOUNT_DEF = {
242331 ...DEF ,
0 commit comments