The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | import { app } from "electron"; |
| 4 | import { expect, vi } from "vite-plus/test"; |
| 5 | import { it } from "@effect/vitest"; |
| 6 | import * as Fiber from "effect/Fiber"; |
| 7 | |
| 8 | const { getAppMetrics, kill } = vi.hoisted(() => ({ getAppMetrics: vi.fn(), kill: vi.fn() })); |
| 9 | vi.mock("node:process", () => ({ kill })); |
| 10 | vi.mock("electron", async () => { |
| 11 | const { EventEmitter } = await import("node:events"); |
| 12 | return { app: Object.assign(new EventEmitter(), { getAppMetrics }) }; |
| 13 | }); |
| 14 | |
| 15 | import { restart } from "./ElectronAudio.ts"; |
| 16 | |
| 17 | const audio = { pid: 42, type: "Utility", serviceName: "audio.mojom.AudioService" }; |
| 18 | |
| 19 | it.effect("coalesces recovery and waits for its own audio utility to exit", () => |
| 20 | Effect.gen(function* () { |
| 21 | getAppMetrics.mockReturnValue([ |
| 22 | { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" }, |
| 23 | { pid: 2, type: "Browser", serviceName: audio.serviceName }, |
| 24 | audio, |
| 25 | ]); |
| 26 | const started = Promise.withResolvers<void>(); |
| 27 | kill.mockReset().mockImplementation(() => { |
| 28 | started.resolve(); |
| 29 | }); |
| 30 | const pending = yield* Effect.forkChild( |
| 31 | Effect.all([restart(), restart()], { concurrency: "unbounded" }), |
| 32 | ); |
| 33 | yield* Effect.promise(() => started.promise); |
| 34 | app.emit( |
| 35 | "child-process-gone", |
| 36 | {}, |
| 37 | { type: "Utility", serviceName: "network.mojom.NetworkService" }, |
| 38 | ); |
| 39 | expect(app.listenerCount("child-process-gone")).toBe(1); |
| 40 | app.emit("child-process-gone", {}, audio); |
| 41 | expect(yield* Fiber.join(pending)).toEqual([true, true]); |
| 42 | expect(kill.mock.calls).toEqual([[42, "SIGTERM"]]); |
| 43 | expect(app.listenerCount("child-process-gone")).toBe(0); |
| 44 | }).pipe(Effect.provideService(HostProcess.Platform, "linux")), |
| 45 | ); |
| 46 | |
| 47 | it.effect.each(["linux", "darwin", "win32"] as const)( |
| 48 | "leaves other processes alone on %s", |
| 49 | (platform) => |
| 50 | Effect.gen(function* () { |
| 51 | getAppMetrics.mockReturnValue([ |
| 52 | { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" }, |
| 53 | ]); |
| 54 | kill.mockReset(); |
| 55 | expect(yield* restart().pipe(Effect.provideService(HostProcess.Platform, platform))).toBe( |
| 56 | false, |
| 57 | ); |
| 58 | expect(kill).not.toHaveBeenCalled(); |
| 59 | }), |
| 60 | ); |