|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { RoutingRule } from "../../../generated/bindings"; |
| 3 | +import { isCatchAllRule } 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 | +}); |
0 commit comments