The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect } from "bun:test"; |
| 2 | import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 4 | import { AgentClient } from "agents/client"; |
| 5 | import type { Counter, CounterState } from "../src/server.ts"; |
| 6 | |
| 7 | // The checks shared by the deployed test and the `alchemy dev` test. |
| 8 | |
| 9 | // A fresh workers.dev route can refuse the first WebSocket upgrade, which |
| 10 | // the client doesn't retry, so connect again until one is ready. |
| 11 | const connect = async (url: string, name: string) => { |
| 12 | const { host, protocol } = new URL(url); |
| 13 | for (let attempt = 1; ; attempt++) { |
| 14 | const agent = new AgentClient<Counter, CounterState>({ |
| 15 | agent: "counter", |
| 16 | name, |
| 17 | host, |
| 18 | protocol: protocol === "https:" ? "wss" : "ws", |
| 19 | }); |
| 20 | const ready = await Promise.race([ |
| 21 | agent.ready.then(() => true), |
| 22 | new Promise<false>((resolve) => setTimeout(() => resolve(false), 15_000)), |
| 23 | ]); |
| 24 | if (ready) return agent; |
| 25 | agent.close(); |
| 26 | if (attempt === 8) throw new Error(`agent ${name} never connected`); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | const eventually = async <T>(read: () => Promise<T>, done: (value: T) => boolean) => { |
| 31 | for (let attempt = 0; attempt < 30; attempt++) { |
| 32 | const value = await read(); |
| 33 | if (done(value)) return value; |
| 34 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 35 | } |
| 36 | return read(); |
| 37 | }; |
| 38 | |
| 39 | /** The SPA shell from `index.html` and a static asset from `public/`. */ |
| 40 | export const checkAssets = async (url: string) => { |
| 41 | const html = await (await fetch(url)).text(); |
| 42 | expect(html).toContain(`<div id="root"></div>`); |
| 43 | // Static assets can take a few seconds to propagate after a deploy. |
| 44 | const asset = await eventually( |
| 45 | async () => (await fetch(`${url}/agents.txt`)).text(), |
| 46 | (body) => body.includes("served from public/"), |
| 47 | ); |
| 48 | expect(asset).toContain("served from public/"); |
| 49 | }; |
| 50 | |
| 51 | /** `@callable()` methods over the agent's WebSocket, with synced state. */ |
| 52 | export const checkCallables = async (url: string) => { |
| 53 | const name = `counter-${crypto.randomUUID()}`; |
| 54 | const agent = await connect(url, name); |
| 55 | try { |
| 56 | expect(await agent.call("increment", [2])).toBe(2); |
| 57 | expect(await agent.call("increment", [3])).toBe(5); |
| 58 | await eventually( |
| 59 | async () => agent.state, |
| 60 | (state) => state?.count === 5, |
| 61 | ); |
| 62 | expect(agent.state?.count).toBe(5); |
| 63 | // The same instance over plain HTTP (`onRequest`). |
| 64 | const state = (await (await fetch(`${url}/agents/counter/${name}`)).json()) as CounterState; |
| 65 | expect(state.count).toBe(5); |
| 66 | } finally { |
| 67 | agent.close(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | /** `schedule()` calls the agent back after a delay. */ |
| 72 | export const checkSchedule = async (url: string) => { |
| 73 | const name = `schedule-${crypto.randomUUID()}`; |
| 74 | const agent = await connect(url, name); |
| 75 | try { |
| 76 | await agent.call("remindInOneSecond", []); |
| 77 | } finally { |
| 78 | agent.close(); |
| 79 | } |
| 80 | const state = await eventually( |
| 81 | async () => (await (await fetch(`${url}/agents/counter/${name}`)).json()) as CounterState, |
| 82 | (state) => state.reminders === 1, |
| 83 | ); |
| 84 | expect(state.reminders).toBe(1); |
| 85 | }; |
| 86 | |
| 87 | /** The `McpAgent` server's tool over MCP's streamable HTTP transport. */ |
| 88 | export const checkMcp = async (url: string) => { |
| 89 | const client = new Client({ name: "alchemy-example-test", version: "1.0.0" }); |
| 90 | await client.connect(new StreamableHTTPClientTransport(new URL(`${url}/mcp`))); |
| 91 | try { |
| 92 | const { tools } = await client.listTools(); |
| 93 | expect(tools.map((tool) => tool.name)).toContain("add"); |
| 94 | const result = (await client.callTool({ name: "add", arguments: { a: 2, b: 3 } })) as { |
| 95 | content: Array<{ type: string; text: string }>; |
| 96 | }; |
| 97 | expect(result.content[0]?.text).toBe("5"); |
| 98 | } finally { |
| 99 | await client.close(); |
| 100 | } |
| 101 | }; |