test: clear cli message helper broad matchers

This commit is contained in:
Peter Steinberger
2026-05-10 14:23:18 +01:00
parent ce2d5093a0
commit 3c59cc4e67

View File

@@ -93,6 +93,28 @@ function expectNoAccountFieldInPassedOptions() {
expect(passedOpts).not.toHaveProperty("account");
}
function requireRecord(value: unknown, label: string): Record<string, unknown> {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
throw new Error(`expected ${label} to be an object`);
}
return value as Record<string, unknown>;
}
function expectMessageCommandOptions(expected: Record<string, unknown>, callIndex = 0): void {
const call = (messageCommandMock.mock.calls as unknown[][])[callIndex];
if (!call) {
throw new Error(`expected messageCommand call ${callIndex}`);
}
const options = requireRecord(call[0], `messageCommand options ${callIndex}`);
for (const [key, expectedValue] of Object.entries(expected)) {
expect(options[key], `messageCommand options.${key}`).toEqual(expectedValue);
}
expect(call[1], "messageCommand runtime").not.toBeNull();
expect(call[1], "messageCommand runtime").not.toBeUndefined();
expect(call[2], "messageCommand deps").not.toBeNull();
expect(call[2], "messageCommand deps").not.toBeUndefined();
}
describe("runMessageAction", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -141,16 +163,12 @@ describe("runMessageAction", () => {
await runSendAction({ target: "channel:12345" });
expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "send",
channel: "discord",
target: "channel:12345",
message: "hi",
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "send",
channel: "discord",
target: "channel:12345",
message: "hi",
});
expect(exitMock).toHaveBeenCalledWith(0);
});
@@ -168,15 +186,11 @@ describe("runMessageAction", () => {
scope: "configured-channels",
onlyChannelIds: ["telegram"],
});
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "broadcast",
targets: ["telegram:1", "telegram:2"],
message: "hi",
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "broadcast",
targets: ["telegram:1", "telegram:2"],
message: "hi",
});
});
it("keeps unknown actions on the local preload path", async () => {
@@ -194,13 +208,7 @@ describe("runMessageAction", () => {
scope: "configured-channels",
onlyChannelIds: ["discord"],
});
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "custom-action",
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({ action: "custom-action" });
});
it("preloads when the scoped channel plugin is not cheaply available", async () => {
@@ -218,15 +226,11 @@ describe("runMessageAction", () => {
await runSendAction({ channel: undefined, target: "telegram:12345" });
expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "send",
target: "telegram:12345",
message: "hi",
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "send",
target: "telegram:12345",
message: "hi",
});
expect(exitMock).toHaveBeenCalledWith(0);
});
@@ -242,21 +246,17 @@ describe("runMessageAction", () => {
});
expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "send",
channel: "telegram",
accountId: "default",
target: "@ops",
message: "hi",
media: "./diagram.png",
presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}',
delivery: '{"pin":true}',
forceDocument: true,
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "send",
channel: "telegram",
accountId: "default",
target: "@ops",
message: "hi",
media: "./diagram.png",
presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}',
delivery: '{"pin":true}',
forceDocument: true,
});
expectNoAccountFieldInPassedOptions();
expect(exitMock).toHaveBeenCalledWith(0);
});
@@ -419,17 +419,13 @@ describe("runMessageAction", () => {
}),
).rejects.toThrow("exit");
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "poll",
channel: "discord",
target: "456",
accountId: "acct-1",
message: "hi",
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "poll",
channel: "discord",
target: "456",
accountId: "acct-1",
message: "hi",
});
// account key should be stripped in favor of accountId
expectNoAccountFieldInPassedOptions();
});
@@ -446,16 +442,12 @@ describe("runMessageAction", () => {
}),
).rejects.toThrow("exit");
expect(messageCommandMock).toHaveBeenCalledWith(
expect.objectContaining({
action: "send",
channel: "discord",
target: "789",
accountId: undefined,
}),
expect.anything(),
expect.anything(),
);
expectMessageCommandOptions({
action: "send",
channel: "discord",
target: "789",
accountId: undefined,
});
expectNoAccountFieldInPassedOptions();
});
});