diff --git a/src/channels/model-overrides.test.ts b/src/channels/model-overrides.test.ts index cffdc45c18c..df10a468468 100644 --- a/src/channels/model-overrides.test.ts +++ b/src/channels/model-overrides.test.ts @@ -3,65 +3,67 @@ import type { OpenClawConfig } from "../config/config.js"; import { resolveChannelModelOverride } from "./model-overrides.js"; describe("resolveChannelModelOverride", () => { - it("matches parent group id when topic suffix is present", () => { - const cfg = { - channels: { - modelByChannel: { - telegram: { - "-100123": "openai/gpt-4.1", + const cases = [ + { + name: "matches parent group id when topic suffix is present", + input: { + cfg: { + channels: { + modelByChannel: { + telegram: { + "-100123": "openai/gpt-4.1", + }, + }, }, - }, + } as unknown as OpenClawConfig, + channel: "telegram", + groupId: "-100123:topic:99", }, - } as unknown as OpenClawConfig; - const resolved = resolveChannelModelOverride({ - cfg, - channel: "telegram", - groupId: "-100123:topic:99", - }); - - expect(resolved?.model).toBe("openai/gpt-4.1"); - expect(resolved?.matchKey).toBe("-100123"); - }); - - it("prefers topic-specific match over parent group id", () => { - const cfg = { - channels: { - modelByChannel: { - telegram: { - "-100123": "openai/gpt-4.1", - "-100123:topic:99": "anthropic/claude-sonnet-4-6", + expected: { model: "openai/gpt-4.1", matchKey: "-100123" }, + }, + { + name: "prefers topic-specific match over parent group id", + input: { + cfg: { + channels: { + modelByChannel: { + telegram: { + "-100123": "openai/gpt-4.1", + "-100123:topic:99": "anthropic/claude-sonnet-4-6", + }, + }, }, - }, + } as unknown as OpenClawConfig, + channel: "telegram", + groupId: "-100123:topic:99", }, - } as unknown as OpenClawConfig; - const resolved = resolveChannelModelOverride({ - cfg, - channel: "telegram", - groupId: "-100123:topic:99", - }); - - expect(resolved?.model).toBe("anthropic/claude-sonnet-4-6"); - expect(resolved?.matchKey).toBe("-100123:topic:99"); - }); - - it("falls back to parent session key when thread id does not match", () => { - const cfg = { - channels: { - modelByChannel: { - discord: { - "123": "openai/gpt-4.1", + expected: { model: "anthropic/claude-sonnet-4-6", matchKey: "-100123:topic:99" }, + }, + { + name: "falls back to parent session key when thread id does not match", + input: { + cfg: { + channels: { + modelByChannel: { + discord: { + "123": "openai/gpt-4.1", + }, + }, }, - }, + } as unknown as OpenClawConfig, + channel: "discord", + groupId: "999", + parentSessionKey: "agent:main:discord:channel:123:thread:456", }, - } as unknown as OpenClawConfig; - const resolved = resolveChannelModelOverride({ - cfg, - channel: "discord", - groupId: "999", - parentSessionKey: "agent:main:discord:channel:123:thread:456", - }); + expected: { model: "openai/gpt-4.1", matchKey: "123" }, + }, + ] as const; - expect(resolved?.model).toBe("openai/gpt-4.1"); - expect(resolved?.matchKey).toBe("123"); - }); + for (const testCase of cases) { + it(testCase.name, () => { + const resolved = resolveChannelModelOverride(testCase.input); + expect(resolved?.model).toBe(testCase.expected.model); + expect(resolved?.matchKey).toBe(testCase.expected.matchKey); + }); + } }); diff --git a/src/channels/telegram/allow-from.test.ts b/src/channels/telegram/allow-from.test.ts index eb60e9481e6..83801d558f7 100644 --- a/src/channels/telegram/allow-from.test.ts +++ b/src/channels/telegram/allow-from.test.ts @@ -3,14 +3,24 @@ import { isNumericTelegramUserId, normalizeTelegramAllowFromEntry } from "./allo describe("telegram allow-from helpers", () => { it("normalizes tg/telegram prefixes", () => { - expect(normalizeTelegramAllowFromEntry(" TG:123 ")).toBe("123"); - expect(normalizeTelegramAllowFromEntry("telegram:@someone")).toBe("@someone"); + const cases = [ + { value: " TG:123 ", expected: "123" }, + { value: "telegram:@someone", expected: "@someone" }, + ] as const; + for (const testCase of cases) { + expect(normalizeTelegramAllowFromEntry(testCase.value)).toBe(testCase.expected); + } }); it("accepts signed numeric IDs", () => { - expect(isNumericTelegramUserId("123456789")).toBe(true); - expect(isNumericTelegramUserId("-1001234567890")).toBe(true); - expect(isNumericTelegramUserId("@someone")).toBe(false); - expect(isNumericTelegramUserId("12 34")).toBe(false); + const cases = [ + { value: "123456789", expected: true }, + { value: "-1001234567890", expected: true }, + { value: "@someone", expected: false }, + { value: "12 34", expected: false }, + ] as const; + for (const testCase of cases) { + expect(isNumericTelegramUserId(testCase.value)).toBe(testCase.expected); + } }); }); diff --git a/src/channels/telegram/api.test.ts b/src/channels/telegram/api.test.ts index cb322289305..caab59b7ec0 100644 --- a/src/channels/telegram/api.test.ts +++ b/src/channels/telegram/api.test.ts @@ -2,55 +2,56 @@ import { describe, expect, it, vi } from "vitest"; import { fetchTelegramChatId } from "./api.js"; describe("fetchTelegramChatId", () => { - it("returns stringified id when Telegram getChat succeeds", async () => { + const cases = [ + { + name: "returns stringified id when Telegram getChat succeeds", + fetchImpl: vi.fn(async () => ({ + ok: true, + json: async () => ({ ok: true, result: { id: 12345 } }), + })), + expected: "12345", + }, + { + name: "returns null when response is not ok", + fetchImpl: vi.fn(async () => ({ + ok: false, + json: async () => ({}), + })), + expected: null, + }, + { + name: "returns null on transport failures", + fetchImpl: vi.fn(async () => { + throw new Error("network failed"); + }), + expected: null, + }, + ] as const; + + for (const testCase of cases) { + it(testCase.name, async () => { + vi.stubGlobal("fetch", testCase.fetchImpl); + + const id = await fetchTelegramChatId({ + token: "abc", + chatId: "@user", + }); + + expect(id).toBe(testCase.expected); + }); + } + + it("calls Telegram getChat endpoint", async () => { const fetchMock = vi.fn(async () => ({ ok: true, json: async () => ({ ok: true, result: { id: 12345 } }), })); vi.stubGlobal("fetch", fetchMock); - const id = await fetchTelegramChatId({ - token: "abc", - chatId: "@user", - }); - - expect(id).toBe("12345"); + await fetchTelegramChatId({ token: "abc", chatId: "@user" }); expect(fetchMock).toHaveBeenCalledWith( "https://api.telegram.org/botabc/getChat?chat_id=%40user", undefined, ); }); - - it("returns null when response is not ok", async () => { - vi.stubGlobal( - "fetch", - vi.fn(async () => ({ - ok: false, - json: async () => ({}), - })), - ); - - const id = await fetchTelegramChatId({ - token: "abc", - chatId: "@user", - }); - - expect(id).toBeNull(); - }); - - it("returns null on transport failures", async () => { - vi.stubGlobal( - "fetch", - vi.fn(async () => { - throw new Error("network failed"); - }), - ); - - const id = await fetchTelegramChatId({ - token: "abc", - chatId: "@user", - }); - - expect(id).toBeNull(); - }); });