test: tighten whatsapp session assertions

This commit is contained in:
Peter Steinberger
2026-05-11 01:17:15 +01:00
parent 49e8f597b3
commit 8b4c4a4e06

View File

@@ -165,6 +165,27 @@ function requireValue<T>(value: T | undefined, label: string): T {
return value;
}
function firstWriteFileCall(writeFileSpy: ReturnType<typeof vi.fn>): {
data: unknown;
options: { flag?: string; mode?: number };
path: string;
} {
const [filePath, data, options] = writeFileSpy.mock.calls[0] ?? [];
expect(typeof filePath).toBe("string");
return {
data,
options: (options ?? {}) as { flag?: string; mode?: number },
path: filePath as string,
};
}
function expectRuntimeLogContaining(
runtime: { log: ReturnType<typeof vi.fn> },
text: string,
): void {
expect(runtime.log.mock.calls.some(([message]) => String(message).includes(text))).toBe(true);
}
describe("web session", () => {
beforeAll(async () => {
({
@@ -198,13 +219,11 @@ describe("web session", () => {
await createWaSocket(true, false, { authDir });
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
expect(makeWASocket).toHaveBeenCalledWith(
expect.objectContaining({
printQRInTerminal: false,
...DEFAULT_WHATSAPP_SOCKET_TIMING,
}),
);
const passed = makeWASocket.mock.calls[0][0];
expect(passed.printQRInTerminal).toBe(false);
expect(passed.keepAliveIntervalMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.keepAliveIntervalMs);
expect(passed.connectTimeoutMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.connectTimeoutMs);
expect(passed.defaultQueryTimeoutMs).toBe(DEFAULT_WHATSAPP_SOCKET_TIMING.defaultQueryTimeoutMs);
const passedLogger = (passed as { logger?: { level?: string; trace?: unknown } }).logger;
expect(passedLogger?.level).toBe("silent");
if (typeof passedLogger?.trace !== "function") {
@@ -213,11 +232,11 @@ describe("web session", () => {
passedLogger.trace("ignored");
await emitCredsUpdate(authDir);
expect(openMock.writeFileSpy).toHaveBeenCalledWith(
expect.stringContaining(path.join(authDir, ".creds.")),
expect.any(String),
expect.objectContaining({ mode: 0o600, flag: "wx" }),
);
const write = firstWriteFileCall(openMock.writeFileSpy);
expect(write.path).toContain(path.join(authDir, ".creds."));
expect(typeof write.data).toBe("string");
expect(write.options.mode).toBe(0o600);
expect(write.options.flag).toBe("wx");
openMock.restore();
});
@@ -228,13 +247,10 @@ describe("web session", () => {
defaultQueryTimeoutMs: 120_000,
});
expect(baileys.makeWASocket).toHaveBeenCalledWith(
expect.objectContaining({
keepAliveIntervalMs: 10_000,
connectTimeoutMs: 90_000,
defaultQueryTimeoutMs: 120_000,
}),
);
const passed = (baileys.makeWASocket as ReturnType<typeof vi.fn>).mock.calls[0]?.[0];
expect(passed.keepAliveIntervalMs).toBe(10_000);
expect(passed.connectTimeoutMs).toBe(90_000);
expect(passed.defaultQueryTimeoutMs).toBe(120_000);
});
it("uses ambient env proxy agent when HTTPS_PROXY is configured", async () => {
@@ -326,9 +342,7 @@ describe("web session", () => {
logWebSelfId("/tmp/wa-creds", runtime as never, true);
expect(runtime.log).toHaveBeenCalledWith(
expect.stringContaining("Web Channel: +12345 (jid 12345@s.whatsapp.net)"),
);
expectRuntimeLogContaining(runtime, "Web Channel: +12345 (jid 12345@s.whatsapp.net)");
creds.restore();
});
@@ -345,8 +359,9 @@ describe("web session", () => {
logWebSelfId("/tmp/wa-creds", runtime as never, true);
expect(runtime.log).toHaveBeenCalledWith(
expect.stringContaining("Web Channel: +12345 (jid 12345@s.whatsapp.net, lid 777@lid)"),
expectRuntimeLogContaining(
runtime,
"Web Channel: +12345 (jid 12345@s.whatsapp.net, lid 777@lid)",
);
creds.restore();
});
@@ -515,13 +530,13 @@ describe("web session", () => {
me: { id: "123@s.whatsapp.net" },
});
expect(openMock.writeFileSpy).toHaveBeenCalledWith(
expect.stringContaining(
path.join("/tmp", "openclaw-oauth", "whatsapp", "default", ".creds."),
),
expect.any(String),
expect.objectContaining({ mode: 0o600, flag: "wx" }),
const write = firstWriteFileCall(openMock.writeFileSpy);
expect(write.path).toContain(
path.join("/tmp", "openclaw-oauth", "whatsapp", "default", ".creds."),
);
expect(typeof write.data).toBe("string");
expect(write.options.mode).toBe(0o600);
expect(write.options.flag).toBe("wx");
expect(openMock.tempHandles).toHaveLength(1);
expect(openMock.tempHandles[0]?.sync).toHaveBeenCalledTimes(1);
expect(openMock.tempHandles[0]?.close).toHaveBeenCalledTimes(1);
@@ -585,7 +600,7 @@ describe("web session", () => {
expect(renameSpy).toHaveBeenCalledOnce();
const parsedCreds = JSON.parse(raw) as unknown;
expect(parsedCreds).toMatchObject(originalCreds);
expect(parsedCreds).toEqual(originalCreds);
expect(tempEntries).toHaveLength(0);
renameSpy.mockRestore();