diff --git a/extensions/discord/src/monitor/monitor.agent-components.test.ts b/extensions/discord/src/monitor/monitor.agent-components.test.ts index 6c6e6e35cfa..e571b12d777 100644 --- a/extensions/discord/src/monitor/monitor.agent-components.test.ts +++ b/extensions/discord/src/monitor/monitor.agent-components.test.ts @@ -99,6 +99,34 @@ describe("agent components", () => { }; }; + async function expectSuccessfulDmButtonInteraction(params: { + dmPolicy: "pairing" | "open"; + expectPairingStoreRead: boolean; + }) { + const button = createAgentComponentButton({ + cfg: createCfg(), + accountId: "default", + dmPolicy: params.dmPolicy, + }); + const { interaction, defer, reply } = createDmButtonInteraction(); + + await button.run(interaction, { componentId: "hello" } as ComponentData); + + expect(defer).not.toHaveBeenCalled(); + expect(reply).toHaveBeenCalledWith({ content: "✓", ephemeral: true }); + expect(enqueueSystemEventMock).toHaveBeenCalledWith( + "[Discord component: hello clicked by Alice#1234 (123456789)]", + expect.objectContaining({ + sessionKey: defaultDmSessionKey, + }), + ); + if (params.expectPairingStoreRead) { + expect(readAllowFromStoreMock).toHaveBeenCalledWith("discord", "default"); + } else { + expect(readAllowFromStoreMock).not.toHaveBeenCalled(); + } + } + beforeEach(() => { resetDiscordComponentRuntimeMocks(); resetSystemEventsForTest(); @@ -220,47 +248,19 @@ describe("agent components", () => { it("authorizes DM interactions from pairing-store entries in pairing mode", async () => { readAllowFromStoreMock.mockResolvedValue(["123456789"]); - const button = createAgentComponentButton({ - cfg: createCfg(), - accountId: "default", + await expectSuccessfulDmButtonInteraction({ dmPolicy: "pairing", + expectPairingStoreRead: true, }); - const { interaction, defer, reply } = createDmButtonInteraction(); - - await button.run(interaction, { componentId: "hello" } as ComponentData); - - expect(defer).not.toHaveBeenCalled(); - expect(reply).toHaveBeenCalledWith({ content: "✓", ephemeral: true }); - expect(enqueueSystemEventMock).toHaveBeenCalledWith( - "[Discord component: hello clicked by Alice#1234 (123456789)]", - expect.objectContaining({ - sessionKey: defaultDmSessionKey, - }), - ); expect(upsertPairingRequestMock).not.toHaveBeenCalled(); - expect(readAllowFromStoreMock).toHaveBeenCalledWith("discord", "default"); }); it("allows DM component interactions in open mode without reading pairing store", async () => { readAllowFromStoreMock.mockResolvedValue(["123456789"]); - const button = createAgentComponentButton({ - cfg: createCfg(), - accountId: "default", + await expectSuccessfulDmButtonInteraction({ dmPolicy: "open", + expectPairingStoreRead: false, }); - const { interaction, defer, reply } = createDmButtonInteraction(); - - await button.run(interaction, { componentId: "hello" } as ComponentData); - - expect(defer).not.toHaveBeenCalled(); - expect(reply).toHaveBeenCalledWith({ content: "✓", ephemeral: true }); - expect(enqueueSystemEventMock).toHaveBeenCalledWith( - "[Discord component: hello clicked by Alice#1234 (123456789)]", - expect.objectContaining({ - sessionKey: defaultDmSessionKey, - }), - ); - expect(readAllowFromStoreMock).not.toHaveBeenCalled(); }); it("uses user conversation ids for direct-message component originating targets", () => {