|
1 | 1 | import fs from "node:fs/promises"; |
2 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { createPluginRecord } from "../plugins/loader-records.js"; |
| 4 | +import { createEmptyPluginRegistry } from "../plugins/registry-empty.js"; |
| 5 | +import { |
| 6 | + captureActivePluginRegistrySnapshot, |
| 7 | + restoreActivePluginRegistrySnapshot, |
| 8 | + stageActivePluginRegistry, |
| 9 | +} from "../plugins/runtime.js"; |
3 | 10 | import { getActiveGatewayRootWorkCount } from "../process/gateway-work-admission.js"; |
4 | 11 | import { getActiveSecretsRuntimeConfigSnapshot } from "../secrets/runtime-state.js"; |
| 12 | +import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js"; |
5 | 13 | import { createOpenClawTestState } from "../test-utils/openclaw-test-state.js"; |
6 | 14 | import { getFreePort } from "../test-utils/ports.js"; |
7 | 15 | import { CLI_DEFAULT_OPERATOR_SCOPES } from "./method-scopes.js"; |
@@ -30,25 +38,65 @@ describe("createGatewayKernel", () => { |
30 | 38 | VITEST: "1", |
31 | 39 | }, |
32 | 40 | }); |
33 | | - const timelinePath = state.path("kernel-startup.jsonl"); |
34 | | - state.envVars.OPENCLAW_DIAGNOSTICS_TIMELINE_PATH = timelinePath; |
35 | | - const token = "gateway-kernel-no-transport-token"; |
36 | | - await state.writeConfig({ |
37 | | - gateway: { |
38 | | - auth: { mode: "token", token }, |
39 | | - controlUi: { enabled: false }, |
40 | | - port, |
41 | | - }, |
| 41 | + const originalPluginRegistry = captureActivePluginRegistrySnapshot(); |
| 42 | + const inspectAccount = vi.fn(() => ({ enabled: true, configured: true })); |
| 43 | + const capturedRegistryCleanup = vi.fn(); |
| 44 | + const ambientPlugin = createChannelTestPluginBase({ |
| 45 | + id: "telegram", |
| 46 | + config: { inspectAccount }, |
42 | 47 | }); |
43 | | - state.applyEnv(); |
44 | | - |
45 | | - const kernel = await createGatewayKernel(port, { |
46 | | - auth: { mode: "token", token }, |
47 | | - bind: "loopback", |
48 | | - controlUiEnabled: false, |
49 | | - sidecarStartup: "defer", |
| 48 | + const ambientRegistry = createTestRegistry([ |
| 49 | + { |
| 50 | + pluginId: ambientPlugin.id, |
| 51 | + plugin: ambientPlugin, |
| 52 | + source: "gateway-kernel-test", |
| 53 | + }, |
| 54 | + ]); |
| 55 | + ambientRegistry.plugins.push( |
| 56 | + createPluginRecord({ |
| 57 | + id: ambientPlugin.id, |
| 58 | + source: "gateway-kernel-test", |
| 59 | + origin: "bundled", |
| 60 | + enabled: true, |
| 61 | + configSchema: false, |
| 62 | + }), |
| 63 | + ); |
| 64 | + ambientRegistry.runtimeLifecycles.push({ |
| 65 | + pluginId: ambientPlugin.id, |
| 66 | + pluginName: ambientPlugin.meta.label, |
| 67 | + lifecycle: { |
| 68 | + id: "gateway-kernel-test-cleanup", |
| 69 | + cleanup: capturedRegistryCleanup, |
| 70 | + }, |
| 71 | + source: "gateway-kernel-test", |
50 | 72 | }); |
| 73 | + let capturedLoadedPluginRegistry: |
| 74 | + | ReturnType<typeof captureActivePluginRegistrySnapshot> |
| 75 | + | undefined; |
| 76 | + let prematureCleanupCalls: number | undefined; |
| 77 | + let kernel: Awaited<ReturnType<typeof createGatewayKernel>> | undefined; |
51 | 78 | try { |
| 79 | + stageActivePluginRegistry(ambientRegistry, null, "default"); |
| 80 | + capturedLoadedPluginRegistry = captureActivePluginRegistrySnapshot(); |
| 81 | + const timelinePath = state.path("kernel-startup.jsonl"); |
| 82 | + state.envVars.OPENCLAW_DIAGNOSTICS_TIMELINE_PATH = timelinePath; |
| 83 | + const token = "gateway-kernel-no-transport-token"; |
| 84 | + await state.writeConfig({ |
| 85 | + gateway: { |
| 86 | + auth: { mode: "token", token }, |
| 87 | + controlUi: { enabled: false }, |
| 88 | + port, |
| 89 | + }, |
| 90 | + }); |
| 91 | + state.applyEnv(); |
| 92 | + stageActivePluginRegistry(createEmptyPluginRegistry(), null, "default"); |
| 93 | + |
| 94 | + kernel = await createGatewayKernel(port, { |
| 95 | + auth: { mode: "token", token }, |
| 96 | + bind: "loopback", |
| 97 | + controlUiEnabled: false, |
| 98 | + sidecarStartup: "defer", |
| 99 | + }); |
52 | 100 | expect(kernel.transportBridge.current()).toBeUndefined(); |
53 | 101 | await expect(kernel.ensureSandboxHostPort()).rejects.toThrow( |
54 | 102 | "Gateway listener must start before the sandbox host", |
@@ -134,9 +182,23 @@ describe("createGatewayKernel", () => { |
134 | 182 | "gateway.request-context", |
135 | 183 | ]); |
136 | 184 | } finally { |
137 | | - await kernel.closeOnStartupFailure(); |
138 | | - await state.cleanup(); |
| 185 | + try { |
| 186 | + await kernel?.closeOnStartupFailure(); |
| 187 | + } finally { |
| 188 | + try { |
| 189 | + await state.cleanup(); |
| 190 | + } finally { |
| 191 | + if (capturedLoadedPluginRegistry) { |
| 192 | + restoreActivePluginRegistrySnapshot(capturedLoadedPluginRegistry); |
| 193 | + } |
| 194 | + prematureCleanupCalls = capturedRegistryCleanup.mock.calls.length; |
| 195 | + restoreActivePluginRegistrySnapshot(originalPluginRegistry); |
| 196 | + } |
| 197 | + } |
139 | 198 | } |
| 199 | + expect(prematureCleanupCalls).toBe(0); |
| 200 | + await vi.waitFor(() => expect(capturedRegistryCleanup).toHaveBeenCalledOnce()); |
| 201 | + expect(inspectAccount).not.toHaveBeenCalled(); |
140 | 202 | }); |
141 | 203 |
|
142 | 204 | it("runs kernel teardown when required TLS material is unavailable", async () => { |
|
0 commit comments