Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, SPEECH_MAX_OPTIONS_BYTES } from "@t3tools/contracts";
3
4import {
5 decodeSpeechPcmRequest,
6 encodeSpeechPcmRequest,
7 resolveSpeechVocabulary,
8} from "./speech.ts";
9
10describe("speech PCM request framing", () => {
11 it("round-trips options and unaligned PCM without changing audio bytes", () => {
12 const pcm = new Uint8Array(new Float32Array([0.25, -0.5]).buffer);
13 const options = { ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, speechLanguage: "fr" };
14 const body = encodeSpeechPcmRequest(pcm, options);
15 const pool = new Uint8Array(body.length + 3);
16 pool.set(body, 3);
17 expect(decodeSpeechPcmRequest(pool.subarray(3))).toEqual({ pcm, options });
18 });
19
20 it("accepts recordings from clients that predate silence filtering", () => {
21 const metadata = new TextEncoder().encode(
22 JSON.stringify({
23 speechLanguage: "fr",
24 speechTranslateToEnglish: false,
25 speechCustomWords: [],
26 speechRemoveFillerWords: false,
27 }),
28 );
29 const body = new Uint8Array(4 + metadata.length + 4);
30 new DataView(body.buffer).setUint32(0, metadata.length, true);
31 body.set(metadata, 4);
32 expect(decodeSpeechPcmRequest(body).options).toEqual({
33 ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
34 speechLanguage: "fr",
35 speechRemoveFillerWords: false,
36 speechSilenceFiltering: true,
37 });
38 });
39
40 it("rejects truncated frames, oversized option lengths and missing audio", () => {
41 expect(() => decodeSpeechPcmRequest(new Uint8Array(3))).toThrow();
42 const oversized = new Uint8Array(5);
43 new DataView(oversized.buffer).setUint32(0, SPEECH_MAX_OPTIONS_BYTES + 1, true);
44 expect(() => decodeSpeechPcmRequest(oversized)).toThrow();
45 expect(() =>
46 decodeSpeechPcmRequest(
47 encodeSpeechPcmRequest(new Uint8Array(), DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS),
48 ),
49 ).toThrow();
50 });
51});
52
53describe("resolveSpeechVocabulary", () => {
54 it("prefers project spellings, keeps hints under the cap, and drops duplicates", () => {
55 const personal = Array.from({ length: 100 }, (_, index) => ({
56 term: `word${index}`,
57 aliases: index === 0 ? ["Effect"] : [],
58 }));
59 const resolved = resolveSpeechVocabulary({
60 personal,
61 project: [{ term: "effect", aliases: ["a fact"] }],
62 hints: [" T3 Code ", "word1", "", "x".repeat(51)],
63 });
64 expect(resolved).toHaveLength(100);
65 expect(resolved.slice(0, 3)).toEqual([
66 { term: "T3 Code", aliases: [] },
67 { term: "effect", aliases: ["a fact"] },
68 { term: "word0", aliases: [] },
69 ]);
70 expect(resolved.filter(({ term }) => term === "word1")).toHaveLength(1);
71 expect(resolved.at(-1)?.term).toBe("word97");
72 });
73});