|
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | +import { AnthropicCUAClient } from "../../lib/v3/agent/AnthropicCUAClient.js"; |
| 3 | +import Anthropic from "@anthropic-ai/sdk"; |
| 4 | + |
| 5 | +vi.mock("@anthropic-ai/sdk", () => { |
| 6 | + const mockCreate = vi.fn(); |
| 7 | + |
| 8 | + return { |
| 9 | + default: class MockAnthropic { |
| 10 | + beta = { |
| 11 | + messages: { |
| 12 | + create: mockCreate, |
| 13 | + }, |
| 14 | + }; |
| 15 | + }, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +describe("AnthropicCUAClient triple_click handling", () => { |
| 20 | + let mockCreate: ReturnType<typeof vi.fn>; |
| 21 | + let client: AnthropicCUAClient; |
| 22 | + let executedActions: Array<Record<string, unknown>>; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + const anthropic = new Anthropic({ apiKey: "test" }); |
| 27 | + mockCreate = anthropic.beta.messages.create as ReturnType<typeof vi.fn>; |
| 28 | + |
| 29 | + client = new AnthropicCUAClient( |
| 30 | + "anthropic", |
| 31 | + "claude-sonnet-4-5-20250929", |
| 32 | + undefined, |
| 33 | + { |
| 34 | + apiKey: "test-key", |
| 35 | + }, |
| 36 | + ); |
| 37 | + client.setViewport(1280, 720); |
| 38 | + client.setScreenshotProvider(async () => "fake-base64-screenshot"); |
| 39 | + |
| 40 | + executedActions = []; |
| 41 | + client.setActionHandler(async (action) => { |
| 42 | + executedActions.push({ ...action }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should convert triple_click with coordinate array to tripleClick action", async () => { |
| 47 | + mockCreate.mockResolvedValue({ |
| 48 | + id: "test-id", |
| 49 | + content: [ |
| 50 | + { |
| 51 | + type: "tool_use", |
| 52 | + id: "tool-1", |
| 53 | + name: "computer", |
| 54 | + input: { |
| 55 | + action: "triple_click", |
| 56 | + coordinate: [640, 360], |
| 57 | + }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + usage: { input_tokens: 10, output_tokens: 20 }, |
| 61 | + }); |
| 62 | + |
| 63 | + const logger = vi.fn(); |
| 64 | + await client.executeStep( |
| 65 | + [{ role: "user", content: "triple click the paragraph" }], |
| 66 | + logger, |
| 67 | + ); |
| 68 | + |
| 69 | + expect(executedActions).toHaveLength(1); |
| 70 | + expect(executedActions[0].type).toBe("tripleClick"); |
| 71 | + expect(executedActions[0].x).toBe(640); |
| 72 | + expect(executedActions[0].y).toBe(360); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should convert triple_click with x/y fields to tripleClick action", async () => { |
| 76 | + mockCreate.mockResolvedValue({ |
| 77 | + id: "test-id", |
| 78 | + content: [ |
| 79 | + { |
| 80 | + type: "tool_use", |
| 81 | + id: "tool-2", |
| 82 | + name: "computer", |
| 83 | + input: { |
| 84 | + action: "triple_click", |
| 85 | + x: 100, |
| 86 | + y: 200, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ], |
| 90 | + usage: { input_tokens: 10, output_tokens: 20 }, |
| 91 | + }); |
| 92 | + |
| 93 | + const logger = vi.fn(); |
| 94 | + await client.executeStep( |
| 95 | + [{ role: "user", content: "triple click the line" }], |
| 96 | + logger, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(executedActions).toHaveLength(1); |
| 100 | + expect(executedActions[0].type).toBe("tripleClick"); |
| 101 | + expect(executedActions[0].x).toBe(100); |
| 102 | + expect(executedActions[0].y).toBe(200); |
| 103 | + }); |
| 104 | +}); |
0 commit comments