Files
nexu-io__open-design/tools/pack/tests/mac-prebundle.test.ts
open-design-crew[bot] 4d0376e437 fix(byok): restore local runtime configuration (#6356)
* fix(byok): restore local runtime configuration

Return BYOK settings to the browser-local configuration path and pass credentials only to the active OpenCode child run.

Remove the unpublished secure-profile migration, credential backends, API, CLI, contracts, and MCP exposure while retaining raw-secret rejection on external MCP calls.

Validated with focused web and daemon suites, full web tests, workspace typecheck, guard, and diff checks.

* test(e2e): align BYOK settings checks with local storage

Update the settings UI contract after rolling BYOK credentials back to local app config storage. Assert the entered key is persisted, keep incomplete BYOK drafts from activating, and seed the Local CLI isolation case with a usable BYOK key.

Validated with all seven P0 cases in settings-api-protocol.test.ts and the e2e TypeScript check.

* fix(daemon): validate plugin request ids explicitly

Use a field-specific canonical UUID/ULID validator for attributed request IDs and document that format in the MCP schema and server instructions. Invalid calls now identify requestId instead of incorrectly blaming pluginWorkflowId, while Codex can generate a valid ID on its first attempt.

Validated with a red/green MCP observability regression test, daemon typecheck, repository guard, and a real Local Codex plugin smoke that completed in one start_run attempt.

* test(pack): follow active PTY consumer in prebundle coverage

Point the macOS prebundle boundary test at the Terminal runtime after the retired BYOK credential service was removed. This keeps native node-pty externalization covered without restoring legacy credential code.

Validated with the focused mac-prebundle suite, the complete tools-pack suite, tools-pack typechecks, repository guard, and deleted-file reference scans.

---------

Co-authored-by: Cheems <94773058+itscheems@users.noreply.github.com>
2026-08-03 09:40:31 +00:00

270 lines
9.0 KiB
TypeScript

import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { build } from "esbuild";
import { describe, expect, it } from "vitest";
import {
MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER,
MAC_PREBUNDLE_COPIED_RUNTIME_DEPENDENCIES,
MAC_PREBUNDLE_ESBUILD_TARGET,
MAC_PREBUNDLE_POLICIES,
MAC_PREBUNDLE_RUNTIME_DEPENDENCIES,
MAC_PREBUNDLED_DAEMON_CLI_RELATIVE_PATH,
MAC_PREBUNDLED_DAEMON_SIDECAR_RELATIVE_PATH,
MAC_PREBUNDLED_WEB_SIDECAR_RELATIVE_PATH,
assertMacPrebundleMetafile,
findForbiddenMacPrebundleInputs,
renderMacPackagedMainEntry,
shouldInstallInternalPackageForMacPrebundle,
shouldUseMacStandalonePrebundle,
} from "../src/mac-prebundle.js";
describe("mac standalone prebundle policy", () => {
it("is enabled only for standalone web output", () => {
expect(shouldUseMacStandalonePrebundle("standalone")).toBe(true);
expect(shouldUseMacStandalonePrebundle("server")).toBe(false);
});
it("keeps server-mode package topology unchanged", () => {
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/web",
webOutputMode: "server",
}),
).toBe(true);
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/packaged",
webOutputMode: "server",
}),
).toBe(true);
});
it("excludes internal packages replaced by mac standalone prebundles", () => {
for (const packageName of [
"@open-design/daemon",
"@open-design/desktop",
"@open-design/packaged",
"@open-design/sidecar",
"@open-design/sidecar-proto",
"@open-design/web",
]) {
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName,
webOutputMode: "standalone",
}),
).toBe(false);
}
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/contracts",
webOutputMode: "standalone",
}),
).toBe(true);
expect(
shouldInstallInternalPackageForMacPrebundle({
packageName: "@open-design/platform",
webOutputMode: "standalone",
}),
).toBe(true);
});
it("documents the explicit code-level bundle boundaries", () => {
expect(MAC_PREBUNDLE_ESBUILD_TARGET).toBe("node24");
expect(MAC_PREBUNDLE_POLICIES.packagedMain.externals).toEqual(["electron"]);
expect(MAC_PREBUNDLE_POLICIES.daemonCli.externals).toEqual([
"better-sqlite3",
"blake3-wasm",
"fsevents",
"node-pty",
]);
expect(MAC_PREBUNDLE_POLICIES.daemonSidecar.externals).toEqual([
"better-sqlite3",
"blake3-wasm",
"fsevents",
"node-pty",
]);
expect(MAC_PREBUNDLE_POLICIES.webSidecar.externals).toEqual([]);
expect(MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER).toContain("createRequire");
// Must match apps/daemon/package.json / the pnpm lockfile, or
// electron-builder's collector drops the module from the shipped app and
// the daemon dies at boot with ERR_MODULE_NOT_FOUND (issue #4638).
expect(MAC_PREBUNDLE_RUNTIME_DEPENDENCIES).toEqual({
"better-sqlite3": "12.10.0",
"blake3-wasm": "2.1.5",
"node-pty": "1.1.0",
});
expect(MAC_PREBUNDLE_COPIED_RUNTIME_DEPENDENCIES).toEqual({ "fsevents": "2.3.3" });
expect(MAC_PREBUNDLED_DAEMON_CLI_RELATIVE_PATH).toBe("app/prebundled/daemon/daemon-cli.mjs");
expect(MAC_PREBUNDLED_DAEMON_SIDECAR_RELATIVE_PATH).toBe("app/prebundled/daemon/daemon-sidecar.mjs");
expect(MAC_PREBUNDLED_WEB_SIDECAR_RELATIVE_PATH).toBe("app/prebundled/web-sidecar.mjs");
});
it.skipIf(process.platform !== "darwin")(
"keeps chokidar's native fsevents binding outside daemon bundles",
async () => {
const workspaceRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
const result = await build({
banner: { js: MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER },
bundle: true,
external: [...MAC_PREBUNDLE_POLICIES.daemonSidecar.externals],
format: "esm",
logLevel: "silent",
metafile: true,
platform: "node",
stdin: {
contents: 'import "chokidar";',
loader: "js",
resolveDir: join(workspaceRoot, "apps", "daemon"),
},
target: MAC_PREBUNDLE_ESBUILD_TARGET,
write: false,
});
expect(Object.keys(result.metafile.inputs).some((input) => input.includes("/node_modules/fsevents/"))).toBe(
false,
);
},
);
it(
"keeps node-pty's native runtime outside daemon bundles",
async () => {
const workspaceRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
const result = await build({
banner: { js: MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER },
bundle: true,
external: [...MAC_PREBUNDLE_POLICIES.daemonSidecar.externals],
format: "esm",
logLevel: "silent",
metafile: true,
platform: "node",
entryPoints: [join(workspaceRoot, "apps", "daemon", "src", "terminals.ts")],
target: MAC_PREBUNDLE_ESBUILD_TARGET,
write: false,
});
expect(
Object.keys(result.metafile.inputs).some((input) => input.includes("/node_modules/node-pty/")),
).toBe(false);
},
);
});
describe("findForbiddenMacPrebundleInputs", () => {
it("matches forbidden dependency roots after path normalization", () => {
expect(
findForbiddenMacPrebundleInputs({
forbiddenInputs: MAC_PREBUNDLE_POLICIES.webSidecar.forbiddenInputs,
inputs: [
"src/index.ts",
"C:\\repo\\node_modules\\next\\dist\\server.js",
"/repo/node_modules/openai/index.mjs",
],
}),
).toEqual([
"C:/repo/node_modules/next/dist/server.js",
"/repo/node_modules/openai/index.mjs",
]);
});
});
describe("assertMacPrebundleMetafile", () => {
it("accepts a safe web sidecar metafile", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "safe.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/apps/web/sidecar/index.ts": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "webSidecar" }),
).resolves.toBeUndefined();
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("rejects a packaged main metafile that pulled in web runtime closure", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "unsafe.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/node_modules/@open-design/web/dist/sidecar/index.js": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "packagedMain" }),
).rejects.toThrow(/packaged main prebundle included forbidden inputs/);
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("rejects a daemon metafile that bundled wasm-backed runtime dependencies", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "unsafe-daemon.json");
try {
await writeFile(
metafilePath,
JSON.stringify({ inputs: { "/repo/node_modules/blake3-wasm/dist/node/index.js": {} } }),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "daemonSidecar" }),
).rejects.toThrow(/daemon sidecar prebundle included forbidden inputs/);
} finally {
await rm(root, { force: true, recursive: true });
}
});
it("rejects a daemon metafile that bundled native runtime dependencies", async () => {
const root = await mkdtemp(join(tmpdir(), "open-design-mac-prebundle-"));
const metafilePath = join(root, "unsafe-native-daemon.json");
try {
await writeFile(
metafilePath,
JSON.stringify({
inputs: {
"/repo/node_modules/fsevents/fsevents.js": {},
"/repo/node_modules/node-pty/lib/index.js": {},
},
}),
"utf8",
);
await expect(
assertMacPrebundleMetafile({ metafilePath, policyName: "daemonSidecar" }),
).rejects.toThrow(/daemon sidecar prebundle included forbidden inputs/);
} finally {
await rm(root, { force: true, recursive: true });
}
});
});
describe("renderMacPackagedMainEntry", () => {
it("renders the prebundled runtime entry shim", () => {
expect(renderMacPackagedMainEntry(true)).toContain("./prebundled/packaged-main.mjs");
expect(renderMacPackagedMainEntry(true)).not.toContain("@open-design/packaged");
});
it("renders the package entry shim for non-prebundled mode", () => {
expect(renderMacPackagedMainEntry(false)).toContain("@open-design/packaged");
expect(renderMacPackagedMainEntry(false)).not.toContain("./prebundled/packaged-main.mjs");
});
});