Skip to content

Commit 0046bb7

Browse files
cyfung1031CodFrm
andauthored
🐛 添加单元测试和修复@exclude问题 (#618)
* Exclusion Test * 优化url match逻辑 * 通过单元测试 * 调整单元测试 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 71e97d5 commit 0046bb7

2 files changed

Lines changed: 61 additions & 27 deletions

File tree

src/pkg/utils/match.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import { dealPatternMatches, parsePatternMatchesURL, UrlMatch } from "./match";
3+
import { v4 as uuidv4 } from "uuid";
34

45
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
56
describe("UrlMatch-google", () => {
@@ -132,6 +133,16 @@ describe("UrlMatch-port2", () => {
132133
});
133134
});
134135

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+
135146
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
136147
describe("dealPatternMatches", () => {
137148
it("https://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=zh-cn#examples", () => {
@@ -299,3 +310,50 @@ describe("parsePatternMatchesURL", () => {
299310
});
300311
});
301312
});
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+
});

src/pkg/utils/match.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,14 @@ export default class Match<T> {
3333
search: match[5],
3434
};
3535
}
36-
// 处理一些特殊情况
37-
switch (url) {
38-
case "*":
39-
case "http*":
40-
return {
41-
scheme: "*",
42-
host: "*",
43-
path: "*",
44-
search: "*",
45-
};
46-
default:
47-
// 无*://的情况
48-
if (!url.includes("://")) {
49-
// 直接转为通配符
50-
const match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
51-
if (match) {
52-
return {
53-
scheme: "*",
54-
host: match[1],
55-
path: match[3] || (url[url.length - 1] === "*" ? "*" : "/"),
56-
search: match[4],
57-
};
58-
}
59-
}
60-
}
6136
return undefined;
6237
}
6338

6439
protected compileRe(url: string): string {
6540
const u = this.parseURL(url);
6641
if (!u) {
67-
return "";
42+
// 直接将*替换为正则
43+
return url.replace(/\*/g, ".*");
6844
}
6945
switch (u.scheme) {
7046
case "*":
@@ -82,7 +58,7 @@ export default class Match<T> {
8258
}
8359
// 处理顶域
8460
if (u.host.endsWith("tld")) {
85-
u.host = `${u.host.substr(0, u.host.length - 3)}.*?`;
61+
u.host = `${u.host.substring(0, u.host.length - 3)}.*?`;
8662
}
8763
// 处理端口
8864
const pos2 = u.host.indexOf(":");

0 commit comments

Comments
 (0)