Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

speech.test.ts

107 lines3.5 KB

1import * as Schema from "effect/Schema";
2import { expect, it } from "vite-plus/test";
3​
4import {
5 EnvironmentSpeechModel,
6 EnvironmentSpeechModelLinkRequest,
7 EnvironmentSpeechStatus,
8} from "./speech.ts";
9​
10const decodeStatus = Schema.decodeUnknownSync(EnvironmentSpeechStatus);
11const encodeStatus = Schema.encodeSync(EnvironmentSpeechStatus);
12const decodeModel = Schema.decodeUnknownSync(EnvironmentSpeechModel);
13const encodeModel = Schema.encodeSync(EnvironmentSpeechModel);
14​
15const legacyStatus = {
16 supported: true,
17 state: "ready",
18 modelId: "test",
19 model: "Test",
20 size: 100,
21 supportsStreaming: false,
22 supportsTranslation: false,
23 acceleration: "auto",
24 modelUnloadTimeout: "never",
25 gpuDevices: [],
26};
27const gpuDevices = [{ id: "gpu", name: "Remote GPU" }];
28​
29it.each([
30 { acceleration: "cpu", gpuDevices, tier: "cpu" },
31 { acceleration: "auto", gpuDevices: [], tier: "cpu" },
32 { acceleration: "auto", gpuDevices, tier: "accelerated" },
33 { acceleration: "gpu:gpu", gpuDevices: [], tier: "accelerated" },
34])("decodes legacy speech status for $acceleration with $gpuDevices", ({ tier, ...settings }) => {
35 const status = decodeStatus({
36 ...legacyStatus,
37 ...settings,
38 });
39 expect(status).toMatchObject({ ...settings, benchmarkTier: tier });
40 expect(encodeStatus(status)).toMatchObject({ benchmarkTier: tier });
41​
42 const fallback = { ...legacyStatus, ...settings, benchmarkTier: "cpu" };
43 expect(decodeStatus(fallback)).toEqual(fallback);
44 expect(() =>
45 decodeStatus({
46 ...legacyStatus,
47 benchmarkTier: "unknown",
48 }),
49 ).toThrow();
50});
51​
52it.each([
53 { wire: { secondsPer30s: 2.1 }, expected: { accelerated: 2.1 } },
54 { wire: { secondsPer30s: null }, expected: { accelerated: null } },
55 { wire: { secondsPer30s: 2.1, cpuSecondsPer30s: 5 }, expected: { cpu: 5, accelerated: 2.1 } },
56 {
57 wire: { secondsPer30s: null, cpuSecondsPer30s: null },
58 expected: { cpu: null, accelerated: null },
59 },
60 {
61 wire: { secondsPer30s: { cpu: 5, accelerated: 2.1 } },
62 expected: { cpu: 5, accelerated: 2.1 },
63 },
64 { wire: { secondsPer30s: { accelerated: 2.1 } }, expected: { accelerated: 2.1 } },
65])("keeps speech benchmark $wire compatible with old and new clients", ({ wire, expected }) => {
66 const model = {
67 id: "test",
68 name: "Test",
69 description: "",
70 size: 100,
71 languages: ["en"],
72 accuracy: 0,
73 speed: 0,
74 benchmark: { accuracy: { en: { grade: "A-", errorPercent: 4 } }, ...wire },
75 recommended: false,
76 supportsStreaming: false,
77 supportsLanguageDetection: false,
78 supportsTranslation: false,
79 active: true,
80 state: "installed",
81 };
82 const decoded = decodeModel(model);
83 expect(decoded.benchmark).toEqual({
84 accuracy: model.benchmark.accuracy,
85 secondsPer30s: expected,
86 });
87 const encoded = encodeModel(decoded);
88 const legacyBenchmark = Schema.Struct({ secondsPer30s: Schema.NullOr(Schema.Finite) });
89 expect(Schema.decodeUnknownSync(legacyBenchmark)(encoded.benchmark)).toEqual({
90 secondsPer30s: expected.accelerated,
91 });
92 expect(decodeModel(encoded)).toEqual(decoded);
93 for (const secondsPer30s of ["2.1", { cpu: 5 }, Number.NaN]) {
94 expect(() =>
95 decodeModel({
96 ...model,
97 benchmark: { ...model.benchmark, secondsPer30s },
98 }),
99 ).toThrow();
100 }
101});
102​
103it("keeps edge whitespace in model link paths and rejects blank ones", () => {
104 const decodeLink = Schema.decodeUnknownSync(EnvironmentSpeechModelLinkRequest);
105 expect(decodeLink({ path: " /tmp/models " })).toEqual({ path: " /tmp/models " });
106 expect(() => decodeLink({ path: " " })).toThrow();
107});