Elffuss BraidHub

kikocis/continue public

main · 3.4 KiBRawPermalink
1import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
3import { PosthogService } from "./posthogService.js";
4
5describe("PosthogService Error Handling", () => {
6 let posthogService: PosthogService;
7 let consoleSpy: any;
8
9 beforeEach(() => {
10 posthogService = new PosthogService();
11 // Spy on console methods but don't mock them - we want to test they don't crash
12 consoleSpy = vi.spyOn(console, "debug").mockImplementation(() => {});
13 });
14
15 afterEach(() => {
16 consoleSpy.mockRestore();
17 });
18
19 it("should handle network errors gracefully during capture", async () => {
20 // Mock the PostHog client to throw a network error
21 const mockClient = {
22 capture: vi.fn().mockImplementation(() => {
23 throw new Error("Network error: SSL wrong version number");
24 }),
25 };
26
27 // Mock the getClient method to return our mock client
28 vi.spyOn(posthogService as any, "getClient").mockResolvedValue(mockClient);
29
30 // This should not throw an error
31 await expect(
32 posthogService.capture("test_event", { test: "data" }),
33 ).resolves.toBeUndefined();
34
35 // Verify the client capture was called
36 expect(mockClient.capture).toHaveBeenCalledWith({
37 distinctId: expect.any(String),
38 event: "test_event",
39 properties: expect.objectContaining({
40 test: "data",
41 os: expect.any(String),
42 extensionVersion: expect.any(String),
43 ideName: "cn",
44 ideType: "cli",
45 }),
46 sendFeatureFlags: true,
47 });
48 });
49
50 it("should handle errors gracefully during shutdown", async () => {
51 // Mock the PostHog client to throw an error during shutdown
52 const mockClient = {
53 shutdown: vi.fn().mockImplementation(() => {
54 throw new Error("Network error during shutdown");
55 }),
56 };
57
58 // Mock the getClient method to return our mock client
59 vi.spyOn(posthogService as any, "getClient").mockResolvedValue(mockClient);
60
61 // This should not throw an error
62 await expect(posthogService.shutdown()).resolves.toBeUndefined();
63
64 // Verify the client shutdown was called
65 expect(mockClient.shutdown).toHaveBeenCalled();
66 });
67
68 it("should handle getClient errors gracefully", async () => {
69 // Mock the getClient method to throw an error
70 vi.spyOn(posthogService as any, "getClient").mockRejectedValue(
71 new Error("Failed to initialize PostHog client"),
72 );
73
74 // This should not throw an error
75 await expect(
76 posthogService.capture("test_event", { test: "data" }),
77 ).resolves.toBeUndefined();
78 });
79
80 it("should not capture when service is disabled", async () => {
81 // Mock environment variable to disable telemetry
82 const originalEnv = process.env.CONTINUE_CLI_ENABLE_TELEMETRY;
83 process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0";
84
85 // Create a new service instance to pick up the env var
86 const disabledService = new PosthogService();
87
88 // Mock the getClient method
89 const getClientSpy = vi.spyOn(disabledService as any, "getClient");
90
91 await disabledService.capture("test_event", { test: "data" });
92
93 // Verify getClient was called but would return undefined due to disabled state
94 expect(getClientSpy).toHaveBeenCalled();
95
96 // Restore environment
97 if (originalEnv === undefined) {
98 delete process.env.CONTINUE_CLI_ENABLE_TELEMETRY;
99 } else {
100 process.env.CONTINUE_CLI_ENABLE_TELEMETRY = originalEnv;
101 }
102 });
103});