From 7fb142d11525ff528539d62398e3843d6d9b0255 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 19 Mar 2026 09:42:13 -0700 Subject: [PATCH] test(whatsapp): override config-runtime mock exports safely --- extensions/whatsapp/src/test-helpers.ts | 90 +++++++++++++++---------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/extensions/whatsapp/src/test-helpers.ts b/extensions/whatsapp/src/test-helpers.ts index 74c5f8c3584..b71f25f9d63 100644 --- a/extensions/whatsapp/src/test-helpers.ts +++ b/extensions/whatsapp/src/test-helpers.ts @@ -36,44 +36,64 @@ vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => { const actual = await importOriginal(); const mockModule = Object.create(null) as Record; Object.defineProperties(mockModule, Object.getOwnPropertyDescriptors(actual)); - Object.defineProperty(mockModule, "loadConfig", { - configurable: true, - enumerable: true, - writable: true, - value: () => { - const getter = (globalThis as Record)[CONFIG_KEY]; - if (typeof getter === "function") { - return getter(); - } - return DEFAULT_CONFIG; + Object.defineProperties(mockModule, { + loadConfig: { + configurable: true, + enumerable: true, + writable: true, + value: () => { + const getter = (globalThis as Record)[CONFIG_KEY]; + if (typeof getter === "function") { + return getter(); + } + return DEFAULT_CONFIG; + }, }, - }); - Object.assign(mockModule, { - updateLastRoute: async (params: { - storePath: string; - sessionKey: string; - deliveryContext: { channel: string; to: string; accountId?: string }; - }) => { - const raw = await fs.readFile(params.storePath, "utf8").catch(() => "{}"); - const store = JSON.parse(raw) as Record>; - const current = store[params.sessionKey] ?? {}; - store[params.sessionKey] = { - ...current, - lastChannel: params.deliveryContext.channel, - lastTo: params.deliveryContext.to, - lastAccountId: params.deliveryContext.accountId, - }; - await fs.writeFile(params.storePath, JSON.stringify(store)); + updateLastRoute: { + configurable: true, + enumerable: true, + writable: true, + value: async (params: { + storePath: string; + sessionKey: string; + deliveryContext: { channel: string; to: string; accountId?: string }; + }) => { + const raw = await fs.readFile(params.storePath, "utf8").catch(() => "{}"); + const store = JSON.parse(raw) as Record>; + const current = store[params.sessionKey] ?? {}; + store[params.sessionKey] = { + ...current, + lastChannel: params.deliveryContext.channel, + lastTo: params.deliveryContext.to, + lastAccountId: params.deliveryContext.accountId, + }; + await fs.writeFile(params.storePath, JSON.stringify(store)); + }, }, - loadSessionStore: (storePath: string) => { - try { - return JSON.parse(fsSync.readFileSync(storePath, "utf8")) as Record; - } catch { - return {}; - } + loadSessionStore: { + configurable: true, + enumerable: true, + writable: true, + value: (storePath: string) => { + try { + return JSON.parse(fsSync.readFileSync(storePath, "utf8")) as Record; + } catch { + return {}; + } + }, + }, + recordSessionMetaFromInbound: { + configurable: true, + enumerable: true, + writable: true, + value: async () => undefined, + }, + resolveStorePath: { + configurable: true, + enumerable: true, + writable: true, + value: actual.resolveStorePath, }, - recordSessionMetaFromInbound: async () => undefined, - resolveStorePath: actual.resolveStorePath, }); return mockModule; });