The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as NodeStream from "@effect/platform-node/NodeStream"; |
| 3 | import { expect, it } from "@effect/vitest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Queue from "effect/Queue"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { vi } from "vite-plus/test"; |
| 10 | |
| 11 | import * as ServerConfig from "../config.ts"; |
| 12 | import * as DesktopBrowserChannel from "./DesktopBrowserChannel.ts"; |
| 13 | |
| 14 | vi.mock("@effect/platform-node/NodeStream", () => ({ fromReadable: vi.fn() })); |
| 15 | vi.mock("node:net", () => ({ |
| 16 | Socket: class { |
| 17 | destroy() {} |
| 18 | }, |
| 19 | })); |
| 20 | |
| 21 | it.effect("protects unknown desktop visibility until an explicit hidden report arrives", () => |
| 22 | Effect.gen(function* () { |
| 23 | const input = yield* Queue.unbounded<Uint8Array>(); |
| 24 | vi.mocked(NodeStream.fromReadable).mockReturnValue(Stream.fromQueue(input)); |
| 25 | const config = yield* ServerConfig.ServerConfig; |
| 26 | yield* Effect.gen(function* () { |
| 27 | const channel = yield* DesktopBrowserChannel.DesktopBrowserChannel; |
| 28 | const key = { threadId: "thread-1", tabId: "tab-1" }; |
| 29 | const send = (event: object) => |
| 30 | Queue.offer(input, new TextEncoder().encode(`${JSON.stringify(event)}\n`)); |
| 31 | |
| 32 | expect(channel.isShown(key)).toBe(true); |
| 33 | yield* send({ type: "attached", ...key }); |
| 34 | yield* channel.awaitAttached(key, "5 seconds"); |
| 35 | expect(channel.isShown(key)).toBe(true); |
| 36 | |
| 37 | yield* send({ type: "shown", ...key, shown: false }); |
| 38 | yield* send({ type: "attached", threadId: key.threadId, tabId: "sentinel-1" }); |
| 39 | yield* channel.awaitAttached({ threadId: key.threadId, tabId: "sentinel-1" }, "5 seconds"); |
| 40 | expect(channel.isShown(key)).toBe(false); |
| 41 | |
| 42 | const shown = yield* channel.awaitShown(key, "5 seconds").pipe(Effect.forkChild); |
| 43 | yield* send({ type: "shown", ...key, shown: true }); |
| 44 | expect(yield* Fiber.join(shown)).toBe(true); |
| 45 | expect(channel.isShown(key)).toBe(true); |
| 46 | |
| 47 | yield* send({ type: "shown", ...key, shown: false }); |
| 48 | yield* send({ type: "detached", ...key }); |
| 49 | yield* send({ type: "attached", threadId: key.threadId, tabId: "sentinel-3" }); |
| 50 | yield* channel.awaitAttached({ threadId: key.threadId, tabId: "sentinel-3" }, "5 seconds"); |
| 51 | expect(channel.isShown(key)).toBe(true); |
| 52 | }).pipe( |
| 53 | Effect.provide( |
| 54 | DesktopBrowserChannel.layer.pipe( |
| 55 | Layer.provide( |
| 56 | ServerConfig.layer({ ...config, desktopBrowserFd: 41, desktopBrowserControlFd: 42 }), |
| 57 | ), |
| 58 | ), |
| 59 | ), |
| 60 | ); |
| 61 | }).pipe( |
| 62 | Effect.provide( |
| 63 | ServerConfig.layerTest(process.cwd(), { prefix: "desktop-browser-channel-" }).pipe( |
| 64 | Layer.provide(NodeServices.layer), |
| 65 | ), |
| 66 | ), |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | it.live("closes a tab's relay connection when the desktop detaches the tab", () => |
| 71 | Effect.gen(function* () { |
| 72 | const input = yield* Queue.unbounded<Uint8Array>(); |
| 73 | vi.mocked(NodeStream.fromReadable).mockReturnValue(Stream.fromQueue(input)); |
| 74 | const config = yield* ServerConfig.ServerConfig; |
| 75 | yield* Effect.gen(function* () { |
| 76 | const channel = yield* DesktopBrowserChannel.DesktopBrowserChannel; |
| 77 | const key = { threadId: "thread-1", tabId: "tab-1" }; |
| 78 | const send = (event: object) => |
| 79 | Queue.offer(input, new TextEncoder().encode(`${JSON.stringify(event)}\n`)); |
| 80 | |
| 81 | yield* send({ type: "attached", ...key }); |
| 82 | yield* channel.awaitAttached(key, "5 seconds"); |
| 83 | const url = yield* channel.endpoint(key); |
| 84 | const socket = new WebSocket(url); |
| 85 | yield* Effect.addFinalizer(() => Effect.sync(() => socket.close())); |
| 86 | const closed = new Promise<void>((resolve) => |
| 87 | socket.addEventListener("close", () => resolve()), |
| 88 | ); |
| 89 | yield* Effect.promise( |
| 90 | () => new Promise<void>((resolve) => socket.addEventListener("open", () => resolve())), |
| 91 | ); |
| 92 | |
| 93 | // Playwright waits on this socket for CDP replies; it must learn the tab is gone. |
| 94 | yield* send({ type: "detached", ...key }); |
| 95 | yield* Effect.promise(() => closed).pipe(Effect.timeout("2 seconds")); |
| 96 | }).pipe( |
| 97 | Effect.scoped, |
| 98 | Effect.provide( |
| 99 | DesktopBrowserChannel.layer.pipe( |
| 100 | Layer.provide( |
| 101 | ServerConfig.layer({ ...config, desktopBrowserFd: 41, desktopBrowserControlFd: 42 }), |
| 102 | ), |
| 103 | ), |
| 104 | ), |
| 105 | ); |
| 106 | }).pipe( |
| 107 | Effect.provide( |
| 108 | ServerConfig.layerTest(process.cwd(), { prefix: "desktop-browser-channel-" }).pipe( |
| 109 | Layer.provide(NodeServices.layer), |
| 110 | ), |
| 111 | ), |
| 112 | ), |
| 113 | ); |