Changesets · Branches · Tags · Compare
| Changeset | 08395f969ed7984cba62b6188c895ba7ecd31b7c4e563e6688b2a2e078263c12 |
|---|---|
| Author | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-10 19:32 +05:30 |
| Committer | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-10 19:32 +05:30 |
| Parents | ac1a04b04a8f |
1 file changed
modified extensions/cli/src/telemetry/posthogService.test.ts +39 −3
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | import path from "node:path"; |
| 4 | 4 | |
| 5 | 5 | import { machineIdSync } from "node-machine-id"; |
| 6 | -import { vi, describe, it, beforeEach, afterEach, expect } from "vitest"; | |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | |
| 7 | 7 | |
| 8 | 8 | // Mock the auth module and node-machine-id |
| 9 | 9 | vi.mock("../auth/workos.js", () => ({ |
| @@ -21,9 +21,14 @@ | ||
| 21 | 21 | }; |
| 22 | 22 | }); |
| 23 | 23 | |
| 24 | -// Import after mocks are set up | |
| 25 | -import { loadAuthConfig, isAuthenticatedConfig } from "../auth/workos.js"; | |
| 24 | +// Mock dns/promises for connection checks | |
| 25 | +vi.mock("dns/promises", () => { | |
| 26 | + const lookup = vi.fn(); | |
| 27 | + return { default: { lookup } }; | |
| 28 | +}); | |
| 26 | 29 | |
| 30 | +// eslint-disable-next-line import/order | |
| 31 | +import { isAuthenticatedConfig, loadAuthConfig } from "../auth/workos.js"; | |
| 27 | 32 | import { PosthogService } from "./posthogService.js"; |
| 28 | 33 | |
| 29 | 34 | describe("PosthogService", () => { |
| @@ -117,4 +122,35 @@ | ||
| 117 | 122 | expect(service.isEnabled).toBe(false); |
| 118 | 123 | }); |
| 119 | 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 | + }); | |
| 120 | 156 | }); |