mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 07:02:44 +00:00
refactor(commands): dedupe message command secret-config tests
This commit is contained in:
@@ -186,26 +186,94 @@ const createTelegramPollPluginRegistration = () => ({
|
|||||||
|
|
||||||
const { messageCommand } = await import("./message.js");
|
const { messageCommand } = await import("./message.js");
|
||||||
|
|
||||||
|
function createTelegramSecretRawConfig() {
|
||||||
|
return {
|
||||||
|
channels: {
|
||||||
|
telegram: {
|
||||||
|
token: { $secret: "vault://telegram/token" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTelegramResolvedTokenConfig(token: string) {
|
||||||
|
return {
|
||||||
|
channels: {
|
||||||
|
telegram: {
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockResolvedCommandConfig(params: {
|
||||||
|
rawConfig: Record<string, unknown>;
|
||||||
|
resolvedConfig: Record<string, unknown>;
|
||||||
|
diagnostics?: string[];
|
||||||
|
}) {
|
||||||
|
testConfig = params.rawConfig;
|
||||||
|
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({
|
||||||
|
resolvedConfig: params.resolvedConfig,
|
||||||
|
diagnostics: params.diagnostics ?? ["resolved channels.telegram.token"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runTelegramDirectOutboundSend(params: {
|
||||||
|
rawConfig: Record<string, unknown>;
|
||||||
|
resolvedConfig: Record<string, unknown>;
|
||||||
|
diagnostics?: string[];
|
||||||
|
}) {
|
||||||
|
mockResolvedCommandConfig(params);
|
||||||
|
const sendText = vi.fn(async (_ctx: { cfg?: unknown; to?: string; text?: string }) => ({
|
||||||
|
channel: "telegram" as const,
|
||||||
|
messageId: "msg-1",
|
||||||
|
chatId: "123456",
|
||||||
|
}));
|
||||||
|
const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({
|
||||||
|
channel: "telegram" as const,
|
||||||
|
messageId: "msg-2",
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
|
||||||
|
return { sendText };
|
||||||
|
}
|
||||||
|
|
||||||
describe("messageCommand", () => {
|
describe("messageCommand", () => {
|
||||||
it("threads resolved SecretRef config into outbound send actions", async () => {
|
it("threads resolved SecretRef config into outbound send actions", async () => {
|
||||||
const rawConfig = {
|
const rawConfig = createTelegramSecretRawConfig();
|
||||||
channels: {
|
const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token");
|
||||||
telegram: {
|
mockResolvedCommandConfig({
|
||||||
token: { $secret: "vault://telegram/token" },
|
rawConfig: rawConfig as unknown as Record<string, unknown>,
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const resolvedConfig = {
|
|
||||||
channels: {
|
|
||||||
telegram: {
|
|
||||||
token: "12345:resolved-token",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
testConfig = rawConfig;
|
|
||||||
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({
|
|
||||||
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
|
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
|
||||||
diagnostics: ["resolved channels.telegram.token"],
|
|
||||||
});
|
});
|
||||||
await setRegistry(
|
await setRegistry(
|
||||||
createTestRegistry([
|
createTestRegistry([
|
||||||
@@ -240,64 +308,12 @@ describe("messageCommand", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("threads resolved SecretRef config into outbound adapter sends", async () => {
|
it("threads resolved SecretRef config into outbound adapter sends", async () => {
|
||||||
const rawConfig = {
|
const rawConfig = createTelegramSecretRawConfig();
|
||||||
channels: {
|
const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token");
|
||||||
telegram: {
|
const { sendText } = await runTelegramDirectOutboundSend({
|
||||||
token: { $secret: "vault://telegram/token" },
|
rawConfig: rawConfig as unknown as Record<string, unknown>,
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const resolvedConfig = {
|
|
||||||
channels: {
|
|
||||||
telegram: {
|
|
||||||
token: "12345:resolved-token",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
testConfig = rawConfig;
|
|
||||||
resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({
|
|
||||||
resolvedConfig: resolvedConfig as unknown as Record<string, unknown>,
|
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,
|
|
||||||
messageId: "msg-1",
|
|
||||||
chatId: "123456",
|
|
||||||
}));
|
|
||||||
const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({
|
|
||||||
channel: "telegram" as const,
|
|
||||||
messageId: "msg-2",
|
|
||||||
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({
|
||||||
@@ -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({
|
||||||
|
|||||||
Reference in New Issue
Block a user