test(discord): share successful dm button assertions

This commit is contained in:
Vincent Koc
2026-04-12 09:29:50 +01:00
parent 0dcc30b39c
commit 6750bc36dd

View File

@@ -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", () => {