Skip to content

Commit 4e91b36

Browse files
committed
🧪 添加GM.*的单元测试
1 parent 1d6d52a commit 4e91b36

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/app/service/content/exec_script.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ExtVersion } from "@App/app/const";
55
import { initTestEnv } from "@Tests/utils";
66
import { describe, expect, it } from "vitest";
77
import type { GMInfoEnv } from "./types";
8+
import { ScriptLoadInfo } from "../service_worker/types";
89

910
initTestEnv();
1011

@@ -183,3 +184,54 @@ describe("none this", () => {
183184
expect(ret).toEqual("ok");
184185
});
185186
});
187+
188+
describe("@grant GM", () => {
189+
it("GM_", async () => {
190+
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
191+
script.metadata.grant = ["GM_getValue", "GM_getTab", "GM_saveTab"];
192+
// @ts-ignore
193+
const exec = new ExecScript(script, undefined, undefined, undefined, envInfo);
194+
script.code = `return {
195+
["GM.getValue"]: GM.getValue,
196+
["GM.getTab"]: GM.getTab,
197+
["GM.setTab"]: GM.setTab,
198+
GM_getValue: this.GM_getValue,
199+
GM_getTab: this.GM_getTab,
200+
GM_saveTab: this.GM_saveTab,
201+
}`;
202+
exec.scriptFunc = compileScript(compileScriptCode(script));
203+
const ret = await exec.exec();
204+
expect(ret).toEqual({
205+
"GM.getValue": undefined,
206+
"GM.getTab": undefined,
207+
"GM.setTab": undefined,
208+
GM_getValue: expect.any(Function),
209+
GM_getTab: expect.any(Function),
210+
GM_saveTab: expect.any(Function),
211+
});
212+
});
213+
it("GM.*", async () => {
214+
const script = Object.assign({}, scriptRes2) as ScriptLoadInfo;
215+
script.metadata.grant = ["GM.getValue", "GM.getTab", "GM.saveTab"];
216+
// @ts-ignore
217+
const exec = new ExecScript(script, undefined, undefined, undefined, envInfo);
218+
script.code = `return {
219+
["GM.getValue"]: GM.getValue,
220+
["GM.getTab"]: GM.getTab,
221+
["GM.saveTab"]: GM.saveTab,
222+
GM_getValue: this.GM_getValue,
223+
GM_getTab: this.GM_getTab,
224+
GM_saveTab: this.GM_saveTab,
225+
}`;
226+
exec.scriptFunc = compileScript(compileScriptCode(script));
227+
const ret = await exec.exec();
228+
expect(ret).toEqual({
229+
"GM.getValue": expect.any(Function),
230+
"GM.getTab": expect.any(Function),
231+
"GM.setTab": expect.any(Function),
232+
GM_getValue: undefined,
233+
GM_getTab: undefined,
234+
GM_setTab: undefined,
235+
});
236+
});
237+
});

src/app/service/content/gm_api.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -785,18 +785,7 @@ export default class GMApi extends GM_Base {
785785
public GM_xmlhttpRequest(details: GMTypes.XHRDetails) {
786786
return GM_xmlhttpRequest(this, details)
787787
}
788-
@GMContext.API({
789-
depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],
790-
})
791-
public GM_xmlHttpRequest(details: GMTypes.XHRDetails) {
792-
return GM_xmlhttpRequest(this, details)
793-
}
794-
@GMContext.API({
795-
depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],
796-
})
797-
public ['GM.xmlhttpRequest'](details: GMTypes.XHRDetails) {
798-
return GM_xmlhttpRequest(this, details)
799-
}
788+
800789
@GMContext.API({
801790
depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],
802791
})

0 commit comments

Comments
 (0)