| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import { telemetryService } from "./telemetryService.js"; |
| 4 | |
| 5 | describe("TelemetryService - Session Metadata", () => { |
| 6 | let originalEnv: NodeJS.ProcessEnv; |
| 7 | let originalArgv: string[]; |
| 8 | |
| 9 | beforeEach(() => { |
| 10 | // Save original environment and argv |
| 11 | originalEnv = { ...process.env }; |
| 12 | originalArgv = [...process.argv]; |
| 13 | |
| 14 | // Mock logger to prevent console output during tests |
| 15 | vi.mock("../util/logger.js", () => ({ |
| 16 | logger: { |
| 17 | debug: vi.fn(), |
| 18 | info: vi.fn(), |
| 19 | warn: vi.fn(), |
| 20 | error: vi.fn(), |
| 21 | }, |
| 22 | })); |
| 23 | |
| 24 | // Enable telemetry for tests |
| 25 | process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "1"; |
| 26 | process.env.OTEL_METRICS_EXPORTER = "console"; |
| 27 | }); |
| 28 | |
| 29 | afterEach(() => { |
| 30 | // Restore original environment and argv |
| 31 | process.env = originalEnv; |
| 32 | process.argv = originalArgv; |
| 33 | vi.clearAllMocks(); |
| 34 | }); |
| 35 | |
| 36 | describe("recordSessionStart", () => { |
| 37 | it("should include is_headless=false when not in headless mode", () => { |
| 38 | // Set up non-headless mode (no -p or --print flags) |
| 39 | process.argv = ["node", "cli.js", "chat"]; |
| 40 | |
| 41 | const mockAdd = vi.fn(); |
| 42 | const mockRecord = vi.fn(); |
| 43 | const service = telemetryService as any; |
| 44 | |
| 45 | // Mock the session counter and startup time histogram |
| 46 | service.sessionCounter = { add: mockAdd }; |
| 47 | service.startupTimeHistogram = { record: mockRecord }; |
| 48 | service.config = { enabled: true }; |
| 49 | service.meter = {}; |
| 50 | |
| 51 | service.recordSessionStart(); |
| 52 | |
| 53 | expect(mockAdd).toHaveBeenCalledWith( |
| 54 | 1, |
| 55 | expect.objectContaining({ |
| 56 | is_headless: "false", |
| 57 | }), |
| 58 | ); |
| 59 | }); |
| 60 | |
| 61 | it("should include is_headless=true when -p flag is present", () => { |
| 62 | // Set up headless mode with -p flag |
| 63 | process.argv = ["node", "cli.js", "-p", "What is the weather?"]; |
| 64 | |
| 65 | const mockAdd = vi.fn(); |
| 66 | const mockRecord = vi.fn(); |
| 67 | const service = telemetryService as any; |
| 68 | |
| 69 | // Mock the session counter and startup time histogram |
| 70 | service.sessionCounter = { add: mockAdd }; |
| 71 | service.startupTimeHistogram = { record: mockRecord }; |
| 72 | service.config = { enabled: true }; |
| 73 | service.meter = {}; |
| 74 | |
| 75 | service.recordSessionStart(); |
| 76 | |
| 77 | expect(mockAdd).toHaveBeenCalledWith( |
| 78 | 1, |
| 79 | expect.objectContaining({ |
| 80 | is_headless: "true", |
| 81 | }), |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | it("should include is_headless=true when --print flag is present", () => { |
| 86 | // Set up headless mode with --print flag |
| 87 | process.argv = ["node", "cli.js", "--print", "What is the weather?"]; |
| 88 | |
| 89 | const mockAdd = vi.fn(); |
| 90 | const mockRecord = vi.fn(); |
| 91 | const service = telemetryService as any; |
| 92 | |
| 93 | // Mock the session counter and startup time histogram |
| 94 | service.sessionCounter = { add: mockAdd }; |
| 95 | service.startupTimeHistogram = { record: mockRecord }; |
| 96 | service.config = { enabled: true }; |
| 97 | service.meter = {}; |
| 98 | |
| 99 | service.recordSessionStart(); |
| 100 | |
| 101 | expect(mockAdd).toHaveBeenCalledWith( |
| 102 | 1, |
| 103 | expect.objectContaining({ |
| 104 | is_headless: "true", |
| 105 | }), |
| 106 | ); |
| 107 | }); |
| 108 | |
| 109 | it("should include is_github_actions=false when not in GitHub Actions", () => { |
| 110 | // Ensure GITHUB_ACTIONS is not set |
| 111 | delete process.env.GITHUB_ACTIONS; |
| 112 | |
| 113 | const mockAdd = vi.fn(); |
| 114 | const mockRecord = vi.fn(); |
| 115 | const service = telemetryService as any; |
| 116 | |
| 117 | // Mock the session counter and startup time histogram |
| 118 | service.sessionCounter = { add: mockAdd }; |
| 119 | service.startupTimeHistogram = { record: mockRecord }; |
| 120 | service.config = { enabled: true }; |
| 121 | service.meter = {}; |
| 122 | |
| 123 | service.recordSessionStart(); |
| 124 | |
| 125 | expect(mockAdd).toHaveBeenCalledWith( |
| 126 | 1, |
| 127 | expect.objectContaining({ |
| 128 | is_github_actions: "false", |
| 129 | is_continue_remote_agent: "false", |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | it("should include is_github_actions=true when in GitHub Actions", () => { |
| 135 | // Set GitHub Actions environment |
| 136 | process.env.GITHUB_ACTIONS = "true"; |
| 137 | |
| 138 | const mockAdd = vi.fn(); |
| 139 | const mockRecord = vi.fn(); |
| 140 | const service = telemetryService as any; |
| 141 | |
| 142 | // Mock the session counter and startup time histogram |
| 143 | service.sessionCounter = { add: mockAdd }; |
| 144 | service.startupTimeHistogram = { record: mockRecord }; |
| 145 | service.config = { enabled: true }; |
| 146 | service.meter = {}; |
| 147 | |
| 148 | service.recordSessionStart(); |
| 149 | |
| 150 | expect(mockAdd).toHaveBeenCalledWith( |
| 151 | 1, |
| 152 | expect.objectContaining({ |
| 153 | is_github_actions: "true", |
| 154 | is_continue_remote_agent: "false", |
| 155 | }), |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | it("should include both headless and GitHub Actions metadata when both are true", () => { |
| 160 | // Set both headless mode and GitHub Actions |
| 161 | process.argv = ["node", "cli.js", "-p", "What is the weather?"]; |
| 162 | process.env.GITHUB_ACTIONS = "true"; |
| 163 | |
| 164 | const mockAdd = vi.fn(); |
| 165 | const mockRecord = vi.fn(); |
| 166 | const service = telemetryService as any; |
| 167 | |
| 168 | // Mock the session counter and startup time histogram |
| 169 | service.sessionCounter = { add: mockAdd }; |
| 170 | service.startupTimeHistogram = { record: mockRecord }; |
| 171 | service.config = { enabled: true }; |
| 172 | service.meter = {}; |
| 173 | |
| 174 | service.recordSessionStart(); |
| 175 | |
| 176 | expect(mockAdd).toHaveBeenCalledWith( |
| 177 | 1, |
| 178 | expect.objectContaining({ |
| 179 | is_headless: "true", |
| 180 | is_github_actions: "true", |
| 181 | is_continue_remote_agent: "false", |
| 182 | }), |
| 183 | ); |
| 184 | }); |
| 185 | |
| 186 | it("should include is_continue_remote_agent=true when in remote agent mode", () => { |
| 187 | // Set Continue remote agent environment |
| 188 | process.env.CONTINUE_REMOTE = "true"; |
| 189 | |
| 190 | const mockAdd = vi.fn(); |
| 191 | const mockRecord = vi.fn(); |
| 192 | const service = telemetryService as any; |
| 193 | |
| 194 | // Mock the session counter and startup time histogram |
| 195 | service.sessionCounter = { add: mockAdd }; |
| 196 | service.startupTimeHistogram = { record: mockRecord }; |
| 197 | service.config = { enabled: true }; |
| 198 | service.meter = {}; |
| 199 | |
| 200 | service.recordSessionStart(); |
| 201 | |
| 202 | expect(mockAdd).toHaveBeenCalledWith( |
| 203 | 1, |
| 204 | expect.objectContaining({ |
| 205 | is_continue_remote_agent: "true", |
| 206 | }), |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | it("should include all environment flags when all are true", () => { |
| 211 | // Set all environments |
| 212 | process.argv = ["node", "cli.js", "-p", "What is the weather?"]; |
| 213 | process.env.GITHUB_ACTIONS = "true"; |
| 214 | process.env.CONTINUE_REMOTE = "true"; |
| 215 | |
| 216 | const mockAdd = vi.fn(); |
| 217 | const mockRecord = vi.fn(); |
| 218 | const service = telemetryService as any; |
| 219 | |
| 220 | // Mock the session counter and startup time histogram |
| 221 | service.sessionCounter = { add: mockAdd }; |
| 222 | service.startupTimeHistogram = { record: mockRecord }; |
| 223 | service.config = { enabled: true }; |
| 224 | service.meter = {}; |
| 225 | |
| 226 | service.recordSessionStart(); |
| 227 | |
| 228 | expect(mockAdd).toHaveBeenCalledWith( |
| 229 | 1, |
| 230 | expect.objectContaining({ |
| 231 | is_headless: "true", |
| 232 | is_github_actions: "true", |
| 233 | is_continue_remote_agent: "true", |
| 234 | }), |
| 235 | ); |
| 236 | }); |
| 237 | }); |
| 238 | }); |