Files
reverse-engineering-anything/tests/process-global/cliUpdateAlias.test.ts
T

114 lines
3.7 KiB
TypeScript

import childProcess from "node:child_process";
import { syncBuiltinESMExports } from "node:module";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { createCli } from "../../src/cli.js";
import { PRODUCT_IDENTITY } from "../../src/identity.js";
import { HOST_PLATFORM } from "../../src/platform/HostPlatform.js";
const mocks = vi.hoisted(() => ({
run: vi.fn(),
host: vi.fn(() => ({ owner: "selected-update-host" })),
dependencyInstall: vi.fn(() => {
throw new Error("Dependency installer must not run in this test.");
}),
}));
vi.mock("../../src/application/Update.js", async (original) => ({
...(await original<typeof import("../../src/application/Update.js")>()),
runUpdate: mocks.run,
}));
vi.mock("../../src/application/UpdateRuntime.js", () => ({
systemUpdateHost: mocks.host,
}));
const initialExitCode = process.exitCode;
const current = {
status: "current",
currentVersion: PRODUCT_IDENTITY.packageVersion,
latestVersion: PRODUCT_IDENTITY.packageVersion,
installMethod: "unknown",
};
beforeEach(async () => {
vi.clearAllMocks();
const root = await createTestTempDirectory("rea-update-api-sandbox-");
await writeFile(
join(root, process.platform === "win32" ? "npm.cmd" : "npm"),
process.platform === "win32"
? "@echo off\r\necho Test package manager refuses installation 1>&2\r\nexit /b 97\r\n"
: "#!/bin/sh\nprintf '%s\\n' 'Test package manager refuses installation' >&2\nexit 97\n",
{ mode: 0o700 },
);
vi.stubEnv("PATH", root);
vi.stubEnv("npm_config_user_agent", "npm/test");
vi.stubEnv("npm_config_prefix", join(root, "global"));
vi.stubEnv("npm_config_cache", join(root, "cache"));
// Externalized dependencies bypass Vitest module factories for Node builtins.
// Patch the real builtin and synchronize named ESM exports; PATH/prefix above
// independently prevent any real installation if this guard stops working.
vi.spyOn(childProcess, "execFile").mockImplementation(
mocks.dependencyInstall,
);
syncBuiltinESMExports();
vi.stubEnv("CI", "1");
vi.stubEnv("NO_UPDATE_NOTIFIER", "1");
vi.stubGlobal(
"fetch",
vi.fn(() => Promise.reject(new Error("No network in this test."))),
);
mocks.run.mockResolvedValue(current);
});
afterEach(() => {
vi.restoreAllMocks();
syncBuiltinESMExports();
vi.unstubAllEnvs();
vi.unstubAllGlobals();
process.exitCode = initialExitCode;
});
const serve = async (
cli: ReturnType<typeof createCli>,
arguments_: string[],
) => {
process.exitCode = 0;
let stdout = "";
let exitCode = 0;
await cli.serve(arguments_, {
env: {},
stdout: (text) => {
stdout += text;
},
exit: (code) => {
exitCode = code;
},
});
return { stdout, exitCode: exitCode || Number(process.exitCode ?? 0) };
};
it("uses the selected environment snapshot when serving the update alias directly", async () => {
const selected = { REA_SELECTED_UPDATE: "original" };
const cli = createCli(selected);
selected.REA_SELECTED_UPDATE = "changed";
const result = await serve(cli, ["--format", "json", "--update"]);
expect(result.exitCode).toBe(0);
expect(JSON.parse(result.stdout)).toMatchObject(current);
expect(mocks.run).toHaveBeenCalledExactlyOnceWith(
PRODUCT_IDENTITY.packageVersion,
{ owner: "selected-update-host" },
"structured",
);
expect(mocks.host).toHaveBeenCalledExactlyOnceWith(
undefined,
undefined,
{ REA_SELECTED_UPDATE: "original" },
HOST_PLATFORM.platform,
);
expect(mocks.dependencyInstall).not.toHaveBeenCalled();
});