refactor(channels): dedupe transport and gateway test scaffolds

This commit is contained in:
Peter Steinberger
2026-02-16 14:52:15 +00:00
parent f717a13039
commit 93ca0ed54f
95 changed files with 4068 additions and 5221 deletions

View File

@@ -8,6 +8,39 @@ import { baileys, getLastSocket, resetBaileysMocks, resetLoadConfigMock } from "
const { createWaSocket, formatError, logWebSelfId, waitForWaConnection } =
await import("./session.js");
function mockCredsJsonSpies(readContents: string) {
const credsSuffix = path.join(".openclaw", "credentials", "whatsapp", "default", "creds.json");
const copySpy = vi.spyOn(fsSync, "copyFileSync").mockImplementation(() => {});
const existsSpy = vi.spyOn(fsSync, "existsSync").mockImplementation((p) => {
if (typeof p !== "string") {
return false;
}
return p.endsWith(credsSuffix);
});
const statSpy = vi.spyOn(fsSync, "statSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return { isFile: () => true, size: 12 } as never;
}
throw new Error(`unexpected statSync path: ${String(p)}`);
});
const readSpy = vi.spyOn(fsSync, "readFileSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return readContents as never;
}
throw new Error(`unexpected readFileSync path: ${String(p)}`);
});
return {
copySpy,
credsSuffix,
restore: () => {
copySpy.mockRestore();
existsSpy.mockRestore();
statSpy.mockRestore();
readSpy.mockRestore();
},
};
}
describe("web session", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -108,27 +141,7 @@ describe("web session", () => {
});
it("does not clobber creds backup when creds.json is corrupted", async () => {
const credsSuffix = path.join(".openclaw", "credentials", "whatsapp", "default", "creds.json");
const copySpy = vi.spyOn(fsSync, "copyFileSync").mockImplementation(() => {});
const existsSpy = vi.spyOn(fsSync, "existsSync").mockImplementation((p) => {
if (typeof p !== "string") {
return false;
}
return p.endsWith(credsSuffix);
});
const statSpy = vi.spyOn(fsSync, "statSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return { isFile: () => true, size: 12 } as never;
}
throw new Error(`unexpected statSync path: ${String(p)}`);
});
const readSpy = vi.spyOn(fsSync, "readFileSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return "{" as never;
}
throw new Error(`unexpected readFileSync path: ${String(p)}`);
});
const creds = mockCredsJsonSpies("{");
await createWaSocket(false, false);
const sock = getLastSocket();
@@ -137,13 +150,10 @@ describe("web session", () => {
sock.ev.emit("creds.update", {});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(copySpy).not.toHaveBeenCalled();
expect(creds.copySpy).not.toHaveBeenCalled();
expect(saveCreds).toHaveBeenCalled();
copySpy.mockRestore();
existsSpy.mockRestore();
statSpy.mockRestore();
readSpy.mockRestore();
creds.restore();
});
it("serializes creds.update saves to avoid overlapping writes", async () => {
@@ -186,7 +196,7 @@ describe("web session", () => {
});
it("rotates creds backup when creds.json is valid JSON", async () => {
const credsSuffix = path.join(".openclaw", "credentials", "whatsapp", "default", "creds.json");
const creds = mockCredsJsonSpies("{}");
const backupSuffix = path.join(
".openclaw",
"credentials",
@@ -195,26 +205,6 @@ describe("web session", () => {
"creds.json.bak",
);
const copySpy = vi.spyOn(fsSync, "copyFileSync").mockImplementation(() => {});
const existsSpy = vi.spyOn(fsSync, "existsSync").mockImplementation((p) => {
if (typeof p !== "string") {
return false;
}
return p.endsWith(credsSuffix);
});
const statSpy = vi.spyOn(fsSync, "statSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return { isFile: () => true, size: 12 } as never;
}
throw new Error(`unexpected statSync path: ${String(p)}`);
});
const readSpy = vi.spyOn(fsSync, "readFileSync").mockImplementation((p) => {
if (typeof p === "string" && p.endsWith(credsSuffix)) {
return "{}" as never;
}
throw new Error(`unexpected readFileSync path: ${String(p)}`);
});
await createWaSocket(false, false);
const sock = getLastSocket();
const saveCreds = (await baileys.useMultiFileAuthState.mock.results[0].value).saveCreds;
@@ -222,15 +212,12 @@ describe("web session", () => {
sock.ev.emit("creds.update", {});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(copySpy).toHaveBeenCalledTimes(1);
const args = copySpy.mock.calls[0] ?? [];
expect(String(args[0] ?? "")).toContain(credsSuffix);
expect(creds.copySpy).toHaveBeenCalledTimes(1);
const args = creds.copySpy.mock.calls[0] ?? [];
expect(String(args[0] ?? "")).toContain(creds.credsSuffix);
expect(String(args[1] ?? "")).toContain(backupSuffix);
expect(saveCreds).toHaveBeenCalled();
copySpy.mockRestore();
existsSpy.mockRestore();
statSpy.mockRestore();
readSpy.mockRestore();
creds.restore();
});
});