From fa39fb34fef662c11cd8780b562fa8008b982823 Mon Sep 17 00:00:00 2001 From: Evandro Fonseca Junior Date: Wed, 30 Sep 2026 17:43:09 -0300 Subject: [PATCH] test: mock OS exports consistently and verify LAN discovery --- README.md | 2 +- .../unit/ipc/proxy-advanced-handler.test.ts | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9b6dc9f..6d1372b 100644 --- a/README.md +++ b/README.md @@ -255,4 +255,4 @@ Retry durations are parsed as complete values: `500ms` is milliseconds, and frac Focused checks: `pnpm exec vitest run src/tests/unit/rate-limit-tracker.test.ts src/tests/unit/ipc/proxy-advanced-handler.test.ts`. Run `pnpm test`, `pnpm type-check`, `pnpm lint` and `pnpm format` for the full non-E2E gates. Native SQLite/keytar are mocked by the unit-test configuration; Electron packaging, real account switching and E2E still require separate validation. -This review passes all 508 tests across 54 files, type checking, lint (327 existing warnings, no errors) and formatting with Node 24.19.0 and pnpm 10.11.0. The original lockfile is retained: its audit still reports two critical findings, in `tar` and `seroval`, requiring a separately validated dependency update. +This review passes all 509 tests across 54 files, type checking, lint (327 existing warnings, no errors) and formatting with Node 24.19.0 and pnpm 10.11.0. The original lockfile is retained: its audit still reports two critical findings, in `tar` and `seroval`, requiring a separately validated dependency update. diff --git a/src/tests/unit/ipc/proxy-advanced-handler.test.ts b/src/tests/unit/ipc/proxy-advanced-handler.test.ts index 3be2f8b..dce4d87 100644 --- a/src/tests/unit/ipc/proxy-advanced-handler.test.ts +++ b/src/tests/unit/ipc/proxy-advanced-handler.test.ts @@ -15,10 +15,15 @@ import { import { TrafficLogsRepo } from '../../../ipc/database/proxyMetricsHandler'; import { proxyAdvancedRegistry } from '../../../ipc/proxy-advanced/service-registry'; -vi.mock('os', async (importOriginal) => ({ - ...(await importOriginal()), - networkInterfaces: vi.fn().mockReturnValue({}), -})); +vi.mock('os', async (importOriginal) => { + const actual = await importOriginal(); + const mockedNetworkInterfaces = vi.fn().mockReturnValue({}); + return { + ...actual, + default: { ...actual, networkInterfaces: mockedNetworkInterfaces }, + networkInterfaces: mockedNetworkInterfaces, + }; +}); vi.mock('../../../utils/logger', () => ({ logger: { @@ -73,6 +78,7 @@ vi.mock('../../../ipc/proxy-advanced/service-registry', () => { describe('Proxy Advanced Handler', () => { beforeEach(() => { vi.clearAllMocks(); + vi.mocked(networkInterfaces).mockReset().mockReturnValue({}); }); describe('getRecentTrafficLogs', () => { @@ -426,10 +432,30 @@ describe('Proxy Advanced Handler', () => { throw new Error('network enumeration denied'); }); const result = await generateIdeConfig('vscode'); + expect(networkInterfaces).toHaveBeenCalledOnce(); expect(result.success, result.error).toBe(true); expect(result.data?.content).toMatchObject({ 'openai.url': 'http://127.0.0.1:8045/v1' }); }); + it('uses the discovered LAN address when enumeration succeeds', async () => { + vi.mocked(networkInterfaces).mockReturnValueOnce({ + eth0: [ + { + address: '192.0.2.10', + netmask: '255.255.255.0', + family: 'IPv4', + mac: '00:11:22:33:44:55', + internal: false, + cidr: '192.0.2.10/24', + }, + ], + }); + const result = await generateIdeConfig('vscode'); + expect(networkInterfaces).toHaveBeenCalledOnce(); + expect(result.success, result.error).toBe(true); + expect(result.data?.content).toMatchObject({ 'openai.url': 'http://192.0.2.10:8045/v1' }); + }); + it('should generate fallback config when proxyIdeConfig is null', async () => { const result = await generateIdeConfig('vscode'); expect(result.success, result.error).toBe(true);