|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { RoutingRule } from "../../../generated/bindings"; |
| 3 | +import { isCatchAllRule, isRedundantCatchAll } from "../helpers"; |
| 4 | + |
| 5 | +const rule = (patch: Partial<RoutingRule>): RoutingRule => ({ |
| 6 | + id: "r1", |
| 7 | + remarks: "", |
| 8 | + enabled: true, |
| 9 | + outboundTag: "proxy", |
| 10 | + ...patch, |
| 11 | +}); |
| 12 | + |
| 13 | +describe("isCatchAllRule", () => { |
| 14 | + it("flags a full port range with no other match field", () => { |
| 15 | + expect(isCatchAllRule(rule({ port: "0-65535" }))).toBe(true); |
| 16 | + expect(isCatchAllRule(rule({ port: "1-65535" }))).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("flags a full range assembled from several parts", () => { |
| 20 | + expect(isCatchAllRule(rule({ port: "1-1000,1001-65535" }))).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it("ignores a range with a gap or a short tail", () => { |
| 24 | + expect(isCatchAllRule(rule({ port: "1-1000,1002-65535" }))).toBe(false); |
| 25 | + expect(isCatchAllRule(rule({ port: "0-65534" }))).toBe(false); |
| 26 | + expect(isCatchAllRule(rule({ port: "443" }))).toBe(false); |
| 27 | + }); |
| 28 | + |
| 29 | + it("ignores rules that also match on domain, ip or protocol", () => { |
| 30 | + expect(isCatchAllRule(rule({ port: "0-65535", domain: ["geosite:private"] }))).toBe(false); |
| 31 | + expect(isCatchAllRule(rule({ port: "0-65535", ip: ["geoip:ru"] }))).toBe(false); |
| 32 | + expect(isCatchAllRule(rule({ port: "0-65535", protocol: ["bittorrent"] }))).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("treats a single-network rule as narrower than catch-all", () => { |
| 36 | + expect(isCatchAllRule(rule({ port: "0-65535", network: "udp" }))).toBe(false); |
| 37 | + expect(isCatchAllRule(rule({ port: "0-65535", network: "tcp,udp" }))).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it("ignores disabled rules and rules the backend drops for having no match field", () => { |
| 41 | + expect(isCatchAllRule(rule({ port: "0-65535", enabled: false }))).toBe(false); |
| 42 | + expect(isCatchAllRule(rule({}))).toBe(false); |
| 43 | + expect(isCatchAllRule(rule({ port: " " }))).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it("ignores a malformed port list", () => { |
| 47 | + expect(isCatchAllRule(rule({ port: "0-abc" }))).toBe(false); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("isRedundantCatchAll", () => { |
| 52 | + const catchAll = (patch: Partial<RoutingRule> = {}) => rule({ port: "0-65535", ...patch }); |
| 53 | + |
| 54 | + it("flags a trailing catch-all that only repeats the final proxy fallback", () => { |
| 55 | + expect(isRedundantCatchAll([rule({ ip: ["geoip:ru"] }), catchAll()], 1)).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it("ignores one that still shadows an enabled rule below", () => { |
| 59 | + expect(isRedundantCatchAll([catchAll(), rule({ ip: ["geoip:ru"] })], 0)).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("looks past disabled rules below", () => { |
| 63 | + expect(isRedundantCatchAll([catchAll(), rule({ ip: ["geoip:ru"], enabled: false })], 0)).toBe( |
| 64 | + true, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("ignores a catch-all routed anywhere but the proxy", () => { |
| 69 | + expect(isRedundantCatchAll([catchAll({ outboundTag: "direct" })], 0)).toBe(false); |
| 70 | + expect(isRedundantCatchAll([catchAll({ outboundTag: "block" })], 0)).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("ignores an index that points at no rule", () => { |
| 74 | + expect(isRedundantCatchAll([catchAll()], -1)).toBe(false); |
| 75 | + expect(isRedundantCatchAll([], 0)).toBe(false); |
| 76 | + }); |
| 77 | +}); |
0 commit comments