|
| 1 | +import * as https from 'https'; |
| 2 | +import { NetworkDetector } from '../../lib/util/network-detector'; |
| 3 | + |
| 4 | +// Mock the https module |
| 5 | +jest.mock('https'); |
| 6 | +const mockHttps = https as jest.Mocked<typeof https>; |
| 7 | + |
| 8 | +describe('NetworkDetector', () => { |
| 9 | + let mockRequest: jest.Mock; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + mockRequest = jest.fn(); |
| 14 | + mockHttps.request.mockImplementation(mockRequest); |
| 15 | + |
| 16 | + // Clear static cache between tests |
| 17 | + (NetworkDetector as any).cachedResult = undefined; |
| 18 | + (NetworkDetector as any).cacheExpiry = 0; |
| 19 | + }); |
| 20 | + |
| 21 | + test('returns true when server responds with success status', async () => { |
| 22 | + const mockReq = { |
| 23 | + on: jest.fn(), |
| 24 | + end: jest.fn(), |
| 25 | + destroy: jest.fn(), |
| 26 | + }; |
| 27 | + |
| 28 | + mockRequest.mockImplementation((_, callback) => { |
| 29 | + callback({ statusCode: 200 }); |
| 30 | + return mockReq; |
| 31 | + }); |
| 32 | + |
| 33 | + const result = await NetworkDetector.hasConnectivity(); |
| 34 | + expect(result).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + test('returns false when server responds with server error', async () => { |
| 38 | + const mockReq = { |
| 39 | + on: jest.fn(), |
| 40 | + end: jest.fn(), |
| 41 | + destroy: jest.fn(), |
| 42 | + }; |
| 43 | + |
| 44 | + mockRequest.mockImplementation((_, callback) => { |
| 45 | + callback({ statusCode: 500 }); |
| 46 | + return mockReq; |
| 47 | + }); |
| 48 | + |
| 49 | + const result = await NetworkDetector.hasConnectivity(); |
| 50 | + expect(result).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + test('returns false on network error', async () => { |
| 54 | + const mockReq = { |
| 55 | + on: jest.fn((event, handler) => { |
| 56 | + if (event === 'error') { |
| 57 | + setTimeout(() => handler(new Error('Network error')), 0); |
| 58 | + } |
| 59 | + }), |
| 60 | + end: jest.fn(), |
| 61 | + destroy: jest.fn(), |
| 62 | + }; |
| 63 | + |
| 64 | + mockRequest.mockReturnValue(mockReq); |
| 65 | + |
| 66 | + const result = await NetworkDetector.hasConnectivity(); |
| 67 | + expect(result).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + test('returns false on timeout', async () => { |
| 71 | + const mockReq = { |
| 72 | + on: jest.fn((event, handler) => { |
| 73 | + if (event === 'timeout') { |
| 74 | + setTimeout(() => handler(), 0); |
| 75 | + } |
| 76 | + }), |
| 77 | + end: jest.fn(), |
| 78 | + destroy: jest.fn(), |
| 79 | + }; |
| 80 | + |
| 81 | + mockRequest.mockReturnValue(mockReq); |
| 82 | + |
| 83 | + const result = await NetworkDetector.hasConnectivity(); |
| 84 | + expect(result).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + test('caches result for subsequent calls', async () => { |
| 88 | + const mockReq = { |
| 89 | + on: jest.fn(), |
| 90 | + end: jest.fn(), |
| 91 | + destroy: jest.fn(), |
| 92 | + }; |
| 93 | + |
| 94 | + mockRequest.mockImplementation((_, callback) => { |
| 95 | + callback({ statusCode: 200 }); |
| 96 | + return mockReq; |
| 97 | + }); |
| 98 | + |
| 99 | + await NetworkDetector.hasConnectivity(); |
| 100 | + await NetworkDetector.hasConnectivity(); |
| 101 | + |
| 102 | + expect(mockRequest).toHaveBeenCalledTimes(1); |
| 103 | + }); |
| 104 | +}); |
0 commit comments