chore: Fix types in tests 25/N.

This commit is contained in:
cpojer
2026-02-17 14:31:02 +09:00
parent 600022cdcc
commit 6e5df1dc0f
16 changed files with 118 additions and 78 deletions

View File

@@ -281,18 +281,18 @@ describe("callGateway error details", () => {
pickPrimaryTailnetIPv4.mockReturnValue(undefined);
vi.useFakeTimers();
let err: Error | null = null;
let errMessage = "";
const promise = callGateway({ method: "health", timeoutMs: 5 }).catch((caught) => {
err = caught as Error;
errMessage = caught instanceof Error ? caught.message : String(caught);
});
await vi.advanceTimersByTimeAsync(5);
await promise;
expect(err?.message).toContain("gateway timeout after 5ms");
expect(err?.message).toContain("Gateway target: ws://127.0.0.1:18789");
expect(err?.message).toContain("Source: local loopback");
expect(err?.message).toContain("Bind: loopback");
expect(errMessage).toContain("gateway timeout after 5ms");
expect(errMessage).toContain("Gateway target: ws://127.0.0.1:18789");
expect(errMessage).toContain("Source: local loopback");
expect(errMessage).toContain("Bind: loopback");
});
it("does not overflow very large timeout values", async () => {
@@ -304,18 +304,18 @@ describe("callGateway error details", () => {
pickPrimaryTailnetIPv4.mockReturnValue(undefined);
vi.useFakeTimers();
let err: Error | null = null;
let errMessage = "";
const promise = callGateway({ method: "health", timeoutMs: 2_592_010_000 }).catch((caught) => {
err = caught as Error;
errMessage = caught instanceof Error ? caught.message : String(caught);
});
await vi.advanceTimersByTimeAsync(1);
expect(err).toBeNull();
expect(errMessage).toBe("");
lastClientOptions?.onClose?.(1006, "");
await promise;
expect(err?.message).toContain("gateway closed (1006");
expect(errMessage).toContain("gateway closed (1006");
});
it("fails fast when remote mode is missing remote url", async () => {

View File

@@ -55,7 +55,7 @@ describe("buildMessageWithAttachments", () => {
expect(() => buildMessageWithAttachments("x", [att], { maxBytes: 16 })).toThrow(
/exceeds size limit/i,
);
const base64Calls = fromSpy.mock.calls.filter((args) => args[1] === "base64");
const base64Calls = fromSpy.mock.calls.filter((args) => (args as unknown[])[1] === "base64");
expect(base64Calls).toHaveLength(0);
fromSpy.mockRestore();
});
@@ -114,7 +114,7 @@ describe("parseMessageWithAttachments", () => {
{ maxBytes: 16, log: { warn: () => {} } },
),
).rejects.toThrow(/exceeds size limit/i);
const base64Calls = fromSpy.mock.calls.filter((args) => args[1] === "base64");
const base64Calls = fromSpy.mock.calls.filter((args) => (args as unknown[])[1] === "base64");
expect(base64Calls).toHaveLength(0);
fromSpy.mockRestore();
});

View File

@@ -57,7 +57,8 @@ async function setTestSessionStore(params: {
}
function latestAgentCall(): AgentCommandCall {
return vi.mocked(agentCommand).mock.calls.at(-1)?.[0] as AgentCommandCall;
const calls = vi.mocked(agentCommand).mock.calls as unknown as Array<[unknown]>;
return calls.at(-1)?.[0] as AgentCommandCall;
}
async function runMainAgentDeliveryWithSession(params: {

View File

@@ -503,7 +503,7 @@ describe("gateway server cron", () => {
expect(notifyRunRes.ok).toBe(true);
await waitForCondition(() => fetchMock.mock.calls.length === 1, 5000);
const [notifyUrl, notifyInit] = fetchMock.mock.calls[0] as [
const [notifyUrl, notifyInit] = fetchMock.mock.calls[0] as unknown as [
string,
{
method?: string;
@@ -527,7 +527,7 @@ describe("gateway server cron", () => {
);
expect(legacyRunRes.ok).toBe(true);
await waitForCondition(() => fetchMock.mock.calls.length === 2, 5000);
const [legacyUrl, legacyInit] = fetchMock.mock.calls[1] as [
const [legacyUrl, legacyInit] = fetchMock.mock.calls[1] as unknown as [
string,
{
method?: string;

View File

@@ -52,13 +52,16 @@ const whatsappOutbound: ChannelOutboundAdapter = {
if (!deps?.sendWhatsApp) {
throw new Error("Missing sendWhatsApp dep");
}
return { channel: "whatsapp", ...(await deps.sendWhatsApp(to, text, {})) };
return { channel: "whatsapp", ...(await deps.sendWhatsApp(to, text, { verbose: false })) };
},
sendMedia: async ({ deps, to, text, mediaUrl }) => {
if (!deps?.sendWhatsApp) {
throw new Error("Missing sendWhatsApp dep");
}
return { channel: "whatsapp", ...(await deps.sendWhatsApp(to, text, { mediaUrl })) };
return {
channel: "whatsapp",
...(await deps.sendWhatsApp(to, text, { verbose: false, mediaUrl })),
};
},
};
@@ -134,11 +137,10 @@ describe("gateway server models + voicewake", () => {
expect(initial.ok).toBe(true);
expect(initial.payload?.triggers).toEqual(["openclaw", "claude", "computer"]);
const changedP = onceMessage<{
type: "event";
event: string;
payload?: unknown;
}>(ws, (o) => o.type === "event" && o.event === "voicewake.changed");
const changedP = onceMessage(
ws,
(o) => o.type === "event" && o.event === "voicewake.changed",
);
const setRes = await rpcReq<{ triggers: string[] }>(ws, "voicewake.set", {
triggers: [" hi ", "", "there"],
@@ -146,7 +148,7 @@ describe("gateway server models + voicewake", () => {
expect(setRes.ok).toBe(true);
expect(setRes.payload?.triggers).toEqual(["hi", "there"]);
const changed = await changedP;
const changed = (await changedP) as { event?: string; payload?: unknown };
expect(changed.event).toBe("voicewake.changed");
expect((changed.payload as { triggers?: unknown } | undefined)?.triggers).toEqual([
"hi",
@@ -173,7 +175,7 @@ describe("gateway server models + voicewake", () => {
const nodeWs = new WebSocket(`ws://127.0.0.1:${port}`);
await new Promise<void>((resolve) => nodeWs.once("open", resolve));
const firstEventP = onceMessage<{ type: "event"; event: string; payload?: unknown }>(
const firstEventP = onceMessage(
nodeWs,
(o) => o.type === "event" && o.event === "voicewake.changed",
);
@@ -187,7 +189,7 @@ describe("gateway server models + voicewake", () => {
},
});
const first = await firstEventP;
const first = (await firstEventP) as { event?: string; payload?: unknown };
expect(first.event).toBe("voicewake.changed");
expect((first.payload as { triggers?: unknown } | undefined)?.triggers).toEqual([
"openclaw",
@@ -195,7 +197,7 @@ describe("gateway server models + voicewake", () => {
"computer",
]);
const broadcastP = onceMessage<{ type: "event"; event: string; payload?: unknown }>(
const broadcastP = onceMessage(
nodeWs,
(o) => o.type === "event" && o.event === "voicewake.changed",
);
@@ -204,7 +206,7 @@ describe("gateway server models + voicewake", () => {
});
expect(setRes.ok).toBe(true);
const broadcast = await broadcastP;
const broadcast = (await broadcastP) as { event?: string; payload?: unknown };
expect(broadcast.event).toBe("voicewake.changed");
expect((broadcast.payload as { triggers?: unknown } | undefined)?.triggers).toEqual([
"openclaw",

View File

@@ -131,10 +131,7 @@ describe("gateway update.run", () => {
},
}),
);
const res = await onceMessage<{ ok: boolean; payload?: unknown }>(
ws,
(o) => o.type === "res" && o.id === id,
);
const res = await onceMessage(ws, (o) => o.type === "res" && o.id === id);
expect(res.ok).toBe(true);
await waitForSignal(() => sigusr1.mock.calls.length > 0);
@@ -173,10 +170,7 @@ describe("gateway update.run", () => {
},
}),
);
const res = await onceMessage<{ ok: boolean; payload?: unknown }>(
ws,
(o) => o.type === "res" && o.id === id,
);
const res = await onceMessage(ws, (o) => o.type === "res" && o.id === id);
expect(res.ok).toBe(true);
expect(updateMock).toHaveBeenCalledOnce();
} finally {

View File

@@ -1,6 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { afterAll, beforeAll, describe, expect, it, type Mock } from "vitest";
import { resolveSessionTranscriptPath } from "../config/sessions.js";
import { emitAgentEvent } from "../infra/agent-events.js";
import { captureEnv } from "../test-utils/env.js";
@@ -37,8 +37,8 @@ afterAll(async () => {
describe("sessions_send gateway loopback", () => {
it("returns reply when lifecycle ends before agent.wait", async () => {
const spy = vi.mocked(agentCommand);
spy.mockImplementation(async (opts) => {
const spy = agentCommand as unknown as Mock<(opts: unknown) => Promise<void>>;
spy.mockImplementation(async (opts: unknown) => {
const params = opts as {
sessionId?: string;
runId?: string;
@@ -124,8 +124,8 @@ describe("sessions_send label lookup", () => {
"utf-8",
);
const spy = vi.mocked(agentCommand);
spy.mockImplementation(async (opts) => {
const spy = agentCommand as unknown as Mock<(opts: unknown) => Promise<void>>;
spy.mockImplementation(async (opts: unknown) => {
const params = opts as {
sessionId?: string;
runId?: string;

View File

@@ -103,7 +103,9 @@ function expectActiveRunCleanup(
requesterSessionKey,
});
expect(sessionCleanupMocks.clearSessionQueues).toHaveBeenCalledTimes(1);
const clearedKeys = sessionCleanupMocks.clearSessionQueues.mock.calls[0]?.[0] as string[];
const clearedKeys = (
sessionCleanupMocks.clearSessionQueues.mock.calls as unknown as Array<[string[]]>
)[0]?.[0];
expect(clearedKeys).toEqual(expect.arrayContaining(expectedQueueKeys));
expect(embeddedRunMock.abortCalls).toEqual([sessionId]);
expect(embeddedRunMock.waitCalls).toEqual([sessionId]);
@@ -656,7 +658,12 @@ describe("gateway server sessions", () => {
});
expect(reset.ok).toBe(true);
expect(sessionHookMocks.triggerInternalHook).toHaveBeenCalledTimes(1);
const [event] = sessionHookMocks.triggerInternalHook.mock.calls[0] ?? [];
const event = (
sessionHookMocks.triggerInternalHook.mock.calls as unknown as Array<[unknown]>
)[0]?.[0] as { context?: { previousSessionEntry?: unknown } } | undefined;
if (!event) {
throw new Error("expected session hook event");
}
expect(event).toMatchObject({
type: "command",
action: "new",
@@ -665,7 +672,7 @@ describe("gateway server sessions", () => {
commandSource: "gateway:sessions.reset",
},
});
expect(event.context.previousSessionEntry).toMatchObject({ sessionId: "sess-main" });
expect(event.context?.previousSessionEntry).toMatchObject({ sessionId: "sess-main" });
ws.close();
});