Elffuss BraidHub

kikocis/continue public

bf136946f6ca · 3.5 KiBRawPermalink
1import fs from "node:fs";
2import os from "node:os";
3import path from "node:path";
4
5import { machineIdSync } from "node-machine-id";
6import { vi, describe, it, beforeEach, afterEach, expect } from "vitest";
7
8// Mock the auth module and node-machine-id
9vi.mock("../auth/workos.js", () => ({
10 loadAuthConfig: vi.fn(),
11 isAuthenticatedConfig: vi.fn(),
12}));
13
14vi.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// Import after mocks are set up
25import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js";
26
27import { PosthogService } from "./posthogService.js";
28
29describe("PosthogService", () => {
30 let tempDir: string;
31
32 beforeEach(() => {
33 // Create a temporary directory for each test
34 tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "posthog-test-"));
35
36 // Mock os.homedir to return our temp directory
37 vi.spyOn(os, "homedir").mockReturnValue(tempDir);
38
39 // Clear environment variable
40 delete process.env.CONTINUE_CLI_ENABLE_TELEMETRY;
41
42 // Reset all mocks
43 vi.clearAllMocks();
44 });
45
46 afterEach(() => {
47 // Clean up temp directory
48 fs.rmSync(tempDir, { recursive: true, force: true });
49
50 // Restore mocks
51 vi.restoreAllMocks();
52 });
53
54 describe("uniqueId generation", () => {
55 it("should use Continue user id when signed in", () => {
56 const mockUserId = "user123";
57
58 vi.mocked(loadAuthConfig).mockReturnValue({ userId: mockUserId } as any);
59 vi.mocked(isAuthenticatedConfig).mockReturnValue(true);
60
61 const service = new PosthogService();
62
63 // Access private field for testing
64 const uniqueId = (service as any).uniqueId;
65
66 expect(uniqueId).toBe(mockUserId);
67 expect(loadAuthConfig).toHaveBeenCalled();
68 expect(isAuthenticatedConfig).toHaveBeenCalled();
69 });
70
71 it("should use machine id when not signed in", () => {
72 const mockMachineId = "test-machine-id";
73
74 vi.mocked(loadAuthConfig).mockReturnValue(null);
75 vi.mocked(isAuthenticatedConfig).mockReturnValue(false);
76 vi.mocked(machineIdSync).mockReturnValue(mockMachineId);
77
78 const service = new PosthogService();
79
80 // Access private field for testing
81 const uniqueId = (service as any).uniqueId;
82
83 expect(uniqueId).toBe(mockMachineId);
84 expect(loadAuthConfig).toHaveBeenCalled();
85 expect(isAuthenticatedConfig).toHaveBeenCalled();
86 expect(machineIdSync).toHaveBeenCalled();
87 });
88
89 it("should use machine id when auth config is environment-based", () => {
90 const mockMachineId = "test-machine-id";
91
92 vi.mocked(loadAuthConfig).mockReturnValue({
93 accessToken: "token",
94 } as any);
95 vi.mocked(isAuthenticatedConfig).mockReturnValue(false);
96 vi.mocked(machineIdSync).mockReturnValue(mockMachineId);
97
98 const service = new PosthogService();
99
100 // Access private field for testing
101 const uniqueId = (service as any).uniqueId;
102
103 expect(uniqueId).toBe(mockMachineId);
104 expect(machineIdSync).toHaveBeenCalled();
105 });
106 });
107
108 describe("telemetry enabled/disabled", () => {
109 it("should be enabled by default", () => {
110 const service = new PosthogService();
111 expect(service.isEnabled).toBe(true);
112 });
113
114 it("should be disabled when CONTINUE_CLI_ENABLE_TELEMETRY is 0", () => {
115 process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0";
116 const service = new PosthogService();
117 expect(service.isEnabled).toBe(false);
118 });
119 });
120});