import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import test from 'node:test'; process.env.APP_MODE = 'gateway'; process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'vpn-proxy-gateway-test-')); process.env.SING_BOX_CACHE = path.join(process.env.DATA_DIR, 'cache.db'); const { buildGatewayConfig } = await import(`../../dist/server/singbox.js?gateway=${Date.now()}`); const subscriptionConfig = { outbounds: [{ type: 'vless', tag: 'test-vpn', server: 'vpn.example.test', server_port: 443, uuid: '00000000-0000-4000-8000-000000000000', tls: { enabled: true }, }], }; test('gateway preserves mixed user-rule order and dynamic VPN target', () => { const config = buildGatewayConfig(subscriptionConfig, 'test-vpn', { routeRules: [ { type: 'domain', value: 'api.example.com', enabled: true, outbound: 'vpn' }, { type: 'domain_suffix', value: 'example.com', enabled: true, outbound: 'direct' }, { type: 'domain_keyword', value: 'disabled', enabled: false, outbound: 'vpn' }, ], }); assert.deepEqual(config.route.rule_set, []); assert.deepEqual(config.inbounds.map((inbound) => inbound.tag), [ 'tproxy-in', 'mixed-in', 'diagnostics-vpn-in', ]); assert.deepEqual(config.inbounds[2], { type: 'mixed', tag: 'diagnostics-vpn-in', listen: '127.0.0.1', listen_port: 18080, set_system_proxy: false, }); assert.deepEqual(config.route.rules, [ { inbound: ['tproxy-in', 'mixed-in', 'diagnostics-vpn-in'], action: 'sniff', sniffer: ['http', 'tls', 'quic'], timeout: '1s', }, { inbound: ['diagnostics-vpn-in'], outbound: 'test-vpn' }, { domain: ['api.example.com'], outbound: 'test-vpn' }, { domain_suffix: ['example.com'], outbound: 'direct' }, { inbound: ['tproxy-in'], outbound: 'test-vpn' }, { inbound: ['mixed-in'], outbound: 'test-vpn' }, ]); assert.deepEqual(config.experimental.clash_api, { external_controller: '127.0.0.1:19090' }); assert.equal(config.route.final, 'test-vpn'); });