From c5dc61e795037f213ef0c1ffeecdf4384922b0f7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 22:03:57 +0000 Subject: [PATCH] test: share session target and outbound mirror helpers --- src/config/sessions/targets.test.ts | 23 ++-- .../outbound/outbound-send-service.test.ts | 101 ++++++++++-------- 2 files changed, 71 insertions(+), 53 deletions(-) diff --git a/src/config/sessions/targets.test.ts b/src/config/sessions/targets.test.ts index 720cc3e892e..43674233a3a 100644 --- a/src/config/sessions/targets.test.ts +++ b/src/config/sessions/targets.test.ts @@ -40,6 +40,14 @@ function createCustomRootCfg(customRoot: string, defaultAgentId = "ops"): OpenCl }; } +async function resolveTargetsForCustomRoot(home: string, agentIds: string[]) { + const customRoot = path.join(home, "custom-state"); + const storePaths = await createAgentSessionStores(customRoot, agentIds); + const cfg = createCustomRootCfg(customRoot); + const targets = await resolveAllAgentSessionStoreTargets(cfg, { env: process.env }); + return { storePaths, targets }; +} + function expectTargetsToContainStores( targets: Array<{ agentId: string; storePath: string }>, stores: Record, @@ -152,11 +160,7 @@ describe("resolveAllAgentSessionStoreTargets", () => { it("discovers retired agent stores under a configured custom session root", async () => { await withTempHome(async (home) => { - const customRoot = path.join(home, "custom-state"); - const storePaths = await createAgentSessionStores(customRoot, ["ops", "retired"]); - const cfg = createCustomRootCfg(customRoot); - - const targets = await resolveAllAgentSessionStoreTargets(cfg, { env: process.env }); + const { storePaths, targets } = await resolveTargetsForCustomRoot(home, ["ops", "retired"]); expectTargetsToContainStores(targets, storePaths); expect(targets.filter((target) => target.storePath === storePaths.ops)).toHaveLength(1); @@ -165,11 +169,10 @@ describe("resolveAllAgentSessionStoreTargets", () => { it("keeps the actual on-disk store path for discovered retired agents", async () => { await withTempHome(async (home) => { - const customRoot = path.join(home, "custom-state"); - const storePaths = await createAgentSessionStores(customRoot, ["ops", "Retired Agent"]); - const cfg = createCustomRootCfg(customRoot); - - const targets = await resolveAllAgentSessionStoreTargets(cfg, { env: process.env }); + const { storePaths, targets } = await resolveTargetsForCustomRoot(home, [ + "ops", + "Retired Agent", + ]); expect(targets).toEqual( expect.arrayContaining([ diff --git a/src/infra/outbound/outbound-send-service.test.ts b/src/infra/outbound/outbound-send-service.test.ts index ac144265753..d4a481a8693 100644 --- a/src/infra/outbound/outbound-send-service.test.ts +++ b/src/infra/outbound/outbound-send-service.test.ts @@ -47,6 +47,47 @@ describe("executeSendAction", () => { }; } + function expectMirrorWrite( + expected: Partial<{ + agentId: string; + sessionKey: string; + text: string; + idempotencyKey: string; + mediaUrls: string[]; + }>, + ) { + expect(mocks.appendAssistantMessageToSessionTranscript).toHaveBeenCalledWith( + expect.objectContaining(expected), + ); + } + + async function executePluginMirroredSend(params: { + mirror?: Partial<{ + sessionKey: string; + agentId?: string; + idempotencyKey?: string; + }>; + mediaUrls?: string[]; + }) { + mocks.dispatchChannelMessageAction.mockResolvedValue(pluginActionResult("msg-plugin")); + + await executeSendAction({ + ctx: { + cfg: {}, + channel: "discord", + params: { to: "channel:123", message: "hello" }, + dryRun: false, + mirror: { + sessionKey: "agent:main:discord:channel:123", + ...params.mirror, + }, + }, + to: "channel:123", + message: "hello", + mediaUrls: params.mediaUrls, + }); + } + beforeEach(() => { mocks.dispatchChannelMessageAction.mockClear(); mocks.sendMessage.mockClear(); @@ -131,59 +172,33 @@ describe("executeSendAction", () => { }); it("passes mirror idempotency keys through plugin-handled sends", async () => { - mocks.dispatchChannelMessageAction.mockResolvedValue(pluginActionResult("msg-plugin")); - - await executeSendAction({ - ctx: { - cfg: {}, - channel: "discord", - params: { to: "channel:123", message: "hello" }, - dryRun: false, - mirror: { - sessionKey: "agent:main:discord:channel:123", - idempotencyKey: "idem-plugin-send-1", - }, + await executePluginMirroredSend({ + mirror: { + idempotencyKey: "idem-plugin-send-1", }, - to: "channel:123", - message: "hello", }); - expect(mocks.appendAssistantMessageToSessionTranscript).toHaveBeenCalledWith( - expect.objectContaining({ - sessionKey: "agent:main:discord:channel:123", - text: "hello", - idempotencyKey: "idem-plugin-send-1", - }), - ); + expectMirrorWrite({ + sessionKey: "agent:main:discord:channel:123", + text: "hello", + idempotencyKey: "idem-plugin-send-1", + }); }); it("falls back to message and media params for plugin-handled mirror writes", async () => { - mocks.dispatchChannelMessageAction.mockResolvedValue(pluginActionResult("msg-plugin")); - - await executeSendAction({ - ctx: { - cfg: {}, - channel: "discord", - params: { to: "channel:123", message: "hello" }, - dryRun: false, - mirror: { - sessionKey: "agent:main:discord:channel:123", - agentId: "agent-9", - }, + await executePluginMirroredSend({ + mirror: { + agentId: "agent-9", }, - to: "channel:123", - message: "hello", mediaUrls: ["https://example.com/a.png", "https://example.com/b.png"], }); - expect(mocks.appendAssistantMessageToSessionTranscript).toHaveBeenCalledWith( - expect.objectContaining({ - agentId: "agent-9", - sessionKey: "agent:main:discord:channel:123", - text: "hello", - mediaUrls: ["https://example.com/a.png", "https://example.com/b.png"], - }), - ); + expectMirrorWrite({ + agentId: "agent-9", + sessionKey: "agent:main:discord:channel:123", + text: "hello", + mediaUrls: ["https://example.com/a.png", "https://example.com/b.png"], + }); }); it("skips plugin dispatch during dry-run sends and forwards gateway + silent to sendMessage", async () => {