39 lines
2.4 KiB
TypeScript
39 lines
2.4 KiB
TypeScript
// 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, 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: сервис недоступен — понятная ошибка", 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));
|
||
});
|
||
});
|