mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-15 12:00:43 +00:00
refactor: share migration and tts test helpers
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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<string, unknown>;
|
||||
expect(body.instructions).toBe("Speak warmly");
|
||||
});
|
||||
await expectTelephonyInstructions("gpt-4o-mini-tts", "Speak warmly");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user