|
| 1 | +// pnpm --filter @roo-code/telemetry test src/__tests__/TelemetryService.task-completed.test.ts |
| 2 | + |
| 3 | +import { TelemetryEventName, type TelemetryClient } from "@roo-code/types" |
| 4 | + |
| 5 | +import { TelemetryService } from "../TelemetryService" |
| 6 | + |
| 7 | +describe("TelemetryService.captureTaskCompleted", () => { |
| 8 | + let mockClient: TelemetryClient |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + mockClient = { |
| 12 | + setProvider: vi.fn(), |
| 13 | + capture: vi.fn().mockResolvedValue(undefined), |
| 14 | + captureException: vi.fn().mockResolvedValue(undefined), |
| 15 | + updateTelemetryState: vi.fn(), |
| 16 | + isTelemetryEnabled: vi.fn().mockReturnValue(true), |
| 17 | + shutdown: vi.fn().mockResolvedValue(undefined), |
| 18 | + } |
| 19 | + }) |
| 20 | + |
| 21 | + it("captures Task Completed with the taskId and a default 'attempt_completion' completionReason when no summary is provided", () => { |
| 22 | + const service = new TelemetryService([mockClient]) |
| 23 | + |
| 24 | + service.captureTaskCompleted("task_1") |
| 25 | + |
| 26 | + expect(mockClient.capture).toHaveBeenCalledWith({ |
| 27 | + event: TelemetryEventName.TASK_COMPLETED, |
| 28 | + properties: { taskId: "task_1", completionReason: "attempt_completion" }, |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + it("includes toolsUsed and messageCount summaries when provided", () => { |
| 33 | + const service = new TelemetryService([mockClient]) |
| 34 | + |
| 35 | + service.captureTaskCompleted( |
| 36 | + "task_1", |
| 37 | + { read_file: { attempts: 3, failures: 0 }, apply_diff: { attempts: 1, failures: 1 } }, |
| 38 | + { user: 4, assistant: 5 }, |
| 39 | + ) |
| 40 | + |
| 41 | + expect(mockClient.capture).toHaveBeenCalledWith({ |
| 42 | + event: TelemetryEventName.TASK_COMPLETED, |
| 43 | + properties: { |
| 44 | + taskId: "task_1", |
| 45 | + completionReason: "attempt_completion", |
| 46 | + toolsUsed: { read_file: { attempts: 3, failures: 0 }, apply_diff: { attempts: 1, failures: 1 } }, |
| 47 | + messageCount: { user: 4, assistant: 5 }, |
| 48 | + }, |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it("includes the given completionReason for idle/shutdown installments", () => { |
| 53 | + const service = new TelemetryService([mockClient]) |
| 54 | + |
| 55 | + service.captureTaskCompleted("task_1", { read_file: { attempts: 1, failures: 0 } }, undefined, "idle") |
| 56 | + |
| 57 | + expect(mockClient.capture).toHaveBeenCalledWith({ |
| 58 | + event: TelemetryEventName.TASK_COMPLETED, |
| 59 | + properties: { |
| 60 | + taskId: "task_1", |
| 61 | + completionReason: "idle", |
| 62 | + toolsUsed: { read_file: { attempts: 1, failures: 0 } }, |
| 63 | + }, |
| 64 | + }) |
| 65 | + }) |
| 66 | +}) |
0 commit comments