-
Notifications
You must be signed in to change notification settings - Fork 0
Add E2E tests for all 10 apps (#1) #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
318af51
Add E2E tests for all 10 apps and fix stale integration tests
frouaix 8b5c753
Fix CI: update pnpm to v10 to match lockfile v9
frouaix 0b21fcc
Initial plan
Copilot c2ed66e
Initial plan
Copilot 570c0db
Initial plan
Copilot e592de2
Update packages/mcp-server/test/integration/e2e-apps.test.ts
frouaix cb28ba6
Fix readResponseById: clear timeout on success, remove handler on tim…
Copilot 7d1d302
Handle JSON-RPC error responses in callTool() helper
Copilot b0dbaa0
Gate E2E tests behind APPLESCRIPT_E2E flag and skip in CI
Copilot d1ecc40
Merge pull request #8 from frouaix/copilot/sub-pr-7
frouaix 792bc3b
Merge pull request #10 from frouaix/copilot/sub-pr-7-another-one
frouaix 1738665
Merge pull request #9 from frouaix/copilot/sub-pr-7-again
frouaix bae1e9c
Fix CI lint: add missing ESLint deps and configure Node globals
frouaix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| /** | ||
| * End-to-end tests for all 10 Apple apps via the MCP server. | ||
| * | ||
| * These tests spawn the real MCP server and communicate via JSON-RPC over | ||
| * stdio, exercising the full pipeline: server → adapter → Swift executor → macOS app. | ||
| * | ||
| * Requirements: | ||
| * - macOS with automation permissions granted for the test runner | ||
| * - Apps may need at least some data (containers) to return results | ||
| * | ||
| * Scope: | ||
| * - Readonly operations (list_containers, list, search) with real execution | ||
| * - Create/action operations with dryRun=true (pipeline validation only) | ||
| * - Error paths (invalid app, wrong mode) | ||
| * - NO destructive operations (update, delete, run_script) | ||
| */ | ||
| import { describe, it, before, after } from "node:test"; | ||
| import { strict as assert } from "node:assert"; | ||
| import { spawn, ChildProcess } from "node:child_process"; | ||
| import { resolve, dirname } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const SERVER_PATH = resolve(__dirname, "../../src/index.ts"); | ||
|
|
||
| let child: ChildProcess; | ||
| let nextId = 100; | ||
|
|
||
| function sendMessage(proc: ChildProcess, message: object): void { | ||
| proc.stdin!.write(JSON.stringify(message) + "\n"); | ||
| } | ||
|
|
||
| function readResponseById(proc: ChildProcess, id: number, timeoutMs = 15000): Promise<Record<string, unknown>> { | ||
| return new Promise((resolve, reject) => { | ||
| let buffer = ""; | ||
| const handler = (chunk: Buffer) => { | ||
| buffer += chunk.toString(); | ||
| const lines = buffer.split("\n"); | ||
| buffer = lines.pop() ?? ""; | ||
| for (const line of lines) { | ||
| if (!line.trim()) continue; | ||
| try { | ||
| const msg = JSON.parse(line) as Record<string, unknown>; | ||
| if (msg["id"] === id) { | ||
| proc.stdout!.off("data", handler); | ||
| clearTimeout(timer); | ||
| resolve(msg); | ||
| return; | ||
| } | ||
| } catch { | ||
| // skip | ||
| } | ||
| } | ||
| }; | ||
| proc.stdout!.on("data", handler); | ||
| const timer = setTimeout(() => { | ||
| proc.stdout!.off("data", handler); | ||
| reject(new Error(`Timeout waiting for response id=${id}`)); | ||
| }, timeoutMs); | ||
| timer.unref(); | ||
| }); | ||
| } | ||
|
|
||
| /** Call a tool and return the parsed first text content. */ | ||
| async function callTool(name: string, args: Record<string, unknown> = {}): Promise<{ | ||
| raw: Record<string, unknown>; | ||
| text: string; | ||
| parsed: unknown; | ||
| isError: boolean; | ||
| }> { | ||
| const id = nextId++; | ||
| const promise = readResponseById(child, id); | ||
| sendMessage(child, { | ||
| jsonrpc: "2.0", | ||
| id, | ||
| method: "tools/call", | ||
| params: { name, arguments: args }, | ||
| }); | ||
| const response = await promise; | ||
| if (response["error"]) { | ||
| const err = response["error"] as Record<string, unknown>; | ||
| const code = err["code"] != null ? ` (code: ${err["code"]})` : ""; | ||
| const text = String(err["message"] ?? JSON.stringify(err)) + code; | ||
| return { raw: response, text, parsed: err, isError: true }; | ||
| } | ||
| const result = response["result"] as Record<string, unknown>; | ||
| const content = result["content"] as Array<Record<string, unknown>>; | ||
| const text = (content[0]?.["text"] as string) ?? ""; | ||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(text); | ||
| } catch { | ||
| parsed = text; | ||
| } | ||
| return { raw: response, text, parsed, isError: !!result["isError"] }; | ||
| } | ||
|
frouaix marked this conversation as resolved.
|
||
|
|
||
| /** Initialize the server and complete the handshake. */ | ||
| async function initServer(): Promise<void> { | ||
| const id = nextId++; | ||
| const promise = readResponseById(child, id); | ||
| sendMessage(child, { | ||
| jsonrpc: "2.0", | ||
| id, | ||
| method: "initialize", | ||
| params: { | ||
| protocolVersion: "2024-11-05", | ||
| capabilities: {}, | ||
| clientInfo: { name: "e2e-test", version: "1.0" }, | ||
| }, | ||
| }); | ||
| await promise; | ||
| sendMessage(child, { jsonrpc: "2.0", method: "notifications/initialized" }); | ||
| await new Promise((r) => setTimeout(r, 300)); | ||
| } | ||
|
|
||
| const ALL_APPS = [ | ||
| "notes", "calendar", "reminders", "mail", "contacts", | ||
| "messages", "photos", "music", "finder", "safari", | ||
| ]; | ||
|
|
||
| // ─── Test suite ─────────────────────────────────────────────────────────────── | ||
| // These tests require macOS with Automation permissions and real apps running. | ||
| // Gate behind APPLESCRIPT_E2E=1 and skip automatically in CI. | ||
|
|
||
| const skipReason: string | undefined = process.env.CI | ||
| ? "Skipped in CI environment (set APPLESCRIPT_E2E=1 and unset CI to run)" | ||
| : !process.env.APPLESCRIPT_E2E | ||
| ? "Set APPLESCRIPT_E2E=1 to run E2E tests locally" | ||
| : undefined; | ||
|
|
||
| describe("E2E app tests", { skip: skipReason }, () => { | ||
| before(async () => { | ||
| const env = { ...process.env }; | ||
| delete env["APPLESCRIPT_MCP_CONFIG"]; | ||
| child = spawn("npx", ["tsx", SERVER_PATH], { | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| env, | ||
| }); | ||
| await initServer(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| child.kill("SIGTERM"); | ||
| }); | ||
|
|
||
| // ── System tools ────────────────────────────────────────────────────────── | ||
|
|
||
| describe("system tools", () => { | ||
| it("ping returns version and app list", async () => { | ||
| const { parsed } = await callTool("applescript.ping"); | ||
| const p = parsed as Record<string, unknown>; | ||
| assert.equal(p.ok, true); | ||
| assert.ok(typeof p.version === "string"); | ||
| assert.ok(Array.isArray(p.apps)); | ||
| assert.equal((p.apps as string[]).length, 10); | ||
| }); | ||
|
|
||
| it("get_mode returns readonly by default", async () => { | ||
| const { parsed } = await callTool("applescript.get_mode"); | ||
| const p = parsed as Record<string, unknown>; | ||
| assert.equal(p.mode, "readonly"); | ||
| }); | ||
| }); | ||
|
|
||
| // ── Readonly per-app ────────────────────────────────────────────────────── | ||
|
|
||
| describe("readonly operations", () => { | ||
| for (const app of ALL_APPS) { | ||
| describe(app, () => { | ||
| let hadSuccess = false; | ||
|
|
||
| after(() => { | ||
| // Ensure that at least one readonly operation succeeded for this app. | ||
| assert.ok( | ||
| hadSuccess, | ||
| `Expected at least one successful readonly operation for app ${app}`, | ||
| ); | ||
| }); | ||
|
|
||
| it(`${app}: list_containers`, async () => { | ||
| const { parsed, isError } = await callTool("app.list_containers", { app }); | ||
| // Some apps may error if not running or no permission — that's useful signal too | ||
| if (!isError) { | ||
| assert.ok( | ||
| Array.isArray(parsed) || typeof parsed === "object", | ||
| `Expected array or object, got ${typeof parsed}`, | ||
| ); | ||
| hadSuccess = true; | ||
| } else { | ||
| // Validate error shape so error paths are also covered. | ||
| assert.ok( | ||
| parsed !== null && typeof parsed === "object", | ||
| `Expected structured error object, got ${typeof parsed}`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it(`${app}: list (limit 3)`, async () => { | ||
| const { parsed, isError } = await callTool("app.list", { app, limit: 3 }); | ||
| if (!isError) { | ||
| assert.ok( | ||
| Array.isArray(parsed) || typeof parsed === "object", | ||
| `Expected array or object, got ${typeof parsed}`, | ||
| ); | ||
| hadSuccess = true; | ||
| } else { | ||
| assert.ok( | ||
| parsed !== null && typeof parsed === "object", | ||
| `Expected structured error object, got ${typeof parsed}`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it(`${app}: search`, async () => { | ||
| const { parsed, isError } = await callTool("app.search", { app, query: "test", limit: 3 }); | ||
| if (!isError) { | ||
| assert.ok( | ||
| Array.isArray(parsed) || typeof parsed === "object", | ||
| `Expected array or object, got ${typeof parsed}`, | ||
| ); | ||
| hadSuccess = true; | ||
| } else { | ||
| assert.ok( | ||
| parsed !== null && typeof parsed === "object", | ||
| `Expected structured error object, got ${typeof parsed}`, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // ── Create mode with dryRun ─────────────────────────────────────────────── | ||
|
|
||
| describe("create mode (dryRun)", () => { | ||
| it("switch to create mode", async () => { | ||
| const { parsed } = await callTool("applescript.set_mode", { mode: "create" }); | ||
| const p = parsed as Record<string, unknown>; | ||
| assert.equal(p.newMode, "create"); | ||
| }); | ||
|
|
||
| const createTests: Array<{ app: string; properties: Record<string, unknown> }> = [ | ||
| { app: "notes", properties: { title: "E2E Test Note", body: "test body" } }, | ||
| { app: "calendar", properties: { title: "E2E Test Event", startDate: "2026-03-01T10:00:00", endDate: "2026-03-01T11:00:00" } }, | ||
| { app: "reminders", properties: { name: "E2E Test Reminder" } }, | ||
| { app: "mail", properties: { to: "test@example.com", subject: "E2E test", body: "test" } }, | ||
| { app: "contacts", properties: { firstName: "E2E", lastName: "Test" } }, | ||
| { app: "safari", properties: { url: "https://example.com" } }, | ||
| ]; | ||
|
|
||
| for (const { app, properties } of createTests) { | ||
| it(`${app}: create (dryRun)`, async () => { | ||
| const { text, isError } = await callTool("app.create", { app, properties, dryRun: true }); | ||
| // dryRun should return the generated script, not an error | ||
| assert.ok(text.length > 0, "Expected non-empty response"); | ||
| // If the adapter supports create and dryRun worked, text should contain script | ||
| if (!isError) { | ||
| assert.ok(typeof text === "string"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const actionTests: Array<{ app: string; action: string; parameters?: Record<string, unknown> }> = [ | ||
| { app: "notes", action: "show" }, | ||
| { app: "calendar", action: "show" }, | ||
| { app: "reminders", action: "show" }, | ||
| { app: "music", action: "now_playing" }, | ||
| ]; | ||
|
|
||
| for (const { app, action, parameters } of actionTests) { | ||
| it(`${app}: action "${action}" (dryRun)`, async () => { | ||
| const { text } = await callTool("app.action", { app, action, parameters: parameters ?? {}, dryRun: true }); | ||
| assert.ok(text.length > 0, "Expected non-empty response"); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // ── Error paths ─────────────────────────────────────────────────────────── | ||
|
|
||
| describe("error paths", () => { | ||
| it("rejects invalid app name", async () => { | ||
| const id = nextId++; | ||
| const promise = readResponseById(child, id); | ||
| sendMessage(child, { | ||
| jsonrpc: "2.0", | ||
| id, | ||
| method: "tools/call", | ||
| params: { name: "app.list_containers", arguments: { app: "nonexistent" } }, | ||
| }); | ||
| const response = await promise; | ||
| // Should return an error (either in result.isError or as JSON-RPC error) | ||
| const error = response["error"] as Record<string, unknown> | undefined; | ||
| const result = response["result"] as Record<string, unknown> | undefined; | ||
| assert.ok(error || result?.["isError"], | ||
| "Expected error for invalid app name"); | ||
| }); | ||
|
|
||
| it("app.create rejected in readonly mode", async () => { | ||
| // Switch back to readonly | ||
| await callTool("applescript.set_mode", { mode: "readonly" }); | ||
| await new Promise((r) => setTimeout(r, 300)); | ||
|
|
||
| // Try to call app.create — should fail (tool not found / disabled) | ||
| const id = nextId++; | ||
| const promise = readResponseById(child, id); | ||
| sendMessage(child, { | ||
| jsonrpc: "2.0", | ||
| id, | ||
| method: "tools/call", | ||
| params: { name: "app.create", arguments: { app: "notes", properties: { title: "should fail" } } }, | ||
| }); | ||
| const response = await promise; | ||
| const error = response["error"] as Record<string, unknown> | undefined; | ||
| const result = response["result"] as Record<string, unknown> | undefined; | ||
| // Disabled tools may return a JSON-RPC error or a result with isError | ||
| assert.ok(error || result?.["isError"], | ||
| "Expected error when calling create in readonly mode"); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.