routing-default-outbound.test.ts 2.0 KB

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