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

McpDeviceToolkit.test.ts

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