@@ -703,6 +703,61 @@ describe('RegistryUpdater', () => {
703703 expect ( registry . entities ) . toBeDefined ( ) ;
704704 expect ( registry . entities . tasks ) . toBeDefined ( ) ;
705705 } ) ;
706+
707+ it ( 'drains watcher updates queued while a batch is processing' , async ( ) => {
708+ jest . useFakeTimers ( ) ;
709+
710+ try {
711+ const updater = createUpdater ( { debounceMs : 10 } ) ;
712+ const batches = [ ] ;
713+ let releaseFirstBatch ;
714+ const firstBatchGate = new Promise ( ( resolve ) => {
715+ releaseFirstBatch = resolve ;
716+ } ) ;
717+
718+ updater . _executeBatch = jest . fn ( async ( batch ) => {
719+ batches . push ( batch . map ( ( item ) => path . basename ( item . filePath ) ) ) ;
720+ updater . _isProcessing = true ;
721+
722+ try {
723+ if ( batches . length === 1 ) {
724+ await firstBatchGate ;
725+ }
726+ return { updated : batch . length , errors : [ ] } ;
727+ } finally {
728+ updater . _isProcessing = false ;
729+ }
730+ } ) ;
731+
732+ const firstFile = createTempFile (
733+ '.aiox-core/development/tasks/queued-a.md' ,
734+ '# Queued A\n\n## Purpose\nFirst queued update.\n' ,
735+ ) ;
736+ updater . _queueUpdate ( 'add' , firstFile ) ;
737+ await jest . advanceTimersByTimeAsync ( 10 ) ;
738+
739+ expect ( batches ) . toEqual ( [ [ 'queued-a.md' ] ] ) ;
740+
741+ const secondFile = createTempFile (
742+ '.aiox-core/development/tasks/queued-b.md' ,
743+ '# Queued B\n\n## Purpose\nSecond queued update.\n' ,
744+ ) ;
745+ updater . _queueUpdate ( 'add' , secondFile ) ;
746+ await jest . advanceTimersByTimeAsync ( 10 ) ;
747+
748+ expect ( updater . getStats ( ) . pendingUpdates ) . toBe ( 1 ) ;
749+
750+ releaseFirstBatch ( ) ;
751+ await Promise . resolve ( ) ;
752+ await jest . advanceTimersByTimeAsync ( 10 ) ;
753+
754+ expect ( updater . _executeBatch ) . toHaveBeenCalledTimes ( 2 ) ;
755+ expect ( batches ) . toEqual ( [ [ 'queued-a.md' ] , [ 'queued-b.md' ] ] ) ;
756+ expect ( updater . getStats ( ) . pendingUpdates ) . toBe ( 0 ) ;
757+ } finally {
758+ jest . useRealTimers ( ) ;
759+ }
760+ } ) ;
706761 } ) ;
707762
708763 describe ( 'Performance (AC: 7)' , ( ) => {
0 commit comments