@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 176 lines 6.57 KB
Raw
1 import { assert, describe, it } from "@effect/vitest";
2 import * as HostProcess from "@t3tools/shared/HostProcess";
3 import * as Deferred from "effect/Deferred";
4 import * as Effect from "effect/Effect";
5 import * as Fiber from "effect/Fiber";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Layer from "effect/Layer";
8 import * as Sink from "effect/Sink";
9 import * as Stream from "effect/Stream";
10 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11
12 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
13 import * as AppleSpeech from "./AppleSpeech.ts";
14
15 const encoder = new TextEncoder();
16 const text = (value: string) => (value ? Stream.make(encoder.encode(value)) : Stream.empty);
17
18 type HelperOutput = {
19 stdout?: string;
20 stderr?: string;
21 exitCode: number;
22 exit?: Deferred.Deferred<void>;
23 };
24
25 function makeProcess(result: HelperOutput) {
26 return ChildProcessSpawner.makeHandle({
27 pid: ChildProcessSpawner.ProcessId(1),
28 stdout: text(result.stdout ?? ""),
29 stderr: text(result.stderr ?? ""),
30 all: Stream.empty,
31 exitCode: (result.exit ? Deferred.await(result.exit) : Effect.void).pipe(
32 Effect.as(ChildProcessSpawner.ExitCode(result.exitCode)),
33 ),
34 isRunning: Effect.succeed(false),
35 kill: () => Effect.void,
36 stdin: Sink.drain,
37 getInputFd: () => Sink.drain,
38 getOutputFd: () => Stream.empty,
39 unref: Effect.succeed(Effect.void),
40 });
41 }
42
43 function setup(result: HelperOutput, kernelRelease = "25.0.0") {
44 const commands: Array<ReadonlyArray<string>> = [];
45 const written: Uint8Array[] = [];
46 const layer = AppleSpeech.layer.pipe(
47 Layer.provide(
48 Layer.mergeAll(
49 Layer.succeed(HostProcess.KernelRelease, kernelRelease),
50 Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
51 platform: "darwin",
52 isPackaged: true,
53 resourcesPath: "/app/Resources",
54 path: { join: (...parts: string[]) => parts.join("/") },
55 } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]),
56 FileSystem.layerNoop({
57 exists: () => Effect.succeed(true),
58 makeTempFileScoped: () => Effect.succeed("/tmp/recording.wav"),
59 writeFile: (_path, data) => Effect.sync(() => void written.push(data)),
60 }),
61 Layer.succeed(
62 ChildProcessSpawner.ChildProcessSpawner,
63 ChildProcessSpawner.make((command) => {
64 const { command: executable, args } = command as unknown as {
65 command: string;
66 args: ReadonlyArray<string>;
67 };
68 commands.push([executable, ...args]);
69 return Effect.succeed(makeProcess(result));
70 }),
71 ),
72 ),
73 ),
74 );
75 return { commands, written, layer };
76 }
77
78 describe("AppleSpeech", () => {
79 it.effect.each([
80 [
81 { stdout: '{"value":"en-US"}', exitCode: 0 },
82 { ok: true, value: "en-US" },
83 ],
84 [
85 { stderr: "Locale not supported: xx\n", exitCode: 2 },
86 { ok: false, code: "unsupported-locale" },
87 ],
88 [{ exitCode: 3 }, { ok: false, code: "unavailable" }],
89 [
90 { stderr: "boom", exitCode: 1 },
91 { ok: false, code: "failed" },
92 ],
93 ] as const)("maps helper output %j to %j", ([output, expected]) => {
94 const { layer, commands } = setup(output);
95 return Effect.gen(function* () {
96 const speech = yield* AppleSpeech.AppleSpeech;
97 assert.deepEqual(yield* speech.prepare("en-US"), expected);
98 assert.deepEqual(commands, [
99 ["/app/Resources/apple-speech/t3-apple-speech", "prepare", "en-US"],
100 ]);
101 }).pipe(Effect.provide(layer));
102 });
103
104 it.effect("shares one helper between prepares that start while the first is running", () =>
105 Effect.gen(function* () {
106 const exit = yield* Deferred.make<void>();
107 const { layer, commands } = setup({ stdout: '{"value":"en-US"}', exitCode: 0, exit });
108 return yield* Effect.gen(function* () {
109 const speech = yield* AppleSpeech.AppleSpeech;
110 const first = yield* speech
111 .prepare("en-US")
112 .pipe(Effect.forkChild({ startImmediately: true }));
113 const second = yield* speech
114 .prepare("en-US")
115 .pipe(Effect.forkChild({ startImmediately: true }));
116 yield* Deferred.succeed(exit, undefined);
117 assert.deepEqual(yield* Fiber.join(first), { ok: true, value: "en-US" });
118 assert.deepEqual(yield* Fiber.join(second), { ok: true, value: "en-US" });
119 assert.lengthOf(commands, 1);
120 }).pipe(Effect.provide(layer));
121 }),
122 );
123
124 it.effect.each([
125 ["25.0.0", 0, ["en-US", "fr-FR"], 1],
126 ["25.0.0", 3, [], 1],
127 ["25.0.0", 1, [], 2],
128 ["24.6.0", 0, [], 0],
129 ] as const)(
130 "on Darwin %s resolves locales on helper exit %i to %j with %i probe(s) across two calls",
131 ([release, exitCode, expected, probes]) => {
132 const { layer, commands } = setup({ stdout: '{"value":"en-US,fr-FR"}', exitCode }, release);
133 return Effect.gen(function* () {
134 const speech = yield* AppleSpeech.AppleSpeech;
135 assert.deepEqual(yield* speech.supportedLocales, expected);
136 assert.deepEqual(yield* speech.supportedLocales, expected);
137 assert.lengthOf(commands, probes);
138 }).pipe(Effect.provide(layer));
139 },
140 );
141
142 it.effect("transcribes Float32 PCM through a 16-bit WAV file", () => {
143 const { layer, commands, written } = setup({ stdout: '{"value":"hello"}', exitCode: 0 });
144 // An unaligned Buffer view: Buffer.slice and subarray do not copy.
145 const pcm = new Uint8Array(17);
146 new DataView(pcm.buffer).setFloat32(1, 0, true);
147 new DataView(pcm.buffer).setFloat32(5, 1, true);
148 new DataView(pcm.buffer).setFloat32(9, -1, true);
149 new DataView(pcm.buffer).setFloat32(13, 2, true);
150 return Effect.gen(function* () {
151 const result = yield* (yield* AppleSpeech.AppleSpeech).transcribe(
152 "en-US",
153 Buffer.from(pcm.buffer).subarray(1),
154 );
155 assert.deepEqual(result, { ok: true, value: "hello" });
156 assert.deepEqual(commands, [
157 [
158 "/app/Resources/apple-speech/t3-apple-speech",
159 "transcribe",
160 "en-US",
161 "/tmp/recording.wav",
162 ],
163 ]);
164 const wav = new DataView(written[0]!.buffer);
165 assert.equal(new TextDecoder().decode(written[0]!.subarray(0, 4)), "RIFF");
166 assert.equal(wav.getUint16(20, true), 1);
167 assert.equal(wav.getUint32(24, true), 16_000);
168 assert.equal(wav.getUint16(34, true), 16);
169 assert.equal(wav.getUint32(40, true), 8);
170 assert.deepEqual(
171 [0, 1, 2, 3].map((index) => wav.getInt16(44 + index * 2, true)),
172 [0, 32767, -32767, 32767],
173 );
174 }).pipe(Effect.provide(layer));
175 });
176 });