


























@@ -702,12 +702,21 @@ function createMockAcpSessionManager() {
702702};
703703}
704704705-function firstMockArg(mockFn: ReturnType<typeof vi.fn>, label: string, index = 0): unknown {
706-const call = mockFn.mock.calls.at(index);
705+function firstMockCall(mockFn: ReturnType<typeof vi.fn>, label: string, index = 0): unknown[] {
706+const call = mockFn.mock.calls[index] as unknown[] | undefined;
707707if (!call) {
708708throw new Error(`expected ${label} call #${index + 1}`);
709709}
710-return call.at(0);
710+return call;
711+}
712+713+function firstMockArg(
714+mockFn: ReturnType<typeof vi.fn>,
715+label: string,
716+index = 0,
717+argIndex = 0,
718+): unknown {
719+return firstMockCall(mockFn, label, index)[argIndex];
711720}
712721713722function firstToolResultPayload(dispatcher: ReplyDispatcher): ReplyPayload | undefined {
@@ -997,7 +1006,7 @@ describe("dispatchReplyFromConfig", () => {
9971006.calls[0]?.[0] as { accountId?: unknown; messageProvider?: unknown } | undefined;
9981007expect(normalizerOptions?.messageProvider).toBe("telegram");
9991008expect(normalizerOptions?.accountId).toBe("acc-1");
1000-const replyDispatchCall = hookMocks.runner.runReplyDispatch.mock.calls.at(0) as
1009+const replyDispatchCall = firstMockCall(hookMocks.runner.runReplyDispatch, "reply dispatch") as
10011010| [
10021011{
10031012originatingChannel?: unknown;
@@ -1082,7 +1091,7 @@ describe("dispatchReplyFromConfig", () => {
10821091expect(mocks.routeReply).not.toHaveBeenCalled();
10831092expect(dispatcher.sendFinalReply).not.toHaveBeenCalled();
10841093expect(result.queuedFinal).toBe(false);
1085-const replyDispatchCall = hookMocks.runner.runReplyDispatch.mock.calls.at(0) as
1094+const replyDispatchCall = firstMockCall(hookMocks.runner.runReplyDispatch, "reply dispatch") as
10861095| [
10871096{
10881097originatingChannel?: unknown;
@@ -2077,7 +2086,7 @@ describe("dispatchReplyFromConfig", () => {
20772086await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
2078208720792088expect(replyResolver).not.toHaveBeenCalled();
2080-const ensureSessionOptions = runtime.ensureSession.mock.calls.at(0)?.[0] as
2089+const ensureSessionOptions = firstMockArg(runtime.ensureSession, "ensure session") as
20812090| { agent?: unknown; mode?: unknown; sessionKey?: unknown }
20822091| undefined;
20832092expect(ensureSessionOptions?.sessionKey).toBe("agent:codex-acp:session-1");
@@ -2421,7 +2430,10 @@ describe("dispatchReplyFromConfig", () => {
2421243024222431await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
242324322424-const bindingLookup = sessionBindingMocks.resolveByConversation.mock.calls.at(0)?.[0];
2433+const bindingLookup = firstMockArg(
2434+sessionBindingMocks.resolveByConversation,
2435+"conversation binding lookup",
2436+) as { accountId?: unknown; channel?: unknown; conversationId?: unknown } | undefined;
24252437expect(bindingLookup?.channel).toBe("discord");
24262438expect(bindingLookup?.accountId).toBe("work");
24272439expect(bindingLookup?.conversationId).toBe("thread-1");
@@ -2506,17 +2518,20 @@ describe("dispatchReplyFromConfig", () => {
25062518conversationId: "C123",
25072519});
25082520expect(sessionBindingMocks.touch).toHaveBeenCalledWith("binding-acp-current");
2509-const ensureSessionOptions = runtime.ensureSession.mock.calls.at(0)?.[0] as
2521+const ensureSessionOptions = firstMockArg(runtime.ensureSession, "ensure session") as
25102522| { agent?: unknown; sessionKey?: unknown }
25112523| undefined;
25122524expect(ensureSessionOptions?.sessionKey).toBe(boundSessionKey);
25132525expect(ensureSessionOptions?.agent).toBe("opencode");
2514-const runTurnOptions = runtime.runTurn.mock.calls.at(0)?.[0] as { text?: unknown } | undefined;
2526+const runTurnOptions = firstMockArg(runtime.runTurn, "run turn") as
2527+| { text?: unknown }
2528+| undefined;
25152529expect(runTurnOptions?.text).toBe("continue");
25162530expect(replyResolver).not.toHaveBeenCalled();
2517-const blockPayload = (dispatcher.sendBlockReply as Mock).mock.calls.at(0)?.[0] as
2518-| ReplyPayload
2519-| undefined;
2531+const blockPayload = firstMockArg(
2532+dispatcher.sendBlockReply as ReturnType<typeof vi.fn>,
2533+"block reply",
2534+) as ReplyPayload | undefined;
25202535expect(blockPayload?.text).toBe("Bound ACP reply");
25212536});
25222537@@ -2706,7 +2721,9 @@ describe("dispatchReplyFromConfig", () => {
2706272127072722await dispatchReplyFromConfig({ ctx, cfg, dispatcher });
270827232709-const closeOptions = runtime.close.mock.calls.at(0)?.[0] as { reason?: unknown } | undefined;
2724+const closeOptions = firstMockArg(runtime.close, "runtime close") as
2725+| { reason?: unknown }
2726+| undefined;
27102727expect(closeOptions?.reason).toBe("oneshot-complete");
27112728});
27122729@@ -2927,7 +2944,10 @@ describe("dispatchReplyFromConfig", () => {
29272944const replyResolver = async () => ({ text: "hi" }) satisfies ReplyPayload;
29282945await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
292929462930-const [event, hookContext] = hookMocks.runner.runMessageReceived.mock.calls.at(0) as
2947+const [event, hookContext] = firstMockCall(
2948+hookMocks.runner.runMessageReceived,
2949+"message received hook",
2950+) as
29312951| [
29322952{
29332953content?: unknown;
@@ -2988,7 +3008,10 @@ describe("dispatchReplyFromConfig", () => {
2988300829893009expect(result).toEqual({ queuedFinal: true, counts: { tool: 0, block: 0, final: 0 } });
29903010expect(hookMocks.runner.runInboundClaim).not.toHaveBeenCalled();
2991-const [event, hookContext] = hookMocks.runner.runMessageReceived.mock.calls.at(0) as
3011+const [event, hookContext] = firstMockCall(
3012+hookMocks.runner.runMessageReceived,
3013+"message received hook",
3014+) as
29923015| [
29933016{ content?: unknown; from?: unknown; metadata?: Record<string, unknown> },
29943017{ accountId?: unknown; channelId?: unknown; conversationId?: unknown },
@@ -3034,7 +3057,10 @@ describe("dispatchReplyFromConfig", () => {
30343057const replyResolver = async () => ({ text: "hi" }) satisfies ReplyPayload;
30353058await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
303630593037-const createHookCall = internalHookMocks.createInternalHookEvent.mock.calls.at(0) as
3060+const createHookCall = firstMockCall(
3061+internalHookMocks.createInternalHookEvent,
3062+"internal hook event",
3063+) as
30383064| [
30393065unknown,
30403066unknown,
@@ -3094,9 +3120,10 @@ describe("dispatchReplyFromConfig", () => {
30943120const replyResolver = async () => ({ text: "reply" }) satisfies ReplyPayload;
30953121await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
309631223097-const createHookCall = internalHookMocks.createInternalHookEvent.mock.calls.at(0) as
3098-| [unknown, unknown, unknown, { content?: unknown; messageId?: unknown }]
3099-| undefined;
3123+const createHookCall = firstMockCall(
3124+internalHookMocks.createInternalHookEvent,
3125+"internal hook event",
3126+) as [unknown, unknown, unknown, { content?: unknown; messageId?: unknown }] | undefined;
31003127expect(createHookCall?.[0]).toBe("message");
31013128expect(createHookCall?.[1]).toBe("received");
31023129expect(createHookCall?.[2]).toBe("agent:main:discord:guild:123");
@@ -3126,9 +3153,10 @@ describe("dispatchReplyFromConfig", () => {
31263153state: "processing",
31273154reason: "message_start",
31283155});
3129-const processedEvent = diagnosticMocks.logMessageProcessed.mock.calls.at(0)?.[0] as
3130-| { channel?: unknown; outcome?: unknown; sessionKey?: unknown }
3131-| undefined;
3156+const processedEvent = firstMockArg(
3157+diagnosticMocks.logMessageProcessed,
3158+"message processed",
3159+) as { channel?: unknown; outcome?: unknown; sessionKey?: unknown } | undefined;
31323160expect(processedEvent?.channel).toBe("slack");
31333161expect(processedEvent?.outcome).toBe("completed");
31343162expect(processedEvent?.sessionKey).toBe("agent:main:main");
@@ -3569,9 +3597,10 @@ describe("dispatchReplyFromConfig", () => {
35693597 replyResolver,
35703598});
357135993572-const notice = (dispatcher.sendToolResult as ReturnType<typeof vi.fn>).mock.calls.at(0)?.[0] as
3573-| ReplyPayload
3574-| undefined;
3600+const notice = firstMockArg(
3601+dispatcher.sendToolResult as ReturnType<typeof vi.fn>,
3602+"tool result",
3603+) as ReplyPayload | undefined;
35753604expect(notice?.text).toContain("is not currently loaded.");
35763605expect(replyResolver).toHaveBeenCalledTimes(1);
35773606expect(hookMocks.runner.runInboundClaim).not.toHaveBeenCalled();
@@ -4165,7 +4194,10 @@ describe("before_dispatch hook", () => {
4165419441664195const result = await dispatchReplyFromConfig({ ctx, cfg: emptyConfig, dispatcher });
416741964168-const beforeDispatchCall = hookMocks.runner.runBeforeDispatch.mock.calls.at(0) as
4197+const beforeDispatchCall = firstMockCall(
4198+hookMocks.runner.runBeforeDispatch,
4199+"before dispatch hook",
4200+) as
41694201| [
41704202{
41714203body?: unknown;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。