|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ServerNotification } from "../../app-server"; |
| 3 | +import type { SessionState } from "../../CodexAcpServer"; |
| 4 | +import { AgentMode } from "../../AgentMode"; |
| 5 | +import { |
| 6 | + createCodexMockTestFixture, |
| 7 | + createTestSessionState, |
| 8 | + setupPromptAndSendNotifications, |
| 9 | + type CodexMockTestFixture |
| 10 | +} from "../acp-test-utils"; |
| 11 | + |
| 12 | +describe("CodexEventHandler - web search events", () => { |
| 13 | + let mockFixture: CodexMockTestFixture; |
| 14 | + const sessionId = "test-session-id"; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockFixture = createCodexMockTestFixture(); |
| 18 | + vi.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + const sessionState: SessionState = createTestSessionState({ |
| 22 | + sessionId, |
| 23 | + currentModelId: "model-id[effort]", |
| 24 | + agentMode: AgentMode.DEFAULT_AGENT_MODE |
| 25 | + }); |
| 26 | + |
| 27 | + it("maps web search start and completion to a search tool call", async () => { |
| 28 | + const notifications: ServerNotification[] = [ |
| 29 | + { |
| 30 | + method: "item/started", |
| 31 | + params: { |
| 32 | + threadId: sessionId, |
| 33 | + turnId: "turn-1", |
| 34 | + startedAtMs: 0, |
| 35 | + item: { |
| 36 | + type: "webSearch", |
| 37 | + id: "web-search-1", |
| 38 | + query: "agent client protocol", |
| 39 | + action: { |
| 40 | + type: "search", |
| 41 | + query: "agent client protocol", |
| 42 | + queries: null, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + method: "item/completed", |
| 49 | + params: { |
| 50 | + threadId: sessionId, |
| 51 | + turnId: "turn-1", |
| 52 | + completedAtMs: 0, |
| 53 | + item: { |
| 54 | + type: "webSearch", |
| 55 | + id: "web-search-1", |
| 56 | + query: "agent client protocol", |
| 57 | + action: { |
| 58 | + type: "search", |
| 59 | + query: "agent client protocol", |
| 60 | + queries: null, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ]; |
| 66 | + |
| 67 | + await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, notifications); |
| 68 | + |
| 69 | + await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot( |
| 70 | + "data/web-search-start-and-complete.json" |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("formats open-page and find-in-page web search actions", async () => { |
| 75 | + const notifications: ServerNotification[] = [ |
| 76 | + { |
| 77 | + method: "item/started", |
| 78 | + params: { |
| 79 | + threadId: sessionId, |
| 80 | + turnId: "turn-1", |
| 81 | + startedAtMs: 0, |
| 82 | + item: { |
| 83 | + type: "webSearch", |
| 84 | + id: "web-open-1", |
| 85 | + query: "https://agentclientprotocol.com", |
| 86 | + action: { |
| 87 | + type: "openPage", |
| 88 | + url: "https://agentclientprotocol.com", |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + method: "item/started", |
| 95 | + params: { |
| 96 | + threadId: sessionId, |
| 97 | + turnId: "turn-1", |
| 98 | + startedAtMs: 0, |
| 99 | + item: { |
| 100 | + type: "webSearch", |
| 101 | + id: "web-find-1", |
| 102 | + query: "protocol", |
| 103 | + action: { |
| 104 | + type: "findInPage", |
| 105 | + url: "https://agentclientprotocol.com/protocol", |
| 106 | + pattern: "tool calls", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ]; |
| 112 | + |
| 113 | + await setupPromptAndSendNotifications(mockFixture, sessionId, sessionState, notifications); |
| 114 | + |
| 115 | + await expect(mockFixture.getAcpConnectionDump([])).toMatchFileSnapshot( |
| 116 | + "data/web-search-action-titles.json" |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments