diff --git a/src/cron/isolated-agent.direct-delivery-core-channels.test.ts b/src/cron/isolated-agent.direct-delivery-core-channels.test.ts index 38b9bd761a8..c66a78b6472 100644 --- a/src/cron/isolated-agent.direct-delivery-core-channels.test.ts +++ b/src/cron/isolated-agent.direct-delivery-core-channels.test.ts @@ -83,6 +83,44 @@ async function runExplicitAnnounceTurn(params: { }); } +type CoreChannelSendFn = CliDeps[ChannelCase["sendKey"]]; + +async function expectCoreChannelAnnounceDelivery({ + assertSend, + meta, + payloads, + testCase, +}: { + assertSend: (sendFn: CoreChannelSendFn) => void; + meta?: Parameters[1]; + payloads: Parameters[0]; + testCase: ChannelCase; +}): Promise { + await withTempCronHome(async (home) => { + const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" }); + const deps = createCliDeps(); + if (meta) { + mockAgentPayloads(payloads, meta); + } else { + mockAgentPayloads(payloads); + } + + const res = await runExplicitAnnounceTurn({ + home, + storePath, + deps, + channel: testCase.channel, + to: testCase.to, + }); + + expect(res.status).toBe("ok"); + expect(res.delivered).toBe(true); + expect(res.deliveryAttempted).toBe(true); + expect(runSubagentAnnounceFlow).not.toHaveBeenCalled(); + assertSend(deps[testCase.sendKey]); + }); +} + type CoreChannel = ChannelCase["channel"]; type TestSendFn = ( to: string, @@ -180,73 +218,46 @@ describe("runCronIsolatedAgentTurn core-channel direct delivery", () => { for (const testCase of CASES) { it(`routes ${testCase.name} text-only announce delivery through the outbound adapter`, async () => { - await withTempCronHome(async (home) => { - const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" }); - const deps = createCliDeps(); - mockAgentPayloads([{ text: "hello from cron" }]); - - const res = await runExplicitAnnounceTurn({ - home, - storePath, - deps, - channel: testCase.channel, - to: testCase.to, - }); - - expect(res.status).toBe("ok"); - expect(res.delivered).toBe(true); - expect(res.deliveryAttempted).toBe(true); - expect(runSubagentAnnounceFlow).not.toHaveBeenCalled(); - - const sendFn = deps[testCase.sendKey]; - expect(sendFn).toHaveBeenCalledTimes(1); - expect(sendFn).toHaveBeenCalledWith( - testCase.expectedTo, - "hello from cron", - expect.any(Object), - ); + await expectCoreChannelAnnounceDelivery({ + testCase, + payloads: [{ text: "hello from cron" }], + assertSend: (sendFn) => { + expect(sendFn).toHaveBeenCalledTimes(1); + expect(sendFn).toHaveBeenCalledWith( + testCase.expectedTo, + "hello from cron", + expect.any(Object), + ); + }, }); }); it(`preserves multi-payload text-only announce delivery for ${testCase.name} even when final assistant text exists`, async () => { - await withTempCronHome(async (home) => { - const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" }); - const deps = createCliDeps(); - mockAgentPayloads([{ text: "Working on it..." }, { text: "Final weather summary" }], { + await expectCoreChannelAnnounceDelivery({ + testCase, + payloads: [{ text: "Working on it..." }, { text: "Final weather summary" }], + meta: { meta: { durationMs: 5, agentMeta: { sessionId: "s", provider: "p", model: "m" }, finalAssistantVisibleText: "Final weather summary", }, - }); - - const res = await runExplicitAnnounceTurn({ - home, - storePath, - deps, - channel: testCase.channel, - to: testCase.to, - }); - - expect(res.status).toBe("ok"); - expect(res.delivered).toBe(true); - expect(res.deliveryAttempted).toBe(true); - expect(runSubagentAnnounceFlow).not.toHaveBeenCalled(); - - const sendFn = deps[testCase.sendKey]; - expect(sendFn).toHaveBeenCalledTimes(2); - expect(sendFn).toHaveBeenNthCalledWith( - 1, - testCase.expectedTo, - "Working on it...", - expect.any(Object), - ); - expect(sendFn).toHaveBeenNthCalledWith( - 2, - testCase.expectedTo, - "Final weather summary", - expect.any(Object), - ); + }, + assertSend: (sendFn) => { + expect(sendFn).toHaveBeenCalledTimes(2); + expect(sendFn).toHaveBeenNthCalledWith( + 1, + testCase.expectedTo, + "Working on it...", + expect.any(Object), + ); + expect(sendFn).toHaveBeenNthCalledWith( + 2, + testCase.expectedTo, + "Final weather summary", + expect.any(Object), + ); + }, }); }); }