test(whatsapp): override config-runtime mock exports safely

This commit is contained in:
Vincent Koc 2026-03-19 09:42:13 -07:00
parent 639f78d257
commit 7fb142d115

View File

@ -36,44 +36,64 @@ vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
const mockModule = Object.create(null) as Record<string, unknown>;
Object.defineProperties(mockModule, Object.getOwnPropertyDescriptors(actual));
Object.defineProperty(mockModule, "loadConfig", {
configurable: true,
enumerable: true,
writable: true,
value: () => {
const getter = (globalThis as Record<symbol, unknown>)[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<symbol, unknown>)[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<string, Record<string, unknown>>;
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<string, Record<string, unknown>>;
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<string, unknown>;
} catch {
return {};
}
loadSessionStore: {
configurable: true,
enumerable: true,
writable: true,
value: (storePath: string) => {
try {
return JSON.parse(fsSync.readFileSync(storePath, "utf8")) as Record<string, unknown>;
} 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;
});