test: guard gateway mock calls

This commit is contained in:
Peter Steinberger
2026-05-12 10:36:43 +01:00
parent 17237fc44f
commit a68b4a5606
14 changed files with 18 additions and 14 deletions

View File

@@ -31,7 +31,7 @@ describe("warnLegacyOpenClawEnvVars", () => {
});
expect(emitWarning).toHaveBeenCalledOnce();
const [message, options] = emitWarning.mock.calls[0] as [
const [message, options] = emitWarning.mock.calls.at(0) as [
string,
{ code: string; type: string },
];

View File

@@ -157,7 +157,7 @@ describe("handleGatewayPostJsonEndpoint", () => {
},
);
const [, requestAuth] = (resolveOperatorScopes.mock.calls[0] as unknown as
const [, requestAuth] = (resolveOperatorScopes.mock.calls.at(0) as unknown as
| [IncomingMessage, { authMethod?: string; trustDeclaredOperatorScopes: boolean }]
| undefined) ?? [undefined, undefined];
expect(requestAuth?.authMethod).toBe("token");

View File

@@ -519,7 +519,7 @@ describe("mcp loopback server", () => {
expect(response.status).toBe(200);
expect(payload.result?.isError).toBe(false);
expect(execute).toHaveBeenCalledTimes(1);
const [callId, params, signal] = execute.mock.calls[0] ?? [];
const [callId, params, signal] = execute.mock.calls.at(0) ?? [];
expect(callId).toMatch(/^mcp-/);
expect(params).toEqual({ body: "hello" });
expect(signal).toBeInstanceOf(AbortSignal);

View File

@@ -183,7 +183,7 @@ function expectResponseError(
respond: ReturnType<typeof vi.fn>,
expected: { code?: string; messageIncludes?: string },
) {
const call = respond.mock.calls[0];
const call = respond.mock.calls.at(0);
if (!call) {
throw new Error("expected response call");
}

View File

@@ -113,7 +113,7 @@ function mockRotateOperatorTokenSuccess(): void {
function expectRespondedErrorMessage(opts: GatewayRequestHandlerOptions, message: string): void {
const respond = opts.respond as ReturnType<typeof vi.fn>;
expect(respond).toHaveBeenCalledTimes(1);
const call = respond.mock.calls[0] as unknown as [boolean, unknown, { message?: string }];
const call = respond.mock.calls.at(0) as unknown as [boolean, unknown, { message?: string }];
expect(call[0]).toBe(false);
expect(call[1]).toBeUndefined();
expect(call[2]?.message).toBe(message);

View File

@@ -40,7 +40,11 @@ async function callEnvironmentMethod(
respond,
context: mockContext(),
} as never);
return respond.mock.calls[0];
const call = respond.mock.calls.at(0);
if (call === undefined) {
throw new Error("expected environments handler to respond");
}
return call;
}
beforeEach(() => {

View File

@@ -236,7 +236,7 @@ describe("models.list", () => {
} as never,
});
const call = respond.mock.calls[0] as
const call = respond.mock.calls.at(0) as
| [boolean, unknown, { code?: number; message?: string }]
| undefined;
expect(call?.[0]).toBe(false);

View File

@@ -55,7 +55,7 @@ describe("native hook relay gateway method", () => {
context: {} as never,
});
const call = respond.mock.calls[0] as
const call = respond.mock.calls.at(0) as
| [boolean, unknown, { code?: string; message?: string }]
| undefined;
expect(call?.[0]).toBe(false);

View File

@@ -53,7 +53,7 @@ function expectRespondError(
respond: ReturnType<typeof vi.fn>,
expected: { code: string; message?: string },
): void {
const call = respond.mock.calls[0];
const call = respond.mock.calls.at(0);
expect(call?.[0]).toBe(false);
expect(call?.[1]).toBeUndefined();
const error = requireRecord(call?.[2]);

View File

@@ -1014,7 +1014,7 @@ describe("gateway send mirroring", () => {
idempotencyKey: "idem-send-options",
});
const options = mocks.deliverOutboundPayloads.mock.calls[0]?.[0];
const options = mocks.deliverOutboundPayloads.mock.calls.at(0)?.[0];
expect(options?.forceDocument).toBe(true);
expect(options?.silent).toBe(true);
expect(options?.formatting).toEqual({ parseMode: "HTML" });

View File

@@ -110,7 +110,7 @@ describe("sessions.send completed subagent follow-up status", () => {
isWebchatConnect: () => false,
});
const call = respondMock.mock.calls[0] as
const call = respondMock.mock.calls.at(0) as
| [boolean, { runId?: string; status?: string; messageSeq?: number }, unknown?, unknown?]
| undefined;
expect(call?.[0]).toBe(true);

View File

@@ -141,7 +141,7 @@ function expectError(result: CallResult, code: string, message: string): void {
}
function firstCallArg<T>(mock: { mock: { calls: unknown[][] } }, _type?: (value: T) => T): T {
const call = mock.mock.calls[0];
const call = mock.mock.calls.at(0);
if (!call) {
throw new Error("Expected first mock call");
}

View File

@@ -76,7 +76,7 @@ describe("ttsHandlers", () => {
context: { getRuntimeConfig: mocks.getRuntimeConfig },
} as never);
const call = respond.mock.calls[0] as
const call = respond.mock.calls.at(0) as
| [boolean, unknown, { code?: number; message?: string }]
| undefined;
expect(call?.[0]).toBe(false);

View File

@@ -157,7 +157,7 @@ describe("gateway usage helpers", () => {
expect(a.totals.totalTokens).toBe(1);
expect(b.totals.totalTokens).toBe(1);
expect(vi.mocked(loadCostUsageSummaryFromCache)).toHaveBeenCalledTimes(1);
expect(vi.mocked(loadCostUsageSummaryFromCache).mock.calls[0]?.[0]?.refreshMode).toBe(
expect(vi.mocked(loadCostUsageSummaryFromCache).mock.calls.at(0)?.[0]?.refreshMode).toBe(
"sync-when-empty",
);
});