11// ACP Core tests cover session behavior.
2- import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
2+ import { beforeEach , describe , expect , it } from "vitest" ;
33import { createInMemorySessionStore } from "./session.js" ;
44
55describe ( "acp session manager" , ( ) => {
@@ -15,10 +15,6 @@ describe("acp session manager", () => {
1515 store = createInMemorySessionStore ( { now } ) ;
1616 } ) ;
1717
18- afterEach ( ( ) => {
19- store . clearAllSessionsForTest ( ) ;
20- } ) ;
21-
2218 it ( "tracks active runs and clears on cancel" , ( ) => {
2319 const session = store . createSession ( {
2420 sessionKey : "acp:test" ,
@@ -95,24 +91,20 @@ describe("acp session manager", () => {
9591 idleTtlMs : Number . NaN ,
9692 now,
9793 } ) ;
98- try {
99- boundedStore . createSession ( {
100- sessionId : "first" ,
101- sessionKey : "acp:first" ,
102- cwd : "/tmp" ,
103- } ) ;
104- advance ( 1 ) ;
105- boundedStore . createSession ( {
106- sessionId : "second" ,
107- sessionKey : "acp:second" ,
108- cwd : "/tmp" ,
109- } ) ;
94+ boundedStore . createSession ( {
95+ sessionId : "first" ,
96+ sessionKey : "acp:first" ,
97+ cwd : "/tmp" ,
98+ } ) ;
99+ advance ( 1 ) ;
100+ boundedStore . createSession ( {
101+ sessionId : "second" ,
102+ sessionKey : "acp:second" ,
103+ cwd : "/tmp" ,
104+ } ) ;
110105
111- expect ( boundedStore . hasSession ( "first" ) ) . toBe ( true ) ;
112- expect ( boundedStore . hasSession ( "second" ) ) . toBe ( true ) ;
113- } finally {
114- boundedStore . clearAllSessionsForTest ( ) ;
115- }
106+ expect ( boundedStore . hasSession ( "first" ) ) . toBe ( true ) ;
107+ expect ( boundedStore . hasSession ( "second" ) ) . toBe ( true ) ;
116108 } ) ;
117109
118110 it ( "falls back for non-finite max session options" , ( ) => {
@@ -121,26 +113,22 @@ describe("acp session manager", () => {
121113 idleTtlMs : 24 * 60 * 60 * 1_000 ,
122114 now,
123115 } ) ;
124- try {
125- for ( let index = 0 ; index < 5_000 ; index += 1 ) {
126- const session = boundedStore . createSession ( {
127- sessionId : `session-${ index } ` ,
128- sessionKey : `acp:${ index } ` ,
129- cwd : "/tmp" ,
130- } ) ;
131- boundedStore . setActiveRun ( session . sessionId , `run-${ index } ` , new AbortController ( ) ) ;
132- }
133-
134- expect ( ( ) =>
135- boundedStore . createSession ( {
136- sessionId : "overflow" ,
137- sessionKey : "acp:overflow" ,
138- cwd : "/tmp" ,
139- } ) ,
140- ) . toThrow ( / s e s s i o n l i m i t r e a c h e d / i) ;
141- } finally {
142- boundedStore . clearAllSessionsForTest ( ) ;
116+ for ( let index = 0 ; index < 5_000 ; index += 1 ) {
117+ const session = boundedStore . createSession ( {
118+ sessionId : `session-${ index } ` ,
119+ sessionKey : `acp:${ index } ` ,
120+ cwd : "/tmp" ,
121+ } ) ;
122+ boundedStore . setActiveRun ( session . sessionId , `run-${ index } ` , new AbortController ( ) ) ;
143123 }
124+
125+ expect ( ( ) =>
126+ boundedStore . createSession ( {
127+ sessionId : "overflow" ,
128+ sessionKey : "acp:overflow" ,
129+ cwd : "/tmp" ,
130+ } ) ,
131+ ) . toThrow ( / s e s s i o n l i m i t r e a c h e d / i) ;
144132 } ) ;
145133
146134 it ( "reaps idle sessions before enforcing the max session cap" , ( ) => {
@@ -149,25 +137,21 @@ describe("acp session manager", () => {
149137 idleTtlMs : 1_000 ,
150138 now,
151139 } ) ;
152- try {
153- boundedStore . createSession ( {
154- sessionId : "old" ,
155- sessionKey : "acp:old" ,
156- cwd : "/tmp" ,
157- } ) ;
158- advance ( 2_000 ) ;
159- const fresh = boundedStore . createSession ( {
160- sessionId : "fresh" ,
161- sessionKey : "acp:fresh" ,
162- cwd : "/tmp" ,
163- } ) ;
140+ boundedStore . createSession ( {
141+ sessionId : "old" ,
142+ sessionKey : "acp:old" ,
143+ cwd : "/tmp" ,
144+ } ) ;
145+ advance ( 2_000 ) ;
146+ const fresh = boundedStore . createSession ( {
147+ sessionId : "fresh" ,
148+ sessionKey : "acp:fresh" ,
149+ cwd : "/tmp" ,
150+ } ) ;
164151
165- expect ( fresh . sessionId ) . toBe ( "fresh" ) ;
166- expect ( boundedStore . getSession ( "old" ) ) . toBeUndefined ( ) ;
167- expect ( boundedStore . hasSession ( "old" ) ) . toBe ( false ) ;
168- } finally {
169- boundedStore . clearAllSessionsForTest ( ) ;
170- }
152+ expect ( fresh . sessionId ) . toBe ( "fresh" ) ;
153+ expect ( boundedStore . getSession ( "old" ) ) . toBeUndefined ( ) ;
154+ expect ( boundedStore . hasSession ( "old" ) ) . toBe ( false ) ;
171155 } ) ;
172156
173157 it ( "uses soft-cap eviction for the oldest idle session when full" , ( ) => {
@@ -176,35 +160,31 @@ describe("acp session manager", () => {
176160 idleTtlMs : 24 * 60 * 60 * 1_000 ,
177161 now,
178162 } ) ;
179- try {
180- const first = boundedStore . createSession ( {
181- sessionId : "first" ,
182- sessionKey : "acp:first" ,
183- cwd : "/tmp" ,
184- } ) ;
185- advance ( 100 ) ;
186- const second = boundedStore . createSession ( {
187- sessionId : "second" ,
188- sessionKey : "acp:second" ,
189- cwd : "/tmp" ,
190- } ) ;
191- const controller = new AbortController ( ) ;
192- boundedStore . setActiveRun ( second . sessionId , "run-2" , controller ) ;
193- advance ( 100 ) ;
163+ const first = boundedStore . createSession ( {
164+ sessionId : "first" ,
165+ sessionKey : "acp:first" ,
166+ cwd : "/tmp" ,
167+ } ) ;
168+ advance ( 100 ) ;
169+ const second = boundedStore . createSession ( {
170+ sessionId : "second" ,
171+ sessionKey : "acp:second" ,
172+ cwd : "/tmp" ,
173+ } ) ;
174+ const controller = new AbortController ( ) ;
175+ boundedStore . setActiveRun ( second . sessionId , "run-2" , controller ) ;
176+ advance ( 100 ) ;
194177
195- const third = boundedStore . createSession ( {
196- sessionId : "third" ,
197- sessionKey : "acp:third" ,
198- cwd : "/tmp" ,
199- } ) ;
178+ const third = boundedStore . createSession ( {
179+ sessionId : "third" ,
180+ sessionKey : "acp:third" ,
181+ cwd : "/tmp" ,
182+ } ) ;
200183
201- expect ( third . sessionId ) . toBe ( "third" ) ;
202- expect ( boundedStore . getSession ( first . sessionId ) ) . toBeUndefined ( ) ;
203- const retainedSession = boundedStore . getSession ( second . sessionId ) ;
204- expect ( retainedSession ?. sessionId ) . toBe ( "second" ) ;
205- } finally {
206- boundedStore . clearAllSessionsForTest ( ) ;
207- }
184+ expect ( third . sessionId ) . toBe ( "third" ) ;
185+ expect ( boundedStore . getSession ( first . sessionId ) ) . toBeUndefined ( ) ;
186+ const retainedSession = boundedStore . getSession ( second . sessionId ) ;
187+ expect ( retainedSession ?. sessionId ) . toBe ( "second" ) ;
208188 } ) ;
209189
210190 it ( "rejects when full and no session is evictable" , ( ) => {
@@ -213,23 +193,19 @@ describe("acp session manager", () => {
213193 idleTtlMs : 24 * 60 * 60 * 1_000 ,
214194 now,
215195 } ) ;
216- try {
217- const only = boundedStore . createSession ( {
218- sessionId : "only" ,
219- sessionKey : "acp:only" ,
196+ const only = boundedStore . createSession ( {
197+ sessionId : "only" ,
198+ sessionKey : "acp:only" ,
199+ cwd : "/tmp" ,
200+ } ) ;
201+ boundedStore . setActiveRun ( only . sessionId , "run-only" , new AbortController ( ) ) ;
202+
203+ expect ( ( ) =>
204+ boundedStore . createSession ( {
205+ sessionId : "next" ,
206+ sessionKey : "acp:next" ,
220207 cwd : "/tmp" ,
221- } ) ;
222- boundedStore . setActiveRun ( only . sessionId , "run-only" , new AbortController ( ) ) ;
223-
224- expect ( ( ) =>
225- boundedStore . createSession ( {
226- sessionId : "next" ,
227- sessionKey : "acp:next" ,
228- cwd : "/tmp" ,
229- } ) ,
230- ) . toThrow ( / s e s s i o n l i m i t r e a c h e d / i) ;
231- } finally {
232- boundedStore . clearAllSessionsForTest ( ) ;
233- }
208+ } ) ,
209+ ) . toThrow ( / s e s s i o n l i m i t r e a c h e d / i) ;
234210 } ) ;
235211} ) ;
0 commit comments