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

McpDeviceToolkit.test.ts

236 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 { McpSchema, McpServer } from "effect/ai";
13​
14import * as ServerConfig from "../config.ts";
15import * as DeviceService from "../device/DeviceService.ts";
16import * as ProjectService from "../project/ProjectService.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 unused = Effect.die("not used");
88const readiness = {
89 hostId: "local",
90 nodePath: process.execPath,
91 hub: { origin: "http://hub.test" },
92 devices: {
93 list: unused,
94 boot: () => unused,
95 attach: () => unused,
96 shutdown: () => unused,
97 screenshot: () => unused,
98 },
99 run: () => unused,
100 helpers: { serveSimAxSettings: null, serveSimCli: null },
101 agentDevice: null,
102};
103const layerProjectsMock = Layer.mock(ProjectService.ProjectService)({
104 getById: () => Effect.succeedNone,
105});
106​
107const layerDeviceServiceMock = Layer.mock(DeviceService.DeviceService)({
108 state: Effect.succeed(state),
109 list: Effect.succeed(state),
110 agentReadinessIfSupported: () => Effect.succeed(readiness),
111 open: (input) =>
112 Effect.succeed({
113 threadId: input.threadId,
114 hostId: "local",
115 deviceId: input.deviceId,
116 platform: input.platform,
117 openedAt: "2026-09-08T00:00:00.000Z",
118 }),
119​
120 sessionsForThread: (id) =>
121 Effect.succeed(
122 id === threadId
123 ? [
124 {
125 threadId,
126 hostId: "local",
127 deviceId: DeviceId.make("UDID-1"),
128 platform: "ios" as const,
129 openedAt: "2026-09-08T00:00:00.000Z",
130 },
131 ]
132 : [],
133 ),
134 screenshot: () => Effect.succeed({ device, png }),
135 close: () => Effect.void,
136 agentCli: Effect.succeed("/cli"),
137 testHost: () => Effect.die("not used"),
138 // As for an EAS device, agent-device knows the device by a different id than T3.
139 agentTarget: () =>
140 Effect.succeed({
141 agentDeviceId: "SIM-UDID",
142 args: ["--config", "/host.json", "--session", "thread-device"],
143 }),
144});
145​
146const layerTest = McpHttpServer.layerDeviceToolkit.pipe(
147 Layer.provideMerge(McpServer.McpServer.layer),
148 Layer.provideMerge(McpToolAccessTestkit.liveThreadsLayer),
149 Layer.provideMerge(layerDeviceServiceMock),
150 Layer.provide(layerProjectsMock),
151 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-device-toolkit-test-" })),
152 Layer.provide(NodeServices.layer),
153);
154​
155it.effect("registers the device tools and returns the screenshot as image content", () =>
156 Effect.scoped(
157 Effect.gen(function* () {
158 const server = yield* McpServer.McpServer;
159 const names = server.tools.map(({ tool }) => tool.name).toSorted();
160 expect(names).toEqual(["device_close", "device_list", "device_open", "device_screenshot"]);
161​
162 const callWith = (capabilities: ReadonlyArray<McpInvocationContext.McpCapability>) =>
163 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities));
164​
165 const opened = yield* server
166 .callTool({ name: "device_open", arguments: { platform: "ios" } })
167 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
168 expect(opened.isError).toBe(false);
169 const openedContent = opened.structuredContent as { quickStart: string };
170 expect(openedContent.quickStart).toContain("--udid SIM-UDID");
171​
172 const shot = yield* server
173 .callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-1" } })
174 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
175 expect(shot.isError).toBe(false);
176 expect(shot.content.map((entry) => entry.type)).toEqual(["text", "image"]);
177 expect(shot.structuredContent).toMatchObject({
178 screenshot: { mimeType: "image/png", width: 1206, height: 2622 },
179 });
180​
181 const foreign = yield* server
182 .callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-2" } })
183 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
184 expect(foreign.isError).toBe(true);
185 expect(foreign.content.map((entry) => entry.type)).toEqual(["text"]);
186​
187 const denied = yield* server
188 .callTool({ name: "device_list", arguments: {} })
189 .pipe(callWith(["preview"]), Effect.provideService(McpSchema.McpServerClient, client));
190 expect(denied.isError).toBe(true);
191 }),
192 ).pipe(Effect.provide(layerTest)),
193);
194​
195it.effect("rejects unavailable agent access before booting or opening a device", () => {
196 const layerUnavailable = Layer.mock(DeviceService.DeviceService)({
197 list: Effect.succeed(state),
198 agentReadinessIfSupported: () =>
199 Effect.fail(
200 new DeviceHostUnavailableError({ hostId: "local", reason: "Agent access is disabled." }),
201 ),
202 open: () => Effect.die("Must not boot or register a device when agent access fails"),
203 });
204 return Effect.gen(function* () {
205 const server = yield* McpServer.McpServer;
206 const result = yield* server
207 .callTool({ name: "device_open", arguments: { platform: "ios" } })
208 .pipe(
209 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(["device"])),
210 Effect.provideService(McpSchema.McpServerClient, client),
211 );
212 expect(result.isError).toBe(true);
213 expect(result.content).toEqual(
214 expect.arrayContaining([
215 expect.objectContaining({
216 type: "text",
217 text: expect.stringContaining("Agent access is disabled."),
218 }),
219 ]),
220 );
221 }).pipe(
222 Effect.scoped,
223 Effect.provide(
224 McpHttpServer.layerDeviceToolkit.pipe(
225 Layer.provideMerge(McpServer.McpServer.layer),
226 Layer.provideMerge(McpToolAccessTestkit.liveThreadsLayer),
227 Layer.provide(layerUnavailable),
228 Layer.provide(layerProjectsMock),
229 Layer.provide(
230 ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-device-toolkit-test-" }),
231 ),
232 Layer.provide(NodeServices.layer),
233 ),
234 ),
235 );
236});