peppa/packages/testkit/test/transcribe.test.ts
peppa-bot f3d10a4c25 PEP-243: платформа называется FORGE — название во всех текстах для людей и подсказках агентам, адрес forge.telecom.quest
«Peppa FACTORY» → «FORGE» в экранах, сообщениях, словарях en/kk, шаблоне и тестах; «Пеппа» → «FORGE» в подсказках
агентам и правилах; логотип, значок, версия кэша service worker; в словаре старые названия — в «Нельзя».
Внутренние имена (CLI peppa, PEPPA_*, MCP-сервер peppa, ключи PEP-N) не меняются. Второй адрес — в mac-route.sh.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 11:18:49 +05:00

59 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-108: the speech-to-text client shared by the Telegram bot and the web dictation route
// (core/transcribe.ts) against mock-transcribe (the OpenAI-compatible contract of
// infra/whisper/server.py).
import assert from "node:assert/strict";
import { after, before, describe, test } from "node:test";
import { transcribe, transcribeConfig, transcribeLanguage, TranscribeError } from "@peppa/core";
import { voiceStoriesEnabled } from "@peppa/shared";
import { MockTranscribe } from "../src/mock-transcribe.ts";
const stt = new MockTranscribe();
before(() => stt.start());
after(() => stt.stop());
describe("распознавание речи", () => {
test("Scenario: не настроено — голоса нет ни в боте, ни в вебе", () => {
assert.equal(transcribeConfig({}), null);
assert.equal(voiceStoriesEnabled({}), false);
assert.equal(voiceStoriesEnabled({ PEPPA_TRANSCRIBE_URL: "http://127.0.0.1:8178/v1" }), true);
});
test("Scenario: адрес сервиса FORGE — язык ru, таймаут 300 с по умолчанию, свой таймаут из env", () => {
const cfg = transcribeConfig({ PEPPA_TRANSCRIBE_URL: "http://127.0.0.1:8178/v1/", PEPPA_TRANSCRIBE_LANG: "ru" })!;
assert.deepEqual(cfg, { url: "http://127.0.0.1:8178/v1", key: null, model: "whisper-1", language: "ru", timeoutMs: 300_000 });
assert.equal(transcribeConfig({ PEPPA_TRANSCRIBE_URL: "http://x/v1", PEPPA_TRANSCRIBE_TIMEOUT_SEC: "30" })!.timeoutMs, 30_000);
});
test("Scenario: запись из браузера (webm) расшифровывается", async () => {
const cfg = transcribeConfig({ PEPPA_TRANSCRIBE_URL: stt.url, PEPPA_TRANSCRIBE_KEY: stt.key })!;
const text = await transcribe(cfg, MockTranscribe.speech("починить вход"), "dictation.webm", "audio/webm");
assert.equal(text, "починить вход");
assert.deepEqual(stt.requests.at(-1), { model: "whisper-1", language: "ru", filename: "dictation.webm", bytes: MockTranscribe.speech("починить вход").length });
});
test("Scenario Outline: язык записи по языку интерфейса (PEP-131)", async () => {
const url = { PEPPA_TRANSCRIBE_URL: stt.url, PEPPA_TRANSCRIBE_KEY: stt.key };
const cases: Array<[string | undefined, "ru" | "kk" | "en", string | null]> = [
[undefined, "ru", "ru"],
[undefined, "kk", "kk"],
[undefined, "en", "en"],
["ru", "kk", "kk"],
["uk", "ru", "uk"],
["auto", "kk", null],
["", "ru", null],
];
for (const [setting, locale, language] of cases) {
const cfg = transcribeConfig(setting === undefined ? url : { ...url, PEPPA_TRANSCRIBE_LANG: setting })!;
assert.equal(transcribeLanguage(cfg, locale), language, `${setting} / ${locale}`);
// …and that is what the service receives.
assert.equal(await transcribe(cfg, MockTranscribe.speech("экспорт қосу"), "voice.ogg", "audio/ogg", transcribeLanguage(cfg, locale)), "экспорт қосу");
assert.equal(stt.requests.at(-1)!.language, language, `${setting} / ${locale}`);
}
});
test("Scenario: сервис недоступен — понятная ошибка", async () => {
const cfg = transcribeConfig({ PEPPA_TRANSCRIBE_URL: "http://127.0.0.1:9/v1", PEPPA_TRANSCRIBE_TIMEOUT_SEC: "5" })!;
await assert.rejects(transcribe(cfg, Buffer.from("x")), (e: unknown) => e instanceof TranscribeError && /сервис расшифровки недоступен/.test((e as Error).message));
});
});