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