





















@@ -89,6 +89,25 @@ async function waitForCronIsolatedRuns(count: number, timeoutMs = 2_000): Promis
8989.toBe(count);
9090}
919192+type HookCronRunCall = {
93+sessionKey?: string;
94+job?: {
95+agentId?: string;
96+payload?: {
97+externalContentSource?: string;
98+model?: string;
99+};
100+};
101+};
102+103+function cronRunCall(index = 0): HookCronRunCall {
104+const call = cronIsolatedRun.mock.calls.at(index)?.[0];
105+if (!call || typeof call !== "object") {
106+throw new Error(`expected cron isolated run call ${index + 1}`);
107+}
108+return call as HookCronRunCall;
109+}
110+92111async function postAgentHookWithIdempotency(
93112port: number,
94113idempotencyKey: string,
@@ -133,9 +152,7 @@ async function expectHookAgentSessionRouting(params: {
133152expect(resAgent.status).toBe(200);
134153await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
135154136-const routedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as
137-| { sessionKey?: string; job?: { agentId?: string } }
138-| undefined;
155+const routedCall = cronRunCall();
139156expect(routedCall?.job?.agentId).toBe("hooks");
140157expect(routedCall?.sessionKey).toBe(params.expectedSessionKey);
141158drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
@@ -180,9 +197,7 @@ describe("gateway server hooks", () => {
180197expect(resAgent.status).toBe(200);
181198const agentEvents = await waitForSystemEvent();
182199expect(agentEvents.some((event) => event.includes("Hook Email: done"))).toBe(true);
183-const firstCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
184-job?: { payload?: { externalContentSource?: string } };
185-};
200+const firstCall = cronRunCall();
186201expect(firstCall?.job?.payload?.externalContentSource).toBe("webhook");
187202drainSystemEvents(resolveMainKey());
188203@@ -194,9 +209,7 @@ describe("gateway server hooks", () => {
194209});
195210expect(resAgentModel.status).toBe(200);
196211await waitForSystemEvent();
197-const call = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
198-job?: { payload?: { model?: string } };
199-};
212+const call = cronRunCall();
200213expect(call?.job?.payload?.model).toBe("openai/gpt-4.1-mini");
201214drainSystemEvents(resolveMainKey());
202215@@ -208,9 +221,7 @@ describe("gateway server hooks", () => {
208221});
209222expect(resAgentWithId.status).toBe(200);
210223await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
211-const routedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
212-job?: { agentId?: string };
213-};
224+const routedCall = cronRunCall();
214225expect(routedCall?.job?.agentId).toBe("hooks");
215226drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
216227@@ -222,9 +233,7 @@ describe("gateway server hooks", () => {
222233});
223234expect(resAgentUnknown.status).toBe(200);
224235await waitForSystemEvent();
225-const fallbackCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
226-job?: { agentId?: string };
227-};
236+const fallbackCall = cronRunCall();
228237expect(fallbackCall?.job?.agentId).toBe("main");
229238drainSystemEvents(resolveMainKey());
230239@@ -297,10 +306,7 @@ describe("gateway server hooks", () => {
297306.poll(() => cronIsolatedRun.mock.calls.length, { timeout: 2_000, interval: 10 })
298307.toBe(1);
299308300-const call = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
301-sessionKey?: string;
302-job?: { payload?: { externalContentSource?: string } };
303-};
309+const call = cronRunCall();
304310expect(call?.sessionKey).toBe("main");
305311expect(call?.job?.payload?.externalContentSource).toBe("gmail");
306312drainSystemEvents(resolveMainKey());
@@ -490,9 +496,7 @@ describe("gateway server hooks", () => {
490496});
491497expect(defaultRoute.status).toBe(200);
492498await waitForSystemEvent();
493-const defaultCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as
494-| { sessionKey?: string }
495-| undefined;
499+const defaultCall = cronRunCall();
496500expect(defaultCall?.sessionKey).toBe("hook:ingress");
497501drainSystemEvents(resolveMainKey());
498502@@ -508,9 +512,7 @@ describe("gateway server hooks", () => {
508512});
509513expect(mappedOk.status).toBe(200);
510514await waitForSystemEvent();
511-const mappedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as
512-| { sessionKey?: string }
513-| undefined;
515+const mappedCall = cronRunCall();
514516expect(mappedCall?.sessionKey).toBe("hook:mapped:42");
515517drainSystemEvents(resolveMainKey());
516518@@ -562,9 +564,7 @@ describe("gateway server hooks", () => {
562564});
563565expect(staticMapped.status).toBe(200);
564566await waitForSystemEvent();
565-const staticCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as
566-| { sessionKey?: string }
567-| undefined;
567+const staticCall = cronRunCall();
568568expect(staticCall?.sessionKey).toBe("hook:gmail:fixed");
569569drainSystemEvents(resolveMainKey());
570570});
@@ -812,9 +812,7 @@ describe("gateway server hooks", () => {
812812const resNoAgent = await postHook(port, "/hooks/agent", { message: "No explicit agent" });
813813expect(resNoAgent.status).toBe(200);
814814await waitForSystemEvent();
815-const noAgentCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
816-job?: { agentId?: string };
817-};
815+const noAgentCall = cronRunCall();
818816expect(noAgentCall?.job?.agentId).toBeUndefined();
819817drainSystemEvents(resolveMainKey());
820818@@ -825,9 +823,7 @@ describe("gateway server hooks", () => {
825823});
826824expect(resAllowed.status).toBe(200);
827825await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
828-const allowedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
829-job?: { agentId?: string };
830-};
826+const allowedCall = cronRunCall();
831827expect(allowedCall?.job?.agentId).toBe("hooks");
832828drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
833829此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。