@@ -134,7 +134,7 @@ describe('admin queries against Postgres', () => {
134134 ) . toBe ( false )
135135 } )
136136
137- it ( 'accumulates all cost rows beyond 5,000 without reading request bodies ' , async ( ) => {
137+ it ( 'aggregates all cost rows beyond 5,000 without hydrating individual logs ' , async ( ) => {
138138 // Real inserts exercise Payload's stored numeric types and pagination.
139139 for ( let offset = 0 ; offset < 5025 ; offset += 25 ) {
140140 await Promise . all (
@@ -155,11 +155,11 @@ describe('admin queries against Postgres', () => {
155155 ) ,
156156 )
157157 }
158- // Observe real query results, not mocked pagination. Correct totals alone
159- // would also pass if an adapter accidentally returned all rows at once .
158+ // Totals must remain correct without transferring thousands of Payload
159+ // documents into the report request, regardless of machine speed .
160160 const find = vi . spyOn ( payload , 'find' )
161161 try {
162- const result = await loadReportCosts ( req , { pipelineRunId : { equals : prefix } } )
162+ const result = await loadReportCosts ( req , { pipelineRunId : prefix } )
163163 expect ( result . aggregate . rowCount ) . toBe ( 5026 )
164164 expect ( result . aggregate . totalUsd ) . toBe ( 5026 * 0.25 )
165165 expect ( result . stages [ 0 ] ) . toMatchObject ( {
@@ -168,21 +168,49 @@ describe('admin queries against Postgres', () => {
168168 outputTokens : 5026 ,
169169 } )
170170 expect ( JSON . stringify ( result ) ) . not . toContain ( 'UNUSED_COST_REQUEST' )
171- const batchSizes : number [ ] = [ ]
172- for ( const [ index , [ options ] ] of find . mock . calls . entries ( ) ) {
173- if ( options . collection === 'cost-log' && options . limit === 1000 ) {
174- const batch = await find . mock . results [ index ] . value
175- batchSizes . push ( batch . docs . length )
176- expect ( JSON . stringify ( batch . docs ) ) . not . toContain ( 'UNUSED_COST_REQUEST' )
177- }
178- }
179- expect ( batchSizes ) . toEqual ( [ 1000 , 1000 , 1000 , 1000 , 1000 , 26 ] )
171+ expect ( find . mock . calls . filter ( ( [ options ] ) => options . collection === 'cost-log' ) ) . toHaveLength ( 0 )
180172 } finally {
181173 find . mockRestore ( )
182174 }
183- const empty = await loadReportCosts ( req , { pipelineRunId : { equals : `${ prefix } -empty` } } )
175+ const empty = await loadReportCosts ( req , { pipelineRunId : `${ prefix } -empty` } )
184176 expect ( empty . aggregate . rowCount ) . toBe ( 0 )
185177 } , 120000 )
178+
179+ it ( 'preserves stage/model totals, null buckets, and inclusive period filters' , async ( ) => {
180+ const pipelineRunId = `${ prefix } -quoted'run`
181+ const rows = [
182+ { stage : 'generate' as const , model : 'alpha' , costUsd : 0.5 , inputTokens : 5 , createdAt : '2026-01-01T00:00:00.000Z' } ,
183+ { stage : 'generate' as const , model : 'alpha' , costUsd : 0.25 , outputTokens : 2 } ,
184+ { stage : 'factCheck' as const , model : 'beta' , costUsd : 1 , inputTokens : 8 , outputTokens : 3 } ,
185+ { } ,
186+ { model : '(unknown)' , costUsd : 0.125 } ,
187+ ]
188+ for ( const row of rows ) {
189+ await payload . create ( {
190+ collection : 'cost-log' ,
191+ overrideAccess : true ,
192+ data : { pipelineRunId, createdAt : '2026-02-01T00:00:00.000Z' , ...row } ,
193+ } )
194+ }
195+ const all = await loadReportCosts ( req , { pipelineRunId } )
196+ expect ( all . aggregate ) . toEqual ( {
197+ rowCount : 5 ,
198+ totalUsd : 1.875 ,
199+ byStage : [ { label : 'factCheck' , usd : 1 } , { label : 'generate' , usd : 0.75 } , { label : '(unknown)' , usd : 0.125 } ] ,
200+ byModel : [ { label : 'beta' , usd : 1 } , { label : 'alpha' , usd : 0.75 } , { label : '(unknown)' , usd : 0.125 } ] ,
201+ } )
202+ expect ( all . stages ) . toEqual ( [
203+ { stage : 'factCheck' , calls : 1 , costUsd : 1 , inputTokens : 8 , outputTokens : 3 } ,
204+ { stage : 'generate' , calls : 2 , costUsd : 0.75 , inputTokens : 5 , outputTokens : 2 } ,
205+ { stage : '(unknown)' , calls : 2 , costUsd : 0.125 , inputTokens : 0 , outputTokens : 0 } ,
206+ ] )
207+ const recent = await loadReportCosts ( req , { pipelineRunId, createdAtFrom : '2026-02-01T00:00:00.000Z' } )
208+ expect ( recent . aggregate . rowCount ) . toBe ( 4 )
209+ expect ( recent . aggregate . totalUsd ) . toBe ( 1.375 )
210+ const empty = await loadReportCosts ( req , { pipelineRunId, createdAtFrom : '2026-03-01T00:00:00.000Z' } )
211+ expect ( empty . aggregate ) . toEqual ( { rowCount : 0 , totalUsd : 0 , byStage : [ ] , byModel : [ ] } )
212+ expect ( empty . stages ) . toEqual ( [ ] )
213+ } )
186214} )
187215
188216it ( 'report summaries preserve missing decisions, QA denominators, and spend' , ( ) => {
0 commit comments