refactor(commands): dedupe message command secret-config tests

This commit is contained in:
Peter Steinberger
2026-03-07 17:03:22 +00:00
parent 0a73328053
commit 8e6acded82

View File

@@ -186,80 +186,45 @@ const createTelegramPollPluginRegistration = () => ({
const { messageCommand } = await import("./message.js"); const { messageCommand } = await import("./message.js");
describe("messageCommand", () => { function createTelegramSecretRawConfig() {
it("threads resolved SecretRef config into outbound send actions", async () => { return {
const rawConfig = {
channels: { channels: {
telegram: { telegram: {
token: { $secret: "vault://telegram/token" }, token: { $secret: "vault://telegram/token" },
}, },
}, },
}; };
const resolvedConfig = { }
function createTelegramResolvedTokenConfig(token: string) {
return {
channels: { channels: {
telegram: { telegram: {
token: "12345:resolved-token", token,
}, },
}, },
}; };
testConfig = rawConfig; }
function mockResolvedCommandConfig(params: {
rawConfig: Record<string, unknown>;
resolvedConfig: Record<string, unknown>;
diagnostics?: string[];
}) {
testConfig = params.rawConfig;
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>, resolvedConfig: params.resolvedConfig,
diagnostics: ["resolved channels.telegram.token"], diagnostics: params.diagnostics ?? ["resolved channels.telegram.token"],
}); });
await setRegistry( }
createTestRegistry([
{
...createTelegramSendPluginRegistration(),
},
]),
);
const deps = makeDeps(); async function runTelegramDirectOutboundSend(params: {
await messageCommand( rawConfig: Record<string, unknown>;
{ resolvedConfig: Record<string, unknown>;
action: "send", diagnostics?: string[];
channel: "telegram", }) {
target: "123456", mockResolvedCommandConfig(params);
message: "hi", const sendText = vi.fn(async (_ctx: { cfg?: unknown; to?: string; text?: string }) => ({
},
deps,
runtime,
);
expect(resolveCommandSecretRefsViaGateway).toHaveBeenCalledWith(
expect.objectContaining({
config: rawConfig,
commandName: "message",
}),
);
expect(handleTelegramAction).toHaveBeenCalledWith(
expect.objectContaining({ action: "send", to: "123456", accountId: undefined }),
resolvedConfig,
);
});
it("threads resolved SecretRef config into outbound adapter sends", async () => {
const rawConfig = {
channels: {
telegram: {
token: { $secret: "vault://telegram/token" },
},
},
};
const resolvedConfig = {
channels: {
telegram: {
token: "12345:resolved-token",
},
},
};
testConfig = rawConfig;
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
diagnostics: ["resolved channels.telegram.token"],
});
const sendText = vi.fn(async (_ctx: { cfg?: unknown; to: string; text: string }) => ({
channel: "telegram" as const, channel: "telegram" as const,
messageId: "msg-1", messageId: "msg-1",
chatId: "123456", chatId: "123456",
@@ -299,6 +264,57 @@ describe("messageCommand", () => {
runtime, runtime,
); );
return { sendText };
}
describe("messageCommand", () => {
it("threads resolved SecretRef config into outbound send actions", async () => {
const rawConfig = createTelegramSecretRawConfig();
const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token");
mockResolvedCommandConfig({
rawConfig: rawConfig as unknown as Record<string, unknown>,
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
});
await setRegistry(
createTestRegistry([
{
...createTelegramSendPluginRegistration(),
},
]),
);
const deps = makeDeps();
await messageCommand(
{
action: "send",
channel: "telegram",
target: "123456",
message: "hi",
},
deps,
runtime,
);
expect(resolveCommandSecretRefsViaGateway).toHaveBeenCalledWith(
expect.objectContaining({
config: rawConfig,
commandName: "message",
}),
);
expect(handleTelegramAction).toHaveBeenCalledWith(
expect.objectContaining({ action: "send", to: "123456", accountId: undefined }),
resolvedConfig,
);
});
it("threads resolved SecretRef config into outbound adapter sends", async () => {
const rawConfig = createTelegramSecretRawConfig();
const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token");
const { sendText } = await runTelegramDirectOutboundSend({
rawConfig: rawConfig as unknown as Record<string, unknown>,
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
});
expect(sendText).toHaveBeenCalledWith( expect(sendText).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
cfg: resolvedConfig, cfg: resolvedConfig,
@@ -324,50 +340,11 @@ describe("messageCommand", () => {
}, },
}, },
}; };
testConfig = rawConfig; const { sendText } = await runTelegramDirectOutboundSend({
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ rawConfig: rawConfig as unknown as Record<string, unknown>,
resolvedConfig: locallyResolvedConfig as unknown as Record<string, unknown>, resolvedConfig: locallyResolvedConfig as unknown as Record<string, unknown>,
diagnostics: ["gateway secrets.resolve unavailable; used local resolver fallback."], diagnostics: ["gateway secrets.resolve unavailable; used local resolver fallback."],
}); });
const sendText = vi.fn(async (_ctx: { cfg?: unknown }) => ({
channel: "telegram" as const,
messageId: "msg-3",
chatId: "123456",
}));
const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({
channel: "telegram" as const,
messageId: "msg-4",
chatId: "123456",
}));
await setRegistry(
createTestRegistry([
{
pluginId: "telegram",
source: "test",
plugin: createStubPlugin({
id: "telegram",
label: "Telegram",
outbound: {
deliveryMode: "direct",
sendText,
sendMedia,
},
}),
},
]),
);
const deps = makeDeps();
await messageCommand(
{
action: "send",
channel: "telegram",
target: "123456",
message: "hi",
},
deps,
runtime,
);
expect(sendText).toHaveBeenCalledWith( expect(sendText).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({