Repository navigation
Expand file tree
/
Copy pathpatch-linux-window-ui.test.js
More file actions
273 lines (257 loc) · 12.6 KB
/
Copy pathpatch-linux-window-ui.test.js
File metadata and controls
273 lines (257 loc) · 12.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
"use strict";
const assert = require("node:assert/strict");
const childProcess = require("node:child_process");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const test = require("node:test");
const {
corePatchDescriptors,
featurePatchDescriptors,
patchExtractedApp,
} = require("./patches/runner.js");
const {
createPatchReport,
enabledFeatureFailuresFromReport,
reportHasPatchChanges,
} = require("./lib/patch-report.js");
test("best-effort feature drift stays non-fatal without hiding changed outputs", () => {
const report = createPatchReport();
report.enabledFeatures = ["best-effort", "strict"];
report.patches = [
{
name: "feature:best-effort:repair",
status: "skipped-optional",
ciPolicy: "optional",
sourceKind: "feature",
featureId: "best-effort",
enforceWhenEnabled: false,
},
];
assert.deepEqual(enabledFeatureFailuresFromReport(report), []);
assert.equal(reportHasPatchChanges(report), false);
report.patches[0].status = "applied-with-warnings";
assert.equal(reportHasPatchChanges(report), true);
report.patches[0] = {
...report.patches[0],
name: "feature:strict:repair",
status: "skipped-optional",
featureId: "strict",
enforceWhenEnabled: true,
};
assert.equal(enabledFeatureFailuresFromReport(report).length, 1);
});
test("official Linux baseline has no required compatibility descriptors", () => {
assert.deepEqual(corePatchDescriptors(), []);
assert.equal(featurePatchDescriptors({
featuresConfigPath: path.join(__dirname, "..", "linux-features", "features.example.json"),
}).length, 0);
});
test("an explicitly empty core registry leaves an extracted official-style app unchanged", (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-empty-patch-registry-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const buildDir = path.join(root, ".vite", "build");
const webviewDir = path.join(root, "webview", "assets");
fs.mkdirSync(buildDir, { recursive: true });
fs.mkdirSync(webviewDir, { recursive: true });
const mainPath = path.join(buildDir, "early-bootstrap.js");
const assetPath = path.join(webviewDir, "app-initial-fixture.js");
fs.writeFileSync(mainPath, "const officialMain=true;\n");
fs.writeFileSync(assetPath, "const officialWebview=true;\n");
const before = new Map([
[mainPath, fs.readFileSync(mainPath)],
[assetPath, fs.readFileSync(assetPath)],
]);
const report = createPatchReport();
patchExtractedApp(root, {
report,
corePatchRoot: path.join(root, "empty-core-registry"),
featuresConfigPath: path.join(__dirname, "..", "linux-features", "features.example.json"),
});
assert.deepEqual(report.patches, []);
for (const [filePath, bytes] of before) assert.deepEqual(fs.readFileSync(filePath), bytes);
});
test("final main-process syntax gate rejects duplicate parameters without executing the app", (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-main-syntax-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const buildDir = path.join(root, ".vite", "build");
fs.mkdirSync(buildDir, { recursive: true });
// A valid entry does not hide an invalid imported chunk. This is the exact
// destructured/formal collision introduced by the 26.1013 CUA adaptation.
fs.writeFileSync(path.join(buildDir, "main.js"), 'throw Error("must never execute");');
const chunk = path.join(buildDir, "cua-config.js");
fs.writeFileSync(chunk, "function Aa({browserBackends:e,deferTools:t,surfaces:n,nodeRepl:r},r){}");
const options = {
corePatchRoot: path.join(root, "empty-core"),
featuresConfigPath: path.join(__dirname, "..", "linux-features", "features.example.json"),
};
assert.throws(() => patchExtractedApp(root, options), (error) =>
error.code === "PATCH_INTEGRITY_FAILURE" &&
/cua-config\.js: SyntaxError: Duplicate parameter name/.test(error.message));
fs.writeFileSync(chunk, "function Aa({browserBackends:e,deferTools:t,surfaces:n,nodeRepl:r},pluginRoot){}");
fs.writeFileSync(path.join(buildDir, "module.mjs"), 'export const value = await Promise.resolve(1); throw Error("must never execute");');
fs.writeFileSync(path.join(buildDir, "common.cjs"), 'return;');
assert.doesNotThrow(() => patchExtractedApp(root, options));
});
test("syntax validation runs after extracted-app post-webview transforms", (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-final-syntax-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const buildDir = path.join(root, ".vite", "build");
const corePatchRoot = path.join(root, "core");
fs.mkdirSync(buildDir, { recursive: true });
fs.mkdirSync(corePatchRoot, { recursive: true });
fs.writeFileSync(path.join(buildDir, "main.js"), "const main=true;");
fs.writeFileSync(path.join(corePatchRoot, "patch.js"), `
module.exports.descriptors = [{
id: "late-syntax-regression", phase: "extracted-app:post-webview",
apply(root) {
require("node:fs").writeFileSync(require("node:path").join(root, ".vite/build/main.js"),
"function Aa({nodeRepl:r},r){}");
return { changed: true };
}
}];
`);
assert.throws(() => patchExtractedApp(root, {
corePatchRoot,
featuresConfigPath: path.join(__dirname, "..", "linux-features", "features.example.json"),
}), /JavaScript syntax validation failed.*main\.js: SyntaxError: Duplicate parameter name/);
});
test("CLI rejects drift in an explicitly enabled feature", (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-enabled-feature-drift-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
fs.mkdirSync(path.join(root, ".vite", "build"), { recursive: true });
fs.mkdirSync(path.join(root, "webview", "assets"), { recursive: true });
fs.writeFileSync(path.join(root, ".vite", "build", "early-bootstrap.js"), "const main=true;\n");
fs.writeFileSync(path.join(root, "webview", "assets", "app-initial-fixture.js"), "const webview=true;\n");
const config = path.join(root, "features.json");
const report = path.join(root, "report.json");
fs.writeFileSync(config, '{"enabled":["frameless-titlebar"]}\n');
const result = childProcess.spawnSync(
process.execPath,
[path.join(__dirname, "patch-linux-window-ui.js"), "--report-json", report, "--enforce-critical", root],
{
encoding: "utf8",
env: { ...process.env, CODEX_LINUX_FEATURES_CONFIG: config },
},
);
assert.notEqual(result.status, 0);
assert.match(result.stderr, /explicitly enabled feature patches drifted/);
});
function patchFeatureFixture(t, moduleSource, manifest = {}) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codex-feature-descriptor-input-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const featuresRoot = path.join(root, "features");
const featureDir = path.join(featuresRoot, "probe");
const appDir = path.join(root, "app");
const mainPath = path.join(appDir, ".vite", "build", "early-bootstrap.js");
const config = path.join(featuresRoot, "features.json");
fs.mkdirSync(featureDir, { recursive: true });
fs.mkdirSync(path.dirname(mainPath), { recursive: true });
fs.writeFileSync(mainPath, "const main=true;\n");
fs.writeFileSync(config, JSON.stringify({
enabled: ["probe"], settings: { probe: { marker: "configured" } },
}));
fs.writeFileSync(path.join(featureDir, "README.md"), "# Probe\n");
fs.writeFileSync(path.join(featureDir, "feature.json"), JSON.stringify({
id: "probe", entrypoints: { patchDescriptors: "./patch.js" }, ...manifest,
}));
fs.writeFileSync(path.join(featureDir, "patch.js"), moduleSource);
return {
root, appDir, mainPath, config,
options: { featuresRoot, featuresConfigPath: config, corePatchRoot: path.join(root, "empty-core") },
};
}
test("malformed enabled descriptor inputs abort loading, patching, and build CLIs", async (t) => {
const invalid = [
["missing apply", "[{ id: 'broken' }]", /must export an apply function/],
["non-callable apply", "[{ id: 'broken', apply: true }]", /must export an apply function/],
["missing id", "[{ apply() {} }]", /must have id or name/],
["invalid id", "[{ id: 42, apply() {} }]", /must have id or name/],
["empty id", "[{ id: '', apply() {} }]", /must have id or name/],
["null descriptor", "[null]", /must be an object/],
["array descriptor", "[[]]", /must be an object/],
["scalar descriptor", "[7]", /must be an object/],
["null export", "null", /must export descriptors/],
["undefined export", "undefined", /must export descriptors/],
["scalar export", "'broken'", /must be an object/],
["function export", "() => []", /must be an object/],
["invalid descriptors export", "{ descriptors: [false] }", /must be an object/],
["later invalid item", "[{ id: 'valid', apply: s => s + '// changed' }, null]", /patch descriptor 2.*must be an object/],
];
for (const [name, exported, reason] of invalid) {
await t.test(name, (t) => {
const fixture = patchFeatureFixture(t, `module.exports = ${exported};\n`);
const before = fs.readFileSync(fixture.mainPath);
const diagnostic = (error) => {
assert.match(error.message, /Linux feature 'probe'/);
assert.match(error.message, /patch\.js/);
assert.match(error.message, reason);
return true;
};
assert.throws(() => featurePatchDescriptors(fixture.options), diagnostic);
assert.throws(() => patchExtractedApp(fixture.appDir, {
...fixture.options, report: createPatchReport(),
}), diagnostic);
const report = path.join(fixture.root, "report.json");
const env = {
...process.env,
CODEX_LINUX_FEATURES_ROOT: fixture.options.featuresRoot,
CODEX_LINUX_FEATURES_CONFIG: fixture.config,
};
for (const args of [
[path.join(__dirname, "lib/linux-features.js"), "--patch-descriptor-count"],
[path.join(__dirname, "patch-linux-window-ui.js"), "--report-json", report, "--enforce-critical", fixture.appDir],
]) {
const result = childProcess.spawnSync(process.execPath, args, { encoding: "utf8", env });
assert.notEqual(result.status, 0, result.stdout);
diagnostic(new Error(result.stderr));
}
assert.equal(fs.existsSync(report), false, "invalid inputs must not produce a successful empty report");
assert.deepEqual(fs.readFileSync(fixture.mainPath), before);
});
}
});
test("valid raw and constructor feature descriptors keep settings, attribution, and idempotence", (t) => {
const descriptorModule = path.join(__dirname, "patches/descriptor.js");
const fixture = patchFeatureFixture(t, `
const { mainBundlePatch } = require(${JSON.stringify(descriptorModule)});
module.exports.descriptors = [
{ name: 'raw', apply(source, { feature }) {
const marker = '// ' + feature.settings.marker;
return source.includes(marker) ? source : source + marker + '\\n';
} },
mainBundlePatch({ id: 'constructed', apply(source) {
return source.includes('// constructor') ? source : source + '// constructor\\n';
} }),
];
`);
for (const status of ["applied", "already-applied"]) {
const report = createPatchReport();
patchExtractedApp(fixture.appDir, { ...fixture.options, report });
assert.deepEqual(report.enabledFeatures, ["probe"]);
assert.deepEqual(report.patches.map(({ name, status, featureId, sourceKind }) => ({
name, status, featureId, sourceKind,
})), ["raw", "constructed"].map((id) => ({
name: `feature:probe:${id}`, status, featureId: "probe", sourceKind: "feature",
})));
assert.deepEqual(enabledFeatureFailuresFromReport(report), []);
}
assert.equal(fs.readFileSync(fixture.mainPath, "utf8"), "const main=true;\n// configured\n// constructor\n");
});
test("empty descriptors, resource-only features, and disabled modules remain valid", (t) => {
for (const [source, manifest, enabled] of [
["module.exports = [];", {}, true],
["module.exports = { descriptors: [] };", {}, true],
["throw Error('resource-only module must not load');", { entrypoints: {} }, true],
["throw Error('disabled module must not load');", {}, false],
]) {
const fixture = patchFeatureFixture(t, source, manifest);
if (!enabled) fs.writeFileSync(fixture.config, '{"enabled":[]}');
assert.deepEqual(featurePatchDescriptors(fixture.options), []);
const report = createPatchReport();
patchExtractedApp(fixture.appDir, { ...fixture.options, report });
assert.deepEqual(report.patches, []);
assert.equal(fs.readFileSync(fixture.mainPath, "utf8"), "const main=true;\n");
}
});