|
1 | | -import { beforeAll, describe, expect, it, vi } from "vitest"; |
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { TELEGRAM_COMMAND_NAME_PATTERN as bundledTelegramCommandNamePattern } from "../../extensions/telegram/src/command-config.ts"; |
| 3 | + |
| 4 | +type BundledChannelContractSurfaceParams = Parameters< |
| 5 | + (typeof import("../channels/plugins/contract-surfaces.js"))["getBundledChannelContractSurfaceModule"] |
| 6 | +>[0]; |
| 7 | + |
| 8 | +type TelegramCommandConfigContract = Pick< |
| 9 | + typeof import("./telegram-command-config.js"), |
| 10 | + | "TELEGRAM_COMMAND_NAME_PATTERN" |
| 11 | + | "normalizeTelegramCommandName" |
| 12 | + | "normalizeTelegramCommandDescription" |
| 13 | + | "resolveTelegramCustomCommands" |
| 14 | +>; |
| 15 | + |
| 16 | +const getBundledChannelContractSurfaceModule = vi.fn< |
| 17 | + (params: BundledChannelContractSurfaceParams) => TelegramCommandConfigContract | null |
| 18 | +>(() => null); |
2 | 19 |
|
3 | 20 | vi.mock("../channels/plugins/contract-surfaces.js", () => ({ |
4 | | - getBundledChannelContractSurfaceModule: vi.fn(() => null), |
| 21 | + getBundledChannelContractSurfaceModule, |
5 | 22 | })); |
6 | 23 |
|
7 | | -let telegramCommandConfig: typeof import("./telegram-command-config.js"); |
8 | | - |
9 | | -beforeAll(async () => { |
| 24 | +async function loadTelegramCommandConfig() { |
10 | 25 | vi.resetModules(); |
11 | | - telegramCommandConfig = await import("./telegram-command-config.js"); |
12 | | -}); |
| 26 | + getBundledChannelContractSurfaceModule.mockClear(); |
| 27 | + return import("./telegram-command-config.js"); |
| 28 | +} |
13 | 29 |
|
14 | 30 | describe("telegram command config fallback", () => { |
15 | | - it("keeps command validation available when the bundled contract surface is unavailable", () => { |
| 31 | + it("keeps the fallback regex in parity with the bundled telegram contract", async () => { |
| 32 | + const telegramCommandConfig = await loadTelegramCommandConfig(); |
| 33 | + |
| 34 | + expect(telegramCommandConfig.TELEGRAM_COMMAND_NAME_PATTERN.toString()).toBe( |
| 35 | + bundledTelegramCommandNamePattern.toString(), |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it("keeps import-time regex access side-effect free", async () => { |
| 40 | + const telegramCommandConfig = await loadTelegramCommandConfig(); |
| 41 | + |
| 42 | + expect(getBundledChannelContractSurfaceModule).not.toHaveBeenCalled(); |
16 | 43 | expect(telegramCommandConfig.TELEGRAM_COMMAND_NAME_PATTERN.test("hello_world")).toBe(true); |
17 | | - expect(telegramCommandConfig.normalizeTelegramCommandName("/Hello-World")).toBe( |
18 | | - "hello_world", |
| 44 | + expect(getBundledChannelContractSurfaceModule).not.toHaveBeenCalled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("lazy-loads the contract pattern only when callers opt in", async () => { |
| 48 | + const contractPattern = /^[a-z]+$/; |
| 49 | + getBundledChannelContractSurfaceModule.mockReturnValueOnce({ |
| 50 | + TELEGRAM_COMMAND_NAME_PATTERN: contractPattern, |
| 51 | + normalizeTelegramCommandName: (value: string) => `contract:${value.trim().toLowerCase()}`, |
| 52 | + normalizeTelegramCommandDescription: (value: string) => `desc:${value.trim()}`, |
| 53 | + resolveTelegramCustomCommands: () => ({ |
| 54 | + commands: [{ command: "from_contract", description: "from contract" }], |
| 55 | + issues: [], |
| 56 | + }), |
| 57 | + }); |
| 58 | + const telegramCommandConfig = await loadTelegramCommandConfig(); |
| 59 | + |
| 60 | + expect(getBundledChannelContractSurfaceModule).not.toHaveBeenCalled(); |
| 61 | + expect(telegramCommandConfig.TELEGRAM_COMMAND_NAME_PATTERN.test("hello_world")).toBe(true); |
| 62 | + expect(getBundledChannelContractSurfaceModule).not.toHaveBeenCalled(); |
| 63 | + expect(telegramCommandConfig.getTelegramCommandNamePattern()).toBe(contractPattern); |
| 64 | + expect(getBundledChannelContractSurfaceModule).toHaveBeenCalledTimes(1); |
| 65 | + expect(telegramCommandConfig.getTelegramCommandNamePattern()).toBe( |
| 66 | + telegramCommandConfig.getTelegramCommandNamePattern(), |
19 | 67 | ); |
| 68 | + expect(telegramCommandConfig.normalizeTelegramCommandName(" Hello ")).toBe("contract:hello"); |
| 69 | + expect(telegramCommandConfig.normalizeTelegramCommandDescription(" hi ")).toBe("desc:hi"); |
| 70 | + expect( |
| 71 | + telegramCommandConfig.resolveTelegramCustomCommands({ |
| 72 | + commands: [{ command: "/ignored", description: "ignored" }], |
| 73 | + }), |
| 74 | + ).toEqual({ |
| 75 | + commands: [{ command: "from_contract", description: "from contract" }], |
| 76 | + issues: [], |
| 77 | + }); |
| 78 | + expect(getBundledChannelContractSurfaceModule).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it("keeps command validation available when the bundled contract surface is unavailable", async () => { |
| 82 | + const telegramCommandConfig = await loadTelegramCommandConfig(); |
| 83 | + |
| 84 | + expect(telegramCommandConfig.TELEGRAM_COMMAND_NAME_PATTERN.test("hello_world")).toBe(true); |
| 85 | + expect(telegramCommandConfig.normalizeTelegramCommandName("/Hello-World")).toBe("hello_world"); |
20 | 86 | expect(telegramCommandConfig.normalizeTelegramCommandDescription(" hi ")).toBe("hi"); |
21 | 87 |
|
22 | 88 | expect( |
@@ -48,5 +114,6 @@ describe("telegram command config fallback", () => { |
48 | 114 | }, |
49 | 115 | ], |
50 | 116 | }); |
| 117 | + expect(getBundledChannelContractSurfaceModule).toHaveBeenCalledTimes(1); |
51 | 118 | }); |
52 | 119 | }); |
0 commit comments