refactor(channels): dedupe slack telegram and web monitor tests

This commit is contained in:
Peter Steinberger
2026-03-02 21:31:26 +00:00
parent 34daed1d1e
commit 6a425d189e
6 changed files with 122 additions and 217 deletions

View File

@@ -6,6 +6,31 @@ import {
syncTelegramMenuCommands,
} from "./bot-native-command-menu.js";
type SyncMenuOptions = {
deleteMyCommands: ReturnType<typeof vi.fn>;
setMyCommands: ReturnType<typeof vi.fn>;
commandsToRegister: Parameters<typeof syncTelegramMenuCommands>[0]["commandsToRegister"];
accountId: string;
botIdentity: string;
runtimeLog?: ReturnType<typeof vi.fn>;
};
function syncMenuCommandsWithMocks(options: SyncMenuOptions): void {
syncTelegramMenuCommands({
bot: {
api: { deleteMyCommands: options.deleteMyCommands, setMyCommands: options.setMyCommands },
} as unknown as Parameters<typeof syncTelegramMenuCommands>[0]["bot"],
runtime: {
log: options.runtimeLog ?? vi.fn(),
error: vi.fn(),
exit: vi.fn(),
} as Parameters<typeof syncTelegramMenuCommands>[0]["runtime"],
commandsToRegister: options.commandsToRegister,
accountId: options.accountId,
botIdentity: options.botIdentity,
});
}
describe("bot-native-command-menu", () => {
it("caps menu entries to Telegram limit", () => {
const allCommands = Array.from({ length: 105 }, (_, i) => ({
@@ -91,14 +116,9 @@ describe("bot-native-command-menu", () => {
callOrder.push("set");
});
syncTelegramMenuCommands({
bot: {
api: {
deleteMyCommands,
setMyCommands,
},
} as unknown as Parameters<typeof syncTelegramMenuCommands>[0]["bot"],
runtime: {} as Parameters<typeof syncTelegramMenuCommands>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
commandsToRegister: [{ command: "cmd", description: "Command" }],
accountId: `test-delete-${Date.now()}`,
botIdentity: "bot-a",
@@ -136,13 +156,10 @@ describe("bot-native-command-menu", () => {
const commands = [{ command: "skip_test", description: "Skip test command" }];
// First sync — no cached hash, should call setMyCommands.
syncTelegramMenuCommands({
bot: {
api: { deleteMyCommands, setMyCommands },
} as unknown as Parameters<typeof syncTelegramMenuCommands>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: commands,
accountId,
botIdentity: "bot-a",
@@ -153,13 +170,10 @@ describe("bot-native-command-menu", () => {
});
// Second sync with the same commands — hash is cached, should skip.
syncTelegramMenuCommands({
bot: {
api: { deleteMyCommands, setMyCommands },
} as unknown as Parameters<typeof syncTelegramMenuCommands>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: commands,
accountId,
botIdentity: "bot-a",
@@ -180,26 +194,20 @@ describe("bot-native-command-menu", () => {
const accountId = `test-bot-identity-${Date.now()}`;
const commands = [{ command: "same", description: "Same" }];
syncTelegramMenuCommands({
bot: { api: { deleteMyCommands, setMyCommands } } as unknown as Parameters<
typeof syncTelegramMenuCommands
>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: commands,
accountId,
botIdentity: "token-bot-a",
});
await vi.waitFor(() => expect(setMyCommands).toHaveBeenCalledTimes(1));
syncTelegramMenuCommands({
bot: { api: { deleteMyCommands, setMyCommands } } as unknown as Parameters<
typeof syncTelegramMenuCommands
>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: commands,
accountId,
botIdentity: "token-bot-b",
@@ -217,26 +225,20 @@ describe("bot-native-command-menu", () => {
const runtimeLog = vi.fn();
const accountId = `test-empty-delete-fail-${Date.now()}`;
syncTelegramMenuCommands({
bot: { api: { deleteMyCommands, setMyCommands } } as unknown as Parameters<
typeof syncTelegramMenuCommands
>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: [],
accountId,
botIdentity: "bot-a",
});
await vi.waitFor(() => expect(deleteMyCommands).toHaveBeenCalledTimes(1));
syncTelegramMenuCommands({
bot: { api: { deleteMyCommands, setMyCommands } } as unknown as Parameters<
typeof syncTelegramMenuCommands
>[0]["bot"],
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() } as Parameters<
typeof syncTelegramMenuCommands
>[0]["runtime"],
syncMenuCommandsWithMocks({
deleteMyCommands,
setMyCommands,
runtimeLog,
commandsToRegister: [],
accountId,
botIdentity: "bot-a",