routing-default-outbound.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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(
  13. getDefaultOutboundTag(
  14. tpl([
  15. { tag: 'warp', protocol: 'socks' },
  16. { tag: 'direct', protocol: 'freedom' },
  17. ]),
  18. ),
  19. ).toBe('warp');
  20. expect(getDefaultOutboundTag(tpl([]))).toBe('direct');
  21. });
  22. it('moves existing outbound to first position', () => {
  23. const tt = tpl([
  24. { tag: 'direct', protocol: 'freedom' },
  25. { tag: 'warp', protocol: 'socks' },
  26. { tag: 'blocked', protocol: 'blackhole' },
  27. ]);
  28. setDefaultOutboundTag(tt, 'warp');
  29. expect(tt.outbounds!.map((o) => o?.tag)).toEqual(['warp', 'direct', 'blocked']);
  30. });
  31. it('creates blocked outbound when missing', () => {
  32. const tt = tpl([{ tag: 'direct', protocol: 'freedom' }]);
  33. setDefaultOutboundTag(tt, 'blocked');
  34. expect(tt.outbounds![0]?.tag).toBe('blocked');
  35. expect(tt.outbounds![0]?.protocol).toBe('blackhole');
  36. });
  37. it('does not prune direct when only blocked rules reference an outbound', () => {
  38. const tt = tpl(
  39. [
  40. { tag: 'direct', protocol: 'freedom', settings: { domainStrategy: 'AsIs' } },
  41. { tag: 'blocked', protocol: 'blackhole' },
  42. { tag: 'warp', protocol: 'socks' },
  43. ],
  44. [
  45. { type: 'field', ip: ['geoip:private'], outboundTag: 'blocked' },
  46. { type: 'field', protocol: ['bittorrent'], outboundTag: 'blocked' },
  47. ],
  48. );
  49. setDefaultOutboundTag(tt, 'warp');
  50. expect(tt.outbounds!.map((o) => o?.tag)).toEqual(['warp', 'direct', 'blocked']);
  51. });
  52. });