The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { |
| 5 | EnvironmentId, |
| 6 | PreviewTabId, |
| 7 | ProviderInstanceId, |
| 8 | ThreadId, |
| 9 | type PreviewAutomationRequest, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as Deferred from "effect/Deferred"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as FileSystem from "effect/FileSystem"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Path from "effect/Path"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | |
| 18 | import { |
| 19 | createPendingAttachmentId, |
| 20 | parseThreadSegmentFromAttachmentId, |
| 21 | } from "../../../attachmentStore.ts"; |
| 22 | import * as ServerConfig from "../../../config.ts"; |
| 23 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 24 | import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 25 | import { liveThreadsLayer } from "../../McpToolAccess.testkit.ts"; |
| 26 | import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; |
| 27 | import { claimPreviewRecording, layerStandard } from "./handlers.ts"; |
| 28 | import { PreviewStandardToolkit } from "./tools.ts"; |
| 29 | |
| 30 | it.effect("preview_open navigates the opened tab to a dev server in one tool call", () => |
| 31 | Effect.gen(function* () { |
| 32 | const scope = yield* McpInvocationContext.McpInvocationContext; |
| 33 | const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker; |
| 34 | const connected = yield* Deferred.make<void>(); |
| 35 | const tabId = PreviewTabId.make("opened-tab"); |
| 36 | const target = { |
| 37 | kind: "environment-port" as const, |
| 38 | port: 3000, |
| 39 | path: "/settings?tab=account", |
| 40 | }; |
| 41 | const requests: PreviewAutomationRequest[] = []; |
| 42 | const status = { |
| 43 | available: true, |
| 44 | visible: false, |
| 45 | tabId, |
| 46 | url: "http://localhost:3000/settings?tab=account", |
| 47 | title: "Settings", |
| 48 | loading: false, |
| 49 | }; |
| 50 | const events = yield* broker.connect({ |
| 51 | clientId: "client-1", |
| 52 | environmentId: scope.environmentId, |
| 53 | }); |
| 54 | yield* Stream.runForEach(events, (event) => { |
| 55 | if (event.type === "connected") return Deferred.succeed(connected, undefined); |
| 56 | requests.push(event.request); |
| 57 | return broker.respond({ |
| 58 | clientId: "client-1", |
| 59 | connectionId: event.connectionId, |
| 60 | requestId: event.request.requestId, |
| 61 | ok: true, |
| 62 | result: event.request.operation === "open" ? { ...status, url: "about:blank" } : status, |
| 63 | }); |
| 64 | }).pipe(Effect.forkScoped); |
| 65 | yield* Deferred.await(connected); |
| 66 | const toolkit = yield* PreviewStandardToolkit.pipe( |
| 67 | Effect.provide(McpToolAccess.HandlersLayer.layer(layerStandard)), |
| 68 | ); |
| 69 | const results = yield* toolkit |
| 70 | .handle("preview_open", { |
| 71 | url: "http://localhost:3000", |
| 72 | target, |
| 73 | open: false, |
| 74 | reuseExistingTab: false, |
| 75 | }) |
| 76 | .pipe(Stream.unwrap, Stream.runCollect); |
| 77 | expect(results.at(-1)?.result).toEqual(status); |
| 78 | expect(requests.map(({ operation, input }) => ({ operation, input }))).toEqual([ |
| 79 | { operation: "open", input: { open: false, show: false, reuseExistingTab: false } }, |
| 80 | { operation: "navigate", input: { target } }, |
| 81 | ]); |
| 82 | expect(requests[1]?.tabId).toBe(tabId); |
| 83 | expect(requests[1]?.tabIdExplicit).toBe(true); |
| 84 | }).pipe( |
| 85 | Effect.provideService(McpInvocationContext.McpInvocationContext, { |
| 86 | environmentId: EnvironmentId.make("environment-1"), |
| 87 | requestNamespace: "provider-session-1", |
| 88 | thread: { |
| 89 | threadId: ThreadId.make("thread-1"), |
| 90 | providerSessionId: "provider-session-1", |
| 91 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 92 | }, |
| 93 | client: undefined, |
| 94 | capabilities: new Set(["preview"] as const), |
| 95 | issuedAt: 1, |
| 96 | }), |
| 97 | Effect.provide( |
| 98 | Layer.mergeAll(PreviewAutomationBroker.layer, liveThreadsLayer).pipe( |
| 99 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | ); |
| 104 | |
| 105 | describe("claimPreviewRecording", () => { |
| 106 | it.effect("overlapping and repeated claims return the same retained recording", () => |
| 107 | Effect.gen(function* () { |
| 108 | const config = yield* ServerConfig.ServerConfig; |
| 109 | const fileSystem = yield* FileSystem.FileSystem; |
| 110 | const path = yield* Path.Path; |
| 111 | const uploadedAttachmentId = createPendingAttachmentId(".webm"); |
| 112 | const pendingPath = path.join(config.attachmentsDir, `${uploadedAttachmentId}.webm`); |
| 113 | yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }); |
| 114 | yield* fileSystem.writeFileString(pendingPath, "video!"); |
| 115 | const response = { |
| 116 | id: "desktop-recording", |
| 117 | tabId: "tab-1", |
| 118 | path: "/desktop/recording.webm", |
| 119 | mimeType: "video/webm", |
| 120 | sizeBytes: 6, |
| 121 | createdAt: "2026-09-07T00:00:00.000Z", |
| 122 | uploadedAttachmentId, |
| 123 | }; |
| 124 | const claim = claimPreviewRecording(ThreadId.make("thread-1"), response); |
| 125 | const [first, second] = yield* Effect.all([claim, claim], { concurrency: "unbounded" }); |
| 126 | expect(first).toEqual(second); |
| 127 | expect(yield* claim).toEqual(first); |
| 128 | expect(yield* fileSystem.readFileString(first.path)).toBe("video!"); |
| 129 | expect(yield* fileSystem.exists(pendingPath)).toBe(false); |
| 130 | const wrongThread = yield* claimPreviewRecording(ThreadId.make("thread-2"), response).pipe( |
| 131 | Effect.result, |
| 132 | ); |
| 133 | expect(wrongThread._tag).toBe("Failure"); |
| 134 | const wrongPath = yield* claimPreviewRecording(ThreadId.make("thread-1"), { |
| 135 | ...response, |
| 136 | uploadedAttachmentId: `../${uploadedAttachmentId}`, |
| 137 | }).pipe(Effect.result); |
| 138 | expect(wrongPath._tag).toBe("Failure"); |
| 139 | }).pipe( |
| 140 | Effect.provide( |
| 141 | ServerConfig.layerTest(process.cwd(), { prefix: "t3-preview-recording-" }).pipe( |
| 142 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 143 | ), |
| 144 | ), |
| 145 | ), |
| 146 | ); |
| 147 | |
| 148 | it.effect.each([6, 5])( |
| 149 | "claims only a complete uploaded recording (reported bytes: %s)", |
| 150 | (sizeBytes) => |
| 151 | Effect.gen(function* () { |
| 152 | const config = yield* ServerConfig.ServerConfig; |
| 153 | const fileSystem = yield* FileSystem.FileSystem; |
| 154 | const path = yield* Path.Path; |
| 155 | const uploadedAttachmentId = createPendingAttachmentId(".webm"); |
| 156 | const pendingPath = path.join(config.attachmentsDir, `${uploadedAttachmentId}.webm`); |
| 157 | yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }); |
| 158 | yield* fileSystem.writeFileString(pendingPath, "video!"); |
| 159 | const response = { |
| 160 | id: "desktop-recording", |
| 161 | tabId: "tab-1", |
| 162 | path: "/desktop/recording.webm", |
| 163 | mimeType: "video/webm", |
| 164 | sizeBytes, |
| 165 | createdAt: "2026-09-07T00:00:00.000Z", |
| 166 | uploadedAttachmentId, |
| 167 | }; |
| 168 | const result = yield* claimPreviewRecording(ThreadId.make("thread-1"), response).pipe( |
| 169 | Effect.result, |
| 170 | ); |
| 171 | if (sizeBytes === 6) { |
| 172 | expect(result._tag).toBe("Success"); |
| 173 | if (result._tag !== "Success") return; |
| 174 | expect(result.success.path).not.toBe(response.path); |
| 175 | expect(parseThreadSegmentFromAttachmentId(result.success.id)).toBe("thread-1"); |
| 176 | expect(yield* fileSystem.readFileString(result.success.path)).toBe("video!"); |
| 177 | expect(yield* fileSystem.exists(pendingPath)).toBe(false); |
| 178 | } else { |
| 179 | expect(result._tag).toBe("Failure"); |
| 180 | if (result._tag !== "Failure") return; |
| 181 | expect(result.failure._tag).toBe("PreviewAutomationRecordingTransferError"); |
| 182 | expect(yield* fileSystem.exists(pendingPath)).toBe(true); |
| 183 | } |
| 184 | }).pipe( |
| 185 | Effect.provide( |
| 186 | ServerConfig.layerTest(process.cwd(), { prefix: "t3-preview-recording-" }).pipe( |
| 187 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 188 | ), |
| 189 | ), |
| 190 | ), |
| 191 | ); |
| 192 | }); |