// Real isLanAccess() — exercises the CIDR parsing in src/utils/lanAccess.js // that authGuard.spec.js mocks out. No top-level vi.mock; we control the // hostname by redefine window.location per test. import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { isLanAccess } from '@/utils/lanAccess.js' const setHostname = (hostname) => { Object.defineProperty(window, 'location', { value: { hostname }, writable: true, configurable: true, }) } describe('isLanAccess — host bypass', () => { beforeEach(() => setHostname('localhost')) it('returns true for localhost', () => { setHostname('localhost') expect(window.location.hostname).toBe('localhost') expect(isLanAccess()).toBe(true) }) it('returns true for 127.0.0.1', () => { setHostname('127.0.0.1') expect(isLanAccess()).toBe(true) }) }) describe('isLanAccess — CIDR allow (192.168.1.0/24)', () => { beforeEach(() => setHostname('192.168.1.50')) it.each([ ['192.168.1.0', 'network address (boundary)'], ['192.168.1.1', 'gateway'], ['192.168.1.50', 'mid-range'], ['192.168.1.255', 'broadcast (boundary)'], ])('returns true for %s (%s)', (ip) => { setHostname(ip) expect(window.location.hostname).toBe(ip) expect(isLanAccess()).toBe(true) }) }) describe('isLanAccess — CIDR allow (192.168.2.0/24)', () => { beforeEach(() => setHostname('192.168.2.100')) it.each([ ['192.168.2.0'], ['192.168.2.100'], ['192.168.2.255'], ])('returns true for %s', (ip) => { setHostname(ip) expect(isLanAccess()).toBe(true) }) }) describe('isLanAccess — deny', () => { beforeEach(() => setHostname('8.8.8.8')) it.each([ ['192.168.3.1', 'adjacent subnet (CIDR out)'], ['192.168.0.1', '192.168.0.x (not in /24s)'], ['10.0.0.1', 'private 10.x (not whitelisted)'], ['172.16.0.1', 'private 172.16.x (not whitelisted)'], ['8.8.8.8', 'public DNS'], ['0.0.0.0', 'unspecified address'], ['', 'empty string'], ['example.com', 'hostname (not IP)'], ])('returns false for %s (%s)', (ip) => { setHostname(ip) expect(isLanAccess()).toBe(false) }) }) afterEach(() => setHostname('localhost'))