Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 130 additions & 182 deletions src/mcp/tools/ui-automation/__tests__/button.test.ts

Large diffs are not rendered by default.

318 changes: 133 additions & 185 deletions src/mcp/tools/ui-automation/__tests__/gesture.test.ts

Large diffs are not rendered by default.

270 changes: 121 additions & 149 deletions src/mcp/tools/ui-automation/__tests__/key_press.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Tests for key_press tool
*/

import { describe, it, expect, beforeEach } from 'vitest';
import * as z from 'zod';
import {
Expand All @@ -13,20 +9,13 @@ import {
import { sessionStore } from '../../../../utils/session-store.ts';
import { schema, handler, key_pressLogic } from '../key_press.ts';
import { AXE_NOT_AVAILABLE_MESSAGE } from '../../../../utils/axe-helpers.ts';
import { allText, runLogic } from '../../../../test-utils/test-helpers.ts';


function createDefaultMockAxeHelpers() {
return {
getAxePath: () => '/usr/local/bin/axe',
getBundledAxeEnvironment: () => ({}),
createAxeNotAvailableResponse: () => ({
content: [
{
type: 'text' as const,
text: AXE_NOT_AVAILABLE_MESSAGE,
},
],
isError: true,
}),
};
}

Expand Down Expand Up @@ -98,13 +87,15 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
trackingExecutor,
mockAxeHelpers,
await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
trackingExecutor,
mockAxeHelpers,
),
);

expect(capturedCommand).toEqual([
Expand All @@ -130,14 +121,16 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 42,
duration: 1.5,
},
trackingExecutor,
mockAxeHelpers,
await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 42,
duration: 1.5,
},
trackingExecutor,
mockAxeHelpers,
),
);

expect(capturedCommand).toEqual([
Expand Down Expand Up @@ -165,13 +158,15 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 255,
},
trackingExecutor,
mockAxeHelpers,
await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 255,
},
trackingExecutor,
mockAxeHelpers,
),
);

expect(capturedCommand).toEqual([
Expand All @@ -198,24 +193,17 @@ describe('Key Press Tool', () => {
const mockAxeHelpers = {
getAxePath: () => '/path/to/bundled/axe',
getBundledAxeEnvironment: () => ({ AXE_PATH: '/some/path' }),
createAxeNotAvailableResponse: () => ({
content: [
{
type: 'text' as const,
text: AXE_NOT_AVAILABLE_MESSAGE,
},
],
isError: true,
}),
};

await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 44,
},
trackingExecutor,
mockAxeHelpers,
await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 44,
},
trackingExecutor,
mockAxeHelpers,
),
);

expect(capturedCommand).toEqual([
Expand All @@ -241,19 +229,19 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
const result = await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
),
);

expect(result).toEqual({
content: [{ type: 'text' as const, text: 'Key press (code: 40) simulated successfully.' }],
isError: false,
});
expect(result.isError).toBeFalsy();
expect(allText(result)).toContain('Key press (code: 40) simulated successfully.');
});

it('should return success for key press with duration', async () => {
Expand All @@ -265,55 +253,41 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 42,
duration: 1.5,
},
mockExecutor,
mockAxeHelpers,
const result = await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 42,
duration: 1.5,
},
mockExecutor,
mockAxeHelpers,
),
);

expect(result).toEqual({
content: [{ type: 'text' as const, text: 'Key press (code: 42) simulated successfully.' }],
isError: false,
});
expect(result.isError).toBeFalsy();
expect(allText(result)).toContain('Key press (code: 42) simulated successfully.');
});

it('should handle DependencyError when axe is not available', async () => {
const mockAxeHelpers = {
getAxePath: () => null,
getBundledAxeEnvironment: () => ({}),
createAxeNotAvailableResponse: () => ({
content: [
{
type: 'text' as const,
text: AXE_NOT_AVAILABLE_MESSAGE,
},
],
isError: true,
}),
};

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
createNoopExecutor(),
mockAxeHelpers,
);

expect(result).toEqual({
content: [
const result = await runLogic(() =>
key_pressLogic(
{
type: 'text' as const,
text: AXE_NOT_AVAILABLE_MESSAGE,
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
],
isError: true,
});
createNoopExecutor(),
mockAxeHelpers,
),
);

expect(result.isError).toBe(true);
expect(allText(result)).toContain(AXE_NOT_AVAILABLE_MESSAGE);
});

it('should handle AxeError from failed command execution', async () => {
Expand All @@ -325,24 +299,21 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
);

expect(result).toEqual({
content: [
const result = await runLogic(() =>
key_pressLogic(
{
type: 'text' as const,
text: "Error: Failed to simulate key press (code: 40): axe command 'key' failed.\nDetails: axe command failed",
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
],
isError: true,
});
mockExecutor,
mockAxeHelpers,
),
);

expect(result.isError).toBe(true);
expect(allText(result)).toContain(
"Failed to simulate key press (code: 40): axe command 'key' failed.",
);
});

it('should handle SystemError from command execution', async () => {
Expand All @@ -352,18 +323,20 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
const result = await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
),
);

expect(result.isError).toBe(true);
expect(result.content[0].text).toContain(
'Error: System error executing axe: Failed to execute axe command: System error occurred',
expect(allText(result)).toContain(
'System error executing axe: Failed to execute axe command: System error occurred',
);
});

Expand All @@ -374,18 +347,20 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
const result = await runLogic(() =>
key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
),
);

expect(result.isError).toBe(true);
expect(result.content[0].text).toContain(
'Error: System error executing axe: Failed to execute axe command: Unexpected error',
expect(allText(result)).toContain(
'System error executing axe: Failed to execute axe command: Unexpected error',
);
});

Expand All @@ -396,24 +371,21 @@ describe('Key Press Tool', () => {

const mockAxeHelpers = createDefaultMockAxeHelpers();

const result = await key_pressLogic(
{
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
mockExecutor,
mockAxeHelpers,
);

expect(result).toEqual({
content: [
const result = await runLogic(() =>
key_pressLogic(
{
type: 'text' as const,
text: 'Error: System error executing axe: Failed to execute axe command: String error',
simulatorId: '12345678-1234-4234-8234-123456789012',
keyCode: 40,
},
],
isError: true,
});
mockExecutor,
mockAxeHelpers,
),
);

expect(result.isError).toBe(true);
expect(allText(result)).toContain(
'System error executing axe: Failed to execute axe command: String error',
);
});
});
});
Loading