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

McpDeviceToolkit.test.ts

176 lines6.2 KB

1import { expect, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import {
4 DeviceHostUnavailableError,
5 EnvironmentId,
6 ProviderInstanceId,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import { McpSchema, McpServer } from "effect/unstable/ai";
12​
13import * as ServerConfig from "../config.ts";
14import * as DeviceService from "../device/DeviceService.ts";
15import * as McpHttpServer from "./McpHttpServer.ts";
16import * as McpInvocationContext from "./McpInvocationContext.ts";
17​
18const environmentId = EnvironmentId.make("environment-device-test");
19const threadId = ThreadId.make("thread-device-test");
20const invocation = (capabilities: ReadonlyArray<McpInvocationContext.McpCapability>) => ({
21 environmentId,
22 threadId,
23 providerSessionId: "provider-session-device-test",
24 providerInstanceId: ProviderInstanceId.make("codex"),
25 capabilities: new Set(capabilities),
26 issuedAt: 1,
27});
28const client = McpSchema.McpServerClient.of({
29 clientId: 1,
30 clientCapabilities: {},
31 clientInfo: { name: "mcp-test", version: "1.0.0" },
32 protocolVersion: "2025-06-18",
33 initializePayload: {
34 protocolVersion: "2025-06-18",
35 capabilities: {},
36 clientInfo: { name: "mcp-test", version: "1.0.0" },
37 },
38 getClient: Effect.die("unused"),
39});
40​
41const device = {
42 hostId: "local",
43 id: "UDID-1",
44 platform: "ios" as const,
45 name: "iPhone 17 Pro",
46 version: "iOS 27.0",
47 booted: true,
48 physical: false,
49};
50const state = {
51 hosts: [
52 {
53 id: "local",
54 kind: "local" as const,
55 label: "This machine",
56 platforms: [
57 { platform: "ios" as const, available: true },
58 { platform: "android" as const, available: false, reason: "No SDK" },
59 ],
60 hubInstalled: true,
61 agentDeviceInstalled: true,
62 },
63 ],
64 hostStatus: "ready" as const,
65 hostStatuses: { local: { status: "ready" as const } },
66 devices: [device],
67 sessions: [],
68 onboardingCompleted: true,
69 agentAccessEnabled: true,
70 hubBasePath: "/api/device-hub",
71 revision: 1,
72};
73const png = new Uint8Array(24);
74new DataView(png.buffer).setUint32(0, 0x89504e47);
75new DataView(png.buffer).setUint32(4, 0x0d0a1a0a);
76new DataView(png.buffer).setUint32(12, 0x49484452);
77new DataView(png.buffer).setUint32(16, 1206);
78new DataView(png.buffer).setUint32(20, 2622);
79​
80const DeviceServiceMock = Layer.mock(DeviceService.DeviceService)({
81 state: Effect.succeed(state),
82 list: Effect.succeed(state),
83 open: (input) =>
84 Effect.succeed({
85 threadId: input.threadId,
86 hostId: "local",
87 deviceId: input.deviceId,
88 platform: input.platform,
89 openedAt: "2026-09-08T00:00:00.000Z",
90 }),
91 sessionsForThread: () => Effect.succeed([]),
92 screenshot: () => Effect.succeed({ device, png }),
93 close: () => Effect.void,
94 agentCli: Effect.succeed("/cli"),
95 testHost: () => Effect.die("not used"),
96 agentTarget: () => Effect.succeed(["--config", "/host.json", "--session", "thread-device"]),
97});
98​
99const TestLayer = McpHttpServer.DeviceToolkitRegistrationLive.pipe(
100 Layer.provideMerge(McpServer.McpServer.layer),
101 Layer.provideMerge(DeviceServiceMock),
102 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-device-toolkit-test-" })),
103 Layer.provide(NodeServices.layer),
104);
105​
106it.effect("registers the device tools and returns the screenshot as image content", () =>
107 Effect.scoped(
108 Effect.gen(function* () {
109 const server = yield* McpServer.McpServer;
110 const names = server.tools.map(({ tool }) => tool.name).toSorted();
111 expect(names).toEqual(["device_close", "device_list", "device_open", "device_screenshot"]);
112​
113 const callWith = (capabilities: ReadonlyArray<McpInvocationContext.McpCapability>) =>
114 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities));
115​
116 const opened = yield* server
117 .callTool({ name: "device_open", arguments: { platform: "ios" } })
118 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
119 expect(opened.isError).toBe(false);
120 const openedContent = opened.structuredContent as { quickStart: string };
121 expect(openedContent.quickStart).toContain("--udid UDID-1");
122​
123 const shot = yield* server
124 .callTool({ name: "device_screenshot", arguments: { deviceId: "UDID-1" } })
125 .pipe(callWith(["device"]), Effect.provideService(McpSchema.McpServerClient, client));
126 expect(shot.isError).toBe(false);
127 expect(shot.content.map((entry) => entry.type)).toEqual(["text", "image"]);
128 expect(shot.structuredContent).toMatchObject({
129 screenshot: { mimeType: "image/png", width: 1206, height: 2622 },
130 });
131​
132 const denied = yield* server
133 .callTool({ name: "device_list", arguments: {} })
134 .pipe(callWith(["preview"]), Effect.provideService(McpSchema.McpServerClient, client));
135 expect(denied.isError).toBe(true);
136 }),
137 ).pipe(Effect.provide(TestLayer)),
138);
139​
140it.effect("rejects unavailable agent access before booting or opening a device", () => {
141 const unavailable = Layer.mock(DeviceService.DeviceService)({
142 list: Effect.succeed(state),
143 agentTarget: () =>
144 Effect.fail(
145 new DeviceHostUnavailableError({ hostId: "local", reason: "Agent access is disabled." }),
146 ),
147 open: () => Effect.die("Must not boot or register a device when agent access fails"),
148 });
149 return Effect.gen(function* () {
150 const server = yield* McpServer.McpServer;
151 const result = yield* server
152 .callTool({ name: "device_open", arguments: { platform: "ios" } })
153 .pipe(
154 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(["device"])),
155 Effect.provideService(McpSchema.McpServerClient, client),
156 );
157 expect(result.isError).toBe(true);
158 expect(result.content).toEqual(
159 expect.arrayContaining([
160 expect.objectContaining({
161 type: "text",
162 text: expect.stringContaining("Agent access is disabled."),
163 }),
164 ]),
165 );
166 }).pipe(
167 Effect.scoped,
168 Effect.provide(
169 McpHttpServer.DeviceToolkitRegistrationLive.pipe(
170 Layer.provideMerge(McpServer.McpServer.layer),
171 Layer.provide(unavailable),
172 Layer.provide(NodeServices.layer),
173 ),
174 ),
175 );
176});