From 7f59a5e1287b8cce6bb997f5faffc5d59c55b5c2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 9 May 2026 18:47:11 +0100 Subject: [PATCH] test: tighten subagent announce assertions --- .../subagent-announce.format.e2e.test.ts | 92 ++++++++++++------- 1 file changed, 59 insertions(+), 33 deletions(-) diff --git a/src/agents/subagent-announce.format.e2e.test.ts b/src/agents/subagent-announce.format.e2e.test.ts index 97c7e125744..a5b36f515d6 100644 --- a/src/agents/subagent-announce.format.e2e.test.ts +++ b/src/agents/subagent-announce.format.e2e.test.ts @@ -69,6 +69,50 @@ function visibleAgentResponse(runId = "run-main") { }; } +function expectInputProvenance( + params: Record | undefined, + sourceSessionKey: string, +) { + const inputProvenance = params?.inputProvenance; + expect(inputProvenance).toBeTruthy(); + if (!inputProvenance || typeof inputProvenance !== "object") { + throw new Error("Expected input provenance"); + } + const provenance = inputProvenance as Record; + expect(provenance.kind).toBe("inter_session"); + expect(provenance.sourceSessionKey).toBe(sourceSessionKey); + expect(provenance.sourceTool).toBe("subagent_announce"); +} + +function getAgentCall(index = 0): AgentCallRequest { + const call = agentSpy.mock.calls[index]?.[0]; + expect(call).toBeDefined(); + if (!call) { + throw new Error(`Expected agent call at index ${index}`); + } + return call; +} + +function expectAgentCallFields( + call: AgentCallRequest, + expected: { + channel?: string; + deliver?: boolean; + sessionKey: string; + to?: string; + }, +) { + expect(call.method).toBe("agent"); + expect(call.params?.sessionKey).toBe(expected.sessionKey); + expect(call.params?.deliver).toBe(expected.deliver); + if ("channel" in expected) { + expect(call.params?.channel).toBe(expected.channel); + } + if ("to" in expected) { + expect(call.params?.to).toBe(expected.to); + } +} + const agentSpy = vi.fn(async (_req: AgentCallRequest) => visibleAgentResponse()); const sendSpy = vi.fn(async (_req: AgentCallRequest) => ({ runId: "send-main", status: "ok" })); const sessionsDeleteSpy = vi.fn((_req: AgentCallRequest) => undefined); @@ -695,11 +739,7 @@ describe("subagent announce formatting", () => { expect(call?.params?.channel).toBe("discord"); expect(call?.params?.to).toBe("channel:12345"); expect(call?.params?.sessionKey).toBe("agent:main:main"); - expect(call?.params?.inputProvenance).toMatchObject({ - kind: "inter_session", - sourceSessionKey: "agent:main:subagent:test", - sourceTool: "subagent_announce", - }); + expectInputProvenance(call?.params, "agent:main:subagent:test"); expect(msg).toContain("final answer: 2"); expect(msg).not.toContain("✅ Subagent"); }); @@ -1171,9 +1211,8 @@ describe("subagent announce formatting", () => { const directTargets = agentSpy.mock.calls.map( (call) => (call?.[0] as { params?: { to?: string } })?.params?.to, ); - expect(directTargets).toEqual( - expect.arrayContaining(["channel:thread-child-a", "channel:thread-child-b"]), - ); + expect(directTargets).toContain("channel:thread-child-a"); + expect(directTargets).toContain("channel:thread-child-b"); expect(directTargets).not.toContain("channel:main-parent-channel"); }); @@ -1859,12 +1898,9 @@ describe("subagent announce formatting", () => { expect(didAnnounce).toBe(true); expect(sendSpy).toHaveBeenCalledTimes(0); expect(agentSpy).toHaveBeenCalledTimes(1); - expect(agentSpy.mock.calls[0]?.[0]).toMatchObject({ - method: "agent", - params: { - sessionKey: "agent:main:main", - deliver: false, - }, + expectAgentCallFields(getAgentCall(), { + sessionKey: "agent:main:main", + deliver: false, }); }); @@ -1888,14 +1924,11 @@ describe("subagent announce formatting", () => { expect(didAnnounce).toBe(true); expect(sendSpy).not.toHaveBeenCalled(); expect(agentSpy).toHaveBeenCalledTimes(1); - expect(agentSpy.mock.calls[0]?.[0]).toMatchObject({ - method: "agent", - params: { - sessionKey: "agent:main:main", - channel: "discord", - to: "channel:12345", - deliver: true, - }, + expectAgentCallFields(getAgentCall(), { + sessionKey: "agent:main:main", + channel: "discord", + to: "channel:12345", + deliver: true, }); }); @@ -2118,7 +2151,8 @@ describe("subagent announce formatting", () => { const accountIds = agentSpy.mock.calls.map( (call) => (call?.[0] as { params?: { accountId?: string } })?.params?.accountId, ); - expect(accountIds).toEqual(expect.arrayContaining(["acct-a", "acct-b"])); + expect(accountIds).toContain("acct-a"); + expect(accountIds).toContain("acct-b"); }); it.each([ @@ -2206,11 +2240,7 @@ describe("subagent announce formatting", () => { expect(call?.params?.channel).toBeUndefined(); expect(call?.params?.to).toBeUndefined(); expect((call?.params as { role?: unknown } | undefined)?.role).toBeUndefined(); - expect(call?.params?.inputProvenance).toMatchObject({ - kind: "inter_session", - sourceSessionKey: "agent:main:subagent:worker", - sourceTool: "subagent_announce", - }); + expectInputProvenance(call?.params, "agent:main:subagent:worker"); }); it("keeps completion-mode announce internal for nested requester subagent sessions", async () => { @@ -2234,11 +2264,7 @@ describe("subagent announce formatting", () => { expect(call?.params?.deliver).toBe(false); expect(call?.params?.channel).toBeUndefined(); expect(call?.params?.to).toBeUndefined(); - expect(call?.params?.inputProvenance).toMatchObject({ - kind: "inter_session", - sourceSessionKey: "agent:main:subagent:orchestrator:subagent:worker", - sourceTool: "subagent_announce", - }); + expectInputProvenance(call?.params, "agent:main:subagent:orchestrator:subagent:worker"); const message = typeof call?.params?.message === "string" ? call.params.message : ""; expect(message).toContain( "Convert this completion into a concise internal orchestration update for your parent agent",