@@ -6,16 +6,15 @@ import {
66 expectOneOffSharedMatrixClient ,
77 matrixClientResolverMocks ,
88 primeMatrixClientResolverMocks ,
9+ setAcquiredMatrixClient ,
910} from "../client-resolver.test-helpers.js" ;
1011
1112const resolveMatrixRoomIdMock = vi . fn ( ) ;
1213
1314const {
14- loadConfigMock,
1515 getMatrixRuntimeMock,
16- getActiveMatrixClientMock,
1716 acquireSharedMatrixClientMock,
18- releaseSharedClientInstanceMock ,
17+ sharedLeaseReleaseMock ,
1918 isBunRuntimeMock,
2019 resolveMatrixAuthContextMock,
2120} = matrixClientResolverMocks ;
@@ -26,20 +25,12 @@ vi.mock("../../runtime.js", () => ({
2625 getMatrixRuntime : ( ) => getMatrixRuntimeMock ( ) ,
2726} ) ) ;
2827
29- vi . mock ( "../active-client.js" , ( ) => ( {
30- getActiveMatrixClient : getActiveMatrixClientMock ,
31- } ) ) ;
32-
3328vi . mock ( "../client.js" , ( ) => ( {
3429 acquireSharedMatrixClient : acquireSharedMatrixClientMock ,
3530 isBunRuntime : ( ) => isBunRuntimeMock ( ) ,
3631 resolveMatrixAuthContext : resolveMatrixAuthContextMock ,
3732} ) ) ;
3833
39- vi . mock ( "../client/shared.js" , ( ) => ( {
40- releaseSharedClientInstance : ( ...args : unknown [ ] ) => releaseSharedClientInstanceMock ( ...args ) ,
41- } ) ) ;
42-
4334vi . mock ( "../send.js" , ( ) => ( {
4435 resolveMatrixRoomId : ( ...args : unknown [ ] ) => resolveMatrixRoomIdMock ( ...args ) ,
4536} ) ) ;
@@ -58,16 +49,14 @@ describe("action client helpers", () => {
5849 primeMatrixClientResolverMocks ( ) ;
5950 resolveMatrixRoomIdMock
6051 . mockReset ( )
61- . mockImplementation ( async ( clientForTest , roomId : string ) => roomId ) ;
52+ . mockImplementation ( async ( _client , roomId : string ) => roomId ) ;
6253 } ) ;
6354
6455 afterEach ( ( ) => {
6556 vi . unstubAllEnvs ( ) ;
6657 } ) ;
6758
68- it ( "stops one-off shared clients when no active monitor client is registered" , async ( ) => {
69- vi . stubEnv ( "OPENCLAW_GATEWAY_PORT" , "18799" ) ;
70-
59+ it ( "borrows and releases one-off action clients" , async ( ) => {
7160 const result = await withResolvedActionClient (
7261 { cfg : TEST_CFG , accountId : "default" } ,
7362 async ( ) => "ok" ,
@@ -77,90 +66,51 @@ describe("action client helpers", () => {
7766 expect ( result ) . toBe ( "ok" ) ;
7867 } ) ;
7968
80- it ( "skips one-off room preparation when readiness is disabled" , async ( ) => {
69+ it ( "forwards the transient retirement signal to action work" , async ( ) => {
70+ const sharedClient = createMockMatrixClient ( ) ;
71+ const lease = setAcquiredMatrixClient ( sharedClient ) ;
72+
8173 await withResolvedActionClient (
82- { cfg : TEST_CFG , accountId : "default" , readiness : "none" } ,
83- async ( ) => { } ,
74+ { cfg : TEST_CFG , accountId : "default" } ,
75+ async ( _client , abortSignal ) => {
76+ expect ( abortSignal ) . toBe ( lease . abortSignal ) ;
77+ } ,
8478 ) ;
85-
86- const sharedClient = await acquireSharedMatrixClientMock . mock . results [ 0 ] ?. value ;
87- expect ( sharedClient . prepareForOneOff ) . not . toHaveBeenCalled ( ) ;
88- expect ( sharedClient . start ) . not . toHaveBeenCalled ( ) ;
89- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "stop" ) ;
90- } ) ;
91-
92- it ( "starts one-off clients when started readiness is required" , async ( ) => {
93- await withStartedActionClient ( { cfg : TEST_CFG , accountId : "default" } , async ( ) => { } ) ;
94-
95- const sharedClient = await acquireSharedMatrixClientMock . mock . results [ 0 ] ?. value ;
96- expect ( sharedClient . start ) . toHaveBeenCalledTimes ( 1 ) ;
97- expect ( sharedClient . prepareForOneOff ) . not . toHaveBeenCalled ( ) ;
98- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "persist" ) ;
9979 } ) ;
10080
101- it ( "reuses active monitor client when available" , async ( ) => {
102- const activeClient = createMockMatrixClient ( ) ;
103- getActiveMatrixClientMock . mockReturnValue ( activeClient ) ;
104-
105- const result = await withResolvedActionClient (
106- { cfg : TEST_CFG , accountId : "default" } ,
107- async ( client ) => {
108- expect ( client ) . toBe ( activeClient ) ;
109- return "ok" ;
110- } ,
81+ it ( "skips preparation when readiness is disabled" , async ( ) => {
82+ await withResolvedActionClient (
83+ { cfg : TEST_CFG , accountId : "default" , readiness : "none" } ,
84+ async ( ) => { } ,
11185 ) ;
11286
113- expect ( result ) . toBe ( "ok" ) ;
114- expect ( acquireSharedMatrixClientMock ) . not . toHaveBeenCalled ( ) ;
115- expect ( activeClient [ "stop" ] ) . not . toHaveBeenCalled ( ) ;
87+ const lease = await acquireSharedMatrixClientMock . mock . results [ 0 ] ?. value ;
88+ expect ( lease . client . prepareForOneOff ) . not . toHaveBeenCalled ( ) ;
89+ expect ( lease . client . start ) . not . toHaveBeenCalled ( ) ;
90+ expect ( sharedLeaseReleaseMock ) . toHaveBeenCalledWith ( { mode : "stop" } ) ;
11691 } ) ;
11792
118- it ( "starts active clients when started readiness is required" , async ( ) => {
119- const activeClient = createMockMatrixClient ( ) ;
120- getActiveMatrixClientMock . mockReturnValue ( activeClient ) ;
93+ it ( "starts through the lease and persists started action clients" , async ( ) => {
94+ await withStartedActionClient ( { cfg : TEST_CFG , accountId : "default" } , async ( ) => { } ) ;
12195
122- await withStartedActionClient ( { cfg : TEST_CFG , accountId : "default" } , async ( client ) => {
123- expect ( client ) . toBe ( activeClient ) ;
96+ await expectOneOffSharedMatrixClient ( {
97+ prepareForOneOffCalls : 0 ,
98+ startCalls : 1 ,
99+ releaseMode : "persist" ,
124100 } ) ;
125-
126- expect ( activeClient [ "start" ] ) . toHaveBeenCalledTimes ( 1 ) ;
127- expect ( activeClient [ "prepareForOneOff" ] ) . not . toHaveBeenCalled ( ) ;
128- expect ( activeClient [ "stop" ] ) . not . toHaveBeenCalled ( ) ;
129- expect ( activeClient [ "stopAndPersist" ] ) . not . toHaveBeenCalled ( ) ;
130101 } ) ;
131102
132- it ( "uses the implicit resolved account id for active client lookup and storage" , async ( ) => {
133- loadConfigMock . mockReturnValue ( {
134- channels : {
135- matrix : {
136- accounts : {
137- ops : {
138- homeserver : "https://ops.example.org" ,
139- userId : "@ops:example.org" ,
140- accessToken : "ops-token" ,
141- } ,
142- } ,
143- } ,
144- } ,
145- } ) ;
103+ it ( "uses the implicit resolved account id for shared acquisition" , async ( ) => {
146104 resolveMatrixAuthContextMock . mockReturnValue ( {
147- cfg : loadConfigMock ( ) ,
105+ cfg : TEST_CFG ,
148106 env : process . env ,
149107 accountId : "ops" ,
150- resolved : {
151- homeserver : "https://ops.example.org" ,
152- userId : "@ops:example.org" ,
153- accessToken : "ops-token" ,
154- deviceId : "OPSDEVICE" ,
155- encryption : true ,
156- } ,
108+ resolved : { } ,
157109 } ) ;
158- await withResolvedActionClient ( { cfg : loadConfigMock ( ) as never } , async ( ) => { } ) ;
159110
160- await expectOneOffSharedMatrixClient ( {
161- cfg : loadConfigMock ( ) ,
162- accountId : "ops" ,
163- } ) ;
111+ await withResolvedActionClient ( { cfg : TEST_CFG } , async ( ) => { } ) ;
112+
113+ await expectOneOffSharedMatrixClient ( { accountId : "ops" } ) ;
164114 } ) ;
165115
166116 it ( "uses explicit cfg instead of loading runtime config" , async ( ) => {
@@ -180,68 +130,62 @@ describe("action client helpers", () => {
180130 } ) ;
181131 } ) ;
182132
183- it ( "stops shared action clients after wrapped calls succeed" , async ( ) => {
184- const sharedClient = createMockMatrixClient ( ) ;
185- acquireSharedMatrixClientMock . mockResolvedValue ( sharedClient ) ;
186-
187- const result = await withResolvedActionClient (
188- { cfg : TEST_CFG , accountId : "default" } ,
189- async ( client ) => {
190- expect ( client ) . toBe ( sharedClient ) ;
191- return "ok" ;
192- } ,
193- ) ;
194-
195- expect ( result ) . toBe ( "ok" ) ;
196- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "stop" ) ;
197- } ) ;
198-
199- it ( "can discard read-only shared action clients without persisting crypto state" , async ( ) => {
133+ it ( "releases shared action clients with the requested discard mode" , async ( ) => {
200134 const sharedClient = createMockMatrixClient ( ) ;
201- acquireSharedMatrixClientMock . mockResolvedValue ( sharedClient ) ;
135+ setAcquiredMatrixClient ( sharedClient ) ;
202136
203- const result = await withResolvedActionClient (
137+ await withResolvedActionClient (
204138 { cfg : TEST_CFG , accountId : "default" } ,
205139 async ( client ) => {
206140 expect ( client ) . toBe ( sharedClient ) ;
207- return "ok" ;
208141 } ,
209142 "discard" ,
210143 ) ;
211144
212- expect ( result ) . toBe ( "ok" ) ;
213- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "discard" ) ;
145+ expect ( sharedLeaseReleaseMock ) . toHaveBeenCalledWith ( { mode : "discard" } ) ;
214146 } ) ;
215147
216- it ( "stops shared action clients when the wrapped call throws" , async ( ) => {
148+ it ( "releases shared action clients when the wrapped call throws" , async ( ) => {
217149 const sharedClient = createMockMatrixClient ( ) ;
218- acquireSharedMatrixClientMock . mockResolvedValue ( sharedClient ) ;
150+ setAcquiredMatrixClient ( sharedClient ) ;
219151
220152 await expect (
221153 withResolvedActionClient ( { cfg : TEST_CFG , accountId : "default" } , async ( ) => {
222154 throw new Error ( "boom" ) ;
223155 } ) ,
224156 ) . rejects . toThrow ( "boom" ) ;
225157
226- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "stop" ) ;
158+ expect ( sharedLeaseReleaseMock ) . toHaveBeenCalledWith ( { mode : "stop" } ) ;
159+ } ) ;
160+
161+ it ( "does not borrow an explicitly injected action client" , async ( ) => {
162+ const injected = createMockMatrixClient ( ) ;
163+
164+ await withResolvedActionClient ( { client : injected } , async ( client ) => {
165+ expect ( client ) . toBe ( injected ) ;
166+ } ) ;
167+
168+ expect ( acquireSharedMatrixClientMock ) . not . toHaveBeenCalled ( ) ;
169+ expect ( sharedLeaseReleaseMock ) . not . toHaveBeenCalled ( ) ;
227170 } ) ;
228171
229172 it ( "resolves room ids before running wrapped room actions" , async ( ) => {
230173 const sharedClient = createMockMatrixClient ( ) ;
231- acquireSharedMatrixClientMock . mockResolvedValue ( sharedClient ) ;
174+ const lease = setAcquiredMatrixClient ( sharedClient ) ;
232175 resolveMatrixRoomIdMock . mockResolvedValue ( "!room:example.org" ) ;
233176
234177 const result = await withResolvedRoomAction (
235178 "room:#ops:example.org" ,
236179 { cfg : TEST_CFG , accountId : "default" } ,
237- async ( client , resolvedRoom ) => {
180+ async ( client , resolvedRoom , abortSignal ) => {
238181 expect ( client ) . toBe ( sharedClient ) ;
182+ expect ( abortSignal ) . toBe ( lease . abortSignal ) ;
239183 return resolvedRoom ;
240184 } ,
241185 ) ;
242186
243187 expect ( resolveMatrixRoomIdMock ) . toHaveBeenCalledWith ( sharedClient , "room:#ops:example.org" ) ;
244188 expect ( result ) . toBe ( "!room:example.org" ) ;
245- expect ( releaseSharedClientInstanceMock ) . toHaveBeenCalledWith ( sharedClient , "stop" ) ;
189+ expect ( sharedLeaseReleaseMock ) . toHaveBeenCalledWith ( { mode : "stop" } ) ;
246190 } ) ;
247191} ) ;
0 commit comments