|
1 | | -// Runtime web-channel plugin tests cover web channel plugin activation and runtime behavior. |
| 1 | +// Runtime web-channel plugin tests cover the public monitor facade and plugin lifecycle. |
2 | 2 | import fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | 5 | import { createTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
6 | 6 |
|
7 | 7 | const tempDirs = createTempDirTracker(); |
8 | 8 |
|
9 | | -afterEach(() => { |
| 9 | +afterEach(async () => { |
| 10 | + const { clearPluginMetadataLifecycleCaches } = await import("../plugin-metadata-lifecycle.js"); |
| 11 | + clearPluginMetadataLifecycleCaches(); |
10 | 12 | tempDirs.cleanup(); |
11 | 13 | vi.doUnmock("./runtime-plugin-boundary.js"); |
12 | 14 | vi.resetModules(); |
13 | 15 | }); |
14 | 16 |
|
15 | 17 | describe("runtime web channel plugin", () => { |
16 | | - it("resolves the default auth dir through the light runtime on each call", async () => { |
17 | | - let authDir = "/tmp/openclaw-default-auth"; |
18 | | - const resolveDefaultWebAuthDir = vi.fn(() => authDir); |
19 | | - const resolvePluginRuntimeRecordByEntryBaseNames = vi.fn(() => ({ |
20 | | - origin: "bundled", |
21 | | - source: "test", |
22 | | - })); |
23 | | - |
24 | | - vi.doMock("./runtime-plugin-boundary.js", () => ({ |
25 | | - loadPluginBoundaryModule: () => ({ resolveDefaultWebAuthDir }), |
26 | | - resolvePluginRuntimeModulePath: () => "/tmp/light-runtime-api.js", |
27 | | - resolvePluginRuntimeRecordByEntryBaseNames, |
28 | | - })); |
29 | | - |
30 | | - const { resolveWebChannelAuthDir } = await import("./runtime-web-channel-plugin.js"); |
31 | | - |
32 | | - expect(resolveWebChannelAuthDir()).toBe("/tmp/openclaw-default-auth"); |
33 | | - authDir = "/tmp/openclaw-profile-auth"; |
34 | | - expect(resolveWebChannelAuthDir()).toBe("/tmp/openclaw-profile-auth"); |
35 | | - expect(resolveDefaultWebAuthDir).toHaveBeenCalledTimes(2); |
36 | | - }); |
37 | | - |
38 | | - it.each(["light", "heavy"] as const)( |
39 | | - "reloads replaced %s runtime artifacts and dependencies after plugin lifecycle clears", |
40 | | - async (kind) => { |
41 | | - const pluginRoot = fs.realpathSync(tempDirs.make("openclaw-web-runtime-replacement-")); |
42 | | - const modulePath = path.join( |
43 | | - pluginRoot, |
44 | | - kind === "light" ? "light-runtime-api.js" : "runtime-api.js", |
45 | | - ); |
46 | | - const dependencyPath = path.join(pluginRoot, "dependency.js"); |
47 | | - fs.writeFileSync(path.join(pluginRoot, "package.json"), '{"type":"commonjs"}\n', "utf8"); |
48 | | - |
49 | | - const writeRuntime = (marker: string) => { |
50 | | - fs.writeFileSync(dependencyPath, `module.exports = ${JSON.stringify(marker)};\n`, "utf8"); |
51 | | - const exportName = kind === "light" ? "resolveDefaultWebAuthDir" : "startWebLoginWithQr"; |
52 | | - fs.writeFileSync( |
53 | | - modulePath, |
54 | | - `module.exports = { ${exportName}: () => ${JSON.stringify(marker)} + ":" + require("./dependency.js") };\n`, |
55 | | - "utf8", |
56 | | - ); |
57 | | - }; |
58 | | - writeRuntime("retired"); |
59 | | - |
60 | | - vi.doMock("./runtime-plugin-boundary.js", async (importOriginal) => ({ |
61 | | - ...(await importOriginal<typeof import("./runtime-plugin-boundary.js")>()), |
62 | | - resolvePluginRuntimeRecordByEntryBaseNames: () => ({ |
63 | | - origin: "global", |
64 | | - rootDir: pluginRoot, |
65 | | - source: path.join(pluginRoot, "index.js"), |
66 | | - }), |
67 | | - resolvePluginRuntimeModulePath: () => modulePath, |
68 | | - })); |
69 | | - |
70 | | - const runtime = await import("./runtime-web-channel-plugin.js"); |
71 | | - const { clearPluginMetadataLifecycleCaches } = |
72 | | - await import("../plugin-metadata-lifecycle.js"); |
73 | | - const invoke = () => |
74 | | - kind === "light" |
75 | | - ? Promise.resolve(runtime.resolveWebChannelAuthDir()) |
76 | | - : runtime.startWebLoginWithQr(); |
77 | | - |
78 | | - await expect(invoke()).resolves.toBe("retired:retired"); |
79 | | - writeRuntime("replacement"); |
80 | | - await expect(invoke()).resolves.toBe("retired:retired"); |
81 | | - |
82 | | - clearPluginMetadataLifecycleCaches(); |
83 | | - |
84 | | - await expect(invoke()).resolves.toBe("replacement:replacement"); |
85 | | - await expect(invoke()).resolves.toBe("replacement:replacement"); |
86 | | - }, |
87 | | - ); |
88 | | - |
89 | | - it("reports heavy runtime load failures as promise rejections", async () => { |
| 18 | + it("forwards library monitor calls lazily with exact arguments and runtime receiver", async () => { |
| 19 | + const result = { marker: "complete" }; |
| 20 | + const monitorWebChannel = vi.fn(async (..._args: unknown[]) => result); |
| 21 | + const runtimeModule = { monitorWebChannel }; |
| 22 | + const loadPluginBoundaryModule = vi.fn(() => runtimeModule); |
90 | 23 | vi.doMock("./runtime-plugin-boundary.js", () => ({ |
91 | | - loadPluginBoundaryModule: () => { |
92 | | - throw new Error("runtime unavailable"); |
93 | | - }, |
| 24 | + loadPluginBoundaryModule, |
94 | 25 | resolvePluginRuntimeModulePath: () => "/tmp/runtime-api.js", |
95 | 26 | resolvePluginRuntimeRecordByEntryBaseNames: () => ({ |
96 | 27 | origin: "bundled", |
97 | 28 | source: "test", |
98 | 29 | }), |
99 | 30 | })); |
100 | | - const runtime = await import("./runtime-web-channel-plugin.js"); |
101 | 31 |
|
102 | | - await expect(runtime.loginWeb(false)).rejects.toThrow("runtime unavailable"); |
103 | | - await expect(runtime.monitorWebChannel()).rejects.toThrow("runtime unavailable"); |
| 32 | + const library = await import("../../library.js"); |
| 33 | + expect(loadPluginBoundaryModule).not.toHaveBeenCalled(); |
| 34 | + const args = [ |
| 35 | + true, |
| 36 | + vi.fn(), |
| 37 | + false, |
| 38 | + vi.fn(), |
| 39 | + { log: vi.fn() }, |
| 40 | + new AbortController().signal, |
| 41 | + { accountId: "work" }, |
| 42 | + ]; |
| 43 | + await expect(library.monitorWebChannel(...args)).resolves.toBe(result); |
| 44 | + expect(monitorWebChannel).toHaveBeenCalledOnce(); |
| 45 | + expect(monitorWebChannel.mock.contexts[0]).toBe(runtimeModule); |
| 46 | + const [receivedArgs] = monitorWebChannel.mock.calls; |
| 47 | + expect(receivedArgs).toHaveLength(args.length); |
| 48 | + for (const [index, arg] of args.entries()) { |
| 49 | + expect(receivedArgs?.[index]).toBe(arg); |
| 50 | + } |
| 51 | + |
| 52 | + const monitorError = new Error("monitor failed"); |
| 53 | + monitorWebChannel.mockRejectedValueOnce(monitorError); |
| 54 | + await expect(library.monitorWebChannel(...args)).rejects.toBe(monitorError); |
104 | 55 | }); |
105 | 56 |
|
106 | | - it("falls back to the older WhatsApp light runtime auth dir export", async () => { |
107 | | - vi.doMock("./runtime-plugin-boundary.js", () => ({ |
108 | | - loadPluginBoundaryModule: () => ({ WA_WEB_AUTH_DIR: "/tmp/openclaw-legacy-auth" }), |
109 | | - resolvePluginRuntimeModulePath: () => "/tmp/light-runtime-api.js", |
| 57 | + it("reloads replaced monitor artifacts and dependencies after plugin lifecycle clears", async () => { |
| 58 | + const pluginRoot = fs.realpathSync(tempDirs.make("openclaw-web-runtime-replacement-")); |
| 59 | + const modulePath = path.join(pluginRoot, "runtime-api.js"); |
| 60 | + const dependencyPath = path.join(pluginRoot, "dependency.js"); |
| 61 | + fs.writeFileSync(path.join(pluginRoot, "package.json"), '{"type":"commonjs"}\n', "utf8"); |
| 62 | + |
| 63 | + const writeRuntime = (marker: string) => { |
| 64 | + fs.writeFileSync(dependencyPath, `module.exports = ${JSON.stringify(marker)};\n`, "utf8"); |
| 65 | + fs.writeFileSync( |
| 66 | + modulePath, |
| 67 | + `module.exports = { monitorWebChannel: () => ${JSON.stringify(marker)} + ":" + require("./dependency.js") };\n`, |
| 68 | + "utf8", |
| 69 | + ); |
| 70 | + }; |
| 71 | + writeRuntime("retired"); |
| 72 | + |
| 73 | + vi.doMock("./runtime-plugin-boundary.js", async (importOriginal) => ({ |
| 74 | + ...(await importOriginal<typeof import("./runtime-plugin-boundary.js")>()), |
110 | 75 | resolvePluginRuntimeRecordByEntryBaseNames: () => ({ |
111 | | - origin: "external", |
112 | | - source: "test", |
| 76 | + origin: "global", |
| 77 | + rootDir: pluginRoot, |
| 78 | + source: path.join(pluginRoot, "index.js"), |
113 | 79 | }), |
| 80 | + resolvePluginRuntimeModulePath: () => modulePath, |
114 | 81 | })); |
115 | 82 |
|
116 | | - const { resolveWebChannelAuthDir } = await import("./runtime-web-channel-plugin.js"); |
| 83 | + const runtime = await import("./runtime-web-channel-plugin.js"); |
| 84 | + const { clearPluginMetadataLifecycleCaches } = await import("../plugin-metadata-lifecycle.js"); |
| 85 | + await expect(runtime.monitorWebChannel()).resolves.toBe("retired:retired"); |
| 86 | + writeRuntime("replacement"); |
| 87 | + await expect(runtime.monitorWebChannel()).resolves.toBe("retired:retired"); |
117 | 88 |
|
118 | | - expect(resolveWebChannelAuthDir()).toBe("/tmp/openclaw-legacy-auth"); |
| 89 | + clearPluginMetadataLifecycleCaches(); |
| 90 | + |
| 91 | + await expect(runtime.monitorWebChannel()).resolves.toBe("replacement:replacement"); |
| 92 | + await expect(runtime.monitorWebChannel()).resolves.toBe("replacement:replacement"); |
119 | 93 | }); |
120 | 94 |
|
121 | | - it("rejects non-string legacy auth dir exports", async () => { |
| 95 | + it("reports heavy runtime load failures as promise rejections", async () => { |
| 96 | + const loadError = new Error("runtime unavailable"); |
| 97 | + const loadPluginBoundaryModule = vi.fn(() => { |
| 98 | + throw loadError; |
| 99 | + }); |
122 | 100 | vi.doMock("./runtime-plugin-boundary.js", () => ({ |
123 | | - loadPluginBoundaryModule: () => ({ |
124 | | - WA_WEB_AUTH_DIR: Object("/tmp/openclaw-string-object-auth"), |
125 | | - }), |
126 | | - resolvePluginRuntimeModulePath: () => "/tmp/light-runtime-api.js", |
| 101 | + loadPluginBoundaryModule, |
| 102 | + resolvePluginRuntimeModulePath: () => "/tmp/runtime-api.js", |
127 | 103 | resolvePluginRuntimeRecordByEntryBaseNames: () => ({ |
128 | | - origin: "external", |
| 104 | + origin: "bundled", |
129 | 105 | source: "test", |
130 | 106 | }), |
131 | 107 | })); |
| 108 | + const runtime = await import("./runtime-web-channel-plugin.js"); |
132 | 109 |
|
133 | | - const { resolveWebChannelAuthDir } = await import("./runtime-web-channel-plugin.js"); |
134 | | - |
135 | | - expect(() => resolveWebChannelAuthDir()).toThrow( |
136 | | - "web channel plugin runtime is missing export 'resolveDefaultWebAuthDir'", |
137 | | - ); |
| 110 | + expect(loadPluginBoundaryModule).not.toHaveBeenCalled(); |
| 111 | + await expect(runtime.monitorWebChannel()).rejects.toBe(loadError); |
138 | 112 | }); |
139 | 113 | }); |
0 commit comments