|
| 1 | +import type { VoiceInputConfigUpdateFork } from "@t3tools/contracts"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Option from "effect/Option"; |
| 6 | +import * as HttpClient from "effect/http/HttpClient"; |
| 7 | +import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | +import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 9 | +import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 10 | +import * as VoiceInput from "./VoiceInput.fork.ts"; |
| 11 | + |
| 12 | +function recording(rate = 16000) { |
| 13 | + const audio = new Uint8Array(44 + rate * 2); |
| 14 | + const view = new DataView(audio.buffer); |
| 15 | + const tag = (at: number, value: string) => audio.set(new TextEncoder().encode(value), at); |
| 16 | + tag(0, "RIFF"); |
| 17 | + view.setUint32(4, audio.length - 8, true); |
| 18 | + tag(8, "WAVE"); |
| 19 | + tag(12, "fmt "); |
| 20 | + view.setUint32(16, 16, true); |
| 21 | + view.setUint16(20, 1, true); |
| 22 | + view.setUint16(22, 1, true); |
| 23 | + view.setUint32(24, rate, true); |
| 24 | + view.setUint32(28, rate * 2, true); |
| 25 | + view.setUint16(32, 2, true); |
| 26 | + view.setUint16(34, 16, true); |
| 27 | + tag(36, "data"); |
| 28 | + view.setUint32(40, rate * 2, true); |
| 29 | + return audio; |
| 30 | +} |
| 31 | +const local: VoiceInputConfigUpdateFork = { |
| 32 | + enabled: true, |
| 33 | + provider: "local", |
| 34 | + endpoint: "http://127.0.0.1:8788/transcribe", |
| 35 | + model: "", |
| 36 | +}; |
| 37 | + |
| 38 | +function harness(body: unknown = { text: " spoken words " }, status = 200) { |
| 39 | + const stored = new Map<string, Uint8Array>(); |
| 40 | + const requests: Array<HttpClientRequest.HttpClientRequest> = []; |
| 41 | + const secrets = ServerSecretStore.ServerSecretStore.of({ |
| 42 | + get: (name) => Effect.sync(() => Option.fromUndefinedOr(stored.get(name))), |
| 43 | + set: (name, value) => |
| 44 | + Effect.sync(() => { |
| 45 | + stored.set(name, value); |
| 46 | + }), |
| 47 | + create: () => Effect.die("not used"), |
| 48 | + getOrCreateRandom: () => Effect.die("not used"), |
| 49 | + remove: (name) => |
| 50 | + Effect.sync(() => { |
| 51 | + stored.delete(name); |
| 52 | + }), |
| 53 | + }); |
| 54 | + const client = HttpClient.make((request) => |
| 55 | + Effect.sync(() => { |
| 56 | + requests.push(request); |
| 57 | + return HttpClientResponse.fromWeb( |
| 58 | + request, |
| 59 | + new Response(JSON.stringify(body), { |
| 60 | + status, |
| 61 | + headers: { "content-type": "application/json" }, |
| 62 | + }), |
| 63 | + ); |
| 64 | + }), |
| 65 | + ); |
| 66 | + const layer = VoiceInput.layer.pipe( |
| 67 | + Layer.provide( |
| 68 | + Layer.mergeAll( |
| 69 | + Layer.succeed(ServerSecretStore.ServerSecretStore, secrets), |
| 70 | + Layer.succeed(HttpClient.HttpClient, client), |
| 71 | + ), |
| 72 | + ), |
| 73 | + ); |
| 74 | + const run = <A, E>(use: (service: VoiceInput.VoiceInput["Service"]) => Effect.Effect<A, E>) => |
| 75 | + Effect.flatMap(VoiceInput.VoiceInput, use).pipe(Effect.provide(layer)); |
| 76 | + return { stored, requests, run }; |
| 77 | +} |
| 78 | + |
| 79 | +describe("external dictation service", () => { |
| 80 | + it.effect("reads the previous custom-service identifier without losing its saved key", () => |
| 81 | + Effect.gen(function* () { |
| 82 | + const h = harness(); |
| 83 | + h.stored.set( |
| 84 | + "fork-voice-input", |
| 85 | + new TextEncoder().encode( |
| 86 | + JSON.stringify({ |
| 87 | + ...local, |
| 88 | + provider: "promptletariat", |
| 89 | + model: "whisper", |
| 90 | + apiKey: "legacy-secret", |
| 91 | + }), |
| 92 | + ), |
| 93 | + ); |
| 94 | + const settings = yield* h.run((s) => s.settings); |
| 95 | + expect(settings).toMatchObject({ |
| 96 | + provider: "openai-compatible", |
| 97 | + hasApiKey: true, |
| 98 | + enabled: true, |
| 99 | + }); |
| 100 | + expect(JSON.stringify(settings)).not.toContain("legacy-secret"); |
| 101 | + yield* h.run((s) => s.transcribe(recording())); |
| 102 | + expect(h.requests[0]?.headers.authorization).toBe("Bearer legacy-secret"); |
| 103 | + }), |
| 104 | + ); |
| 105 | + it.effect("starts disabled and redacts keys", () => |
| 106 | + Effect.gen(function* () { |
| 107 | + const h = harness(); |
| 108 | + expect(yield* h.run((s) => s.settings)).toMatchObject({ enabled: false, hasApiKey: false }); |
| 109 | + const settings = yield* h.run((s) => s.configure({ ...local, apiKey: "test-secret" })); |
| 110 | + expect(settings.hasApiKey).toBe(true); |
| 111 | + expect(JSON.stringify(settings)).not.toContain("test-secret"); |
| 112 | + expect(settings).not.toHaveProperty("apiKey"); |
| 113 | + }), |
| 114 | + ); |
| 115 | + it.effect("uploads raw WAV with optional bearer auth", () => |
| 116 | + Effect.gen(function* () { |
| 117 | + const h = harness(); |
| 118 | + const audio = recording(); |
| 119 | + const text = yield* h.run((s) => |
| 120 | + Effect.gen(function* () { |
| 121 | + yield* s.configure({ ...local, apiKey: "local-key" }); |
| 122 | + return yield* s.transcribe(audio); |
| 123 | + }), |
| 124 | + ); |
| 125 | + expect(text).toBe("spoken words"); |
| 126 | + expect(h.requests[0]?.url).toBe(local.endpoint); |
| 127 | + expect(h.requests[0]?.headers).toMatchObject({ |
| 128 | + authorization: "Bearer local-key", |
| 129 | + "content-type": "audio/wav", |
| 130 | + }); |
| 131 | + expect(h.requests[0]?.body).toMatchObject({ _tag: "Uint8Array", body: audio }); |
| 132 | + }), |
| 133 | + ); |
| 134 | + it.effect("uses the OpenAI-compatible multipart contract", () => |
| 135 | + Effect.gen(function* () { |
| 136 | + const h = harness(); |
| 137 | + yield* h.run((s) => |
| 138 | + Effect.gen(function* () { |
| 139 | + yield* s.configure({ |
| 140 | + ...local, |
| 141 | + provider: "openai-compatible", |
| 142 | + endpoint: "http://localhost:9000/v1/audio/transcriptions", |
| 143 | + model: "nemo", |
| 144 | + }); |
| 145 | + yield* s.transcribe(recording()); |
| 146 | + }), |
| 147 | + ); |
| 148 | + const body = h.requests[0]?.body; |
| 149 | + if (body?._tag !== "FormData") throw new Error("Expected upload"); |
| 150 | + expect(body.formData.get("model")).toBe("nemo"); |
| 151 | + expect(body.formData.get("response_format")).toBe("json"); |
| 152 | + expect(body.formData.get("file")).toBeInstanceOf(Blob); |
| 153 | + expect(h.requests[0]?.headers.authorization).toBeUndefined(); |
| 154 | + }), |
| 155 | + ); |
| 156 | + it.effect("retains keys only for the same destination and supports removal", () => |
| 157 | + Effect.gen(function* () { |
| 158 | + const h = harness(); |
| 159 | + yield* h.run((s) => |
| 160 | + Effect.gen(function* () { |
| 161 | + yield* s.configure({ ...local, apiKey: "original" }); |
| 162 | + expect((yield* s.configure(local)).hasApiKey).toBe(true); |
| 163 | + expect( |
| 164 | + (yield* s.configure({ ...local, endpoint: "http://localhost:9000/transcribe" })) |
| 165 | + .hasApiKey, |
| 166 | + ).toBe(false); |
| 167 | + yield* s.configure({ ...local, apiKey: "original" }); |
| 168 | + expect((yield* s.configure({ ...local, apiKey: "" })).hasApiKey).toBe(false); |
| 169 | + }), |
| 170 | + ); |
| 171 | + }), |
| 172 | + ); |
| 173 | + it.effect("refuses disabled and stub providers before any request", () => |
| 174 | + Effect.gen(function* () { |
| 175 | + const h = harness(); |
| 176 | + expect(yield* h.run((s) => s.transcribe(recording())).pipe(Effect.flip)).toMatchObject({ |
| 177 | + reason: "disabled", |
| 178 | + }); |
| 179 | + yield* h.run((s) => s.configure({ ...local, provider: "deepgram", model: "nova" })); |
| 180 | + expect(yield* h.run((s) => s.transcribe(recording())).pipe(Effect.flip)).toMatchObject({ |
| 181 | + reason: "unsupported", |
| 182 | + }); |
| 183 | + expect(h.requests).toHaveLength(0); |
| 184 | + }), |
| 185 | + ); |
| 186 | + it.effect.each([ |
| 187 | + "file:///tmp/audio", |
| 188 | + "https://user:[email protected]/transcribe", |
| 189 | + "https://example.com/#token", |
| 190 | + ])("rejects unsafe endpoint %s", (endpoint) => |
| 191 | + Effect.gen(function* () { |
| 192 | + const h = harness(); |
| 193 | + expect( |
| 194 | + yield* h.run((s) => s.configure({ ...local, endpoint })).pipe(Effect.flip), |
| 195 | + ).toMatchObject({ reason: "settings" }); |
| 196 | + expect(h.stored.size).toBe(0); |
| 197 | + }), |
| 198 | + ); |
| 199 | + it.effect.each([new Uint8Array(0), new Uint8Array(100), recording(48000)])( |
| 200 | + "refuses invalid recordings", |
| 201 | + (audio) => |
| 202 | + Effect.gen(function* () { |
| 203 | + const h = harness(); |
| 204 | + yield* h.run((s) => s.configure(local)); |
| 205 | + expect(yield* h.run((s) => s.transcribe(audio)).pipe(Effect.flip)).toMatchObject({ |
| 206 | + reason: "audio", |
| 207 | + }); |
| 208 | + expect(h.requests).toHaveLength(0); |
| 209 | + }), |
| 210 | + ); |
| 211 | + it.effect("reports status without private provider details", () => |
| 212 | + Effect.gen(function* () { |
| 213 | + const h = harness({ error: "private detail" }, 401); |
| 214 | + yield* h.run((s) => s.configure(local)); |
| 215 | + expect((yield* h.run((s) => s.transcribe(recording())).pipe(Effect.flip)).message).toBe( |
| 216 | + "Dictation provider returned HTTP 401.", |
| 217 | + ); |
| 218 | + }), |
| 219 | + ); |
| 220 | + it.effect("rejects invalid transcripts and accepts empty speech", () => |
| 221 | + Effect.gen(function* () { |
| 222 | + const invalid = harness({ text: 42 }); |
| 223 | + yield* invalid.run((s) => s.configure(local)); |
| 224 | + expect(yield* invalid.run((s) => s.transcribe(recording())).pipe(Effect.flip)).toMatchObject({ |
| 225 | + reason: "response", |
| 226 | + }); |
| 227 | + const empty = harness({ text: " " }); |
| 228 | + yield* empty.run((s) => s.configure(local)); |
| 229 | + expect(yield* empty.run((s) => s.transcribe(recording()))).toBe(""); |
| 230 | + }), |
| 231 | + ); |
| 232 | +}); |
0 commit comments