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

speech.test.ts

97 lines3.2 KB

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