Files
copilotkit__copilotkit/scripts/release/lib/npm-cli.test.ts
Benjamin Taylor 6a35e3cdbf perf(release): cut canary publish wall-clock roughly in half
The canary flow took ~11.5 min steady-state (and 20 min in an observed
run). Measured from run 30473499191, the time went to five avoidable
places rather than to real work.

1. `npx --yes npm@11.15.0 publish` ran per package, and npx re-resolves
   the spec against the registry on EVERY invocation: ~16s of each
   package's ~21s. A 9-package channels canary paid ~2.4 min of pure npx
   overhead; a 16-package monorepo release paid over 4 min. Hoist the
   pinned npm into lib/npm-cli.ts, install it once into a throwaway
   prefix, and reuse the binary.

2. publish-release.yml was the only workflow in the repo with no pnpm
   store cache, so all three jobs installed 4608 packages cold every
   time. Usually ~45s each, but registry-bandwidth bound and heavy
   tailed: the observed run spent 9m08s here on tarballs arriving at
   2-49 KiB/s. Add the same node-version-keyed cache the rest of CI uses.

3. The notify job ran for canaries only to compute "post nothing" — the
   builder already returns should_post=false for mode=prerelease and the
   self-watchdog is already gated off. ~85s of dead work on the critical
   path, since canary.yml waits for the whole run. Skip the job, keeping
   it reachable for a python_publish dispatch.

4. The build job fetched full history for canaries, which need none (no
   tag, no GH Release, no release-note commit range, and `nx run-many`
   resolves no merge base). That rode along in the 837 MiB workspace
   artifact too. Shallow-fetch prereleases; stable keeps depth 0 because
   its publish job pushes tags out of that artifact's .git.

5. Two smaller ones: the artifact was gzipped and then re-deflated into
   the artifact zip (compression-level: 0), and the orchestrator's
   run-discovery loop slept 6s before its first poll.

Verified: 143 release-script tests pass (6 new for the npm-cli helper),
actionlint + shellcheck + the scope-dropdown guard are clean, the
prerelease dry-run path still enumerates all 9 channels packages, and a
live probe confirms the helper installs npm 11.15.0 once (3.2s) and
memoizes thereafter (0ms).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 19:29:38 -05:00

88 lines
2.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import {
NPM_PUBLISH_VERSION,
resetPublishNpmCache,
resolvePublishNpm,
} from "./npm-cli.js";
const spawnSyncMock = vi.hoisted(() => vi.fn());
const existsSyncMock = vi.hoisted(() => vi.fn());
vi.mock("child_process", () => ({
spawnSync: spawnSyncMock,
}));
vi.mock("fs", () => ({
default: {
mkdtempSync: (prefix: string) => `${prefix}test`,
existsSync: existsSyncMock,
},
}));
beforeEach(() => {
resetPublishNpmCache();
spawnSyncMock.mockReset();
existsSyncMock.mockReset();
existsSyncMock.mockReturnValue(true);
});
describe("resolvePublishNpm", () => {
it("installs the pinned npm into a throwaway prefix, never mutating the ambient npm", () => {
spawnSyncMock.mockReturnValue({ status: 0 });
const bin = resolvePublishNpm();
expect(spawnSyncMock).toHaveBeenCalledTimes(1);
const [command, args] = spawnSyncMock.mock.calls[0];
expect(command).toBe("npm");
expect(args).toContain(`npm@${NPM_PUBLISH_VERSION}`);
// --prefix is what keeps this hermetic; a bare `-g` would replace the
// runner's (or a developer's) global npm.
expect(args).toContain("--prefix");
expect(bin).toMatch(/\/bin\/npm$/);
});
it("pins a version npm can actually publish with via OIDC (>= 11.5.1)", () => {
const [major, minor, patch] = NPM_PUBLISH_VERSION.split(".").map(Number);
expect(major).toBeGreaterThanOrEqual(11);
// Guard the exact floor so a future downgrade to e.g. 11.4.x — which cannot
// do OIDC trusted publishing — fails here instead of at publish time.
if (major === 11 && minor === 5) expect(patch).toBeGreaterThanOrEqual(1);
if (major === 11) expect(minor).toBeGreaterThanOrEqual(5);
});
it("installs only once across many packages (the whole point of the helper)", () => {
spawnSyncMock.mockReturnValue({ status: 0 });
const first = resolvePublishNpm();
const second = resolvePublishNpm();
const third = resolvePublishNpm();
expect(first).toBe(second);
expect(second).toBe(third);
expect(spawnSyncMock).toHaveBeenCalledTimes(1);
});
it("throws rather than falling back to the too-old ambient npm when install fails", () => {
spawnSyncMock.mockReturnValue({ status: 1 });
expect(() => resolvePublishNpm()).toThrow(/Failed to install npm@/);
});
it("throws when the install reports success but produced no binary", () => {
spawnSyncMock.mockReturnValue({ status: 0 });
existsSyncMock.mockReturnValue(false);
expect(() => resolvePublishNpm()).toThrow(/does not exist/);
});
it("does not memoize a failed install", () => {
spawnSyncMock.mockReturnValueOnce({ status: 1 });
expect(() => resolvePublishNpm()).toThrow();
spawnSyncMock.mockReturnValue({ status: 0 });
expect(resolvePublishNpm()).toMatch(/\/bin\/npm$/);
expect(spawnSyncMock).toHaveBeenCalledTimes(2);
});
});