Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/ci-registry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ concurrency:
jobs:
e2e-registry:
name: hermetic registry e2e (local verdaccio; loopback only, zero secrets)
runs-on: ubuntu-latest
timeout-minutes: 15
runs-on: ${{ matrix.os }}
timeout-minutes: 25
strategy:
fail-fast: false
matrix:
# Windows is where real installs live (npm -g shims, NTFS junctions,
# Program Files paths) — validated on a real win10 VM before adding.
os: [ubuntu-latest, windows-latest]
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4

Expand All @@ -34,11 +43,12 @@ jobs:
if: failure()
run: |
mkdir -p tmp/e2e-artifacts
for d in tmp/e2e-registry-* tmp/e2e-advisory-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done
for d in tmp/e2e-registry-* tmp/e2e-advisory-* tmp/e2e-npm-global-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done

- uses: actions/upload-artifact@v4
if: failure()
name: e2e-registry-artifacts (with os suffix)
with:
name: e2e-registry-artifacts
name: e2e-registry-artifacts-${{ matrix.os }}
path: tmp/e2e-artifacts
if-no-files-found: ignore
114 changes: 114 additions & 0 deletions tests/e2e/crossplat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Cross-platform helpers for the hermetic e2e suites (registry, advisory
// rollback, release canary). Windows compatibility notes learned on a real
// win10 VM (#1872 follow-up audit):
// - `execFile("npm")` / `spawnSync("npm")` cannot spawn npm.cmd without a
// shell (ENOENT) — run npm as `node <npm-cli.js>` resolved next to the
// running node so no shell is involved and argv stays literal.
// - A fully stripped env breaks node children on Windows (SystemRoot is
// required); npm additionally keys its userconfig off USERPROFILE, not
// HOME. Mirror the isolated HOME onto the Windows profile vars.
// - Fake client bins: plain `#!/bin/sh` scripts cannot execute on Windows —
// emit a .cmd shim instead (the product's client probe already routes
// .cmd/.bat through the shell with quoting, see detectOpencodeMajor).
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export const IS_WIN = process.platform === "win32";

/** npm's cli.js next to the running node — the standard layout on every
* platform and CI runner (nodejs dir / hostedtoolcache / nvm symlinks). */
export function npmCliPath(): string | undefined {
const exeDir = path.dirname(process.execPath);
const candidates = IS_WIN
? [path.join(exeDir, "node_modules", "npm", "bin", "npm-cli.js")]
: [
path.join(exeDir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"),
path.join(exeDir, "lib", "node_modules", "npm", "bin", "npm-cli.js"),
];
for (const c of candidates) if (fs.existsSync(c)) return c;
return undefined;
}

/** System vars node/npm need even in an isolated env on Windows. */
export function windowsSystemEnv(): Record<string, string> {
if (!IS_WIN) return {};
const keep: Record<string, string> = {};
for (const key of ["SystemRoot", "SystemDrive", "ComSpec", "TEMP", "TMP", "PATHEXT", "ProgramFiles", "NUMBER_OF_PROCESSORS", "PROCESSOR_ARCHITECTURE"]) {
const v = process.env[key];
if (v) keep[key] = v;
}
return keep;
}

/** Env pointing HOME (and its Windows mirrors) at an isolated `homeDir`. */
export function npmHomeEnv(homeDir: string): Record<string, string> {
if (!IS_WIN) return { HOME: homeDir };
return {
HOME: homeDir,
USERPROFILE: homeDir,
APPDATA: path.join(homeDir, "AppData", "Roaming"),
LOCALAPPDATA: path.join(homeDir, "AppData", "Local"),
};
}

/** Isolated HOME/XDG_* (+ Windows mirrors) under `work`, same shape the
* suites used to build inline (POSIX-only before this helper). */
export function isolatedEnv(work: string): Record<string, string> {
const env: Record<string, string> = {};
const pairs: Array<[string, string]> = [
["HOME", "home"],
["XDG_CONFIG_HOME", "config"],
["XDG_CACHE_HOME", "cache"],
["XDG_STATE_HOME", "state"],
["XDG_DATA_HOME", "data"],
];
if (IS_WIN) {
pairs.push(
["USERPROFILE", "home"],
["APPDATA", path.join("home", "AppData", "Roaming")],
["LOCALAPPDATA", path.join("home", "AppData", "Local")],
);
}
for (const [key, dir] of pairs) {
const p = path.join(work, dir);
fs.mkdirSync(p, { recursive: true });
env[key] = p;
}
return env;
}

/** Env for spawning the bili CLI: PATH + isolation + required system vars. */
export function biliSpawnEnv(iso: Record<string, string>): Record<string, string> {
return { PATH: process.env.PATH ?? "", ...iso, ...windowsSystemEnv() };
}

/** Run npm synchronously without a shell: `node npm-cli.js args...`. Only
* falls back to the npm shim through a shell when cli.js cannot be located. */
export function npmRunSync(args: string[], opts: { cwd: string; env: Record<string, string> }): string {
const env: Record<string, string> = { ...opts.env, ...windowsSystemEnv() };
if (opts.env.NPM_ALLOW_DANGEROUS) env.NPM_ALLOW_DANGEROUS = opts.env.NPM_ALLOW_DANGEROUS;
const cli = npmCliPath();
if (cli) {
return execFileSync(process.execPath, [cli, ...args], { cwd: opts.cwd, encoding: "utf8", timeout: 180_000, env, windowsHide: true });
}
const line = ["npm", ...args.map((a) => (/\s/.test(a) ? `"${a}"` : a))].join(" ");
return execFileSync(line, { cwd: opts.cwd, encoding: "utf8", timeout: 180_000, shell: true, env, windowsHide: true });
}

/** Cross-platform fake client bin that prints `version` for `<bin>
* --version` probes (detectOpencodeMajor & friends). Returns a path the
* product can spawn as-is: shebang script on POSIX, .cmd shim on Windows. */
export function fakeVersionBin(work: string, name: string, version: string): string {
const dir = path.join(work, "bin");
fs.mkdirSync(dir, { recursive: true });
if (!IS_WIN) {
const shim = path.join(dir, name);
fs.writeFileSync(shim, `#!/bin/sh\necho ${version}\n`);
fs.chmodSync(shim, 0o755);
return shim;
}
const cmd = path.join(dir, `${name}.cmd`);
fs.writeFileSync(cmd, `@echo ${version}\r\n`);
return cmd;
}
56 changes: 17 additions & 39 deletions tests/e2e/e2e-advisory-rollback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
// Loopback only, zero secrets, zero tokens. Gated like ACP_TEST_REGISTRY so
// plain `npm test` stays free; requires `npm run build` first.
import { spawn, type ChildProcess } from "node:child_process";
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import net from "node:net";
import path from "node:path";
import { strict as assert } from "node:assert";
import { test } from "node:test";
import * as tar from "tar";
import { startRegistry } from "./registry-fixture.js";
import { biliSpawnEnv, isolatedEnv, npmHomeEnv, npmRunSync } from "./crossplat.ts";
import { rmrf } from "../tmp-rm.ts";

const run = process.env.ACP_TEST_REGISTRY === "1";
Expand Down Expand Up @@ -73,26 +73,20 @@ function freePort(): Promise<number> {
});
}

function isolatedEnv(work: string): Record<string, string> {
const env: Record<string, string> = {};
for (const [key, dir] of [
["HOME", "home"],
["XDG_CONFIG_HOME", "config"],
["XDG_CACHE_HOME", "cache"],
["XDG_STATE_HOME", "state"],
["XDG_DATA_HOME", "data"],
] as const) {
const p = path.join(work, dir);
fs.mkdirSync(p, { recursive: true });
env[key] = p;
}
return env;
}

async function readPkgVersion(dir: string): Promise<string> {
return (JSON.parse(await fs.promises.readFile(path.join(dir, "package.json"), "utf8")) as { version: string }).version;
}

function packTarball(stage: string, packs: string, home: string): string {
const listing = npmRunSync(["pack", "--silent", "--pack-destination", packs], { cwd: stage, env: { PATH: process.env.PATH ?? "", ...npmHomeEnv(home) } })
.trim()
.split("\n")
.pop()
?.trim();
return listing ?? "";
}

// Stage a publishable tarball of THIS package at a synthetic version (same
// shape as e2e-registry.test.ts's helper).
async function makeFixtureTarball(work: string, version: string): Promise<string> {
Expand All @@ -107,17 +101,9 @@ async function makeFixtureTarball(work: string, version: string): Promise<string
}
const home = path.join(work, "home-pkg");
fs.mkdirSync(home, { recursive: true });
const listing = execFileSync("npm", ["pack", "--silent", "--pack-destination", packs], {
cwd: stage,
encoding: "utf8",
env: { PATH: process.env.PATH ?? "", HOME: home },
})
.trim()
.split("\n")
.pop()
?.trim();
assert.ok(listing?.endsWith(".tgz"), `npm pack produced no tarball for ${version}: ${listing}`);
return path.join(packs, listing!);
const listing = packTarball(stage, packs, home);
assert.ok(listing.endsWith(".tgz"), `npm pack produced no tarball for ${version}: ${listing}`);
return path.join(packs, listing);
}

// The companion advisory package: a minimal tarball whose package.json
Expand All @@ -144,17 +130,9 @@ async function makeAdvisoryTarball(work: string): Promise<string> {
const home = path.join(work, "home-pkg");
const packs = path.join(work, "packs");
fs.mkdirSync(packs, { recursive: true });
const listing = execFileSync("npm", ["pack", "--silent", "--pack-destination", packs], {
cwd: stage,
encoding: "utf8",
env: { PATH: process.env.PATH ?? "", HOME: home },
})
.trim()
.split("\n")
.pop()
?.trim();
assert.ok(listing?.endsWith(".tgz"), `npm pack produced no advisory tarball: ${listing}`);
return path.join(packs, listing!);
const listing = packTarball(stage, packs, home);
assert.ok(listing.endsWith(".tgz"), `npm pack produced no advisory tarball: ${listing}`);
return path.join(packs, listing);
}

// The fake install MUST sit under a node_modules directory (isNpmInstallForm
Expand All @@ -171,7 +149,7 @@ type BiliProc = { child: ChildProcess; port: number; stderr: string };

function spawnBiliStart(installDir: string, port: number, env: Record<string, string>): BiliProc {
const child = spawn(process.execPath, [path.join(installDir, "dist", "index.js"), "start", "--port", String(port)], {
env: { PATH: process.env.PATH ?? "", ...env },
env: biliSpawnEnv(env),
});
const proc: BiliProc = { child, port, stderr: "" };
child.stderr?.on("data", (d) => (proc.stderr += d));
Expand Down
90 changes: 59 additions & 31 deletions tests/e2e/e2e-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// in-place install → disk flip — plus the post-update opencode plugin entry,
// against a local verdaccio. Loopback only, zero secrets, zero tokens.
// Gated like ACP_TEST_E2E so plain `npm test` stays free.
import { execFileSync, spawnSync } from "node:child_process";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { strict as assert } from "node:assert";
import { test } from "node:test";
import * as tar from "tar";
import { startRegistry } from "./registry-fixture.js";
import { IS_WIN, biliSpawnEnv, fakeVersionBin, isolatedEnv, npmHomeEnv, npmRunSync } from "./crossplat.ts";
import { rmrf } from "../tmp-rm.ts";

const run = process.env.ACP_TEST_REGISTRY === "1";
Expand All @@ -32,27 +33,11 @@ function escapeRe(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

function isolatedEnv(work: string): Record<string, string> {
const env: Record<string, string> = {};
for (const [key, dir] of [
["HOME", "home"],
["XDG_CONFIG_HOME", "config"],
["XDG_CACHE_HOME", "cache"],
["XDG_STATE_HOME", "state"],
["XDG_DATA_HOME", "data"],
] as const) {
const p = path.join(work, dir);
fs.mkdirSync(p, { recursive: true });
env[key] = p;
}
return env;
}

function runBili(installDir: string, args: string[], env: Record<string, string>): { code: number; stdout: string; stderr: string } {
const res = spawnSync(process.execPath, [path.join(installDir, "dist", "index.js"), ...args], {
encoding: "utf8",
timeout: 180_000,
env: { PATH: process.env.PATH ?? "", ...env },
env: biliSpawnEnv(env),
});
return { code: res.status ?? -1, stdout: res.stdout ?? "", stderr: res.stderr ?? "" };
}
Expand All @@ -63,23 +48,24 @@ async function readPkgVersion(dir: string): Promise<string> {

// Stage a publishable tarball of THIS package at a synthetic version: same
// files field, same dist build, rewritten package.json version.
async function makeFixtureTarball(work: string, version: string): Promise<string> {
// `bare` strips runtime dependencies — needed when the REAL npm client
// resolves the tree (`install -g`): the hermetic registry has no uplinks, so
// any dependency fetch would 404. The flip/layout mechanics under test
// don't involve dependency resolution.
async function makeFixtureTarball(work: string, version: string, opts?: { bare?: boolean }): Promise<string> {
const packs = path.join(work, "packs");
fs.mkdirSync(packs, { recursive: true });
const stage = path.join(work, "fixtures", version);
fs.mkdirSync(stage, { recursive: true });
fs.writeFileSync(path.join(stage, "package.json"), `${JSON.stringify({ ...PKG, version }, null, 2)}\n`);
const stagedPkg = opts?.bare ? { ...PKG, version, dependencies: {}, optionalDependencies: {} } : { ...PKG, version };
fs.writeFileSync(path.join(stage, "package.json"), `${JSON.stringify(stagedPkg, null, 2)}\n`);
for (const entry of PKG.files) {
const src = path.join(REPO_ROOT, entry);
if (fs.existsSync(src)) await fs.promises.cp(src, path.join(stage, entry), { recursive: true });
}
const home = path.join(work, "home-pkg");
fs.mkdirSync(home, { recursive: true });
const listing = execFileSync("npm", ["pack", "--silent", "--pack-destination", packs], {
cwd: stage,
encoding: "utf8",
env: { PATH: process.env.PATH ?? "", HOME: home },
})
const listing = npmRunSync(["pack", "--silent", "--pack-destination", packs], { cwd: stage, env: { PATH: process.env.PATH ?? "", ...npmHomeEnv(home) } })
.trim()
.split("\n")
.pop()
Expand Down Expand Up @@ -110,13 +96,10 @@ function seedOpencodeConfig(work: string): void {

// Deterministic host-major probe target: prints a 2.x version so
// detectOpencodeMajor() lands on key "plugins" regardless of what (if
// anything) is installed on the host.
// anything) is installed on the host. Cross-platform: .cmd shim on Windows
// (the product routes .cmd through the shell), shebang script on POSIX.
function fakeOpencodeBin(work: string): string {
const bin = path.join(work, "bin", "fake-opencode");
fs.mkdirSync(path.dirname(bin), { recursive: true });
fs.writeFileSync(bin, "#!/bin/sh\necho 2.4.0\n");
fs.chmodSync(bin, 0o755);
return bin;
return fakeVersionBin(work, "fake-opencode", "2.4.0");
}

test("hermetic registry e2e", { skip: skipReason }, async (t) => {
Expand Down Expand Up @@ -180,3 +163,48 @@ test("hermetic registry e2e", { skip: skipReason }, async (t) => {
assert.match(res.stdout, /plugins present/);
});
});

test("hermetic npm -g install e2e (real npm client, real global layout)", { skip: skipReason }, async (t) => {
assert.ok(fs.existsSync(DIST_ENTRY), "dist/index.js missing — run `npm run build` first");
const workRoot = path.join(process.cwd(), "tmp");
fs.mkdirSync(workRoot, { recursive: true });
const work = fs.mkdtempSync(path.join(workRoot, "e2e-npm-global-"));
t.after(() => rmrf(work));

const reg = await startRegistry(path.join(work, "registry"));
t.after(() => reg.stop());
await reg.publish(await makeFixtureTarball(work, OLD_VERSION, { bare: true }));
await reg.publish(await makeFixtureTarball(work, NEW_VERSION, { bare: true }));
const envBase = isolatedEnv(work);

// The REAL npm client installing globally into an isolated prefix — not
// the manual tar extraction the flip test above uses. This is the layout
// real users get from `npm install -g billion-context`, per platform:
// win32: <prefix>/node_modules/<pkg> + <prefix>/bili.cmd shim
// POSIX: <prefix>/lib/node_modules/<pkg> + <prefix>/bin/bili shim
const prefix = path.join(work, "npm-global");
fs.mkdirSync(prefix, { recursive: true });
await reg.npm(["install", "--global", `${PKG.name}@${OLD_VERSION}`, "--prefix", prefix]);
const installDir = fs.existsSync(path.join(prefix, "node_modules", PKG.name))
? path.join(prefix, "node_modules", PKG.name)
: path.join(prefix, "lib", "node_modules", PKG.name);
assert.equal(await readPkgVersion(installDir), OLD_VERSION, `npm -g did not lay the package at ${installDir}`);
const shim = IS_WIN ? path.join(prefix, "bili.cmd") : path.join(prefix, "bin", "bili");
assert.ok(fs.existsSync(shim), `npm -g must lay the bili shim at ${shim} for this platform`);

// Self-update must classify the npm-global layout as bili-owned
// (hostManagedInstall) and flip it in place over the real chain.
const res = runBili(installDir, ["update"], { ...envBase, BILI_UPDATE_REGISTRY: reg.url });
assert.equal(res.code, 0, `bili update failed:\n${res.stderr}`);
assert.match(res.stderr, new RegExp(`installed ${escapeRe(OLD_VERSION)} → ${escapeRe(NEW_VERSION)}\. Restart to finish\.`));
assert.equal(await readPkgVersion(installDir), NEW_VERSION, "npm-global on-disk version must flip to the published one");

// The shim npm laid down still resolves through to the flipped tree.
// (.cmd shims cannot be spawned without a shell on Windows — route them
// through cmd like the product's own client probe does.)
const probe = IS_WIN
? spawnSync(`"${shim}" --version`, { shell: true, encoding: "utf8", timeout: 60_000, env: biliSpawnEnv(envBase) })
: spawnSync(shim, ["--version"], { encoding: "utf8", timeout: 60_000, env: biliSpawnEnv(envBase) });
const shimVersion = (probe.stdout ?? "").trim();
assert.match(shimVersion, new RegExp(`^v?${escapeRe(NEW_VERSION)}$`), `bili shim must report the flipped version, got: ${JSON.stringify(shimVersion)}`);
});
Loading
Loading