import { tap, expect } from '@git.zone/tstest/tapbundle'; import { findMatchingRoutes, findBestMatchingRoute, routeMatchesDomain, routeMatchesPort, routeMatchesPath, } from '../ts/proxies/smart-proxy/utils/route-utils.js'; import { validateRouteConfig, isValidDomain, isValidPort, } from '../ts/proxies/smart-proxy/utils/route-validator.js'; import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js'; tap.test('route validation - validateRouteConfig accepts valid route', async () => { const route: IRouteConfig = { match: { ports: 80, domains: 'valid.example.com' }, action: { type: 'forward', targets: [{ host: '10.0.0.1', port: 8080 }] } }; const result = validateRouteConfig(route); expect(result.valid).toBeTrue(); expect(result.errors).toHaveLength(0); }); tap.test('route validation - validateRouteConfig rejects missing action', async () => { const badRoute = { match: { ports: 80 } } as any; const result = validateRouteConfig(badRoute); expect(result.valid).toBeFalse(); expect(result.errors.length).toBeGreaterThan(0); }); tap.test('route validation - isValidDomain checks correctly', async () => { expect(isValidDomain('example.com')).toBeTrue(); expect(isValidDomain('*.example.com')).toBeTrue(); expect(isValidDomain('')).toBeFalse(); }); tap.test('route validation - isValidPort checks correctly', async () => { expect(isValidPort(80)).toBeTrue(); expect(isValidPort(443)).toBeTrue(); expect(isValidPort(0)).toBeFalse(); expect(isValidPort(70000)).toBeFalse(); expect(isValidPort(-1)).toBeFalse(); }); tap.test('domain matching - exact domain', async () => { const route: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } }; expect(routeMatchesDomain(route, 'example.com')).toBeTrue(); expect(routeMatchesDomain(route, 'other.com')).toBeFalse(); }); tap.test('domain matching - wildcard domain', async () => { const route: IRouteConfig = { match: { ports: 80, domains: '*.example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } }; expect(routeMatchesDomain(route, 'sub.example.com')).toBeTrue(); expect(routeMatchesDomain(route, 'example.com')).toBeFalse(); }); tap.test('port matching - single port', async () => { const route: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } }; expect(routeMatchesPort(route, 80)).toBeTrue(); expect(routeMatchesPort(route, 443)).toBeFalse(); }); tap.test('route finding - findBestMatchingRoute selects by priority', async () => { const lowPriority: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] }, priority: 10 }; const highPriority: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 4000 }] }, priority: 100 }; const routes: IRouteConfig[] = [lowPriority, highPriority]; const best = findBestMatchingRoute(routes, { domain: 'example.com', port: 80 }); expect(best).toBeDefined(); expect(best!.priority).toEqual(100); expect(best!.action.targets![0].port).toEqual(4000); }); tap.test('route finding - findMatchingRoutes returns all matches', async () => { const route1: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 3000 }] } }; const route2: IRouteConfig = { match: { ports: 80, domains: 'example.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 4000 }] } }; const route3: IRouteConfig = { match: { ports: 80, domains: 'other.com' }, action: { type: 'forward', targets: [{ host: '127.0.0.1', port: 5000 }] } }; const matches = findMatchingRoutes([route1, route2, route3], { domain: 'example.com', port: 80 }); expect(matches).toHaveLength(2); }); export default tap.start();