|
| 1 | +import type { AgentSideConnection } from "@agentclientprotocol/sdk"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { PostHogAPIConfig } from "../../types"; |
| 4 | + |
| 5 | +vi.mock("@anthropic-ai/claude-agent-sdk", () => ({ |
| 6 | + query: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const mocks = vi.hoisted(() => ({ |
| 10 | + updateTaskRun: vi.fn(), |
| 11 | + constructedConfigs: [] as unknown[], |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../../posthog-api", async (importOriginal) => { |
| 15 | + const actual = await importOriginal<typeof import("../../posthog-api")>(); |
| 16 | + class FakePostHogAPIClient { |
| 17 | + updateTaskRun = mocks.updateTaskRun; |
| 18 | + constructor(config: unknown) { |
| 19 | + mocks.constructedConfigs.push(config); |
| 20 | + } |
| 21 | + } |
| 22 | + return { ...actual, PostHogAPIClient: FakePostHogAPIClient }; |
| 23 | +}); |
| 24 | + |
| 25 | +const { ClaudeAcpAgent } = await import("./claude-agent"); |
| 26 | + |
| 27 | +const API_CONFIG: PostHogAPIConfig = { |
| 28 | + apiUrl: "https://us.posthog.com", |
| 29 | + getApiKey: () => "key", |
| 30 | + projectId: 1, |
| 31 | +}; |
| 32 | + |
| 33 | +type RequestFinish = ( |
| 34 | + status: "completed" | "failed", |
| 35 | + message?: string, |
| 36 | +) => Promise<void>; |
| 37 | + |
| 38 | +function buildRequestFinish( |
| 39 | + posthogApiConfig: PostHogAPIConfig | undefined, |
| 40 | + taskId: string | undefined, |
| 41 | + taskRunId: string | undefined, |
| 42 | +): RequestFinish | undefined { |
| 43 | + const client = { |
| 44 | + sessionUpdate: vi.fn().mockResolvedValue(undefined), |
| 45 | + } as unknown as AgentSideConnection; |
| 46 | + const agent = new ClaudeAcpAgent(client, { posthogApiConfig }); |
| 47 | + return ( |
| 48 | + agent as unknown as { |
| 49 | + buildRequestFinish( |
| 50 | + taskId: string | undefined, |
| 51 | + taskRunId: string | undefined, |
| 52 | + ): RequestFinish | undefined; |
| 53 | + } |
| 54 | + ).buildRequestFinish(taskId, taskRunId); |
| 55 | +} |
| 56 | + |
| 57 | +describe("ClaudeAcpAgent.buildRequestFinish", () => { |
| 58 | + beforeEach(() => { |
| 59 | + mocks.updateTaskRun.mockReset().mockResolvedValue({}); |
| 60 | + mocks.constructedConfigs.length = 0; |
| 61 | + }); |
| 62 | + |
| 63 | + it.each([ |
| 64 | + { |
| 65 | + name: "no posthogApiConfig", |
| 66 | + config: undefined, |
| 67 | + taskId: "task-1", |
| 68 | + taskRunId: "run-1", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "no taskId", |
| 72 | + config: API_CONFIG, |
| 73 | + taskId: undefined, |
| 74 | + taskRunId: "run-1", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "no taskRunId", |
| 78 | + config: API_CONFIG, |
| 79 | + taskId: "task-1", |
| 80 | + taskRunId: undefined, |
| 81 | + }, |
| 82 | + ])("is unavailable with $name", ({ config, taskId, taskRunId }) => { |
| 83 | + expect(buildRequestFinish(config, taskId, taskRunId)).toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("marks the run completed without an error_message", async () => { |
| 87 | + const requestFinish = buildRequestFinish(API_CONFIG, "task-1", "run-1"); |
| 88 | + await requestFinish?.("completed"); |
| 89 | + |
| 90 | + expect(mocks.constructedConfigs).toEqual([API_CONFIG]); |
| 91 | + expect(mocks.updateTaskRun).toHaveBeenCalledWith("task-1", "run-1", { |
| 92 | + status: "completed", |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("marks the run failed with the message as error_message", async () => { |
| 97 | + const requestFinish = buildRequestFinish(API_CONFIG, "task-1", "run-1"); |
| 98 | + await requestFinish?.("failed", "blocked on missing credentials"); |
| 99 | + |
| 100 | + expect(mocks.updateTaskRun).toHaveBeenCalledWith("task-1", "run-1", { |
| 101 | + status: "failed", |
| 102 | + error_message: "blocked on missing credentials", |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("omits error_message on a failed finish without a message", async () => { |
| 107 | + const requestFinish = buildRequestFinish(API_CONFIG, "task-1", "run-1"); |
| 108 | + await requestFinish?.("failed"); |
| 109 | + |
| 110 | + expect(mocks.updateTaskRun).toHaveBeenCalledWith("task-1", "run-1", { |
| 111 | + status: "failed", |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("rethrows when the API update fails", async () => { |
| 116 | + mocks.updateTaskRun.mockRejectedValue(new Error("api down")); |
| 117 | + const requestFinish = buildRequestFinish(API_CONFIG, "task-1", "run-1"); |
| 118 | + |
| 119 | + await expect(requestFinish?.("completed")).rejects.toThrow("api down"); |
| 120 | + }); |
| 121 | +}); |
0 commit comments