| 1 | import fs from "node:fs"; |
| 2 | import os from "node:os"; |
| 3 | import path from "node:path"; |
| 4 | |
| 5 | import { machineIdSync } from "node-machine-id"; |
| 6 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | |
| 8 | // Mock the auth module and node-machine-id |
| 9 | vi.mock("../auth/workos.js", () => ({ |
| 10 | loadAuthConfig: vi.fn(), |
| 11 | isAuthenticatedConfig: vi.fn(), |
| 12 | })); |
| 13 | |
| 14 | vi.mock("node-machine-id", () => { |
| 15 | const mockFn = vi.fn(() => "test-machine-id"); |
| 16 | return { |
| 17 | default: { |
| 18 | machineIdSync: mockFn, |
| 19 | }, |
| 20 | machineIdSync: mockFn, |
| 21 | }; |
| 22 | }); |
| 23 | |
| 24 | // Mock dns/promises for connection checks |
| 25 | vi.mock("dns/promises", () => { |
| 26 | const lookup = vi.fn(); |
| 27 | return { default: { lookup } }; |
| 28 | }); |
| 29 | |
| 30 | // eslint-disable-next-line import/order |
| 31 | import { isAuthenticatedConfig, loadAuthConfig } from "../auth/workos.js"; |
| 32 | import { PosthogService } from "./posthogService.js"; |
| 33 | |
| 34 | describe("PosthogService", () => { |
| 35 | let tempDir: string; |
| 36 | |
| 37 | beforeEach(() => { |
| 38 | // Create a temporary directory for each test |
| 39 | tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "posthog-test-")); |
| 40 | |
| 41 | // Mock os.homedir to return our temp directory |
| 42 | vi.spyOn(os, "homedir").mockReturnValue(tempDir); |
| 43 | |
| 44 | // Clear environment variable |
| 45 | delete process.env.CONTINUE_CLI_ENABLE_TELEMETRY; |
| 46 | |
| 47 | // Reset all mocks |
| 48 | vi.clearAllMocks(); |
| 49 | }); |
| 50 | |
| 51 | afterEach(() => { |
| 52 | // Clean up temp directory |
| 53 | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 54 | |
| 55 | // Restore mocks |
| 56 | vi.restoreAllMocks(); |
| 57 | }); |
| 58 | |
| 59 | describe("uniqueId generation", () => { |
| 60 | it("should use Continue user id when signed in", () => { |
| 61 | const mockUserId = "user123"; |
| 62 | |
| 63 | vi.mocked(loadAuthConfig).mockReturnValue({ userId: mockUserId } as any); |
| 64 | vi.mocked(isAuthenticatedConfig).mockReturnValue(true); |
| 65 | |
| 66 | const service = new PosthogService(); |
| 67 | |
| 68 | // Access private field for testing |
| 69 | const uniqueId = (service as any).uniqueId; |
| 70 | |
| 71 | expect(uniqueId).toBe(mockUserId); |
| 72 | expect(loadAuthConfig).toHaveBeenCalled(); |
| 73 | expect(isAuthenticatedConfig).toHaveBeenCalled(); |
| 74 | }); |
| 75 | |
| 76 | it("should use machine id when not signed in", () => { |
| 77 | const mockMachineId = "test-machine-id"; |
| 78 | |
| 79 | vi.mocked(loadAuthConfig).mockReturnValue(null); |
| 80 | vi.mocked(isAuthenticatedConfig).mockReturnValue(false); |
| 81 | vi.mocked(machineIdSync).mockReturnValue(mockMachineId); |
| 82 | |
| 83 | const service = new PosthogService(); |
| 84 | |
| 85 | // Access private field for testing |
| 86 | const uniqueId = (service as any).uniqueId; |
| 87 | |
| 88 | expect(uniqueId).toBe(mockMachineId); |
| 89 | expect(loadAuthConfig).toHaveBeenCalled(); |
| 90 | expect(isAuthenticatedConfig).toHaveBeenCalled(); |
| 91 | expect(machineIdSync).toHaveBeenCalled(); |
| 92 | }); |
| 93 | |
| 94 | it("should use machine id when auth config is environment-based", () => { |
| 95 | const mockMachineId = "test-machine-id"; |
| 96 | |
| 97 | vi.mocked(loadAuthConfig).mockReturnValue({ |
| 98 | accessToken: "token", |
| 99 | } as any); |
| 100 | vi.mocked(isAuthenticatedConfig).mockReturnValue(false); |
| 101 | vi.mocked(machineIdSync).mockReturnValue(mockMachineId); |
| 102 | |
| 103 | const service = new PosthogService(); |
| 104 | |
| 105 | // Access private field for testing |
| 106 | const uniqueId = (service as any).uniqueId; |
| 107 | |
| 108 | expect(uniqueId).toBe(mockMachineId); |
| 109 | expect(machineIdSync).toHaveBeenCalled(); |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe("telemetry enabled/disabled", () => { |
| 114 | it("should be enabled by default", () => { |
| 115 | const service = new PosthogService(); |
| 116 | expect(service.isEnabled).toBe(true); |
| 117 | }); |
| 118 | |
| 119 | it("should be disabled when CONTINUE_CLI_ENABLE_TELEMETRY is 0", () => { |
| 120 | process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0"; |
| 121 | const service = new PosthogService(); |
| 122 | expect(service.isEnabled).toBe(false); |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | describe("hasInternetConnection and offline client", () => { |
| 127 | let service: PosthogService; |
| 128 | |
| 129 | beforeEach(() => { |
| 130 | service = new PosthogService(); |
| 131 | }); |
| 132 | |
| 133 | it("returns false on DNS error, caches and refetches", async () => { |
| 134 | const dns: any = (await import("dns/promises")).default; |
| 135 | dns.lookup.mockRejectedValueOnce(new Error("offline")); |
| 136 | const first = await (service as any).hasInternetConnection(); |
| 137 | expect(first).toBe(false); |
| 138 | dns.lookup.mockResolvedValueOnce({ |
| 139 | address: "1.1.1.1", |
| 140 | family: 4, |
| 141 | } as any); |
| 142 | await (service as any).hasInternetConnection(); |
| 143 | const second = (service as any)._hasInternetConnection; |
| 144 | expect(second).toBe(true); |
| 145 | expect(dns.lookup).toHaveBeenCalledTimes(2); |
| 146 | }); |
| 147 | |
| 148 | it("getClient returns undefined when offline", async () => { |
| 149 | const dns: any = (await import("dns/promises")).default; |
| 150 | dns.lookup.mockRejectedValueOnce(new Error("offline")); |
| 151 | const client = await (service as any).getClient(); |
| 152 | expect(client).toBeUndefined(); |
| 153 | expect(dns.lookup).toHaveBeenCalledTimes(1); |
| 154 | }); |
| 155 | }); |
| 156 | }); |