"use strict"; const assert = require("node:assert/strict"); const { execFileSync } = require("node:child_process"); const fs = require("node:fs"); const path = require("node:path"); const test = require("node:test"); const repoRoot = path.resolve(__dirname, "../.."); function read(relativePath) { return fs.readFileSync(path.join(repoRoot, relativePath), "utf8"); } function job(workflow, jobName) { const lines = workflow.split("\n"); const start = lines.findIndex((line) => line === ` ${jobName}:`); assert.notEqual(start, -1, `expected ${jobName} job`); const end = lines.findIndex( (line, index) => index > start && /^ [a-zA-Z0-9_-]+:$/.test(line), ); return lines.slice(start, end === -1 ? undefined : end).join("\n"); } // Every tracked test file, found by name rather than by the runner's glob. function trackedTestFiles() { return execFileSync("git", ["ls-files"], { cwd: repoRoot, encoding: "utf8" }) .split("\n") .filter((file) => /(^|\/)test\.js$|\.test\.js$/.test(file)) .sort(); } // The files scripts/ci/run-node-checks.sh runs. function guardedTestFiles() { const runner = read("scripts/ci/run-node-checks.sh"); const match = runner.match(/git ls-files ((?:'[^']*test\.js' ?)+)\)/); assert.ok(match, "run-node-checks.sh lists its test files with git ls-files"); const pathspecs = match[1].trim().split(/\s+/).map((spec) => spec.slice(1, -1)); return execFileSync("git", ["ls-files", ...pathspecs], { cwd: repoRoot, encoding: "utf8" }) .split("\n") .filter(Boolean) .sort(); } test("CI runs every Node test through the TMPDIR guard", () => { const sourceAndNode = job(read(".github/workflows/ci.yml"), "source-and-node"); assert.match(sourceAndNode, /^ run: bash scripts\/ci\/run-node-checks\.sh all$/m); assert.doesNotMatch(sourceAndNode, /node --test/); assert.deepEqual(guardedTestFiles(), trackedTestFiles()); }); test("CI runs the complete workflow regression suite", () => { const workflow = read(".github/workflows/ci.yml"); assert.ok(guardedTestFiles().includes("scripts/ci/github-actions-workflows.test.js")); const nixBuild = job(workflow, "nix-build"); assert.match(nixBuild, /system: x86_64-linux/); assert.match(nixBuild, /system: aarch64-linux/); assert.match(nixBuild, /runner: ubuntu-24\.04-arm/); assert.match(nixBuild, /checks\.\$\{\{ matrix\.system \}\}\.modules/); assert.match(nixBuild, /nix-runtime-chronicle-skysight/); assert.match(nixBuild, /nix-runtime-computer-use/); assert.match(nixBuild, /nix-runtime-maximal-directory-watch/); assert.match(nixBuild, /nix-runtime-maximal-shallow-watch/); assert.match(nixBuild, /nix-installer/); const rust = job(workflow, "rust"); assert.match(rust, /cargo test -p codex-record-replay-linux/); assert.match(rust, /cargo clippy -p codex-record-replay-linux --all-targets -- -D warnings/); const nixVm = job(workflow, "nix-vm"); assert.match(nixVm, /checks\.x86_64-linux\.nixos-vm/); const nixGate = job(workflow, "nix"); assert.match(nixGate, /if: \$\{\{ always\(\) \}\}/); assert.match(nixGate, /test "\$NIX_BUILD_RESULT" = success/); assert.match(nixGate, /test "\$NIX_VM_RESULT" = success/); }); test("normal CI and container smoke run local package verification policy regressions", () => { const suite = "scripts/lib/install-verification.test.js"; // CI runs it through the guarded runner (checked above), smoke directly. assert.ok(guardedTestFiles().includes(suite), `run-node-checks.sh must execute ${suite}`); const commands = read("tests/scripts_smoke.sh").replace(/\\\r?\n/g, " ").split("\n").map(line => line.trim()); assert.ok(commands.some(line => line.startsWith("node --test ") && line.split(/\s+/).includes(suite)), `the container smoke entrypoint must execute ${suite}`); const core = read("scripts/ci/container-entrypoint.sh").split("run_core() {", 2)[1].split("\n}", 1)[0]; assert.match(core, /bash tests\/scripts_smoke\.sh/); }); test("official Linux validation runs fully on every pull request but not hourly", () => { const workflow = read(".github/workflows/upstream-build-app.yml"); assert.doesNotMatch(workflow, /^ schedule:/m); assert.match(workflow, /^ pull_request:\s*\n push:/m); assert.match(workflow, /^ - \.github\/workflows\/upstream-build-app\.yml$/m); const signedBaseline = job(workflow, "signed-baseline"); assert.match(signedBaseline, /architecture: \[amd64, arm64\]/); assert.match(signedBaseline, /name: Build required default baseline/); assert.match(signedBaseline, /report\.enabledFeatures\.length !== 0/); assert.match(signedBaseline, /requiredPatchNamesForProfile\("upstream-build"\)/); assert.match(signedBaseline, /report\.patches\.length !== required\.size/); assert.match(signedBaseline, /!required\.has\(patch\.name\)/); assert.match(signedBaseline, /patch\.ciPolicy !== "required-upstream"/); assert.match(signedBaseline, /patch\.sourceKind !== "core"/); assert.match(signedBaseline, /patch\.status !== "applied"/); assert.match(signedBaseline, /preservedByteForByte !== \(required\.size === 0\)/); assert.match(signedBaseline, /report\.upstreamAppAsar\?\.sha256 !== upstreamSha256/); assert.match(signedBaseline, /report\.outputAppAsar\?\.sha256 !== outputSha256/); assert.match(signedBaseline, /required\.size === 0/); assert.match(signedBaseline, /unexpected required default patch report/); assert.doesNotMatch(signedBaseline, /quit-confirmation-focus/); assert.match(signedBaseline, /validate-patch-report\.js/); assert.match(signedBaseline, /--profile upstream-build/); assert.match(signedBaseline, /name: Test required core patches against the signed bundle/); assert.match( signedBaseline, /dpkg-deb --fsys-tarfile "\$package" \| \\\s+tar -xf - -C "\$asar_dir" --strip-components=4 \\\s+\.\/usr\/lib\/chatgpt\/ChatGPT \\\s+\.\/usr\/lib\/chatgpt\/resources\/app\.asar \\\s+\.\/usr\/lib\/chatgpt\/resources\/app\.asar\.unpacked/, ); assert.match(signedBaseline, /asar="\$asar_dir\/resources\/app\.asar"/); assert.match(signedBaseline, /test -x "\$asar_dir\/ChatGPT"/); assert.match(signedBaseline, /test -s "\$asar"/); assert.match(signedBaseline, /test -d "\$asar\.unpacked"/); assert.match(signedBaseline, /echo "asar=\$asar" >> "\$GITHUB_OUTPUT"/); assert.match(signedBaseline, /echo "app_dir=\$asar_dir" >> "\$GITHUB_OUTPUT"/); assert.match(signedBaseline, /echo "executable=\$asar_dir\/ChatGPT" >> "\$GITHUB_OUTPUT"/); assert.doesNotMatch(signedBaseline, /dpkg-deb -x/); assert.equal( (signedBaseline.match(/trap 'rm -rf "\$(?:extracted_asar|fix_root|runtime_metadata)"' EXIT/g) || []).length, 6, ); assert.match(signedBaseline, /CODEX_SIGNED_EXTRACTED_APP="\$extracted_asar"/); assert.match(signedBaseline, /corePatchDescriptors\(\)/); assert.match(signedBaseline, /ciPolicy \}\) => ciPolicy === "required-upstream"/); assert.match(signedBaseline, /path\.join\(path\.dirname\(sourcePath\), "test\.js"\)/); assert.match(signedBaseline, /if \(tests\.length > 0\)/); assert.doesNotMatch(signedBaseline, /tests\.length === 0 \|\|/); assert.doesNotMatch( signedBaseline, /scripts\/patches\/core\/[^\s"']+\/test\.js/, ); assert.match(signedBaseline, /name: Require signed renderer dependency regression/); assert.doesNotMatch( signedBaseline, /name: Require signed renderer dependency regression\n\s+if:/, ); assert.match(signedBaseline, /EXPECTED_SIGNED_VERSION: \$\{\{ inputs\.version \}\}/); assert.match( signedBaseline, /EXPECTED_SIGNED_REPOSITORY_PATH: \$\{\{ matrix\.architecture == 'amd64' && inputs\.amd64_repository_path \|\| inputs\.arm64_repository_path \}\}/, ); assert.match( signedBaseline, /EXPECTED_SIGNED_SHA256: \$\{\{ matrix\.architecture == 'amd64' && inputs\.amd64_sha256 \|\| inputs\.arm64_sha256 \}\}/, ); assert.match(signedBaseline, /process\.env\.GITHUB_EVENT_NAME === "workflow_dispatch"/); assert.match(signedBaseline, /signed renderer regression is not bound to the expected campaign/); assert.match( signedBaseline, /"\$\{\{ steps\.upstream\.outputs\.asar \}\}"/, ); assert.match(signedBaseline, /for \(const directory of \["", "async"\]\)/); assert.match(signedBaseline, /expected rspack async webview chunks in webview\/assets\/async/); assert.match(signedBaseline, /statically imports missing/); assert.match(signedBaseline, /the import scan is stale/); assert.match(signedBaseline, /webview chunks restored a static import cycle/); assert.doesNotMatch(signedBaseline, /authed-route|app-primary/); assert.doesNotMatch(signedBaseline, /\b\d{2}\.\d{3}\.\d{5}\b/); assert.doesNotMatch( signedBaseline, /pool\/main\/c\/chatgpt\/chatgpt_[^\s"']+\.deb/, ); assert.doesNotMatch(signedBaseline, /\b[0-9a-f]{64}\b/); assert.match( signedBaseline, /verify-official-linux-features\.js \\\s+"\$extracted_asar" \\\s+"\$\{\{ steps\.upstream\.outputs\.app_dir \}\}"/, ); assert.match( signedBaseline, /name: Validate the Nix ELF contract against the official payload[\s\S]*?env:\n CODEX_INSTALL_DIR:.*matrix\.architecture[\s\S]*?nix\/elf-runtime\.cjs fix[\s\S]*?nix\/elf-runtime\.cjs audit/, ); assert.match( signedBaseline, /nix\/elf-runtime\.cjs validate-upstream[\s\S]*?--root "\$CODEX_INSTALL_DIR"[\s\S]*?--arch "\$\{\{ matrix\.architecture \}\}"/, ); assert.match( signedBaseline, /matrix:\n architecture: \[amd64, arm64\][\s\S]*?name: Qualify Watchbound against the signed runtime executable\n run:/, ); assert.match( signedBaseline, /official_asar="\$\(realpath -- "\$\{\{ steps\.upstream\.outputs\.asar \}\}"\)"[\s\S]*?@electron\/asar extract-file \\\s+"\$official_asar" \\\s+package\.json[\s\S]*?test -s "\$runtime_metadata\/package\.json"[\s\S]*?"\$runtime_metadata" \\\s+"\$\{\{ steps\.upstream\.outputs\.executable \}\}" \\\s+"\$\{\{ matrix\.architecture \}\}"/, ); assert.match(signedBaseline, /validateArtifactManifest\(manifest\)/); assert.match( signedBaseline, /validateTargetRuntime\(\s+process\.argv\[2\],[\s\S]*?process\.argv\[3\],\s+\)/, ); assert.match( signedBaseline, /runtime\.qualification !== "signed-runtime-executable"/, ); const packageMatrix = job(workflow, "package-matrix"); assert.match(packageMatrix, /architecture: amd64/); assert.match(packageMatrix, /architecture: arm64/); assert.match(packageMatrix, /\.\/scripts\/build-deb\.sh/); assert.match(packageMatrix, /\.\/scripts\/build-rpm\.sh/); assert.match(packageMatrix, /\.\/scripts\/build-pacman\.sh/); assert.match(packageMatrix, /\.\/scripts\/build-appimage\.sh/); const dockFeatureAlone = job(workflow, "dock-icon-feature-alone"); assert.match( dockFeatureAlone, /ref: \$\{\{ github\.event_name == 'pull_request' && github\.event\.pull_request\.head\.sha \|\| github\.sha \}\}/, ); assert.match(dockFeatureAlone, /"enabled": \["ui-tweaks"\]/); assert.match(dockFeatureAlone, /"dockIcon": \{ "enabled": true \}/); assert.match(dockFeatureAlone, /EXPECTED_RELEASE_ID: \$\{\{ inputs\.release_id \}\}/); assert.match(dockFeatureAlone, /EXPECTED_VERSION: \$\{\{ inputs\.version \}\}/); assert.match( dockFeatureAlone, /EXPECTED_REPOSITORY_PATH: \$\{\{ inputs\.amd64_repository_path \}\}/, ); assert.match(dockFeatureAlone, /EXPECTED_SHA256: \$\{\{ inputs\.amd64_sha256 \}\}/); assert.match(dockFeatureAlone, /process\.env\.GITHUB_EVENT_NAME === "workflow_dispatch"/); assert.match(dockFeatureAlone, /metadata\.version !== process\.env\.EXPECTED_VERSION/); assert.match( dockFeatureAlone, /metadata\.repositoryPath !== process\.env\.EXPECTED_REPOSITORY_PATH/, ); assert.match(dockFeatureAlone, /metadata\.sha256 !== process\.env\.EXPECTED_SHA256/); assert.match(dockFeatureAlone, /Dock icon package does not match the dispatched campaign/); assert.match( dockFeatureAlone, /--require-applied feature:ui-tweaks:appearance-dock-icon-main-process/, ); assert.match( dockFeatureAlone, /--require-applied feature:ui-tweaks:appearance-dock-icon-settings-row/, ); assert.match(dockFeatureAlone, /Expected Dock descriptors 2\/2 applied/); assert.match(dockFeatureAlone, /scripts\/lib\/upstream-linux-package\.js/); assert.match( dockFeatureAlone, /--key-base64 assets\/openai-codex-linux-repository-key\.gpg\.base64/, ); assert.match(dockFeatureAlone, /\.\/install\.sh "\$package"/); assert.match( dockFeatureAlone, /find "\$payload" -mindepth 1 -maxdepth 1 -printf '%f\\n'/, ); assert.doesNotMatch(dockFeatureAlone, /find "\$payload"[^\n]+-type f/); assert.match(dockFeatureAlone, /test ! -L "\$payload\/\$resource"/); for (const sourceComparison of [ /cmp "\$upstream_root\/usr\/lib\/chatgpt\/resources\/icon-chatgpt\.png" "\$payload\/icon-chatgpt\.png"/, /cmp assets\/codex-linux\.png "\$payload\/icon-codex-dark-color\.png"/, /cmp assets\/codex-linux\.png "\$payload\/icon-codex-light\.png"/, /cmp linux-features\/ui-tweaks\/sync-desktop-icon\.sh "\$payload\/sync-desktop-icon\.sh"/, /cmp linux-features\/ui-tweaks\/sync-desktop-icon\.sh "\$hook"/, ]) { assert.match(dockFeatureAlone, sourceComparison); } assert.match(dockFeatureAlone, /test -x "\$payload\/sync-desktop-icon\.sh"/); assert.match(dockFeatureAlone, /ui-tweaks-dock-icon-cleanup\.sh/); assert.match(dockFeatureAlone, /test -x "\$hook"/); }); test("install-deps workflow covers apt and pacman Rust bootstrap", () => { const workflow = read(".github/workflows/install-deps.yml"); assert.match(workflow, /^ - scripts\/lib\/install-deps-rust\.sh$/m); assert.match(workflow, /^ - tests\/install_deps_pacman_rust_matrix\.sh$/m); const apt = job(workflow, "apt-node-bootstrap"); for (const image of [ "docker.io/library/ubuntu:22.04", "docker.io/library/ubuntu:24.04", "docker.io/library/debian:12", ]) { assert.match(apt, new RegExp(image.replaceAll(".", "\\."))); } const pacman = job(workflow, "pacman-rust-bootstrap"); assert.match(pacman, /docker\.io\/library\/archlinux:latest/); assert.match(pacman, /CODEX_RUN_ARCH_INSTALL_DEPS_MATRIX: "1"/); assert.match(pacman, /bash tests\/install_deps_pacman_rust_matrix\.sh/); for (const rustState of [ "working-distro-cargo", "rustup-without-toolchain", "neither-rust-nor-rustup", "shadowed-user-local-proxy", ]) { assert.match(pacman, new RegExp(`^ - ${rustState}$`, "m")); } }); test("Community profile isolation is validated alone against the signed package", () => { const workflow = read(".github/workflows/upstream-build-app.yml"); const feature = job(workflow, "community-profile-isolation-feature-alone"); assert.match( feature, /ref: \$\{\{ github\.event_name == 'pull_request' && github\.event\.pull_request\.head\.sha \|\| github\.sha \}\}/, ); assert.match(feature, /"enabled": \["community-profile-isolation"\]/); assert.match(feature, /scripts\/lib\/upstream-linux-package\.js/); assert.match(feature, /\.\/install\.sh "\$package"/); assert.match( feature, /node --test linux-features\/community-profile-isolation\/test\.js/, ); assert.match(feature, /CODEX_ELECTRON_USER_DATA_PATH/); const signedBaseline = job(workflow, "signed-baseline"); assert.match( signedBaseline, /Test profile isolation, Computer Use and account menu against the signed bundle/, ); assert.match( signedBaseline, /CODEX_SIGNED_EXTRACTED_APP="\$extracted_asar"/, ); assert.match( signedBaseline, /node --test linux-features\/community-profile-isolation\/test\.js/, ); const logicalBaseline = signedBaseline.replace(/\\\r?\n[ \t]*/g, " "); const signedCommands = logicalBaseline.match( /CODEX_SIGNED_EXTRACTED_APP="\$extracted_asar"[ \t]+node --test[ \t]+[^\r\n]+/g, ) ?? []; const computerUseCommand = signedCommands.find((command) => command.includes("linux-features/computer-use-linux/unified.test.js")); assert.ok(computerUseCommand, "Computer Use tests must use the signed extracted app"); const testPaths = computerUseCommand.split("node --test")[1].trim().split(/\s+/); assert.ok(testPaths.every((value) => /^linux-features\/[\w./-]+\.js$/.test(value))); for (const file of [ "linux-features/computer-use-linux/unified.test.js", "linux-features/computer-use-linux/resume.test.js", "linux-features/computer-use-linux/current-signed.test.js", "linux-features/computer-use-linux/native-applications.test.js", "linux-features/account-switcher/test.js", ]) { assert.ok(testPaths.includes(file), `${file} must run in the signed baseline`); } }); test("official Linux metadata expires after seven days", () => { const workflow = read(".github/workflows/upstream-build-app.yml"); const signedBaseline = job(workflow, "signed-baseline"); assert.match(signedBaseline, /name: official-linux-\$\{\{ matrix\.architecture \}\}-metadata/); assert.match(signedBaseline, /retention-days: 7/); }); test("official Linux gate fails closed unless every dependency succeeds", () => { const workflow = read(".github/workflows/upstream-build-app.yml"); const gate = job(workflow, "official-linux-gate"); assert.match( gate, /^ official-linux-gate:\n if: \$\{\{ always\(\) \}\}\n needs:\n - signed-baseline\n - package-matrix\n - community-profile-isolation-feature-alone\n - dock-icon-feature-alone\n - watchdog\n runs-on:/, ); for (const [dependency, resultVariable] of [ ["signed-baseline", "SIGNED_BASELINE_RESULT"], ["package-matrix", "PACKAGE_MATRIX_RESULT"], ["community-profile-isolation-feature-alone", "COMMUNITY_PROFILE_ISOLATION_FEATURE_ALONE_RESULT"], ["dock-icon-feature-alone", "DOCK_ICON_FEATURE_ALONE_RESULT"], ["watchdog", "WATCHDOG_RESULT"], ]) { assert.match( gate, new RegExp( `^ ${resultVariable}: \\$\\{\\{ needs\\.${dependency}\\.result \\}\\}$`, "m", ), ); assert.match( gate, new RegExp(`^ test "\\$${resultVariable}" = success$`, "m"), ); } }); test("Nix pin refresh is watchdog-dispatched and campaign-bound", () => { const workflow = read(".github/workflows/update-official-linux-pins.yml"); assert.doesNotMatch(workflow, /^ schedule:/m); for (const input of [ "release_id", "expected_main_sha", "version", "amd64_repository_path", "amd64_sha256", "arm64_repository_path", "arm64_sha256", ]) { assert.match(workflow, new RegExp(`^ ${input}:$`, "m")); } assert.match(workflow, /^ actions: write$/m); assert.match(workflow, /Require the accepted commit to be current main/); assert.match(workflow, /persist-credentials: false/); assert.match(workflow, /gh api "repos\/\$GITHUB_REPOSITORY\/commits\/main" --jq \.sha/); assert.match(workflow, /ref: \$\{\{ inputs\.expected_main_sha \}\}/); assert.match(workflow, /codex\/official-linux-pins-\$\{RELEASE_ID:0:12\}/); assert.match(workflow, /dispatch_if_missing ci\.yml/); assert.match(workflow, /dispatch_if_missing upstream-build-app\.yml/); assert.match(workflow, /Official-Linux-Release-ID:/); assert.match(workflow, /nix build \.#checks\.x86_64-linux\.nix-runtime --no-link/); assert.match(workflow, /--force-with-lease=refs\/heads\/\$branch:\$remote_head/); assert.match(workflow, /git rev-parse 'FETCH_HEAD\^\{tree\}'/); assert.match(workflow, /--head "\$GITHUB_REPOSITORY_OWNER:\$branch"/); }); test("manual official Linux validation accepts an exact campaign", () => { const workflow = read(".github/workflows/upstream-build-app.yml"); assert.match(workflow, /run-name:.*Official Linux campaign.*inputs\.release_id/); for (const input of ["release_id", "version", "amd64_sha256", "arm64_sha256"]) { assert.match(workflow, new RegExp(`^ ${input}:$`, "m")); } assert.equal((workflow.match(/^ required: true$/gm) || []).length, 6); assert.match(workflow, /Require the dispatched signed campaign/); assert.match(workflow, /resolved signed package does not match the dispatched campaign/); assert.match(workflow, /Resolve and bind the two-architecture campaign/); assert.match(workflow, /actual\.releaseId !== expected\.EXPECTED_RELEASE_ID/); const packageMatrix = job(workflow, "package-matrix"); assert.match(packageMatrix, /Resolve exact package matrix input/); assert.match(packageMatrix, /package matrix input does not match the dispatched campaign/); assert.match(packageMatrix, /\.\/install\.sh "\$\{\{ steps\.upstream\.outputs\.package \}\}"/); });