diff --git a/src/gateway/server-plugins.test.ts b/src/gateway/server-plugins.test.ts index 7e6bce579e1..cbf94250d81 100644 --- a/src/gateway/server-plugins.test.ts +++ b/src/gateway/server-plugins.test.ts @@ -205,6 +205,18 @@ function requireRecord(value: unknown, label: string): Record { return value; } +function getLastMockFirstArg( + mock: { mock: { calls: ReadonlyArray> } }, + label: string, +): unknown { + const calls = mock.mock.calls; + const call = calls[calls.length - 1]; + if (!call) { + throw new Error(`Expected ${label} mock to have at least one call`); + } + return call[0]; +} + function readRecordField(record: Record, key: string, label: string) { const value = record[key]; if (!isRecord(value)) { @@ -214,7 +226,10 @@ function readRecordField(record: Record, key: string, label: st } function getLastPluginLoadOptions(): Record { - return requireRecord(loadOpenClawPlugins.mock.calls.at(-1)?.[0], "plugin load options"); + return requireRecord( + getLastMockFirstArg(loadOpenClawPlugins, "plugin load"), + "plugin load options", + ); } function getLastPluginLoadOption(key: string) { @@ -222,12 +237,16 @@ function getLastPluginLoadOption(key: string) { } function getLastDispatchedContext(): GatewayRequestContext | undefined { - const call = handleGatewayRequest.mock.calls.at(-1)?.[0]; + const call = getLastMockFirstArg(handleGatewayRequest, "gateway request") as + | HandleGatewayRequestOptions + | undefined; return call?.context; } function getLastDispatchedParams(): Record | undefined { - const call = handleGatewayRequest.mock.calls.at(-1)?.[0]; + const call = getLastMockFirstArg(handleGatewayRequest, "gateway request") as + | HandleGatewayRequestOptions + | undefined; return call?.req?.params as Record | undefined; } @@ -236,13 +255,17 @@ function getRequiredLastDispatchedParams(): Record { } function getLastDispatchedClientScopes(): string[] { - const call = handleGatewayRequest.mock.calls.at(-1)?.[0]; + const call = getLastMockFirstArg(handleGatewayRequest, "gateway request") as + | HandleGatewayRequestOptions + | undefined; const scopes = call?.client?.connect?.scopes; return Array.isArray(scopes) ? scopes : []; } function getLastDispatchedClientInternal(): Record { - const call = handleGatewayRequest.mock.calls.at(-1)?.[0]; + const call = getLastMockFirstArg(handleGatewayRequest, "gateway request") as + | HandleGatewayRequestOptions + | undefined; return (call?.client?.internal ?? {}) as Record; } @@ -252,7 +275,7 @@ function getLastPluginLoadLogger(): { error: (message: string) => void; debug?: (message: string) => void; } { - const call = loadOpenClawPlugins.mock.calls.at(-1)?.[0] as + const call = getLastMockFirstArg(loadOpenClawPlugins, "plugin load") as | { logger?: { info: (message: string) => void; @@ -292,7 +315,7 @@ async function createSubagentRuntime( coreGatewayHandlers: {}, baseMethods: [], }); - const call = loadOpenClawPlugins.mock.calls.at(-1)?.[0] as + const call = getLastMockFirstArg(loadOpenClawPlugins, "plugin load") as | { runtimeOptions?: { allowGatewaySubagentBinding?: boolean } } | undefined; if (call?.runtimeOptions?.allowGatewaySubagentBinding !== true) {