|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { afterEach, test, vi } from 'vitest'; |
| 6 | +import { createAgentDeviceClient } from '../../agent-device-client.ts'; |
| 7 | +import type { AgentDeviceClient, AgentDeviceDaemonTransport } from '../../client/client-types.ts'; |
| 8 | +import type { CommandExecutionResult } from '../../commands/command-surface.ts'; |
| 9 | +import { createCommandToolExecutor, listCommandTools } from '../command-tools.ts'; |
| 10 | +import { validateAgainstSchema } from './output-schema-validator.ts'; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + vi.unstubAllEnvs(); |
| 14 | + if (temporaryDirectory) { |
| 15 | + fs.rmSync(temporaryDirectory, { recursive: true, force: true }); |
| 16 | + temporaryDirectory = undefined; |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +let temporaryDirectory: string | undefined; |
| 21 | + |
| 22 | +test('MCP collection results use object envelopes without changing object results or text', async () => { |
| 23 | + const results = { |
| 24 | + devices: [ |
| 25 | + { |
| 26 | + id: 'device-1', |
| 27 | + name: 'iPhone', |
| 28 | + platform: 'ios', |
| 29 | + target: 'mobile', |
| 30 | + kind: 'device', |
| 31 | + identifiers: {}, |
| 32 | + }, |
| 33 | + ], |
| 34 | + apps: ['com.example.app'], |
| 35 | + wait: { waitedMs: 10 }, |
| 36 | + } satisfies Record<'devices' | 'apps' | 'wait', CommandExecutionResult>; |
| 37 | + const executor = createCommandToolExecutor({ |
| 38 | + createClient: () => ({}) as AgentDeviceClient, |
| 39 | + runCommand: async (_client, name) => results[name as keyof typeof results], |
| 40 | + }); |
| 41 | + |
| 42 | + const devices = await executor.execute('devices', {}); |
| 43 | + const apps = await executor.execute('apps', {}); |
| 44 | + const wait = await executor.execute('wait', {}); |
| 45 | + |
| 46 | + assert.deepEqual(devices.structuredContent, { |
| 47 | + devices: [ |
| 48 | + { id: 'device-1', name: 'iPhone', platform: 'ios', target: 'mobile', kind: 'device' }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + assert.deepEqual(apps.structuredContent, { apps: results.apps }); |
| 52 | + assert.deepEqual(wait.structuredContent, results.wait); |
| 53 | + assert.equal(devices.content[0]?.text, 'iPhone (ios device target=mobile)'); |
| 54 | + assert.equal(apps.content[0]?.text, 'com.example.app'); |
| 55 | + |
| 56 | + for (const [name, result] of [ |
| 57 | + ['devices', devices], |
| 58 | + ['apps', apps], |
| 59 | + ] as const) { |
| 60 | + const schema = listCommandTools().find((tool) => tool.name === name)?.outputSchema; |
| 61 | + assert.ok(schema, `${name} advertises an output schema`); |
| 62 | + assert.deepEqual(validateAgainstSchema(result.structuredContent, schema), []); |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +test('MCP applies config-backed command defaults with explicit-input precedence and applicability', async () => { |
| 67 | + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-mcp-config-')); |
| 68 | + temporaryDirectory = home; |
| 69 | + const configuredXctestrun = path.join(home, 'configured.xctestrun'); |
| 70 | + fs.mkdirSync(path.join(home, '.agent-device')); |
| 71 | + fs.writeFileSync( |
| 72 | + path.join(home, '.agent-device', 'config.json'), |
| 73 | + JSON.stringify({ iosXctestrunFile: configuredXctestrun, appsFilter: 'all' }), |
| 74 | + ); |
| 75 | + vi.stubEnv('HOME', home); |
| 76 | + |
| 77 | + const calls: Array<Parameters<AgentDeviceDaemonTransport>[0]> = []; |
| 78 | + const transport: AgentDeviceDaemonTransport = async (request) => { |
| 79 | + calls.push(request); |
| 80 | + return { ok: true, data: { nodes: [], truncated: false } }; |
| 81 | + }; |
| 82 | + const executor = createCommandToolExecutor({ |
| 83 | + createClient: (config) => createAgentDeviceClient(config, { transport }), |
| 84 | + }); |
| 85 | + |
| 86 | + await executor.execute('snapshot', {}); |
| 87 | + await executor.execute('snapshot', { iosXctestrunFile: '/explicit/runner.xctestrun' }); |
| 88 | + |
| 89 | + assert.deepEqual( |
| 90 | + calls.map((request) => request.flags?.iosXctestrunFile), |
| 91 | + [configuredXctestrun, '/explicit/runner.xctestrun'], |
| 92 | + ); |
| 93 | + assert.ok(calls.every((request) => request.command === 'snapshot')); |
| 94 | + assert.ok(calls.every((request) => request.flags?.appsFilter === undefined)); |
| 95 | +}); |
0 commit comments