1
0

routing.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, it, expect } from 'vitest';
  2. import {
  3. buildBalancer,
  4. buildRule,
  5. buildObservatory,
  6. buildRouting,
  7. buildRoutingJson,
  8. type RoutingInput,
  9. } from './routing';
  10. describe('buildBalancer', () => {
  11. it('emits tag, selector, and strategy.type; omits fallbackTag when empty', () => {
  12. const b = buildBalancer({ tag: 'lb', selector: ['proxy', 'hk-'], strategy: 'roundRobin' });
  13. expect(b.tag).toBe('lb');
  14. expect(b.selector).toEqual(['proxy', 'hk-']);
  15. expect((b.strategy as Record<string, unknown>).type).toBe('roundRobin');
  16. expect('fallbackTag' in b).toBe(false);
  17. });
  18. it('includes fallbackTag when set', () => {
  19. const b = buildBalancer({ tag: 'lb', selector: ['a'], strategy: 'random', fallbackTag: 'direct' });
  20. expect(b.fallbackTag).toBe('direct');
  21. });
  22. });
  23. describe('buildRule', () => {
  24. it('emits only the matchers that are set, with outboundTag', () => {
  25. const r = buildRule({ domain: ['geosite:google'], target: { kind: 'outbound', tag: 'warp' } });
  26. expect(r.type).toBe('field');
  27. expect(r.domain).toEqual(['geosite:google']);
  28. expect('ip' in r).toBe(false);
  29. expect(r.outboundTag).toBe('warp');
  30. expect('balancerTag' in r).toBe(false);
  31. });
  32. it('uses balancerTag (and not outboundTag) when the target is a balancer', () => {
  33. const r = buildRule({ ip: ['geoip:cn'], target: { kind: 'balancer', tag: 'lb' } });
  34. expect(r.balancerTag).toBe('lb');
  35. expect('outboundTag' in r).toBe(false);
  36. expect(r.ip).toEqual(['geoip:cn']);
  37. });
  38. it('passes port + network through and carries ruleTag', () => {
  39. const r = buildRule({
  40. port: '443,8443',
  41. network: 'tcp,udp',
  42. ruleTag: 'r1',
  43. target: { kind: 'outbound', tag: 'direct' },
  44. });
  45. expect(r.port).toBe('443,8443');
  46. expect(r.network).toBe('tcp,udp');
  47. expect(r.ruleTag).toBe('r1');
  48. });
  49. });
  50. describe('buildObservatory', () => {
  51. it('observatory mode emits probeURL default + enableConcurrency, no burst', () => {
  52. const o = buildObservatory({ mode: 'observatory', subjectSelector: ['proxy'] });
  53. const obs = (o as Record<string, Record<string, unknown>>).observatory;
  54. expect(obs.probeURL).toBe('https://www.google.com/generate_204');
  55. expect(obs.subjectSelector).toEqual(['proxy']);
  56. expect(obs.enableConcurrency).toBe(true);
  57. expect('burstObservatory' in o).toBe(false);
  58. });
  59. it('burst mode emits pingConfig.destination default and no observatory', () => {
  60. const o = buildObservatory({ mode: 'burst', subjectSelector: ['proxy'] });
  61. const burst = (o as Record<string, Record<string, Record<string, unknown>>>).burstObservatory;
  62. expect(burst.pingConfig.destination).toBe('https://www.google.com/generate_204');
  63. expect('observatory' in o).toBe(false);
  64. });
  65. });
  66. describe('buildRouting', () => {
  67. const base: RoutingInput = {
  68. balancers: [{ tag: 'lb', selector: ['proxy'], strategy: 'random' }],
  69. rules: [{ domain: ['geosite:category-ads-all'], target: { kind: 'outbound', tag: 'block' } }],
  70. };
  71. it('nests rules and balancers under routing', () => {
  72. const out = buildRouting(base) as { routing: { rules: unknown[]; balancers: unknown[] } };
  73. expect(out.routing.rules).toHaveLength(1);
  74. expect(out.routing.balancers).toHaveLength(1);
  75. });
  76. it('puts observatory at the TOP level (not under routing) for a leastPing balancer', () => {
  77. const out = buildRouting({
  78. ...base,
  79. balancers: [{ tag: 'lb', selector: ['proxy'], strategy: 'leastPing' }],
  80. }) as Record<string, Record<string, unknown>>;
  81. expect(out.observatory).toBeDefined();
  82. expect('observatory' in out.routing).toBe(false);
  83. });
  84. it('uses a burstObservatory for a leastLoad balancer', () => {
  85. const out = buildRouting({
  86. ...base,
  87. balancers: [{ tag: 'lb', selector: ['proxy'], strategy: 'leastLoad' }],
  88. }) as Record<string, unknown>;
  89. expect(out.burstObservatory).toBeDefined();
  90. });
  91. it('carries domainStrategy when set', () => {
  92. const out = buildRouting({ ...base, domainStrategy: 'IPIfNonMatch' }) as {
  93. routing: Record<string, unknown>;
  94. };
  95. expect(out.routing.domainStrategy).toBe('IPIfNonMatch');
  96. });
  97. it('does not auto-add an observatory for random/roundRobin balancers', () => {
  98. const out = buildRouting(base) as Record<string, unknown>;
  99. expect('observatory' in out).toBe(false);
  100. expect('burstObservatory' in out).toBe(false);
  101. });
  102. });
  103. describe('buildRoutingJson', () => {
  104. it('round-trips and is 2-space indented', () => {
  105. const input: RoutingInput = {
  106. balancers: [],
  107. rules: [{ ip: ['geoip:private'], target: { kind: 'outbound', tag: 'direct' } }],
  108. };
  109. const json = buildRoutingJson(input);
  110. expect(json).toContain('\n "');
  111. expect(JSON.parse(json)).toEqual(buildRouting(input));
  112. });
  113. });