peppa/packages/testkit/test/transcribe.test.ts
2026-11-18 18:02:09 +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: адрес сервиса Пеппы — язык 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));
});
});