From 8ddaca1763b81ea972df226ee98c97eeef99da33 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 18:58:37 +0000 Subject: [PATCH] refactor: share migration and tts test helpers --- src/commands/doctor-state-migrations.test.ts | 41 +++++------ src/tts/tts.test.ts | 71 ++++++++------------ 2 files changed, 44 insertions(+), 68 deletions(-) diff --git a/src/commands/doctor-state-migrations.test.ts b/src/commands/doctor-state-migrations.test.ts index 4116a6fca6e..d6541c74577 100644 --- a/src/commands/doctor-state-migrations.test.ts +++ b/src/commands/doctor-state-migrations.test.ts @@ -26,6 +26,21 @@ async function makeRootWithEmptyCfg() { return { root, cfg }; } +function writeLegacyTelegramAllowFromStore(oauthDir: string) { + fs.writeFileSync( + path.join(oauthDir, "telegram-allowFrom.json"), + JSON.stringify( + { + version: 1, + allowFrom: ["123456"], + }, + null, + 2, + ) + "\n", + "utf-8", + ); +} + afterEach(async () => { resetAutoMigrateLegacyStateForTest(); resetAutoMigrateLegacyStateDirForTest(); @@ -278,18 +293,7 @@ describe("doctor legacy state migrations", () => { it("migrates legacy Telegram pairing allowFrom store to account-scoped default file", async () => { const { root, cfg } = await makeRootWithEmptyCfg(); const oauthDir = ensureCredentialsDir(root); - fs.writeFileSync( - path.join(oauthDir, "telegram-allowFrom.json"), - JSON.stringify( - { - version: 1, - allowFrom: ["123456"], - }, - null, - 2, - ) + "\n", - "utf-8", - ); + writeLegacyTelegramAllowFromStore(oauthDir); const detected = await detectLegacyStateMigrations({ cfg, @@ -324,18 +328,7 @@ describe("doctor legacy state migrations", () => { }, }; const oauthDir = ensureCredentialsDir(root); - fs.writeFileSync( - path.join(oauthDir, "telegram-allowFrom.json"), - JSON.stringify( - { - version: 1, - allowFrom: ["123456"], - }, - null, - 2, - ) + "\n", - "utf-8", - ); + writeLegacyTelegramAllowFromStore(oauthDir); const detected = await detectLegacyStateMigrations({ cfg, diff --git a/src/tts/tts.test.ts b/src/tts/tts.test.ts index eedc325fd4f..b326b4835e5 100644 --- a/src/tts/tts.test.ts +++ b/src/tts/tts.test.ts @@ -91,6 +91,22 @@ const mockAssistantMessage = (content: AssistantMessage["content"]): AssistantMe timestamp: Date.now(), }); +function createOpenAiTelephonyCfg(model: "tts-1" | "gpt-4o-mini-tts"): OpenClawConfig { + return { + messages: { + tts: { + provider: "openai", + openai: { + apiKey: "test-key", + model, + voice: "alloy", + instructions: "Speak warmly", + }, + }, + }, + }; +} + describe("tts", () => { beforeEach(() => { vi.clearAllMocks(); @@ -592,25 +608,14 @@ describe("tts", () => { } }; - it("omits instructions for unsupported speech models", async () => { - const cfg: OpenClawConfig = { - messages: { - tts: { - provider: "openai", - openai: { - apiKey: "test-key", - model: "tts-1", - voice: "alloy", - instructions: "Speak warmly", - }, - }, - }, - }; - + async function expectTelephonyInstructions( + model: "tts-1" | "gpt-4o-mini-tts", + expectedInstructions: string | undefined, + ) { await withMockedTelephonyFetch(async (fetchMock) => { const result = await tts.textToSpeechTelephony({ text: "Hello there, friendly caller.", - cfg, + cfg: createOpenAiTelephonyCfg(model), }); expect(result.success).toBe(true); @@ -618,38 +623,16 @@ describe("tts", () => { const [, init] = fetchMock.mock.calls[0] as [string, RequestInit]; expect(typeof init.body).toBe("string"); const body = JSON.parse(init.body as string) as Record; - expect(body.instructions).toBeUndefined(); + expect(body.instructions).toBe(expectedInstructions); }); + } + + it("omits instructions for unsupported speech models", async () => { + await expectTelephonyInstructions("tts-1", undefined); }); it("includes instructions for gpt-4o-mini-tts", async () => { - const cfg: OpenClawConfig = { - messages: { - tts: { - provider: "openai", - openai: { - apiKey: "test-key", - model: "gpt-4o-mini-tts", - voice: "alloy", - instructions: "Speak warmly", - }, - }, - }, - }; - - await withMockedTelephonyFetch(async (fetchMock) => { - const result = await tts.textToSpeechTelephony({ - text: "Hello there, friendly caller.", - cfg, - }); - - expect(result.success).toBe(true); - expect(fetchMock).toHaveBeenCalledTimes(1); - const [, init] = fetchMock.mock.calls[0] as [string, RequestInit]; - expect(typeof init.body).toBe("string"); - const body = JSON.parse(init.body as string) as Record; - expect(body.instructions).toBe("Speak warmly"); - }); + await expectTelephonyInstructions("gpt-4o-mini-tts", "Speak warmly"); }); });