routing-reference-cleanup.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. applyBalancerDeletion,
  4. applyOutboundDeletion,
  5. planBalancerDeletion,
  6. planOutboundDeletion,
  7. } from '@/pages/xray/reference-cleanup';
  8. import type { XraySettingsValue } from '@/hooks/useXraySetting';
  9. function tpl(parts: Record<string, unknown>): XraySettingsValue {
  10. return parts as unknown as XraySettingsValue;
  11. }
  12. function dialerProxyOf(tt: XraySettingsValue, tag: string): string | undefined {
  13. const o = tt.outbounds?.find((x) => x?.tag === tag);
  14. return (o as { streamSettings?: { sockopt?: { dialerProxy?: string } } } | undefined)
  15. ?.streamSettings?.sockopt?.dialerProxy;
  16. }
  17. describe('outbound deletion', () => {
  18. it('drops a rule whose only destination was the deleted outbound', () => {
  19. const tt = tpl({
  20. outbounds: [{ tag: 'proxy-us' }],
  21. routing: { rules: [{ type: 'field', inboundTag: ['in-443'], outboundTag: 'proxy-us' }], balancers: [] },
  22. });
  23. applyOutboundDeletion(tt, 0);
  24. expect(tt.routing!.rules).toEqual([]);
  25. expect(tt.outbounds).toEqual([]);
  26. });
  27. it('keeps a rule that still has a balancer, dropping only the dead outboundTag', () => {
  28. const tt = tpl({
  29. outbounds: [{ tag: 'proxy-us' }],
  30. routing: {
  31. rules: [{ type: 'field', outboundTag: 'proxy-us', balancerTag: 'eu-pool' }],
  32. balancers: [{ tag: 'eu-pool', selector: ['direct'] }],
  33. },
  34. });
  35. applyOutboundDeletion(tt, 0);
  36. expect(tt.routing!.rules).toHaveLength(1);
  37. expect(tt.routing!.rules![0].outboundTag).toBeUndefined();
  38. expect(tt.routing!.rules![0].balancerTag).toBe('eu-pool');
  39. });
  40. it('reduces a multi-target selector and leaves the balancer and its rules intact', () => {
  41. const tt = tpl({
  42. outbounds: [{ tag: 'proxy-us' }, { tag: 'proxy-uk' }],
  43. routing: {
  44. rules: [{ type: 'field', inboundTag: ['in'], balancerTag: 'pool' }],
  45. balancers: [{ tag: 'pool', selector: ['proxy-us', 'proxy-uk'] }],
  46. },
  47. });
  48. applyOutboundDeletion(tt, 0);
  49. expect(tt.routing!.balancers![0].selector).toEqual(['proxy-uk']);
  50. expect(tt.routing!.rules).toHaveLength(1);
  51. expect(tt.routing!.rules![0].balancerTag).toBe('pool');
  52. expect((tt.outbounds || []).map((o) => o?.tag)).toEqual(['proxy-uk']);
  53. });
  54. it('cascade-removes a balancer whose selector is emptied, repairing its rules', () => {
  55. const tt = tpl({
  56. outbounds: [{ tag: 'proxy-us' }],
  57. routing: {
  58. rules: [
  59. { type: 'field', inboundTag: ['in'], balancerTag: 'pool' },
  60. { type: 'field', outboundTag: 'direct', balancerTag: 'pool' },
  61. ],
  62. balancers: [{ tag: 'pool', selector: ['proxy-us'] }],
  63. },
  64. });
  65. const impact = planOutboundDeletion(tt, 0);
  66. expect(impact.balancers).toEqual([{ tag: 'pool', reason: 'selectorEmptied' }]);
  67. expect(impact.rules).toEqual([
  68. { index: 0, label: '#1', fate: 'removed' },
  69. { index: 1, label: '#2', fate: 'modified', keeps: 'direct' },
  70. ]);
  71. applyOutboundDeletion(tt, 0);
  72. expect(tt.routing!.balancers).toEqual([]);
  73. expect(tt.routing!.rules).toHaveLength(1);
  74. expect(tt.routing!.rules![0].outboundTag).toBe('direct');
  75. expect(tt.routing!.rules![0].balancerTag).toBeUndefined();
  76. });
  77. it('cascade-removes the burst observer when deleting an outbound removes the last leastLoad balancer', () => {
  78. const tt = tpl({
  79. outbounds: [{ tag: 'll-out' }],
  80. routing: {
  81. rules: [],
  82. balancers: [{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }],
  83. },
  84. burstObservatory: { subjectSelector: ['ll-out'] },
  85. });
  86. const impact = planOutboundDeletion(tt, 0);
  87. expect(impact.balancers).toEqual([{ tag: 'll', reason: 'selectorEmptied' }]);
  88. expect(impact.burst).toBe(true);
  89. applyOutboundDeletion(tt, 0);
  90. expect(tt.burstObservatory).toBeUndefined();
  91. expect(tt.routing!.balancers).toEqual([]);
  92. });
  93. it('cascade-switches from burst to regular observer when only leastPing remains', () => {
  94. const tt = tpl({
  95. outbounds: [{ tag: 'lp-out' }, { tag: 'll-out' }],
  96. routing: {
  97. rules: [],
  98. balancers: [
  99. { tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
  100. { tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
  101. ],
  102. },
  103. burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
  104. });
  105. const impact = planOutboundDeletion(tt, 1);
  106. expect(impact.balancers).toEqual([{ tag: 'll', reason: 'selectorEmptied' }]);
  107. expect(impact.burst).toBe(true);
  108. applyOutboundDeletion(tt, 1);
  109. expect(tt.burstObservatory).toBeUndefined();
  110. expect((tt.observatory as { subjectSelector: string[] }).subjectSelector).toEqual(['lp-out']);
  111. expect(tt.routing!.balancers).toEqual([{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } }]);
  112. });
  113. it('cascade-keeps burst observer when leastPing is removed but leastLoad remains', () => {
  114. const tt = tpl({
  115. outbounds: [{ tag: 'lp-out' }, { tag: 'll-out' }],
  116. routing: {
  117. rules: [],
  118. balancers: [
  119. { tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
  120. { tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
  121. ],
  122. },
  123. burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
  124. });
  125. const impact = planOutboundDeletion(tt, 0);
  126. expect(impact.balancers).toEqual([{ tag: 'lp', reason: 'selectorEmptied' }]);
  127. expect(impact.burst).toBe(false);
  128. applyOutboundDeletion(tt, 0);
  129. expect(tt.observatory).toBeUndefined();
  130. expect((tt.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['ll-out']);
  131. expect(tt.routing!.balancers).toEqual([{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }]);
  132. });
  133. it('clears a fallbackTag and a dialerProxy pointing at the deleted outbound', () => {
  134. const tt = tpl({
  135. outbounds: [
  136. { tag: 'proxy-us' },
  137. { tag: 'chain', streamSettings: { sockopt: { dialerProxy: 'proxy-us' } } },
  138. ],
  139. routing: {
  140. rules: [],
  141. balancers: [{ tag: 'pool', selector: ['proxy-us', 'proxy-uk'], fallbackTag: 'proxy-us' }],
  142. },
  143. });
  144. applyOutboundDeletion(tt, 0);
  145. expect(tt.routing!.balancers![0].selector).toEqual(['proxy-uk']);
  146. expect(tt.routing!.balancers![0].fallbackTag).toBe('');
  147. expect(dialerProxyOf(tt, 'chain')).toBeUndefined();
  148. });
  149. it('never cascade-removes a tagless balancer (an empty tag must not match others)', () => {
  150. const tt = tpl({
  151. outbounds: [{ tag: 'proxy-us' }],
  152. routing: {
  153. rules: [],
  154. balancers: [
  155. { tag: '', selector: ['proxy-us'] },
  156. { tag: '', selector: ['keep-me'] },
  157. ],
  158. },
  159. });
  160. applyOutboundDeletion(tt, 0);
  161. expect(tt.routing!.balancers).toHaveLength(2);
  162. });
  163. it('does not throw on null entries in rules/balancers/outbounds (unvalidated config)', () => {
  164. const tt = tpl({
  165. outbounds: [{ tag: 'proxy-us' }, null],
  166. routing: {
  167. rules: [null, { type: 'field', inboundTag: ['in'], outboundTag: 'proxy-us' }],
  168. balancers: [null, { tag: 'pool', selector: ['keep'] }],
  169. },
  170. });
  171. expect(() => planOutboundDeletion(tt, 0)).not.toThrow();
  172. expect(() => applyOutboundDeletion(tt, 0)).not.toThrow();
  173. expect(tt.routing!.balancers).toEqual([{ tag: 'pool', selector: ['keep'] }]);
  174. });
  175. it('drops a rule that loses BOTH destinations in one cascade', () => {
  176. const tt = tpl({
  177. outbounds: [{ tag: 'proxy-us' }],
  178. routing: {
  179. rules: [{ type: 'field', outboundTag: 'proxy-us', balancerTag: 'pool' }],
  180. balancers: [{ tag: 'pool', selector: ['proxy-us'] }],
  181. },
  182. });
  183. applyOutboundDeletion(tt, 0);
  184. expect(tt.routing!.rules).toEqual([]);
  185. expect(tt.routing!.balancers).toEqual([]);
  186. });
  187. it('cleans a disabled rule too', () => {
  188. const tt = tpl({
  189. outbounds: [{ tag: 'proxy-us' }],
  190. routing: {
  191. rules: [{ type: 'field', enabled: false, inboundTag: ['in'], outboundTag: 'proxy-us' }],
  192. balancers: [],
  193. },
  194. });
  195. applyOutboundDeletion(tt, 0);
  196. expect(tt.routing!.rules).toEqual([]);
  197. });
  198. it('leaves unrelated rules and outbounds untouched', () => {
  199. const tt = tpl({
  200. outbounds: [{ tag: 'proxy-us' }, { tag: 'direct' }],
  201. routing: {
  202. rules: [
  203. { type: 'field', inboundTag: ['in'], outboundTag: 'proxy-us' },
  204. { type: 'field', inboundTag: ['in2'], outboundTag: 'direct' },
  205. ],
  206. balancers: [],
  207. },
  208. });
  209. applyOutboundDeletion(tt, 0);
  210. expect(tt.routing!.rules).toHaveLength(1);
  211. expect(tt.routing!.rules![0].outboundTag).toBe('direct');
  212. expect((tt.outbounds || []).map((o) => o?.tag)).toEqual(['direct']);
  213. });
  214. it('removes a referenced outbound with no rules and reports an empty impact', () => {
  215. const tt = tpl({ outbounds: [{ tag: 'lonely' }], routing: { rules: [], balancers: [] } });
  216. expect(planOutboundDeletion(tt, 0)).toEqual({ rules: [], balancers: [], observatory: false, burst: false });
  217. applyOutboundDeletion(tt, 0);
  218. expect(tt.outbounds).toEqual([]);
  219. });
  220. it('uses ruleTag as the impact label when present', () => {
  221. const tt = tpl({
  222. outbounds: [{ tag: 'x' }],
  223. routing: { rules: [{ type: 'field', ruleTag: 'block-ads', outboundTag: 'x' }], balancers: [] },
  224. });
  225. expect(planOutboundDeletion(tt, 0).rules[0].label).toBe('block-ads');
  226. });
  227. it('does not mutate the template when only planning', () => {
  228. const tt = tpl({
  229. outbounds: [{ tag: 'proxy-us' }],
  230. routing: {
  231. rules: [{ type: 'field', outboundTag: 'proxy-us', balancerTag: 'pool' }],
  232. balancers: [{ tag: 'pool', selector: ['proxy-us'] }],
  233. },
  234. burstObservatory: { subjectSelector: ['proxy-us'] },
  235. });
  236. const before = JSON.stringify(tt);
  237. planOutboundDeletion(tt, 0);
  238. expect(JSON.stringify(tt)).toBe(before);
  239. });
  240. it('predicts the surviving rule count exactly (plan/apply parity)', () => {
  241. const make = () =>
  242. tpl({
  243. outbounds: [{ tag: 'proxy-us' }],
  244. routing: {
  245. rules: [
  246. { type: 'field', inboundTag: ['a'], outboundTag: 'proxy-us' },
  247. { type: 'field', outboundTag: 'proxy-us', balancerTag: 'pool' },
  248. { type: 'field', inboundTag: ['b'], outboundTag: 'direct' },
  249. { type: 'field', inboundTag: ['c'], balancerTag: 'pool' },
  250. ],
  251. balancers: [{ tag: 'pool', selector: ['proxy-us'] }],
  252. },
  253. });
  254. const planned = make();
  255. const applied = make();
  256. const total = planned.routing!.rules!.length;
  257. const removed = planOutboundDeletion(planned, 0).rules.filter((r) => r.fate === 'removed').length;
  258. applyOutboundDeletion(applied, 0);
  259. expect(applied.routing!.rules!.length).toBe(total - removed);
  260. });
  261. });
  262. describe('balancer deletion', () => {
  263. it('drops a rule whose only destination was the deleted balancer', () => {
  264. const tt = tpl({
  265. routing: { rules: [{ type: 'field', inboundTag: ['in'], balancerTag: 'pool' }], balancers: [{ tag: 'pool', selector: ['a'] }] },
  266. });
  267. applyBalancerDeletion(tt, 0);
  268. expect(tt.routing!.balancers).toEqual([]);
  269. expect(tt.routing!.rules).toEqual([]);
  270. });
  271. it('keeps a rule that still has an outbound, dropping only the dead balancerTag', () => {
  272. const tt = tpl({
  273. routing: { rules: [{ type: 'field', outboundTag: 'direct', balancerTag: 'pool' }], balancers: [{ tag: 'pool', selector: ['a'] }] },
  274. });
  275. applyBalancerDeletion(tt, 0);
  276. expect(tt.routing!.rules).toHaveLength(1);
  277. expect(tt.routing!.rules![0].balancerTag).toBeUndefined();
  278. expect(tt.routing!.rules![0].outboundTag).toBe('direct');
  279. });
  280. it('reports and removes the observer when deleting the last leastPing balancer', () => {
  281. const tt = tpl({
  282. routing: { rules: [], balancers: [{ tag: 'b1', selector: ['a'], strategy: { type: 'leastPing' } }] },
  283. observatory: { subjectSelector: ['a'] },
  284. });
  285. expect(planBalancerDeletion(tt, 0).observatory).toBe(true);
  286. applyBalancerDeletion(tt, 0);
  287. expect(tt.observatory).toBeUndefined();
  288. expect(tt.routing!.balancers).toEqual([]);
  289. });
  290. it('reports and removes the burst observer when deleting the last leastLoad balancer', () => {
  291. const tt = tpl({
  292. routing: { rules: [], balancers: [{ tag: 'll', selector: ['a'], strategy: { type: 'leastLoad' } }] },
  293. burstObservatory: { subjectSelector: ['a'] },
  294. });
  295. expect(planBalancerDeletion(tt, 0).burst).toBe(true);
  296. applyBalancerDeletion(tt, 0);
  297. expect(tt.burstObservatory).toBeUndefined();
  298. expect(tt.routing!.balancers).toEqual([]);
  299. });
  300. it('reports and removes the burst observer when deleting the last fallback balancer', () => {
  301. const tt = tpl({
  302. routing: { rules: [], balancers: [{ tag: 'rf', selector: ['a'], fallbackTag: 'direct' }] },
  303. burstObservatory: { subjectSelector: ['a'] },
  304. });
  305. expect(planBalancerDeletion(tt, 0).burst).toBe(true);
  306. applyBalancerDeletion(tt, 0);
  307. expect(tt.burstObservatory).toBeUndefined();
  308. expect(tt.routing!.balancers).toEqual([]);
  309. });
  310. it('switches from burst to regular observer when the deleted balancer was the last burst-required one', () => {
  311. const tt = tpl({
  312. routing: {
  313. rules: [],
  314. balancers: [
  315. { tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
  316. { tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
  317. ],
  318. },
  319. burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
  320. });
  321. const impact = planBalancerDeletion(tt, 1);
  322. expect(impact.burst).toBe(true);
  323. expect(impact.observatory).toBe(false);
  324. applyBalancerDeletion(tt, 1);
  325. expect(tt.burstObservatory).toBeUndefined();
  326. expect((tt.observatory as { subjectSelector: string[] }).subjectSelector).toEqual(['lp-out']);
  327. expect(tt.routing!.balancers).toEqual([{ tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } }]);
  328. });
  329. it('keeps burst observer when deleting leastPing but a burst-required balancer remains', () => {
  330. const tt = tpl({
  331. routing: {
  332. rules: [],
  333. balancers: [
  334. { tag: 'lp', selector: ['lp-out'], strategy: { type: 'leastPing' } },
  335. { tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } },
  336. ],
  337. },
  338. burstObservatory: { subjectSelector: ['lp-out', 'll-out'] },
  339. });
  340. const impact = planBalancerDeletion(tt, 0);
  341. expect(impact.burst).toBe(false);
  342. expect(impact.observatory).toBe(false);
  343. applyBalancerDeletion(tt, 0);
  344. expect(tt.observatory).toBeUndefined();
  345. expect((tt.burstObservatory as { subjectSelector: string[] }).subjectSelector).toEqual(['ll-out']);
  346. expect(tt.routing!.balancers).toEqual([{ tag: 'll', selector: ['ll-out'], strategy: { type: 'leastLoad' } }]);
  347. });
  348. it('does not report rules when the deleted balancer is unreferenced', () => {
  349. const tt = tpl({
  350. routing: { rules: [{ type: 'field', inboundTag: ['in'], outboundTag: 'direct' }], balancers: [{ tag: 'pool', selector: ['a'] }] },
  351. });
  352. expect(planBalancerDeletion(tt, 0).rules).toEqual([]);
  353. applyBalancerDeletion(tt, 0);
  354. expect(tt.routing!.rules).toHaveLength(1);
  355. });
  356. });