|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { XraySettingsValue } from '@/hooks/useXraySetting'; |
| 4 | +import { getDefaultOutboundTag, setDefaultOutboundTag } from '@/pages/xray/basics/helpers'; |
| 5 | + |
| 6 | +function tpl( |
| 7 | + outbounds: Array<{ tag?: string; protocol?: string; settings?: unknown }>, |
| 8 | + rules: Array<{ type: string; outboundTag?: string; ip?: string[]; protocol?: string[] }> = [], |
| 9 | +): XraySettingsValue { |
| 10 | + return { outbounds, routing: { rules } } as XraySettingsValue; |
| 11 | +} |
| 12 | + |
| 13 | +describe('routing default outbound', () => { |
| 14 | + it('reads first outbound tag', () => { |
| 15 | + expect(getDefaultOutboundTag(tpl([{ tag: 'warp', protocol: 'socks' }, { tag: 'direct', protocol: 'freedom' }]))).toBe('warp'); |
| 16 | + expect(getDefaultOutboundTag(tpl([]))).toBe('direct'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('moves existing outbound to first position', () => { |
| 20 | + const tt = tpl([ |
| 21 | + { tag: 'direct', protocol: 'freedom' }, |
| 22 | + { tag: 'warp', protocol: 'socks' }, |
| 23 | + { tag: 'blocked', protocol: 'blackhole' }, |
| 24 | + ]); |
| 25 | + setDefaultOutboundTag(tt, 'warp'); |
| 26 | + expect(tt.outbounds!.map((o) => o?.tag)).toEqual(['warp', 'direct', 'blocked']); |
| 27 | + }); |
| 28 | + |
| 29 | + it('creates blocked outbound when missing', () => { |
| 30 | + const tt = tpl([{ tag: 'direct', protocol: 'freedom' }]); |
| 31 | + setDefaultOutboundTag(tt, 'blocked'); |
| 32 | + expect(tt.outbounds![0]?.tag).toBe('blocked'); |
| 33 | + expect(tt.outbounds![0]?.protocol).toBe('blackhole'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not prune direct when only blocked rules reference an outbound', () => { |
| 37 | + const tt = tpl( |
| 38 | + [ |
| 39 | + { tag: 'direct', protocol: 'freedom', settings: { domainStrategy: 'AsIs' } }, |
| 40 | + { tag: 'blocked', protocol: 'blackhole' }, |
| 41 | + { tag: 'warp', protocol: 'socks' }, |
| 42 | + ], |
| 43 | + [ |
| 44 | + { type: 'field', ip: ['geoip:private'], outboundTag: 'blocked' }, |
| 45 | + { type: 'field', protocol: ['bittorrent'], outboundTag: 'blocked' }, |
| 46 | + ], |
| 47 | + ); |
| 48 | + setDefaultOutboundTag(tt, 'warp'); |
| 49 | + expect(tt.outbounds!.map((o) => o?.tag)).toEqual(['warp', 'direct', 'blocked']); |
| 50 | + }); |
| 51 | +}); |
0 commit comments