11import type {
22 OpenClawPluginApi ,
3- OpenClawPluginService ,
4- OpenClawPluginServiceContext ,
3+ PluginRuntimeLifecycleRegistration ,
54} from "openclaw/plugin-sdk/plugin-entry" ;
5+ import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api" ;
6+ import { createPluginRegistryFixture } from "openclaw/plugin-sdk/plugin-test-contracts" ;
7+ import {
8+ createEmptyPluginRegistry ,
9+ createPluginRecord ,
10+ getActivePluginRegistry ,
11+ resetPluginRuntimeStateForTest ,
12+ setActivePluginRegistry ,
13+ } from "openclaw/plugin-sdk/plugin-test-runtime" ;
14+ import {
15+ getSandboxBackendFactory ,
16+ getSandboxBackendManager ,
17+ getSandboxBackendWorkdirResolver ,
18+ } from "openclaw/plugin-sdk/sandbox" ;
619import { afterEach , beforeEach , describe , expect , test , vi } from "vitest" ;
720
821const {
922 assertMxcReadinessMock,
1023 warnMxcHostPrepIfNeededMock,
1124 createMxcSandboxBackendFactoryMock,
12- factoryMock,
1325 mxcSandboxBackendManagerMock,
14- registerSandboxBackendMock,
1526 resolveMxcBinaryPathMock,
16- unregisterMock,
1727} = vi . hoisted ( ( ) => {
18- const factory = { id : "factory" } ;
19- const unregister = vi . fn ( ) ;
2028 return {
2129 assertMxcReadinessMock : vi . fn ( ) ,
2230 warnMxcHostPrepIfNeededMock : vi . fn ( ) ,
23- createMxcSandboxBackendFactoryMock : vi . fn ( ( ) => factory ) ,
24- factoryMock : factory ,
25- mxcSandboxBackendManagerMock : { id : "manager" } ,
26- registerSandboxBackendMock : vi . fn ( ( ) => unregister ) ,
31+ createMxcSandboxBackendFactoryMock : vi . fn ( ( ) => async ( ) => {
32+ throw new Error ( "MXC provider must not run in registration tests" ) ;
33+ } ) ,
34+ mxcSandboxBackendManagerMock : { describeRuntime : vi . fn ( ) , removeRuntime : vi . fn ( ) } ,
2735 resolveMxcBinaryPathMock : vi . fn ( ( ) => "mxc-test-binary" ) ,
28- unregisterMock : unregister ,
2936 } ;
3037} ) ;
3138
32- vi . mock ( "openclaw/plugin-sdk/sandbox" , ( ) => ( {
33- registerSandboxBackend : registerSandboxBackendMock ,
34- } ) ) ;
35-
3639vi . mock ( "../src/binary-resolver.js" , ( ) => ( {
3740 resolveMxcBinaryPath : resolveMxcBinaryPathMock ,
3841} ) ) ;
@@ -54,10 +57,15 @@ import { registerMxcPlugin } from "../src/plugin.js";
5457
5558const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
5659
57- type MxcPluginApiForTest = Pick <
58- OpenClawPluginApi ,
59- "pluginConfig" | "registerService" | "registrationMode"
60- > ;
60+ function readBackend ( ) {
61+ return {
62+ factory : getSandboxBackendFactory ( "mxc" ) ,
63+ manager : getSandboxBackendManager ( "mxc" ) ,
64+ resolveWorkdir : getSandboxBackendWorkdirResolver ( "mxc" ) ,
65+ } ;
66+ }
67+
68+ const stops : Array < ( ) => Promise < void > > = [ ] ;
6169
6270const nonFullRegistrationModes = [
6371 "discovery" ,
@@ -84,26 +92,27 @@ function restoreProcessPlatformForTest(): void {
8492function createApi (
8593 pluginConfig : Record < string , unknown > | undefined = { } ,
8694 registrationMode : OpenClawPluginApi [ "registrationMode" ] = "full" ,
87- ) : {
88- api : OpenClawPluginApi ;
89- registerService : ReturnType < typeof vi . fn > ;
90- services : OpenClawPluginService [ ] ;
91- } {
92- const services : OpenClawPluginService [ ] = [ ] ;
93- const registerService = vi . fn ( ( service : OpenClawPluginService ) : void => {
94- services . push ( service ) ;
95- } ) ;
96- const api = {
95+ ) {
96+ const lifecycles : PluginRuntimeLifecycleRegistration [ ] = [ ] ;
97+ const registerService = vi . fn ( ) ;
98+ const api = createTestPluginApi ( {
99+ id : "mxc" ,
97100 pluginConfig,
98101 registrationMode,
99102 registerService,
100- } satisfies MxcPluginApiForTest ;
101-
102- return {
103- api : api as unknown as OpenClawPluginApi ,
104- registerService,
105- services,
103+ registerRuntimeLifecycle : ( lifecycle ) => lifecycles . push ( lifecycle ) ,
104+ } ) ;
105+ const cleanup = async (
106+ context : Parameters < NonNullable < PluginRuntimeLifecycleRegistration [ "cleanup" ] > > [ 0 ] ,
107+ ) => {
108+ for ( const lifecycle of lifecycles . toReversed ( ) ) {
109+ await lifecycle . cleanup ?.( context ) ;
110+ }
106111 } ;
112+ const stop = ( ) => cleanup ( { reason : "disable" } ) ;
113+ stops . push ( stop ) ;
114+
115+ return { api, registerService, lifecycles, cleanup, stop } ;
107116}
108117
109118describe ( "registerMxcPlugin" , ( ) => {
@@ -113,22 +122,24 @@ describe("registerMxcPlugin", () => {
113122 assertMxcReadinessMock . mockClear ( ) ;
114123 warnMxcHostPrepIfNeededMock . mockClear ( ) ;
115124 createMxcSandboxBackendFactoryMock . mockClear ( ) ;
116- registerSandboxBackendMock . mockClear ( ) ;
117125 resolveMxcBinaryPathMock . mockReset ( ) ;
118126 resolveMxcBinaryPathMock . mockReturnValue ( "mxc-test-binary" ) ;
119- unregisterMock . mockClear ( ) ;
120127 setProcessPlatformForTest ( "win32" ) ;
121128 warnSpy = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => undefined ) ;
122129 } ) ;
123130
124- afterEach ( ( ) => {
131+ afterEach ( async ( ) => {
132+ for ( const stop of stops . splice ( 0 ) . toReversed ( ) ) {
133+ await stop ( ) ;
134+ }
125135 warnSpy . mockRestore ( ) ;
126136 restoreProcessPlatformForTest ( ) ;
127137 } ) ;
128138
129139 test ( "warns and stays dormant on non-Windows platforms" , ( ) => {
130140 setProcessPlatformForTest ( "darwin" ) ;
131- const { api, registerService } = createApi ( ) ;
141+ const original = readBackend ( ) ;
142+ const { api, registerService, lifecycles } = createApi ( ) ;
132143
133144 registerMxcPlugin ( api ) ;
134145
@@ -137,14 +148,19 @@ describe("registerMxcPlugin", () => {
137148 ) ;
138149 expect ( resolveMxcBinaryPathMock ) . not . toHaveBeenCalled ( ) ;
139150 expect ( assertMxcReadinessMock ) . not . toHaveBeenCalled ( ) ;
140- expect ( registerSandboxBackendMock ) . not . toHaveBeenCalled ( ) ;
151+ expect ( readBackend ( ) ) . toEqual ( original ) ;
152+ expect ( lifecycles ) . toEqual ( [ ] ) ;
141153 expect ( registerService ) . not . toHaveBeenCalled ( ) ;
142154 } ) ;
143155
144156 test . each ( nonFullRegistrationModes ) (
145157 "does not register runtime hooks during %s registration" ,
146158 ( registrationMode ) => {
147- const { api, registerService } = createApi ( { timeoutSeconds : 60 } , registrationMode ) ;
159+ const original = readBackend ( ) ;
160+ const { api, registerService, lifecycles } = createApi (
161+ { timeoutSeconds : 60 } ,
162+ registrationMode ,
163+ ) ;
148164
149165 registerMxcPlugin ( api ) ;
150166
@@ -153,47 +169,123 @@ describe("registerMxcPlugin", () => {
153169 expect ( assertMxcReadinessMock ) . not . toHaveBeenCalled ( ) ;
154170 expect ( warnMxcHostPrepIfNeededMock ) . not . toHaveBeenCalled ( ) ;
155171 expect ( createMxcSandboxBackendFactoryMock ) . not . toHaveBeenCalled ( ) ;
156- expect ( registerSandboxBackendMock ) . not . toHaveBeenCalled ( ) ;
172+ expect ( readBackend ( ) ) . toEqual ( original ) ;
173+ expect ( lifecycles ) . toEqual ( [ ] ) ;
157174 expect ( registerService ) . not . toHaveBeenCalled ( ) ;
158175 } ,
159176 ) ;
160177
161- test ( "registers the sandbox backend on Windows when feature probes pass" , ( ) => {
162- const { api, services } = createApi ( { timeoutSeconds : 60 } ) ;
178+ test . each ( [ "disable" , "restart" ] as const ) (
179+ "registers eagerly on Windows and restores hooks on global %s" ,
180+ async ( reason ) => {
181+ const original = readBackend ( ) ;
182+ const { api, cleanup, stop } = createApi ( { timeoutSeconds : 60 } ) ;
163183
164- registerMxcPlugin ( api ) ;
184+ registerMxcPlugin ( api ) ;
165185
166- expect ( resolveMxcBinaryPathMock ) . toHaveBeenCalledWith ( undefined ) ;
167- expect ( assertMxcReadinessMock ) . toHaveBeenCalledWith ( ) ;
168- expect ( warnMxcHostPrepIfNeededMock ) . toHaveBeenCalledWith ( ) ;
169- expect ( createMxcSandboxBackendFactoryMock ) . toHaveBeenCalledWith (
170- expect . objectContaining ( {
171- timeoutSeconds : 60 ,
172- } ) ,
173- ) ;
174- expect ( registerSandboxBackendMock ) . toHaveBeenCalledWith ( "mxc" , {
175- factory : factoryMock ,
176- manager : mxcSandboxBackendManagerMock ,
177- } ) ;
178- expect ( services ) . toHaveLength ( 1 ) ;
186+ expect ( resolveMxcBinaryPathMock ) . toHaveBeenCalledWith ( undefined ) ;
187+ expect ( assertMxcReadinessMock ) . toHaveBeenCalledWith ( ) ;
188+ expect ( warnMxcHostPrepIfNeededMock ) . toHaveBeenCalledWith ( ) ;
189+ expect ( createMxcSandboxBackendFactoryMock ) . toHaveBeenCalledWith (
190+ expect . objectContaining ( {
191+ timeoutSeconds : 60 ,
192+ } ) ,
193+ ) ;
194+ expect ( readBackend ( ) ) . toEqual ( {
195+ factory : expect . any ( Function ) ,
196+ manager : mxcSandboxBackendManagerMock ,
197+ resolveWorkdir : null ,
198+ } ) ;
199+ await cleanup ( { reason } ) ;
200+ expect ( readBackend ( ) ) . toEqual ( original ) ;
201+ await stop ( ) ;
202+ expect ( readBackend ( ) ) . toEqual ( original ) ;
203+ } ,
204+ ) ;
205+
206+ test . each ( [ "disable" , "restart" , "reset" , "delete" ] as const ) (
207+ "preserves backend hooks during scoped %s cleanup" ,
208+ async ( reason ) => {
209+ const generation = createApi ( ) ;
210+ registerMxcPlugin ( generation . api ) ;
211+ const backend = readBackend ( ) ;
212+ for ( const scope of [
213+ { sessionKey : "agent:other:main" } ,
214+ { runId : "other-run" } ,
215+ { sessionKey : "" } ,
216+ { runId : "" } ,
217+ ] ) {
218+ await generation . cleanup ( { reason, ...scope } ) ;
219+ expect ( readBackend ( ) ) . toEqual ( backend ) ;
220+ }
221+ if ( reason === "reset" || reason === "delete" ) {
222+ await generation . cleanup ( { reason } ) ;
223+ expect ( readBackend ( ) ) . toEqual ( backend ) ;
224+ }
225+ } ,
226+ ) ;
227+
228+ test . each ( [ "older-first" , "newer-first" ] as const ) (
229+ "preserves live registrations when generations retire %s" ,
230+ async ( order ) => {
231+ const original = readBackend ( ) ;
232+ const older = createApi ( ) ;
233+ registerMxcPlugin ( older . api ) ;
234+ const olderBackend = readBackend ( ) ;
235+ const newer = createApi ( ) ;
236+ registerMxcPlugin ( newer . api ) ;
237+ const newerBackend = readBackend ( ) ;
238+ expect ( newerBackend . factory ) . not . toBe ( olderBackend . factory ) ;
239+ const first = order === "older-first" ? older : newer ;
240+ const last = order === "older-first" ? newer : older ;
241+ await first . stop ( ) ;
242+ expect ( readBackend ( ) ) . toEqual ( order === "older-first" ? newerBackend : olderBackend ) ;
243+ await last . stop ( ) ;
244+ expect ( readBackend ( ) ) . toEqual ( original ) ;
245+ await first . stop ( ) ;
246+ expect ( readBackend ( ) ) . toEqual ( original ) ;
247+ } ,
248+ ) ;
179249
180- void services [ 0 ] ?. stop ?.( { } as OpenClawPluginServiceContext ) ;
181- expect ( unregisterMock ) . toHaveBeenCalledTimes ( 1 ) ;
250+ test ( "retires a registered backend even when no plugin services ever start" , async ( ) => {
251+ const original = readBackend ( ) ;
252+ const originalRegistry = getActivePluginRegistry ( ) ;
253+ const { registry } = createPluginRegistryFixture ( ) ;
254+ const record = createPluginRecord ( { id : "mxc" } ) ;
255+ registry . registry . plugins . push ( record ) ;
256+ registerMxcPlugin ( registry . createApi ( record , { config : { } , pluginConfig : { } } ) ) ;
257+ try {
258+ expect ( readBackend ( ) . factory ) . toEqual ( expect . any ( Function ) ) ;
259+ setActivePluginRegistry ( registry . registry ) ;
260+ setActivePluginRegistry ( createEmptyPluginRegistry ( ) ) ;
261+ await expect . poll ( readBackend ) . toEqual ( original ) ;
262+ } finally {
263+ for ( const { lifecycle } of registry . registry . runtimeLifecycles . toReversed ( ) ) {
264+ await lifecycle . cleanup ?.( { reason : "disable" } ) ;
265+ }
266+ if ( originalRegistry ) {
267+ setActivePluginRegistry ( originalRegistry ) ;
268+ } else {
269+ resetPluginRuntimeStateForTest ( ) ;
270+ }
271+ }
182272 } ) ;
183273
184274 test ( "keeps the existing binary-resolution failure path after host support passes" , ( ) => {
185275 resolveMxcBinaryPathMock . mockImplementation ( ( ) => {
186276 throw new Error ( "missing binary" ) ;
187277 } ) ;
188- const { api, registerService } = createApi ( ) ;
278+ const original = readBackend ( ) ;
279+ const { api, registerService, lifecycles } = createApi ( ) ;
189280
190281 expect ( ( ) => registerMxcPlugin ( api ) ) . toThrow (
191282 "[mxc] MXC sandbox backend cannot load: missing binary. Install @microsoft/mxc-sdk or set mxcBinaryPath." ,
192283 ) ;
193284
194285 expect ( warnSpy ) . not . toHaveBeenCalled ( ) ;
195286 expect ( assertMxcReadinessMock ) . not . toHaveBeenCalled ( ) ;
196- expect ( registerSandboxBackendMock ) . not . toHaveBeenCalled ( ) ;
287+ expect ( readBackend ( ) ) . toEqual ( original ) ;
288+ expect ( lifecycles ) . toEqual ( [ ] ) ;
197289 expect ( registerService ) . not . toHaveBeenCalled ( ) ;
198290 } ) ;
199291} ) ;
0 commit comments