Repository navigation
Expand file tree
/
Copy pathcurrent-signed.test.js
More file actions
109 lines (102 loc) · 4.51 KB
/
Copy pathcurrent-signed.test.js
File metadata and controls
109 lines (102 loc) · 4.51 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
"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const vm = require("node:vm");
const descriptors = require("./patch.js");
const { findMainProcessChunks } = require("../../scripts/patches/lib/assets.js");
function signedChunks() {
const mainProcess = findMainProcessChunks(process.env.CODEX_SIGNED_EXTRACTED_APP);
assert(mainProcess, "Expected the verified main-process chunks");
return mainProcess.chunks.map(name => {
const file = path.join(mainProcess.buildDir, name);
return { name, file, source: fs.readFileSync(file, "utf8") };
});
}
function quietly(fn) {
const warn = console.warn;
console.warn = () => {};
try {
return fn();
} finally {
console.warn = warn;
}
}
test("native metadata stays feature-owned without retired core IPC helpers", () => {
const source = fs.readFileSync(path.join(__dirname, "../../scripts/patches/impl/computer-use.js"), "utf8");
assert.doesNotMatch(source, /linuxNativeDesktopAppsHelper|applyLinuxNativeDesktopAppsHandlerPatch/);
const descriptor = descriptors.find(({ id }) => id === "native-applications");
assert(descriptor);
assert.equal(descriptor.phase, "main-bundle");
});
test("current signed native metadata factory patches without changing the trusted input", {
skip: !process.env.CODEX_SIGNED_EXTRACTED_APP,
}, () => {
// Locate the chunk holding the fetch handler semantically, not by chunk id.
const owners = signedChunks().filter(({ source }) =>
source.includes("nativeApplications:this.desktopServices.nativeApplications"));
assert.equal(owners.length, 1, "Expected one chunk exposing native Applications metadata");
const [{ file, source: actual }] = owners;
const patch = descriptors.find(({ id }) => id === "native-applications").apply;
const patched = patch(actual);
assert.notEqual(patched, actual);
assert.equal(patch(patched), patched);
assert.doesNotThrow(() => new vm.Script(patched));
assert.equal(fs.readFileSync(file, "utf8"), actual);
});
test("every current signed main-process descriptor patches exactly one chunk idempotently", {
skip: !process.env.CODEX_SIGNED_EXTRACTED_APP,
}, () => {
const chunks = signedChunks();
for (const descriptor of descriptors.filter(({ phase }) => phase === "main-bundle")) {
const changed = [];
for (const chunk of chunks) {
let patched;
try {
patched = quietly(() => descriptor.apply(chunk.source));
} catch {
continue;
}
if (patched === chunk.source) continue;
changed.push(chunk.name);
assert.equal(quietly(() => descriptor.apply(patched)), patched, `${descriptor.id} is not idempotent`);
assert.doesNotThrow(() => new vm.Script(patched), `${descriptor.id} produced invalid JavaScript`);
}
assert.equal(changed.length, 1, `${descriptor.id} changed ${changed.join(", ") || "no chunk"}`);
}
});
test("every current signed webview descriptor patches its rspack owners idempotently", {
skip: !process.env.CODEX_SIGNED_EXTRACTED_APP,
}, () => {
const { findWebviewAssetsContaining } = require("../../scripts/patches/lib/assets.js");
const { patchBreaksJavaScriptSyntax } = require("../../scripts/patches/lib/javascript-syntax.js");
const root = process.env.CODEX_SIGNED_EXTRACTED_APP;
const expectedOwners = {
"ui-availability": 1,
"host-platform": 1,
// The Plugins presentation module is duplicated into several lazy chunks.
"native-settings-visibility": null,
};
for (const descriptor of descriptors.filter(({ phase }) => phase === "webview-asset")) {
const owners = findWebviewAssetsContaining(root, descriptor.assetNeedles)
.map(assetName => ({
assetName,
source: fs.readFileSync(path.join(root, "webview", "assets", assetName), "utf8"),
}))
.filter(({ source }) => descriptor.assetMatch?.(source) ?? true);
const expected = expectedOwners[descriptor.id];
if (expected == null) assert.ok(owners.length > 1, descriptor.id);
else assert.equal(owners.length, expected, descriptor.id);
for (const { assetName, source } of owners) {
const patched = quietly(() => descriptor.apply(source));
assert.notEqual(patched, source, `${descriptor.id} did not change ${assetName}`);
assert.equal(descriptor.apply(patched), patched, `${descriptor.id} is not idempotent`);
assert.equal(
patchBreaksJavaScriptSyntax(source, patched),
false,
`${descriptor.id} produced invalid JavaScript in ${assetName}`,
);
}
}
});