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

McpDeviceToolkit.test.ts

247 lines8.7 KB

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