@@ -123,4 +123,113 @@ describe("TerminalRegistry", () => {
123123 }
124124 } )
125125 } )
126+
127+ describe ( "maxTerminalPoolSize" , ( ) => {
128+ it ( "has a default pool size of 5" , ( ) => {
129+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 5 )
130+ } )
131+
132+ it ( "allows setting pool size within bounds" , ( ) => {
133+ const original = TerminalRegistry . getMaxTerminalPoolSize ( )
134+ try {
135+ TerminalRegistry . setMaxTerminalPoolSize ( 10 )
136+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 10 )
137+
138+ TerminalRegistry . setMaxTerminalPoolSize ( 1 )
139+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 1 )
140+
141+ TerminalRegistry . setMaxTerminalPoolSize ( 20 )
142+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 20 )
143+ } finally {
144+ TerminalRegistry . setMaxTerminalPoolSize ( original )
145+ }
146+ } )
147+
148+ it ( "clamps pool size to minimum of 1" , ( ) => {
149+ const original = TerminalRegistry . getMaxTerminalPoolSize ( )
150+ try {
151+ TerminalRegistry . setMaxTerminalPoolSize ( 0 )
152+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 1 )
153+
154+ TerminalRegistry . setMaxTerminalPoolSize ( - 5 )
155+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 1 )
156+ } finally {
157+ TerminalRegistry . setMaxTerminalPoolSize ( original )
158+ }
159+ } )
160+
161+ it ( "clamps pool size to maximum of 20" , ( ) => {
162+ const original = TerminalRegistry . getMaxTerminalPoolSize ( )
163+ try {
164+ TerminalRegistry . setMaxTerminalPoolSize ( 25 )
165+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 20 )
166+
167+ TerminalRegistry . setMaxTerminalPoolSize ( 100 )
168+ expect ( TerminalRegistry . getMaxTerminalPoolSize ( ) ) . toBe ( 20 )
169+ } finally {
170+ TerminalRegistry . setMaxTerminalPoolSize ( original )
171+ }
172+ } )
173+
174+ it ( "disposes oldest idle terminal when pool is at capacity" , ( ) => {
175+ const original = TerminalRegistry . getMaxTerminalPoolSize ( )
176+ try {
177+ TerminalRegistry . setMaxTerminalPoolSize ( 2 )
178+
179+ // Create 2 terminals to reach the limit
180+ const t1 = TerminalRegistry . createTerminal ( "/test/path1" , "vscode" )
181+ const t2 = TerminalRegistry . createTerminal ( "/test/path2" , "vscode" )
182+
183+ // The first terminal should have been disposed to make room for the second
184+ // since pool size is 2, creating the 2nd should be fine
185+ // but creating a 3rd should dispose the first idle one
186+ const t3 = TerminalRegistry . createTerminal ( "/test/path3" , "vscode" )
187+
188+ // t1's underlying vscode terminal should have been disposed
189+ expect ( ( t1 as Terminal ) . terminal . dispose ) . toHaveBeenCalled ( )
190+ } finally {
191+ TerminalRegistry . setMaxTerminalPoolSize ( original )
192+ }
193+ } )
194+ } )
195+
196+ describe ( "releaseTerminalsForTask" , ( ) => {
197+ it ( "disposes idle terminals when releasing a task" , ( ) => {
198+ const t1 = TerminalRegistry . createTerminal ( "/test/path" , "vscode" )
199+ t1 . taskId = "task-1"
200+
201+ TerminalRegistry . releaseTerminalsForTask ( "task-1" )
202+
203+ // The terminal should have been disposed since it was idle
204+ expect ( ( t1 as Terminal ) . terminal . dispose ) . toHaveBeenCalled ( )
205+ } )
206+
207+ it ( "does not dispose busy terminals when releasing a task" , ( ) => {
208+ const t1 = TerminalRegistry . createTerminal ( "/test/path" , "vscode" )
209+ t1 . taskId = "task-2"
210+ t1 . busy = true
211+
212+ TerminalRegistry . releaseTerminalsForTask ( "task-2" )
213+
214+ // The terminal should NOT have been disposed since it was busy
215+ expect ( ( t1 as Terminal ) . terminal . dispose ) . not . toHaveBeenCalled ( )
216+ // But its taskId should have been cleared
217+ expect ( t1 . taskId ) . toBeUndefined ( )
218+ } )
219+
220+ it ( "does not dispose terminals belonging to other tasks" , ( ) => {
221+ const t1 = TerminalRegistry . createTerminal ( "/test/path" , "vscode" )
222+ t1 . taskId = "task-3"
223+
224+ const t2 = TerminalRegistry . createTerminal ( "/test/path" , "vscode" )
225+ t2 . taskId = "task-4"
226+
227+ TerminalRegistry . releaseTerminalsForTask ( "task-3" )
228+
229+ // t1 should be disposed (idle, belongs to task-3)
230+ expect ( ( t1 as Terminal ) . terminal . dispose ) . toHaveBeenCalled ( )
231+ // t2 should NOT be disposed (belongs to task-4)
232+ expect ( ( t2 as Terminal ) . terminal . dispose ) . not . toHaveBeenCalled ( )
233+ } )
234+ } )
126235} )
0 commit comments