1- import { expect , test } from 'vitest' ;
2- import { isAllRepoReindexingRequired , isRepoReindexingRequired } from './main' ;
3- import { Repository , Settings } from './types' ;
1+ import { expect , test , vi } from 'vitest' ;
2+ import { deleteStaleRepository , isAllRepoReindexingRequired , isRepoReindexingRequired } from './main' ;
3+ import { AppContext , GitRepository , LocalRepository , Repository , Settings } from './types' ;
4+ import { DEFAULT_DB_DATA } from './db' ;
5+ import { createMockDB } from './db.test' ;
6+ import { rm } from 'fs/promises' ;
7+ import path from 'path' ;
8+ import { glob } from 'glob' ;
9+
10+ vi . mock ( 'fs/promises' , ( ) => ( {
11+ rm : vi . fn ( ) ,
12+ } ) ) ;
13+
14+ vi . mock ( 'glob' , ( ) => ( {
15+ glob : vi . fn ( ) . mockReturnValue ( [ 'fake_index.zoekt' ] ) ,
16+ } ) ) ;
17+
18+ const createMockContext = ( rootPath : string = '/app' ) => {
19+ return {
20+ configPath : path . join ( rootPath , 'config.json' ) ,
21+ cachePath : path . join ( rootPath , '.sourcebot' ) ,
22+ indexPath : path . join ( rootPath , '.sourcebot/index' ) ,
23+ reposPath : path . join ( rootPath , '.sourcebot/repos' ) ,
24+ } satisfies AppContext ;
25+ }
26+
427
528test ( 'isRepoReindexingRequired should return false when no changes are made' , ( ) => {
629 const previous : Repository = {
@@ -80,6 +103,7 @@ test('isRepoReindexingRequired should return true when local excludedPaths chang
80103test ( 'isAllRepoReindexingRequired should return false when fileLimitSize has not changed' , ( ) => {
81104 const previous : Settings = {
82105 maxFileSize : 1000 ,
106+ autoDeleteStaleRepos : true ,
83107 }
84108 const current : Settings = {
85109 ...previous ,
@@ -90,10 +114,89 @@ test('isAllRepoReindexingRequired should return false when fileLimitSize has not
90114test ( 'isAllRepoReindexingRequired should return true when fileLimitSize has changed' , ( ) => {
91115 const previous : Settings = {
92116 maxFileSize : 1000 ,
117+ autoDeleteStaleRepos : true ,
93118 }
94119 const current : Settings = {
95120 ...previous ,
96121 maxFileSize : 2000 ,
97122 }
98123 expect ( isAllRepoReindexingRequired ( previous , current ) ) . toBe ( true ) ;
124+ } ) ;
125+
126+ test ( 'isAllRepoReindexingRequired should return false when autoDeleteStaleRepos has changed' , ( ) => {
127+ const previous : Settings = {
128+ maxFileSize : 1000 ,
129+ autoDeleteStaleRepos : true ,
130+ }
131+ const current : Settings = {
132+ ...previous ,
133+ autoDeleteStaleRepos : false ,
134+ }
135+ expect ( isAllRepoReindexingRequired ( previous , current ) ) . toBe ( false ) ;
136+ } ) ;
137+
138+ test ( 'deleteStaleRepository can delete a git repository' , async ( ) => {
139+ const ctx = createMockContext ( ) ;
140+
141+ const repo : GitRepository = {
142+ id : 'github.com/sourcebot-dev/sourcebot' ,
143+ vcs : 'git' ,
144+ name : 'sourcebot' ,
145+ cloneUrl : 'https://github.com/sourcebot-dev/sourcebot' ,
146+ path : `${ ctx . reposPath } /github.com/sourcebot-dev/sourcebot` ,
147+ branches : [ 'main' ] ,
148+ tags : [ '' ] ,
149+ isStale : true ,
150+ }
151+
152+ const db = createMockDB ( {
153+ ...DEFAULT_DB_DATA ,
154+ repos : {
155+ 'github.com/sourcebot-dev/sourcebot' : repo ,
156+ }
157+ } ) ;
158+
159+
160+ await deleteStaleRepository ( repo , db , ctx ) ;
161+
162+ expect ( db . data . repos [ 'github.com/sourcebot-dev/sourcebot' ] ) . toBeUndefined ( ) ; ;
163+ expect ( rm ) . toHaveBeenCalledWith ( `${ ctx . reposPath } /github.com/sourcebot-dev/sourcebot` , {
164+ recursive : true ,
165+ } ) ;
166+ expect ( glob ) . toHaveBeenCalledWith ( `github.com%2Fsourcebot-dev%2Fsourcebot*.zoekt` , {
167+ cwd : ctx . indexPath ,
168+ absolute : true
169+ } ) ;
170+ expect ( rm ) . toHaveBeenCalledWith ( `fake_index.zoekt` ) ;
171+ } ) ;
172+
173+ test ( 'deleteStaleRepository can delete a local repository' , async ( ) => {
174+ const ctx = createMockContext ( ) ;
175+
176+ const repo : LocalRepository = {
177+ vcs : 'local' ,
178+ name : 'UnrealEngine' ,
179+ id : '/path/to/UnrealEngine' ,
180+ path : '/path/to/UnrealEngine' ,
181+ watch : false ,
182+ excludedPaths : [ ] ,
183+ isStale : true ,
184+ }
185+
186+ const db = createMockDB ( {
187+ ...DEFAULT_DB_DATA ,
188+ repos : {
189+ '/path/to/UnrealEngine' : repo ,
190+ }
191+ } ) ;
192+
193+ await deleteStaleRepository ( repo , db , ctx ) ;
194+
195+ expect ( db . data . repos [ '/path/to/UnrealEngine' ] ) . toBeUndefined ( ) ;
196+ expect ( rm ) . not . toHaveBeenCalledWith ( '/path/to/UnrealEngine' ) ;
197+ expect ( glob ) . toHaveBeenCalledWith ( `UnrealEngine*.zoekt` , {
198+ cwd : ctx . indexPath ,
199+ absolute : true
200+ } ) ;
201+ expect ( rm ) . toHaveBeenCalledWith ( 'fake_index.zoekt' ) ;
99202} ) ;
0 commit comments