Repository navigation
Expand file tree
/
Copy pathrunner.test.js
More file actions
209 lines (197 loc) · 8.6 KB
/
Copy pathrunner.test.js
File metadata and controls
209 lines (197 loc) · 8.6 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const test = require("node:test");
const {
createPatchReport,
enabledFeatureFailuresFromReport,
} = require("../lib/patch-report.js");
const {
allPatchPolicies,
corePatchDescriptors,
createMainBundleContext,
patchExtractedApp,
requiredPatchNamesForProfile,
} = require("./runner.js");
const { findMainProcessChunks } = require("./lib/assets.js");
const {
findMatchingBrace,
} = require("./lib/minified-js.js");
const emptyConfig = path.join(__dirname, "..", "..", "linux-features", "features.example.json");
test("the official Linux baseline needs no core compatibility patch", () => {
assert.deepEqual(corePatchDescriptors(), []);
assert.deepEqual(allPatchPolicies({ featuresConfigPath: emptyConfig }), []);
assert.deepEqual(requiredPatchNamesForProfile("upstream-build", {
featuresConfigPath: emptyConfig,
}), []);
});
test("the signed baseline owns approved quit completion and shell startup", {
skip: process.env.CODEX_SIGNED_EXTRACTED_APP == null,
}, () => {
const mainProcess = findMainProcessChunks(process.env.CODEX_SIGNED_EXTRACTED_APP);
assert.ok(mainProcess, "Expected the verified main-process chunks");
const chunks = mainProcess.chunks.map((name) => ({
name,
source: fs.readFileSync(path.join(mainProcess.buildDir, name), "utf8"),
}));
const uniqueOwner = (markers, description) => {
const owners = chunks.filter(({ source }) =>
markers.every((marker) => source.includes(marker)));
assert.equal(
owners.length,
1,
`Expected one ${description}; found ${owners.map(({ name }) => name).join(", ") || "none"}`,
);
return owners[0].source;
};
const quitOwner = uniqueOwner([
"canQuitWithoutPrompt(){return this.allowQuitWithoutPrompt}",
"markQuitApproved(){this.allowQuitWithoutPrompt=!0}",
"hasPendingLocalGitReviewComments:",
'.app.on("before-quit",',
], "native approved-quit lifecycle owner");
const identifier = "[A-Za-z_$][\\w$]*";
const lifecycleHeader = new RegExp(
`function\\(\\{[^{}]*quitState:(?<quitState>${identifier}),` +
`windows:(?<windows>${identifier}),[^{}]*` +
`hasPendingLocalGitReviewComments:(?<pending>${identifier}),[^{}]*\\}\\)\\{`,
"gu",
);
const lifecycleMatches = [...quitOwner.matchAll(lifecycleHeader)];
assert.equal(lifecycleMatches.length, 1, "Expected one native quit lifecycle owner");
const { quitState, windows, pending } = lifecycleMatches[0].groups;
const lifecycleOpen = lifecycleMatches[0].index + lifecycleMatches[0][0].length - 1;
const lifecycleClose = findMatchingBrace(quitOwner, lifecycleOpen);
assert.ok(lifecycleClose > lifecycleOpen, "Expected a complete native quit lifecycle owner");
const lifecycleBody = quitOwner.slice(lifecycleOpen, lifecycleClose + 1);
const beforeQuitPattern = new RegExp(
`${identifier}\\.app\\.on\\("before-quit",(?<event>${identifier})=>\\{`,
"gu",
);
const beforeQuitMatches = [...lifecycleBody.matchAll(beforeQuitPattern)];
assert.equal(beforeQuitMatches.length, 1, "Expected one native before-quit handler");
const handlerOpen = beforeQuitMatches[0].index + beforeQuitMatches[0][0].length - 1;
const handlerClose = findMatchingBrace(lifecycleBody, handlerOpen);
assert.ok(handlerClose > handlerOpen, "Expected a complete native before-quit handler");
const handlerBody = lifecycleBody.slice(handlerOpen, handlerClose + 1);
const approvalGate = `!${quitState}.canQuitWithoutPrompt()`;
const pendingCheck = `${pending}()`;
const localApproval = `${quitState}.markQuitApproved()`;
const quittingTransition = `${windows}.markAppQuitting()`;
assert.equal(handlerBody.split(approvalGate).length - 1, 1);
const pendingMatches = [...handlerBody.matchAll(new RegExp(`(?<![\\w$.])${pending}\\(\\)`, "g"))];
assert.equal(pendingMatches.length, 1);
assert.equal(handlerBody.split(localApproval).length - 1, 1);
assert.equal(handlerBody.split(quittingTransition).length - 1, 1);
assert.ok(handlerBody.indexOf(approvalGate) < pendingMatches[0].index);
assert.ok(pendingMatches[0].index < handlerBody.indexOf(localApproval));
assert.ok(handlerBody.indexOf(localApproval) < handlerBody.indexOf(quittingTransition));
const shellOwner = uniqueOwner([
"main app.whenReady resolved",
"shell environment hydrated",
], "upstream shell-startup owner");
const readyPattern = new RegExp(
`await ${identifier}\\.app\\.whenReady\\(\\),${identifier}\\(` +
`"main app\\.whenReady resolved"`,
"gu",
);
const shellStartPattern = new RegExp(
`(?:let|const) ${identifier}=Date\\.now\\(\\),${identifier}=\\(0,` +
`${identifier}\\.${identifier}\\)\\(\\),${identifier}=${identifier}=>\\{` +
`${identifier}\\("shell environment hydrated"`,
"gu",
);
const readyMatches = [...shellOwner.matchAll(readyPattern)];
const shellStartMatches = [...shellOwner.matchAll(shellStartPattern)];
assert.equal(readyMatches.length, 1, "Expected one awaited main app readiness boundary");
assert.equal(shellStartMatches.length, 1, "Expected one shell-environment startup call");
assert.ok(
shellStartMatches[0].index > readyMatches[0].index,
"Shell-environment startup must follow awaited app readiness",
);
});
test("runner context exposes enabled feature IDs", () => {
const temp = fs.mkdtempSync(path.join(os.tmpdir(), "runner-context-"));
try {
const config = path.join(temp, "features.json");
fs.writeFileSync(config, '{"enabled":["frameless-titlebar"]}\n');
const context = createMainBundleContext(null, { featuresConfigPath: config });
assert.deepEqual(context.enabledFeatureIds, ["frameless-titlebar"]);
} finally {
fs.rmSync(temp, { recursive: true, force: true });
}
});
test("an empty core registry leaves unmatched application assets unchanged", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "runner-baseline-"));
try {
const mainDir = path.join(root, ".vite", "build");
const webviewDir = path.join(root, "webview", "assets");
fs.mkdirSync(mainDir, { recursive: true });
fs.mkdirSync(webviewDir, { recursive: true });
const webview = path.join(webviewDir, "app-initial-A.js");
const main = path.join(mainDir, "early-bootstrap.js");
fs.writeFileSync(main, "const officialMain=true;\n");
fs.writeFileSync(webview, "official-webview\n");
const mainBefore = fs.readFileSync(main);
const webviewBefore = fs.readFileSync(webview);
const report = createPatchReport();
patchExtractedApp(root, {
report,
featuresConfigPath: emptyConfig,
});
assert.deepEqual(fs.readFileSync(main), mainBefore);
assert.deepEqual(fs.readFileSync(webview), webviewBefore);
assert.deepEqual(report.patches, []);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test("missing main bundle records enabled feature drift", (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "runner-missing-main-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const config = path.join(root, "features.json");
fs.writeFileSync(config, '{"enabled":["frameless-titlebar"]}\n');
const report = createPatchReport();
patchExtractedApp(root, { report, featuresConfigPath: config });
const entry = report.patches.find((patch) =>
patch.name === "feature:frameless-titlebar:main-process");
assert.ok(entry);
assert.equal(entry.status, "skipped-optional");
assert.equal(entry.enforceWhenEnabled, true);
assert.equal(entry.unavailable, true);
assert.equal(
enabledFeatureFailuresFromReport(report).some((failure) => failure.name === entry.name),
true,
);
});
test("main-process chunks are the rspack early-bootstrap entry and its numbered chunks", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "runner-rspack-chunks-"));
try {
const buildDir = path.join(root, ".vite", "build");
fs.mkdirSync(buildDir, { recursive: true });
for (const name of [
"early-bootstrap.js",
"early-bootstrap-916-2f6483d5bfd826f1.js",
"early-bootstrap-87-929aa7e5d829f2a8.js",
"worker.js",
"worker-904-f6f849be9d0ea1f7.js",
"preload.js",
"main-legacy.js",
]) {
fs.writeFileSync(path.join(buildDir, name), "");
}
assert.deepEqual(findMainProcessChunks(root), {
buildDir,
chunks: [
"early-bootstrap-87-929aa7e5d829f2a8.js",
"early-bootstrap-916-2f6483d5bfd826f1.js",
"early-bootstrap.js",
],
});
assert.equal(findMainProcessChunks(path.join(root, "missing")), null);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});