|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | | -import { ThreadId } from "@t3tools/contracts"; |
| 3 | +import { |
| 4 | + EnvironmentId, |
| 5 | + PreviewTabId, |
| 6 | + ProviderInstanceId, |
| 7 | + ThreadId, |
| 8 | + type PreviewAutomationRequest, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as Deferred from "effect/Deferred"; |
4 | 11 | import * as Effect from "effect/Effect"; |
5 | 12 | import * as FileSystem from "effect/FileSystem"; |
6 | 13 | import * as Layer from "effect/Layer"; |
7 | 14 | import * as Path from "effect/Path"; |
| 15 | +import * as Stream from "effect/Stream"; |
8 | 16 |
|
9 | 17 | import { |
10 | 18 | createPendingAttachmentId, |
11 | 19 | parseThreadSegmentFromAttachmentId, |
12 | 20 | } from "../../../attachmentStore.ts"; |
13 | 21 | import * as ServerConfig from "../../../config.ts"; |
14 | | -import { claimPreviewRecording } from "./handlers.ts"; |
| 22 | +import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 23 | +import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 24 | +import { liveThreadsLayer } from "../../McpToolAccess.testkit.ts"; |
| 25 | +import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; |
| 26 | +import { claimPreviewRecording, layerStandard } from "./handlers.ts"; |
| 27 | +import { PreviewStandardToolkit } from "./tools.ts"; |
| 28 | + |
| 29 | +it.effect("preview_open navigates the opened tab to a dev server in one tool call", () => |
| 30 | + Effect.scoped( |
| 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(NodeServices.layer), |
| 100 | + ), |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | +); |
15 | 105 |
|
16 | 106 | describe("claimPreviewRecording", () => { |
17 | 107 | it.effect("overlapping and repeated claims return the same retained recording", () => |
|
0 commit comments