|
1 | 1 | import type { VoiceInputConfigUpdateFork } from "@t3tools/contracts"; |
2 | 2 | import { describe, expect, it } from "@effect/vitest"; |
3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | +import * as Deferred from "effect/Deferred"; |
| 5 | +import * as Fiber from "effect/Fiber"; |
| 6 | +import * as TestClock from "effect/testing/TestClock"; |
4 | 7 | import * as Layer from "effect/Layer"; |
5 | 8 | import * as Option from "effect/Option"; |
6 | 9 | import * as HttpClient from "effect/http/HttpClient"; |
@@ -35,7 +38,11 @@ const local: VoiceInputConfigUpdateFork = { |
35 | 38 | model: "", |
36 | 39 | }; |
37 | 40 |
|
38 | | -function harness(body: unknown = { text: " spoken words " }, status = 200) { |
| 41 | +function harness( |
| 42 | + body: unknown = { text: " spoken words " }, |
| 43 | + status = 200, |
| 44 | + customClient?: HttpClient.HttpClient, |
| 45 | +) { |
39 | 46 | const stored = new Map<string, Uint8Array>(); |
40 | 47 | const requests: Array<HttpClientRequest.HttpClientRequest> = []; |
41 | 48 | const secrets = ServerSecretStore.ServerSecretStore.of({ |
@@ -67,7 +74,7 @@ function harness(body: unknown = { text: " spoken words " }, status = 200) { |
67 | 74 | Layer.provide( |
68 | 75 | Layer.mergeAll( |
69 | 76 | Layer.succeed(ServerSecretStore.ServerSecretStore, secrets), |
70 | | - Layer.succeed(HttpClient.HttpClient, client), |
| 77 | + Layer.succeed(HttpClient.HttpClient, customClient ?? client), |
71 | 78 | ), |
72 | 79 | ), |
73 | 80 | ); |
@@ -102,6 +109,84 @@ describe("external dictation service", () => { |
102 | 109 | expect(h.requests[0]?.headers.authorization).toBe("Bearer legacy-secret"); |
103 | 110 | }), |
104 | 111 | ); |
| 112 | + it.effect("bounds provider latency and interrupts pending requests", () => |
| 113 | + Effect.gen(function* () { |
| 114 | + const started = yield* Deferred.make<void>(); |
| 115 | + let released = false; |
| 116 | + const client = HttpClient.make(() => |
| 117 | + Deferred.succeed(started, undefined).pipe( |
| 118 | + Effect.andThen(Effect.never), |
| 119 | + Effect.ensuring( |
| 120 | + Effect.sync(() => { |
| 121 | + released = true; |
| 122 | + }), |
| 123 | + ), |
| 124 | + ), |
| 125 | + ); |
| 126 | + const h = harness(undefined, 200, client); |
| 127 | + yield* h.run((s) => s.configure(local)); |
| 128 | + const pending = yield* h |
| 129 | + .run((s) => s.transcribe(recording())) |
| 130 | + .pipe(Effect.flip, Effect.forkScoped); |
| 131 | + yield* Deferred.await(started); |
| 132 | + yield* TestClock.adjust("61 seconds"); |
| 133 | + expect(yield* Fiber.join(pending)).toMatchObject({ reason: "upstream" }); |
| 134 | + expect(released).toBe(true); |
| 135 | + }), |
| 136 | + ); |
| 137 | + it.effect("uses Meta's JSON request and WAV audio parts and reads transcript", () => |
| 138 | + Effect.gen(function* () { |
| 139 | + const h = harness({ transcript: " meta words " }); |
| 140 | + const audio = recording(); |
| 141 | + const text = yield* h.run((s) => |
| 142 | + Effect.gen(function* () { |
| 143 | + yield* s.configure({ |
| 144 | + enabled: true, |
| 145 | + provider: "meta", |
| 146 | + endpoint: "https://api.meta.ai/v1/asr/transcribe", |
| 147 | + model: "muse-voice-transcribe-1.0", |
| 148 | + apiKey: "meta-key", |
| 149 | + }); |
| 150 | + return yield* s.transcribe(audio); |
| 151 | + }), |
| 152 | + ); |
| 153 | + expect(text).toBe("meta words"); |
| 154 | + expect(h.requests[0]?.headers.authorization).toBe("Bearer meta-key"); |
| 155 | + const body = h.requests[0]?.body; |
| 156 | + if (body?._tag !== "FormData") throw new Error("Expected Meta multipart upload"); |
| 157 | + const request = body.formData.get("request"); |
| 158 | + const file = body.formData.get("audio"); |
| 159 | + if (!(request instanceof Blob) || !(file instanceof Blob)) |
| 160 | + throw new Error("Expected JSON and WAV parts"); |
| 161 | + expect(request.type).toBe("application/json"); |
| 162 | + expect(JSON.parse(yield* Effect.promise(() => request.text()))).toEqual({ |
| 163 | + model: "muse-voice-transcribe-1.0", |
| 164 | + audioEncoding: "WAV", |
| 165 | + mode: "PUSH_TO_TALK", |
| 166 | + }); |
| 167 | + expect(file.type).toBe("audio/wav"); |
| 168 | + expect(new Uint8Array(yield* Effect.promise(() => file.arrayBuffer()))).toEqual(audio); |
| 169 | + expect(body.formData.has("file")).toBe(false); |
| 170 | + }), |
| 171 | + ); |
| 172 | + it.effect.each([{ text: "wrong response shape" }, { transcript: 42 }])( |
| 173 | + "rejects invalid Meta responses", |
| 174 | + (body) => |
| 175 | + Effect.gen(function* () { |
| 176 | + const h = harness(body); |
| 177 | + yield* h.run((s) => |
| 178 | + s.configure({ |
| 179 | + enabled: true, |
| 180 | + provider: "meta", |
| 181 | + endpoint: "https://api.meta.ai/v1/asr/transcribe", |
| 182 | + model: "muse-voice-transcribe-1.0", |
| 183 | + }), |
| 184 | + ); |
| 185 | + expect(yield* h.run((s) => s.transcribe(recording())).pipe(Effect.flip)).toMatchObject({ |
| 186 | + reason: "response", |
| 187 | + }); |
| 188 | + }), |
| 189 | + ); |
105 | 190 | it.effect("starts disabled and redacts keys", () => |
106 | 191 | Effect.gen(function* () { |
107 | 192 | const h = harness(); |
|
0 commit comments