|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { dealPatternMatches, parsePatternMatchesURL, UrlMatch } from "./match"; |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
3 | 4 |
|
4 | 5 | // https://developer.chrome.com/docs/extensions/mv3/match_patterns/ |
5 | 6 | describe("UrlMatch-google", () => { |
@@ -132,6 +133,16 @@ describe("UrlMatch-port2", () => { |
132 | 133 | }); |
133 | 134 | }); |
134 | 135 |
|
| 136 | +describe("UrlMatch-exclude", () => { |
| 137 | + it("exclue-port", () => { |
| 138 | + const url = new UrlMatch<string>(); |
| 139 | + url.add("*://*/*", "ok3"); |
| 140 | + url.exclude("*:5244*", "ok3"); |
| 141 | + expect(url.match("http://test.list.ggnb.top:5244/search")).toEqual([]); |
| 142 | + expect(url.match("http://test.list.ggnb.top:80/search")).toEqual(["ok3"]); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
135 | 146 | // https://developer.chrome.com/docs/extensions/mv3/match_patterns/ |
136 | 147 | describe("dealPatternMatches", () => { |
137 | 148 | it("https://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=zh-cn#examples", () => { |
@@ -299,3 +310,50 @@ describe("parsePatternMatchesURL", () => { |
299 | 310 | }); |
300 | 311 | }); |
301 | 312 | }); |
| 313 | + |
| 314 | +const makeUrlMatcher = (uuid: string, matchesList: string[], excludeMatchesList: string[]) => { |
| 315 | + const patternMatches = dealPatternMatches(matchesList); |
| 316 | + const matchesResult = patternMatches.result; |
| 317 | + const matches = patternMatches.patternResult; |
| 318 | + const result = dealPatternMatches(excludeMatchesList, { |
| 319 | + exclude: true, |
| 320 | + }); |
| 321 | + const excludeMatchesResult = result.result; |
| 322 | + const excludeMatches = result.patternResult; |
| 323 | + |
| 324 | + const urlMatcher = new UrlMatch<string>(); |
| 325 | + for (const match of matchesResult) { |
| 326 | + urlMatcher.add(match, uuid); |
| 327 | + } |
| 328 | + for (const exclude of excludeMatchesResult) { |
| 329 | + urlMatcher.exclude(exclude, uuid); |
| 330 | + } |
| 331 | + |
| 332 | + return { urlMatcher, matches, excludeMatches }; |
| 333 | +}; |
| 334 | + |
| 335 | +describe("UrlMatch-exclusion", () => { |
| 336 | + it("exclusion-1", () => { |
| 337 | + const matchesList: string[] = ["*://**/*"]; |
| 338 | + const excludeMatchesList: string[] = [ |
| 339 | + "*://steamcommunity.com/*", |
| 340 | + "*.jd.com/*", |
| 341 | + "*docs.google.com/*", |
| 342 | + "*://*.amazon.tld/*", |
| 343 | + "*shop*", |
| 344 | + "/.*(?<!test)store.*/", |
| 345 | + "*/releases", |
| 346 | + "*/releases/*", |
| 347 | + "*:5244*", |
| 348 | + ]; |
| 349 | + const uuid = uuidv4(); |
| 350 | + const { urlMatcher } = makeUrlMatcher(uuid, matchesList, excludeMatchesList); |
| 351 | + expect(urlMatcher.match("https://foo.api.bar/baz")).toEqual([uuid]); |
| 352 | + expect(urlMatcher.match("https://steamcommunity.com/foo")).toEqual([]); |
| 353 | + expect(urlMatcher.match("https://jd.com/foo")).toEqual([]); |
| 354 | + expect(urlMatcher.match("https://docs.google.com/foo")).toEqual([]); |
| 355 | + expect(urlMatcher.match("https://amazon.com/foo")).toEqual([]); |
| 356 | + expect(urlMatcher.match("https://test.store.com/aaa")).toEqual([]); |
| 357 | + expect(urlMatcher.match("https://foo.api.bar:5244/baz")).toEqual([]); |
| 358 | + }); |
| 359 | +}); |
0 commit comments