| 1 | import { DevDataLogEvent } from "@continuedev/config-yaml"; |
| 2 | import fs from "fs"; |
| 3 | import path from "path"; |
| 4 | import { |
| 5 | afterAll, |
| 6 | beforeAll, |
| 7 | beforeEach, |
| 8 | describe, |
| 9 | expect, |
| 10 | it, |
| 11 | vi, |
| 12 | } from "vitest"; |
| 13 | import { IdeInfo, IdeSettings } from ".."; |
| 14 | import { Core } from "../core"; |
| 15 | import { getDevDataFilePath } from "../util/paths"; |
| 16 | import { DataLogger } from "./log"; |
| 17 | |
| 18 | // Only mock fetch, not fs |
| 19 | vi.mock("@continuedev/fetch"); |
| 20 | |
| 21 | const TEST_EVENT: DevDataLogEvent = { |
| 22 | name: "tokensGenerated", |
| 23 | data: { |
| 24 | generatedTokens: 100, |
| 25 | model: "gpt-4", |
| 26 | promptTokens: 50, |
| 27 | provider: "openai", |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | const TEST_AGENT_INTERACTION_EVENT: DevDataLogEvent = { |
| 32 | name: "chatInteraction", |
| 33 | data: { |
| 34 | prompt: "Hello, world!", |
| 35 | completion: "Hello, world!", |
| 36 | modelProvider: "openai", |
| 37 | modelName: "gpt-4", |
| 38 | modelTitle: "gpt-4", |
| 39 | sessionId: "1234", |
| 40 | tools: ["test-tool1"], |
| 41 | }, |
| 42 | }; |
| 43 | |
| 44 | const SCHEMA = "0.2.0"; |
| 45 | |
| 46 | describe("DataLogger", () => { |
| 47 | let dataLogger: DataLogger; |
| 48 | const tempDir = path.join(process.cwd(), "temp-test-data"); |
| 49 | const testFilePath = path.join(tempDir, "tokensGenerated-test.jsonl"); |
| 50 | |
| 51 | // Create temp directory for test files |
| 52 | beforeAll(() => { |
| 53 | if (!fs.existsSync(tempDir)) { |
| 54 | fs.mkdirSync(tempDir, { recursive: true }); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | // Clean up temp directory after tests |
| 59 | afterAll(() => { |
| 60 | if (fs.existsSync(tempDir)) { |
| 61 | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | beforeEach(() => { |
| 66 | // Reset mocks |
| 67 | vi.clearAllMocks(); |
| 68 | |
| 69 | // Remove test file if it exists |
| 70 | if (fs.existsSync(testFilePath)) { |
| 71 | fs.unlinkSync(testFilePath); |
| 72 | } |
| 73 | |
| 74 | // Get singleton instance |
| 75 | dataLogger = DataLogger.getInstance(); |
| 76 | |
| 77 | // Mock core and required promises |
| 78 | dataLogger.core = { |
| 79 | configHandler: { |
| 80 | loadConfig: vi.fn().mockResolvedValue({ |
| 81 | config: { |
| 82 | data: [], |
| 83 | }, |
| 84 | }), |
| 85 | currentProfile: { |
| 86 | profileDescription: { |
| 87 | id: "test-profile-id", |
| 88 | }, |
| 89 | }, |
| 90 | controlPlaneClient: { |
| 91 | getAccessToken: vi.fn().mockResolvedValue("test-access-token"), |
| 92 | }, |
| 93 | }, |
| 94 | } as unknown as Core; |
| 95 | |
| 96 | dataLogger.ideSettingsPromise = Promise.resolve({ |
| 97 | userToken: "test-user-token", |
| 98 | } as IdeSettings); |
| 99 | |
| 100 | dataLogger.ideInfoPromise = Promise.resolve({ |
| 101 | name: "VSCode", |
| 102 | version: "1.0.0", |
| 103 | extensionVersion: "0.1.0", |
| 104 | } as IdeInfo); |
| 105 | }); |
| 106 | |
| 107 | describe("getInstance", () => { |
| 108 | it("should return the same instance when called multiple times", () => { |
| 109 | const instance1 = DataLogger.getInstance(); |
| 110 | const instance2 = DataLogger.getInstance(); |
| 111 | expect(instance1).toBe(instance2); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | describe("addBaseValues", () => { |
| 116 | it("should add base values to event data based on schema", async () => { |
| 117 | const mockZodSchema = { |
| 118 | shape: { |
| 119 | eventName: true, |
| 120 | timestamp: true, |
| 121 | schema: true, |
| 122 | userAgent: true, |
| 123 | selectedProfileId: true, |
| 124 | userId: true, |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | const result = await dataLogger.addBaseValues( |
| 129 | { customField: "value" }, |
| 130 | "testEvent", |
| 131 | SCHEMA, |
| 132 | mockZodSchema as any, |
| 133 | ); |
| 134 | |
| 135 | expect(result).toEqual({ |
| 136 | customField: "value", |
| 137 | eventName: "testEvent", |
| 138 | timestamp: expect.any(String), |
| 139 | schema: SCHEMA, |
| 140 | userAgent: "VSCode/1.0.0 (Continue/0.1.0)", |
| 141 | selectedProfileId: "test-profile-id", |
| 142 | userId: "test-user-token", |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | it("should not add fields not present in schema shape", async () => { |
| 147 | const mockZodSchema = { |
| 148 | shape: { |
| 149 | eventName: true, |
| 150 | // No other fields |
| 151 | }, |
| 152 | }; |
| 153 | |
| 154 | const result = await dataLogger.addBaseValues( |
| 155 | { customField: "value" }, |
| 156 | "testEvent", |
| 157 | SCHEMA, |
| 158 | mockZodSchema as any, |
| 159 | ); |
| 160 | |
| 161 | expect(result).toEqual({ |
| 162 | customField: "value", |
| 163 | eventName: "testEvent", |
| 164 | }); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | describe("logLocalData", () => { |
| 169 | it("should actually write data to the local file", async () => { |
| 170 | // Call the method to log data locally |
| 171 | await dataLogger.logLocalData(TEST_EVENT); |
| 172 | |
| 173 | // Verify the file was created |
| 174 | const filepath = getDevDataFilePath(TEST_EVENT.name, SCHEMA); |
| 175 | expect(fs.existsSync(filepath)).toBe(true); |
| 176 | |
| 177 | // Read file contents and verify |
| 178 | const fileContent = fs.readFileSync(filepath, "utf8"); |
| 179 | expect(fileContent).toContain('"generatedTokens":100'); |
| 180 | expect(fileContent).toContain('"model":"gpt-4"'); |
| 181 | expect(fileContent).toContain('"eventName":"tokensGenerated"'); |
| 182 | }); |
| 183 | |
| 184 | it("should write agent interaction data to local file", async () => { |
| 185 | // Call the method to log data locally |
| 186 | await dataLogger.logLocalData(TEST_AGENT_INTERACTION_EVENT); |
| 187 | |
| 188 | // Verify the file was created |
| 189 | const filepath = getDevDataFilePath( |
| 190 | TEST_AGENT_INTERACTION_EVENT.name, |
| 191 | SCHEMA, |
| 192 | ); |
| 193 | expect(fs.existsSync(filepath)).toBe(true); |
| 194 | |
| 195 | // Read file contents and verify |
| 196 | const fileContent = fs.readFileSync(filepath, "utf8"); |
| 197 | expect(fileContent).toContain('"eventName":"chatInteraction"'); |
| 198 | expect(fileContent).toContain('"prompt":"Hello, world!"'); |
| 199 | expect(fileContent).toContain('"completion":"Hello, world!"'); |
| 200 | expect(fileContent).toContain('"tools":["test-tool1"]'); |
| 201 | }); |
| 202 | }); |
| 203 | |
| 204 | describe("logDevData", () => { |
| 205 | it("should log data locally and to configured destinations", async () => { |
| 206 | // Spy on logLocalData and logToOneDestination |
| 207 | const logLocalDataSpy = vi |
| 208 | .spyOn(dataLogger, "logLocalData") |
| 209 | .mockResolvedValue(); |
| 210 | const logToOneDestinationSpy = vi |
| 211 | .spyOn(dataLogger, "logToOneDestination") |
| 212 | .mockResolvedValue(); |
| 213 | |
| 214 | // Mock config with multiple data destinations |
| 215 | const mockConfig = { |
| 216 | config: { |
| 217 | data: [ |
| 218 | { destination: "https://example.com/logs", schema: SCHEMA }, |
| 219 | { destination: "file:///logs", schema: SCHEMA }, |
| 220 | ], |
| 221 | }, |
| 222 | }; |
| 223 | |
| 224 | dataLogger.core!.configHandler.loadConfig = vi |
| 225 | .fn() |
| 226 | .mockResolvedValue(mockConfig); |
| 227 | |
| 228 | await dataLogger.logDevData(TEST_EVENT); |
| 229 | |
| 230 | expect(logLocalDataSpy).toHaveBeenCalledWith(TEST_EVENT); |
| 231 | expect(logToOneDestinationSpy).toHaveBeenCalledTimes(2); |
| 232 | }); |
| 233 | }); |
| 234 | }); |