Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

McpDeviceToolkit.test.ts

237 lines8.1 KB

1import { expect, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import {
4 DeviceHostUnavailableError,
5 DeviceId,
6 EnvironmentId,
7 ProviderInstanceId,
8 ThreadId,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13import { McpSchema, McpServer } from "effect/ai";
14​
15import * as ServerConfig from "../config.ts";
16import * as DeviceService from "../device/DeviceService.ts";
17import * as ProjectService from "../project/ProjectService.ts";
18import * as McpHttpServer from "./McpHttpServer.ts";
19import * as McpInvocationContext from "./McpInvocationContext.ts";
20import * as McpToolAccessTestkit from "./McpToolAccess.testkit.ts";
21​
22const environmentId = EnvironmentId.make("environment-device-test");
23const threadId = ThreadId.make("thread-device-test");
24const invocation = (capabilities: ReadonlyArray<McpInvocationContext.McpCapability>) => ({
25 environmentId,
26 requestNamespace: "provider-session-device-test",
27 thread: {
28 threadId,
29 providerSessionId: "provider-session-device-test",
30 providerInstanceId: ProviderInstanceId.make("codex"),
31 },
32 client: undefined,
33 capabilities: new Set(capabilities),
34 issuedAt: 1,
35});
36const client = McpSchema.McpServerClient.of({
37 clientId: 1,
38 clientCapabilities: {},
39 clientInfo: { name: "mcp-test", version: "1.0.0" },
40 protocolVersion: "2025-06-18",
41 initializePayload: {
42 protocolVersion: "2025-06-18",
43 capabilities: {},
44 clientInfo: { name: "mcp-test", version: "1.0.0" },
45 },
46 getClient: Effect.die("unused"),
47});
48​
49const device = {
50 hostId: "local",
51 id: "UDID-1",
52 platform: "ios" as const,
53 name: "iPhone 17 Pro",
54 version: "iOS 27.0",
55 booted: true,
56 physical: false,
57};
58const state = {
59 hosts: [
60 {
61 id: "local",
62 kind: "local" as const,
63 label: "This machine",
64 platforms: [
65 { platform: "ios" as const, available: true },
66 { platform: "android" as const, available: false, reason: "No SDK" },
67 ],
68 hubInstalled: true,
69 agentDeviceInstalled: true,
70 },
71 ],
72 hostStatus: "ready" as const,
73 hostStatuses: { local: { status: "ready" as const } },
74 devices: [device],
75 sessions: [],
76 onboardingCompleted: true,
77 agentAccessEnabled: true,
78 hubBasePath: "/api/device-hub",
79 revision: 1,
80};
81const png = new Uint8Array(24);
82new DataView(png.buffer).setUint32(0, 0x89504e47);
83new DataView(png.buffer).setUint32(4, 0x0d0a1a0a);
84new DataView(png.buffer).setUint32(12, 0x49484452);
85new DataView(png.buffer).setUint32(16, 1206);
86new DataView(png.buffer).setUint32(20, 2622);
87​
88const unused = Effect.die("not used");
89const readiness = {
90 hostId: "local",
91 nodePath: process.execPath,
92 hub: { origin: "http://hub.test" },
93 devices: {
94 list: unused,
95 boot: () => unused,
96 attach: () => unused,
97 shutdown: () => unused,
98 screenshot: () => unused,
99 },
100 run: () => unused,
101 helpers: { serveSimAxSettings: null, serveSimCli: null },
102 agentDevice: null,
103};
104const layerProjectsMock = Layer.mock(ProjectService.ProjectService)({
105 getById: () => Effect.succeed(Option.none()),
106});
107​
108const layerDeviceServiceMock = Layer.mock(DeviceService.DeviceService)({
109 state: Effect.succeed(state),
110 list: Effect.succeed(state),
111 agentReadinessIfSupported: () => Effect.succeed(readiness),
112 open: (input) =>
113 Effect.succeed({
114 threadId: input.threadId,
115 hostId: "local",
116 deviceId: input.deviceId,
117 platform: input.platform,
118 openedAt: "2026-09-08T00:00:00.000Z",
119 }),
120​
121 sessionsForThread: (id) =>
122 Effect.succeed(
123 id === threadId
124 ? [
125 {
126 threadId,
127 hostId: "local",
128 deviceId: DeviceId.make("UDID-1"),
129 platform: "ios" as const,
130 openedAt: "2026-09-08T00:00:00.000Z",
131 },
132 ]
133 : [],
134 ),
135 screenshot: () => Effect.succeed({ device, png }),
136 close: () => Effect.void,
137 agentCli: Effect.succeed("/cli"),
138 testHost: () => Effect.die("not used"),
139 // As for an EAS device, agent-device knows the device by a different id than T3.
140 agentTarget: () =>
141 Effect.succeed({
142 agentDeviceId: "SIM-UDID",
143 args: ["--config", "/host.json", "--session", "thread-device"],
144 }),
145});
146​
147const layerTest = McpHttpServer.layerDeviceToolkit.pipe(
148 Layer.provideMerge(McpServer.McpServer.layer),
149 Layer.provideMerge(McpToolAccessTestkit.liveThreadsLayer),
150 Layer.provideMerge(layerDeviceServiceMock),
151 Layer.provide(layerProjectsMock),
152 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-device-toolkit-test-" })),
153 Layer.provide(NodeServices.layer),
154);
155​
156it.effect("registers the device tools and returns the screenshot as image content", () =>
157 Effect.scoped(
158 Effect.gen(function* () {
159 const server = yield* McpServer.McpServer;
160 const names = server.tools.map(({ tool }) => tool.name).toSorted();
161 expect(names).toEqual(["device_close", "device_list", "device_open", "device_screenshot"]);
162​
163 const callWith = (capabilities: ReadonlyArray<McpInvocationContext.McpCapability>) =>
164 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities));
165​
166 const opened = yield* server
167 .callTool({ name: "device_open", arguments: { platform: "ios" } })
168 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
169 expect(opened.isError).toBe(false);
170 const openedContent = opened.structuredContent as { quickStart: string };
171 expect(openedContent.quickStart).toContain("--udid SIM-UDID");
172​
173 const shot = yield* server
174 .callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-1" } })
175 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
176 expect(shot.isError).toBe(false);
177 expect(shot.content.map((entry) => entry.type)).toEqual(["text", "image"]);
178 expect(shot.structuredContent).toMatchObject({
179 screenshot: { mimeType: "image/png", width: 1206, height: 2622 },
180 });
181​
182 const foreign = yield* server
183 .callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-2" } })
184 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
185 expect(foreign.isError).toBe(true);
186 expect(foreign.content.map((entry) => entry.type)).toEqual(["text"]);
187​
188 const denied = yield* server
189 .callTool({ name: "device_list", arguments: {} })
190 .pipe(callWith(["preview"]), Effect.provideService(McpSchema.McpServerClient, client));
191 expect(denied.isError).toBe(true);
192 }),
193 ).pipe(Effect.provide(layerTest)),
194);
195​
196it.effect("rejects unavailable agent access before booting or opening a device", () => {
197 const layerUnavailable = Layer.mock(DeviceService.DeviceService)({
198 list: Effect.succeed(state),
199 agentReadinessIfSupported: () =>
200 Effect.fail(
201 new DeviceHostUnavailableError({ hostId: "local", reason: "Agent access is disabled." }),
202 ),
203 open: () => Effect.die("Must not boot or register a device when agent access fails"),
204 });
205 return Effect.gen(function* () {
206 const server = yield* McpServer.McpServer;
207 const result = yield* server
208 .callTool({ name: "device_open", arguments: { platform: "ios" } })
209 .pipe(
210 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(["device"])),
211 Effect.provideService(McpSchema.McpServerClient, client),
212 );
213 expect(result.isError).toBe(true);
214 expect(result.content).toEqual(
215 expect.arrayContaining([
216 expect.objectContaining({
217 type: "text",
218 text: expect.stringContaining("Agent access is disabled."),
219 }),
220 ]),
221 );
222 }).pipe(
223 Effect.scoped,
224 Effect.provide(
225 McpHttpServer.layerDeviceToolkit.pipe(
226 Layer.provideMerge(McpServer.McpServer.layer),
227 Layer.provideMerge(McpToolAccessTestkit.liveThreadsLayer),
228 Layer.provide(layerUnavailable),
229 Layer.provide(layerProjectsMock),
230 Layer.provide(
231 ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-device-toolkit-test-" }),
232 ),
233 Layer.provide(NodeServices.layer),
234 ),
235 ),
236 );
237});