@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
22import * as fs from "fs" ;
33import * as path from "path" ;
44import * as os from "os" ;
5- import { FileWatcher , GitHeadWatcher , FileChange } from "../src/watcher/index.js" ;
5+ import { FileWatcher , GitHeadWatcher , FileChange , createWatcherWithIndexer } from "../src/watcher/index.js" ;
66import { ParsedCodebaseIndexConfig } from "../src/config/schema.js" ;
77
88const createTestConfig = ( overrides : Partial < ParsedCodebaseIndexConfig > = { } ) : ParsedCodebaseIndexConfig => ( {
@@ -119,6 +119,73 @@ describe("FileWatcher", () => {
119119 expect ( tsChanges . length ) . toBeGreaterThanOrEqual ( 0 ) ;
120120 expect ( mdChanges . length ) . toBe ( 0 ) ;
121121 } ) ;
122+
123+ it ( "should include matching root-level files" , async ( ) => {
124+ const changes : FileChange [ ] = [ ] ;
125+ watcher = new FileWatcher ( tempDir , createTestConfig ( { include : [ "**/*.ts" ] } ) ) ;
126+
127+ watcher . start ( async ( c ) => {
128+ changes . push ( ...c ) ;
129+ } ) ;
130+
131+ await new Promise ( ( r ) => setTimeout ( r , 100 ) ) ;
132+
133+ fs . writeFileSync ( path . join ( tempDir , "root.ts" ) , "export const root = 1;" ) ;
134+
135+ await new Promise ( ( r ) => setTimeout ( r , 1500 ) ) ;
136+
137+ expect ( changes . some ( ( c ) => c . path . endsWith ( "root.ts" ) ) ) . toBe ( true ) ;
138+ } ) ;
139+ } ) ;
140+
141+ describe ( "createWatcherWithIndexer" , ( ) => {
142+ it ( "uses the latest indexer instance for file-triggered reindexing" , async ( ) => {
143+ const staleIndexer = {
144+ index : vi . fn ( ) . mockResolvedValue ( undefined ) ,
145+ } ;
146+ const refreshedIndexer = {
147+ index : vi . fn ( ) . mockResolvedValue ( undefined ) ,
148+ } ;
149+
150+ let currentIndexer = staleIndexer ;
151+ const combinedWatcher = createWatcherWithIndexer (
152+ ( ) => currentIndexer ,
153+ tempDir ,
154+ createTestConfig ( )
155+ ) ;
156+
157+ await new Promise ( ( r ) => setTimeout ( r , 100 ) ) ;
158+ currentIndexer = refreshedIndexer ;
159+
160+ fs . writeFileSync ( path . join ( tempDir , "src" , "reindex-me.ts" ) , "export const value = 1;" ) ;
161+
162+ await new Promise ( ( r ) => setTimeout ( r , 1500 ) ) ;
163+
164+ expect ( refreshedIndexer . index ) . toHaveBeenCalledTimes ( 1 ) ;
165+ expect ( staleIndexer . index ) . not . toHaveBeenCalled ( ) ;
166+
167+ combinedWatcher . stop ( ) ;
168+ } ) ;
169+
170+ it ( "stops the watcher cleanly after start" , ( ) => {
171+ const indexer = {
172+ index : vi . fn ( ) . mockResolvedValue ( undefined ) ,
173+ } ;
174+
175+ const combinedWatcher = createWatcherWithIndexer (
176+ ( ) => indexer ,
177+ tempDir ,
178+ createTestConfig ( )
179+ ) ;
180+
181+ expect ( combinedWatcher . fileWatcher . isRunning ( ) ) . toBe ( true ) ;
182+ expect ( combinedWatcher . gitWatcher ?. isRunning ( ) ?? false ) . toBe ( false ) ;
183+
184+ combinedWatcher . stop ( ) ;
185+
186+ expect ( combinedWatcher . fileWatcher . isRunning ( ) ) . toBe ( false ) ;
187+ expect ( combinedWatcher . gitWatcher ?. isRunning ( ) ?? false ) . toBe ( false ) ;
188+ } ) ;
122189 } ) ;
123190} ) ;
124191
0 commit comments