The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { VOICE_RECORDING_LIMIT_SECONDS } from "@t3tools/client-runtime/voice-input"; |
| 2 | import { expect, it, vi } from "vite-plus/test"; |
| 3 | import { createEnvironmentTranscriber } from "./environmentTranscriber"; |
| 4 | |
| 5 | it("never uploads a recording whose grant changed while the phone decoded it", async () => { |
| 6 | let grant = 1; |
| 7 | const decoding = Promise.withResolvers<void>(); |
| 8 | const decoded = Promise.withResolvers<Uint8Array>(); |
| 9 | const upload = vi.fn(async () => "hello"); |
| 10 | const transcriber = createEnvironmentTranscriber({ |
| 11 | canSend: () => grant === 1, |
| 12 | prepare: async () => "en", |
| 13 | decode: () => { |
| 14 | decoding.resolve(); |
| 15 | return decoded.promise; |
| 16 | }, |
| 17 | upload, |
| 18 | }); |
| 19 | const options = { signal: new AbortController().signal }; |
| 20 | const recording = await transcriber.prepare(options); |
| 21 | const transcription = recording.transcribe("file:///draft.m4a", options); |
| 22 | await decoding.promise; |
| 23 | grant++; |
| 24 | decoded.resolve(new Uint8Array([0, 128, 0, 64])); |
| 25 | await expect(transcription).rejects.toMatchObject({ code: "cancelled" }); |
| 26 | expect(upload).not.toHaveBeenCalled(); |
| 27 | }); |
| 28 | |
| 29 | it("uploads normalized mono samples only after the recording is finished", async () => { |
| 30 | const upload = vi.fn(async (bytes: Uint8Array) => { |
| 31 | expect(Array.from(new Float32Array(Uint8Array.from(bytes).buffer))).toEqual([-1, 0.5]); |
| 32 | return "hello"; |
| 33 | }); |
| 34 | const ready = Promise.withResolvers<string>(); |
| 35 | const transcriber = createEnvironmentTranscriber({ |
| 36 | canSend: () => true, |
| 37 | prepare: () => ready.promise, |
| 38 | decode: async () => new Uint8Array([255, 0, 128, 0, 64, 255]).subarray(1, 5), |
| 39 | upload, |
| 40 | }); |
| 41 | const options = { signal: new AbortController().signal }; |
| 42 | const recording = await transcriber.prepare(options); |
| 43 | const starting = recording.start?.("file:///draft.m4a", options); |
| 44 | expect(upload).not.toHaveBeenCalled(); |
| 45 | ready.resolve("en"); |
| 46 | await starting; |
| 47 | expect(recording.locale).toBe("en"); |
| 48 | expect(upload).not.toHaveBeenCalled(); |
| 49 | await expect(recording.transcribe("file:///draft.m4a", options)).resolves.toBe("hello"); |
| 50 | }); |
| 51 | |
| 52 | it("trims at most one second of recorder padding and rejects longer recordings", async () => { |
| 53 | const limit = 16_000 * VOICE_RECORDING_LIMIT_SECONDS; |
| 54 | const padded = new Uint8Array((limit + 16_000 + 1) * 2); |
| 55 | padded[(limit - 1) * 2 + 1] = 64; |
| 56 | let bytes = padded.subarray(0, -2); |
| 57 | const upload = vi.fn(async (body: Uint8Array) => { |
| 58 | expect(body.byteLength).toBe(limit * 4); |
| 59 | expect( |
| 60 | new DataView(body.buffer, body.byteOffset, body.byteLength).getFloat32( |
| 61 | body.byteLength - 4, |
| 62 | true, |
| 63 | ), |
| 64 | ).toBe(0.5); |
| 65 | return "hello"; |
| 66 | }); |
| 67 | const options = { signal: new AbortController().signal }; |
| 68 | const recording = await createEnvironmentTranscriber({ |
| 69 | canSend: () => true, |
| 70 | prepare: async () => "en", |
| 71 | decode: async () => bytes, |
| 72 | upload, |
| 73 | }).prepare(options); |
| 74 | await expect(recording.transcribe("file:///padded.m4a", options)).resolves.toBe("hello"); |
| 75 | bytes = padded; |
| 76 | await expect(recording.transcribe("file:///too-long.m4a", options)).rejects.toThrow( |
| 77 | "This recording couldn’t be read. Try recording again.", |
| 78 | ); |
| 79 | expect(upload).toHaveBeenCalledOnce(); |
| 80 | }); |