Repository navigation
Expand file tree
/
Copy pathbuild-config.test.js
More file actions
144 lines (125 loc) · 5.68 KB
/
Copy pathbuild-config.test.js
File metadata and controls
144 lines (125 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, it, expect } from "vitest";
import {
resolveAgentEnabled,
applyAgentManifest,
applyFirefoxSandboxManifest,
FIREFOX_SANDBOX_CSP,
createChromeManifest,
createFirefoxManifest,
} from "./build-config.js";
describe("构建配置 - agent 开关", () => {
describe("resolveAgentEnabled - 打包判断(稳定版屏蔽、beta 开启,SC_DISABLE_AGENT 覆盖)", () => {
it("稳定版(非 beta)默认屏蔽 agent", () => {
expect(resolveAgentEnabled({ isBeta: false, disableEnv: undefined })).toBe(false);
});
it("beta 版默认开启 agent", () => {
expect(resolveAgentEnabled({ isBeta: true, disableEnv: undefined })).toBe(true);
});
it("SC_DISABLE_AGENT='true' 强制屏蔽,覆盖 beta 默认开启", () => {
expect(resolveAgentEnabled({ isBeta: true, disableEnv: "true" })).toBe(false);
});
it("SC_DISABLE_AGENT='false' 强制开启,覆盖稳定版默认屏蔽", () => {
expect(resolveAgentEnabled({ isBeta: false, disableEnv: "false" })).toBe(true);
});
it("SC_DISABLE_AGENT 为非 'true'/'false' 的值时回退到版本派生", () => {
expect(resolveAgentEnabled({ isBeta: false, disableEnv: "1" })).toBe(false);
expect(resolveAgentEnabled({ isBeta: true, disableEnv: "1" })).toBe(true);
});
});
describe("applyAgentManifest", () => {
const makeManifest = () => ({
permissions: ["tabs", "debugger", "storage"],
optional_permissions: ["background", "userScripts"],
});
it("启用 agent 时原样返回 manifest 且保留 debugger 权限", () => {
const manifest = makeManifest();
const result = applyAgentManifest(manifest, true);
expect(result).toBe(manifest);
expect(result.permissions).toContain("debugger");
});
it("禁用 agent 时移除 debugger 权限", () => {
const result = applyAgentManifest(makeManifest(), false);
expect(result.permissions).not.toContain("debugger");
expect(result.permissions).toEqual(["tabs", "storage"]);
});
it("禁用 agent 时不改动其它权限", () => {
const result = applyAgentManifest(makeManifest(), false);
expect(result.optional_permissions).toEqual(["background", "userScripts"]);
});
it("禁用 agent 时不修改入参 manifest", () => {
const manifest = makeManifest();
applyAgentManifest(manifest, false);
expect(manifest.permissions).toContain("debugger");
});
});
});
describe("构建配置 - Firefox sandbox manifest", () => {
it("仅为 Firefox 产物添加 sandbox CSP,且不修改共用 manifest", () => {
const manifest = {
manifest_version: 3,
sandbox: { pages: ["src/sandbox.html"] },
};
const result = applyFirefoxSandboxManifest(manifest);
expect(result.content_security_policy).toEqual({
sandbox:
"sandbox allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-presentation allow-scripts allow-storage-access-by-user-activation allow-top-navigation allow-top-navigation-by-user-activation allow-top-navigation-to-custom-protocols; script-src 'unsafe-inline' 'unsafe-eval' https: http: data: blob: 'self';",
});
expect(manifest).not.toHaveProperty("content_security_policy");
});
});
describe("构建配置 - 浏览器专用 manifest", () => {
it("Chrome 产物保留原有权限语义并移除 Firefox 专用字段", () => {
const source = {
permissions: ["tabs", "userScripts", "debugger"],
optional_permissions: ["background", "userScripts"],
background: {
service_worker: "src/service_worker.js",
scripts: ["src/service_worker.js"],
},
content_security_policy: {
extension_pages: "script-src 'self'",
sandbox: "firefox-only",
},
};
const result = createChromeManifest(source, true);
expect(result).toEqual({
permissions: ["tabs", "userScripts", "debugger"],
optional_permissions: ["background"],
background: {
service_worker: "src/service_worker.js",
},
content_security_policy: {
extension_pages: "script-src 'self'",
},
});
expect(source.background).toHaveProperty("scripts");
expect(source.optional_permissions).toContain("userScripts");
expect(source.content_security_policy).toHaveProperty("sandbox");
});
it("Firefox 产物仅保留支持的后台字段和权限", () => {
const source = {
permissions: ["tabs", "userScripts", "debugger", "offscreen"],
optional_permissions: ["background", "userScripts"],
background: {
service_worker: "src/service_worker.js",
scripts: ["src/service_worker.js"],
},
sandbox: { pages: ["src/sandbox.html"] },
incognito: "split",
message_serialization: "structured_clone",
};
const result = createFirefoxManifest(source, true, "{firefox-id}");
expect(result.permissions).toEqual(["tabs"]);
expect(result.optional_permissions).toEqual(["userScripts", "webRequestBlocking"]);
expect(result.background).toEqual({ scripts: ["src/service_worker.js"] });
expect(result.sandbox).toEqual(source.sandbox);
expect(result.content_security_policy?.sandbox).toBe(FIREFOX_SANDBOX_CSP);
expect(result.incognito).toBe("spanning");
expect(result).not.toHaveProperty("message_serialization");
expect(result.browser_specific_settings.gecko.id).toBe("{firefox-id}");
expect(result.browser_specific_settings.gecko.strict_min_version).toBe("154.0");
expect(result.commands).toEqual({ _execute_action: {} });
expect(source.optional_permissions).toEqual(["background", "userScripts"]);
expect(source.background).toHaveProperty("service_worker");
});
});