Files
OmniRoute/tests/unit/pack-artifact-policy.test.ts
backryun c6c134300b perf(electron): ship optional ML/browser deps as installable packs (#10382)
Stage 7 of issue #10321 moves the optional ML and browser automation dependency closures out of the desktop bundle into checksummed, versioned packs installed on demand through the omniroute packs command.

- scripts/build/optionalPackStaging.mjs stages pack members under .build/optional-packs, creates release tarballs, and emits optional-packs.index.json with per-member SHA-256 checksums.
- scripts/packs provides manifest, install, remove, and verification helpers plus the packs CLI commands.
- Runtime lookup includes installed pack node_modules directories, while LLMLingua and browser executors continue to degrade gracefully when packs are absent.

The measured darwin-arm64 staging closure was about 534 MB of the 929 MB standalone node_modules tree (57%).
2026-08-16 02:20:59 -03:00

230 lines
8.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import {
APP_STAGING_ALLOWED_EXACT_PATHS,
APP_STAGING_ALLOWED_PATH_PREFIXES,
PACK_ARTIFACT_ALLOWED_EXACT_PATHS,
PACK_ARTIFACT_ALLOWED_PATH_PREFIXES,
PACK_ARTIFACT_REQUIRED_PATHS,
findMissingArtifactPaths,
findUnexpectedArtifactPaths,
normalizeArtifactPath,
parseJsonArrayOutput,
parseJsonValuesOutput,
} from "../../scripts/build/pack-artifact-policy.ts";
test("normalizeArtifactPath normalizes slashes and leading relative markers", () => {
assert.equal(
normalizeArtifactPath("./app\\scripts\\ad-hoc\\test.js"),
"app/scripts/ad-hoc/test.js"
);
});
test("parseJsonArrayOutput extracts the first valid array from mixed command output", () => {
const output = [
"notice [not-json]",
'[{"path":"src/[literal].ts","files":[["nested"]]}]',
"notice [second-array]",
].join("\n");
assert.deepEqual(parseJsonArrayOutput(output), [
{ path: "src/[literal].ts", files: [["nested"]] },
]);
});
test("parseJsonArrayOutput can skip valid arrays that are not the target payload", () => {
const output = `[]
[{"filename":"omniroute.tgz","files":[{"path":"src/index.ts"}]}]`;
assert.deepEqual(
parseJsonArrayOutput(output, (candidate) =>
candidate.some(
(entry) =>
typeof entry === "object" &&
entry !== null &&
Array.isArray((entry as { files?: unknown }).files)
)
),
[{ filename: "omniroute.tgz", files: [{ path: "src/index.ts" }] }]
);
});
test("parseJsonValuesOutput extracts object reports as well as arrays", () => {
assert.deepEqual(parseJsonValuesOutput('notice\n{"files":[{"path":"src/index.ts"}]}'), [
{ files: [{ path: "src/index.ts" }] },
]);
});
test("findUnexpectedArtifactPaths flags staged app files outside the allowlist", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(
[
"open-sse/services/compression/engines/rtk/filters/generic-output.json",
"open-sse/services/compression/rules/en/filler.json",
"package-lock.json",
"scripts/dev/sync-env.mjs",
"server.js",
],
{
exactPaths: APP_STAGING_ALLOWED_EXACT_PATHS,
prefixPaths: APP_STAGING_ALLOWED_PATH_PREFIXES,
}
);
assert.deepEqual(unexpectedPaths, ["package-lock.json"]);
});
test("findUnexpectedArtifactPaths flags app pack files outside the allowlist", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(
[
"dist/open-sse/services/compression/engines/rtk/filters/generic-output.json",
"dist/open-sse/services/compression/rules/en/filler.json",
"dist/server.js",
"dist/scripts/dev/sync-env.mjs",
"dist/scripts/build/prepublish.mjs",
"docs/extra.md",
],
{
exactPaths: PACK_ARTIFACT_ALLOWED_EXACT_PATHS,
prefixPaths: PACK_ARTIFACT_ALLOWED_PATH_PREFIXES,
}
);
assert.deepEqual(unexpectedPaths, ["dist/scripts/build/prepublish.mjs", "docs/extra.md"]);
});
test("findUnexpectedArtifactPaths flags node_modules even inside an allowed prefix", () => {
// Regression guard: the allowlist grants the whole `@omniroute/opencode-provider/`
// prefix, which used to authorize a nested node_modules inside it — 79 MB of
// devDependencies (80% of the tarball) whenever the publish ran from a machine
// that had installed inside that subpackage. package.json `files[]` excludes it
// at the source; this asserts the gate FAILS instead of allowing a regression.
const unexpectedPaths = findUnexpectedArtifactPaths(
[
"@omniroute/opencode-provider/node_modules/tsup/package.json",
"@omniroute/opencode-provider/node_modules/esbuild/lib/main.js",
"@omniroute/opencode-provider/dist/index.js",
"@omniroute/opencode-provider/package.json",
],
{
exactPaths: [],
prefixPaths: ["@omniroute/opencode-provider/"],
}
);
assert.deepEqual(unexpectedPaths, [
"@omniroute/opencode-provider/node_modules/esbuild/lib/main.js",
"@omniroute/opencode-provider/node_modules/tsup/package.json",
]);
});
test("package.json files[] excludes nested node_modules from the published package", () => {
// The gate above is defence-in-depth; this pins the actual fix. Without the
// "!**/node_modules/**" negation the tarball was 99.4 MB unpacked (31.3 MB
// packed) instead of 20.0 MB (5.3 MB).
const files: string[] = JSON.parse(
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
).files;
assert.ok(
files.includes("!**/node_modules/**"),
'package.json "files" must keep the "!**/node_modules/**" negation — without it, ' +
"a nested install inside @omniroute/* ships ~79 MB of devDependencies."
);
});
test("build-next-isolated sibling imports are allowed in the published package", () => {
const buildDependencies = [
"scripts/build/assembleStandalone.mjs",
"scripts/build/backendOnlyPages.mjs",
"scripts/build/build-tproxy-native.mjs",
];
const unexpectedPaths = findUnexpectedArtifactPaths(buildDependencies, {
exactPaths: PACK_ARTIFACT_ALLOWED_EXACT_PATHS,
prefixPaths: PACK_ARTIFACT_ALLOWED_PATH_PREFIXES,
});
assert.deepEqual(unexpectedPaths, []);
});
test("webdav-handler.mjs is allowed in staging dist/ (server-ws.mjs dependency, missed in 3.8.22 build)", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(["webdav-handler.mjs"], {
exactPaths: APP_STAGING_ALLOWED_EXACT_PATHS,
prefixPaths: APP_STAGING_ALLOWED_PATH_PREFIXES,
});
assert.deepEqual(unexpectedPaths, []);
});
test("tls-options.mjs is allowed in staging dist/ (server-ws.mjs dependency, missed in 3.8.41 build — #5452)", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(["tls-options.mjs"], {
exactPaths: APP_STAGING_ALLOWED_EXACT_PATHS,
prefixPaths: APP_STAGING_ALLOWED_PATH_PREFIXES,
});
assert.deepEqual(unexpectedPaths, []);
});
test("dist/tls-options.mjs is a required tarball path (regression guard for #5452)", () => {
const missingPaths = findMissingArtifactPaths([], PACK_ARTIFACT_REQUIRED_PATHS);
assert.ok(
missingPaths.includes("dist/tls-options.mjs"),
"dist/tls-options.mjs must be enforced by the pack-artifact gate"
);
});
test("setupPolyfill.ts is allowed in the tarball (bin/omniroute.mjs imports it at startup)", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(["open-sse/utils/setupPolyfill.ts"], {
exactPaths: PACK_ARTIFACT_ALLOWED_EXACT_PATHS,
prefixPaths: PACK_ARTIFACT_ALLOWED_PATH_PREFIXES,
});
assert.deepEqual(unexpectedPaths, []);
});
test("findMissingArtifactPaths flags missing root runtime files in the tarball", () => {
const missingPaths = findMissingArtifactPaths(
[
"dist/server.js",
"bin/omniroute.mjs",
"package.json",
"scripts/build/postinstall.mjs",
"scripts/build/postinstallSupport.mjs",
],
PACK_ARTIFACT_REQUIRED_PATHS
);
// findMissingArtifactPaths returns the missing required paths sorted
// alphabetically (bin/ < dist/ < scripts/ < src/), minus the paths present
// above (dist/server.js, bin/omniroute.mjs, package.json, the postinstall scripts).
assert.deepEqual(missingPaths, [
"bin/aliasResolver.mjs",
"bin/aliasResolverHook.mjs",
"bin/cli/data-dir.mjs",
"bin/cli/program.mjs",
"bin/cli/utils/ensureAndroidCacheDir.mjs",
"bin/cli/utils/parseEnvValue.mjs",
"bin/cli/utils/storageKeyProvision.mjs",
"bin/cli/utils/versionFastPath.mjs",
"bin/mcp-server.mjs",
"bin/mcpStdioConsoleGuard.mjs",
"bin/nodeRuntimeSupport.mjs",
"dist/head-response-guard.cjs",
"dist/http-method-guard.cjs",
"dist/main-server-timeouts.mjs",
"dist/open-sse/services/compression/engines/rtk/filters/generic-output.json",
"dist/open-sse/services/compression/rules/en/filler.json",
"dist/open-sse/vendor/codex-chatgpt-web/adapters/chatgpt-web/mcp-server.js",
"dist/peer-stamp.mjs",
"dist/responses-ws-proxy.mjs",
"dist/server-ws.mjs",
"dist/tls-options.mjs",
"dist/webdav-handler.mjs",
"scripts/build/colocateOptionals.mjs",
"scripts/build/fixTlsClientNodeBinary.mjs",
"scripts/build/native-binary-compat.mjs",
"scripts/build/runtime-env.mjs",
"scripts/packs/optionalPackInstaller.mjs",
"scripts/packs/optionalPackManifest.mjs",
"src/shared/utils/nodeRuntimeSupport.ts",
]);
});