fix(ci): reconcile Git ownership and preserve portable checks

This commit is contained in:
morluto
2026-10-13 20:11:02 +08:00
2143 changed files with 516719 additions and 21121 deletions
+185 -1
View File
@@ -1,8 +1,9 @@
import { mkdir, readFile, rm, writeFile, access } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { delimiter, dirname, join, relative, resolve } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { err, ok } from "../../src/domain/result.js";
import { runUpdate, type UpdateResult } from "../../src/application/Update.js";
import { PRODUCT_IDENTITY } from "../../src/identity.js";
import { supportedClients } from "../../src/application/SupportedClients.js";
import {
@@ -125,6 +126,41 @@ describe("OMP integration maintenance", () => {
});
});
describe("maintenance activation and layered configuration", () => {
it("leaves a natively disabled ZCode registration outside maintenance", async () => {
const configPath = await writeClient(
"zcode",
JSON.stringify({
mcp: {
servers: {
rea: { command: entryPoint, args: ["mcp"], enable: false },
},
},
}),
);
const original = await readFile(configPath, "utf8");
await expect(
existingMaintenanceScope(home, entryPoint, process.env),
).resolves.toMatchObject({ clients: [] });
expect(await readFile(configPath, "utf8")).toBe(original);
});
it("selects an enabled registration inherited through layered OpenCode files", async () => {
const directory = join(home, ".config", "opencode");
await mkdir(directory, { recursive: true });
await writeFile(
join(directory, "opencode.json"),
JSON.stringify({
mcp: { rea: { type: "local", command: [entryPoint, "mcp"] } },
}),
);
await writeClient("opencode", JSON.stringify({ theme: "user-selected" }));
await expect(
existingMaintenanceScope(home, entryPoint, process.env),
).resolves.toMatchObject({ clients: ["opencode"] });
});
});
describe("existing REA integration maintenance", () => {
it("selects owned registrations while preserving unconfigured, foreign, and disabled choices", async () => {
await writeClient("codex", staleCodex);
@@ -383,9 +419,157 @@ describe("update subprocess boundaries", () => {
await writeFile(script, 'process.stdout.write("3.3.0\\n")');
await expect(
systemUpdateHost(packageRoot, home).installedVersion({
manager: "npm",
prefix: home,
packageRoot,
}),
).resolves.toEqual(ok("3.3.0"));
});
});
describe("Bun global installation update", () => {
it.each([
["1.4.2", "updated", "absolute"],
["1.2.17", "updated", "absolute"],
["1.2.17", "updated", "relative"],
["1.2.14", "failed", "absolute"],
["1.2.17-canary.1", "failed", "absolute"],
["unknown", "failed", "absolute"],
])(
"handles Bun %s with outcome %s using %s global-directory configuration",
async (bunVersion, status, directoryMode) => {
// A recording `bun` on PATH crosses the real subprocess boundary without registry access.
const root = await createTestTempDirectory("rea-update-bun-");
const globalDirectory = join(root, "bun", "install", "global");
const packageRoot = join(
globalDirectory,
"node_modules",
PRODUCT_IDENTITY.packageName,
);
const versionPath = join(root, "installed-version");
const logPath = join(root, "bun-invocations.jsonl");
await mkdir(join(packageRoot, "scripts"), { recursive: true });
await writeFile(
join(globalDirectory, "package.json"),
'{"dependencies":{"rea-agents":"3.2.1"}}\n',
);
await writeFile(versionPath, "3.2.1");
await writeFile(
join(packageRoot, "scripts", "rea.mjs"),
`import { readFileSync } from "node:fs";\nprocess.stdout.write(readFileSync(${JSON.stringify(versionPath)}, "utf8") + "\\n");\n`,
);
const binDirectory = join(root, "bin");
const recorder = join(binDirectory, "record-bun.mjs");
await mkdir(binDirectory);
await writeFile(
recorder,
[
'import { appendFileSync, writeFileSync } from "node:fs";',
"const args = process.argv.slice(2);",
`appendFileSync(${JSON.stringify(logPath)}, JSON.stringify({ args, cwd: process.cwd(), globalDirectory: process.env.BUN_INSTALL_GLOBAL_DIR }) + "\\n");`,
`if (JSON.stringify(args) === '["--version"]') console.log(${JSON.stringify(bunVersion)});`,
`else if (JSON.stringify(args) === ${JSON.stringify(JSON.stringify(["info", PRODUCT_IDENTITY.packageName, "dist-tags.latest", "--json"]))}) process.stdout.write('"3.3.0"\\n');`,
`else if (JSON.stringify(args) === ${JSON.stringify(JSON.stringify(["add", "--global", "--ignore-scripts", `${PRODUCT_IDENTITY.packageName}@3.3.0`]))}) writeFileSync(${JSON.stringify(versionPath)}, "3.3.0");`,
"else process.exitCode = 2;",
"",
].join("\n"),
);
if (process.platform === "win32") {
await writeFile(
join(binDirectory, "bun.cmd"),
`@"${process.execPath}" "${recorder}" %*\r\n`,
);
} else {
await writeFile(
join(binDirectory, "bun"),
`#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(recorder)} "$@"\n`,
{ mode: 0o755 },
);
}
const pathKey =
Object.keys(process.env).find((key) => key.toUpperCase() === "PATH") ??
"PATH";
const environment: NodeJS.ProcessEnv = {
...process.env,
[pathKey]: `${binDirectory}${delimiter}${process.env[pathKey] ?? ""}`,
BUN_INSTALL: join(root, "bun"),
};
delete environment.BUN_INSTALL_GLOBAL_DIR;
if (directoryMode === "relative")
environment.BUN_INSTALL_GLOBAL_DIR = relative(root, globalDirectory);
const callerDirectory = process.cwd();
let result: UpdateResult;
try {
process.chdir(root);
result = await runUpdate(
"3.2.1",
systemUpdateHost(packageRoot, home, environment),
"structured",
);
} finally {
process.chdir(callerDirectory);
}
const invocations = (await readFile(logPath, "utf8"))
.trim()
.split("\n")
.map((line): unknown => JSON.parse(line));
const versionInvocation = {
args: ["--version"],
cwd: globalDirectory,
globalDirectory,
};
if (status === "failed") {
expect(result).toEqual({
status: "failed",
currentVersion: "3.2.1",
latestVersion: null,
reason: "version-check",
remediation: `REA updates for Bun global installations require Bun 1.2.17 or later; bun --version returned ${JSON.stringify(bunVersion)}. Upgrade Bun manually, then rerun rea update.`,
});
expect(invocations).toEqual([versionInvocation]);
expect(await readFile(versionPath, "utf8")).toBe("3.2.1");
return;
}
expect(result).toMatchObject({
status: "updated",
installMethod: "bun",
installedVersion: "3.3.0",
command: [
"bun",
"add",
"--global",
"--ignore-scripts",
`${PRODUCT_IDENTITY.packageName}@3.3.0`,
],
});
// `bun info` needs a project in its working directory, and the caller's
// directory must never be selected, so all commands run in the owner.
expect(invocations).toEqual([
versionInvocation,
{
args: [
"info",
PRODUCT_IDENTITY.packageName,
"dist-tags.latest",
"--json",
],
cwd: globalDirectory,
globalDirectory,
},
{
args: [
"add",
"--global",
"--ignore-scripts",
`${PRODUCT_IDENTITY.packageName}@3.3.0`,
],
cwd: globalDirectory,
globalDirectory,
},
]);
},
);
});