Fix test corrupting production sessions.json
The test 'falls back to most recent session when no to is provided' was using resolveStorePath() which returns the real ~/.warelay/sessions.json. This overwrote production session data with test values, causing session fragmentation issues. Changed to use a temp directory like other tests.
This commit is contained in:
@@ -3,32 +3,37 @@ import { vi } from "vitest";
|
||||
import type { MockBaileysSocket } from "../../test/mocks/baileys.js";
|
||||
import { createMockBaileys } from "../../test/mocks/baileys.js";
|
||||
|
||||
let loadConfigMock: () => unknown = () => ({
|
||||
// Use globalThis to store the mock config so it survives vi.mock hoisting
|
||||
const CONFIG_KEY = Symbol.for("warelay:testConfigMock");
|
||||
const DEFAULT_CONFIG = {
|
||||
inbound: {
|
||||
allowFrom: ["*"], // Allow all in tests by default
|
||||
messagePrefix: undefined, // No message prefix in tests
|
||||
responsePrefix: undefined, // No response prefix in tests
|
||||
timestampPrefix: false, // No timestamp in tests
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export function setLoadConfigMock(fn: () => unknown) {
|
||||
loadConfigMock = fn;
|
||||
// Initialize default if not set
|
||||
if (!(globalThis as Record<symbol, unknown>)[CONFIG_KEY]) {
|
||||
(globalThis as Record<symbol, unknown>)[CONFIG_KEY] = () => DEFAULT_CONFIG;
|
||||
}
|
||||
|
||||
export function setLoadConfigMock(fn: (() => unknown) | unknown) {
|
||||
(globalThis as Record<symbol, unknown>)[CONFIG_KEY] =
|
||||
typeof fn === "function" ? fn : () => fn;
|
||||
}
|
||||
|
||||
export function resetLoadConfigMock() {
|
||||
loadConfigMock = () => ({
|
||||
inbound: {
|
||||
allowFrom: ["*"],
|
||||
messagePrefix: undefined,
|
||||
responsePrefix: undefined,
|
||||
timestampPrefix: false,
|
||||
},
|
||||
});
|
||||
(globalThis as Record<symbol, unknown>)[CONFIG_KEY] = () => DEFAULT_CONFIG;
|
||||
}
|
||||
|
||||
vi.mock("../config/config.js", () => ({
|
||||
loadConfig: () => loadConfigMock(),
|
||||
loadConfig: () => {
|
||||
const getter = (globalThis as Record<symbol, unknown>)[CONFIG_KEY];
|
||||
if (typeof getter === "function") return getter();
|
||||
return DEFAULT_CONFIG;
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../media/store.js", () => ({
|
||||
|
||||
Reference in New Issue
Block a user