22 * @vitest -environment node
33 */
44
5- import { dbChainMock , dbChainMockFns } from '@sim/testing'
5+ import { dbChainMock , dbChainMockFns , resetDbChainMock } from '@sim/testing'
66import { beforeEach , describe , expect , it , vi } from 'vitest'
77
88vi . mock ( '@sim/db' , ( ) => dbChainMock )
@@ -24,6 +24,7 @@ function returnRows(count: number) {
2424describe ( 'drainRowsByColumn' , ( ) => {
2525 beforeEach ( ( ) => {
2626 vi . clearAllMocks ( )
27+ resetDbChainMock ( )
2728 } )
2829
2930 it ( 'drains in batches until a short batch and reports the set fully drained' , async ( ) => {
@@ -37,17 +38,31 @@ describe('drainRowsByColumn', () => {
3738 expect ( dbChainMockFns . returning ) . toHaveBeenCalledTimes ( 2 )
3839 } )
3940
40- it ( 'stops at the row budget and reports the set not fully drained' , async ( ) => {
41+ it ( 'stops at the budget and reports not fully drained when rows remain ' , async ( ) => {
4142 dbChainMockFns . returning
4243 . mockResolvedValueOnce ( returnRows ( 2 ) )
4344 . mockResolvedValueOnce ( returnRows ( 2 ) )
45+ // Existence probe after the budget is spent finds a leftover row.
46+ dbChainMockFns . limit . mockResolvedValue ( [ { id : 'leftover' } ] )
4447
4548 const result = await drainRowsByColumn ( { ...baseOpts , batchSize : 2 , rowBudget : 4 } )
4649
4750 expect ( result ) . toEqual ( { deleted : 4 , fullyDrained : false } )
4851 expect ( dbChainMockFns . returning ) . toHaveBeenCalledTimes ( 2 )
4952 } )
5053
54+ it ( 'reports fully drained when the budget is hit but the set emptied exactly' , async ( ) => {
55+ dbChainMockFns . returning
56+ . mockResolvedValueOnce ( returnRows ( 2 ) )
57+ . mockResolvedValueOnce ( returnRows ( 2 ) )
58+ // Existence probe finds nothing remaining.
59+ dbChainMockFns . limit . mockResolvedValue ( [ ] )
60+
61+ const result = await drainRowsByColumn ( { ...baseOpts , batchSize : 2 , rowBudget : 4 } )
62+
63+ expect ( result ) . toEqual ( { deleted : 4 , fullyDrained : true } )
64+ } )
65+
5166 it ( 'reports fully drained immediately when the match set is already empty' , async ( ) => {
5267 dbChainMockFns . returning . mockResolvedValueOnce ( [ ] )
5368
0 commit comments