Файли
openidea/scripts/dogfood.test.ts
T
Deng Liuтаdengliu 9aab39de87 feat(dogfood): cross-build remote servers locally (#8124)
Add `dogfood:servers` to cross-build static Linux x86_64 and aarch64 openidea-server binaries with cargo-zigbuild and stage them for upload on the next remote connection. `dogfood:all` builds the local app and the servers together, and still accepts `<remote_name>` to build natively on that host.

Zig and cargo-zigbuild are declared as task-level mise tools for `dogfood:servers` and `dogfood:all` only, so `mise run setup` stays a plain `pnpm install`; the musl Rust targets are added with rustup the first time a cross build needs them.

Co-authored-by: dengliu <[email protected]>
2026-12-05 14:39:07 +08:00

231 рядки
9.7 KiB
TypeScript

// @vitest-environment node
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import {
DEFAULT_DOGFOOD_APP_NAME,
DOGFOOD_SERVER_TARGETS,
deferRestartArgs,
dogfoodBuildEnv,
dogfoodCliArgs,
dogfoodPaths,
dogfoodServerTargetDir,
dogfoodTauriConfig,
generateDogfoodIcons,
installDogfood,
parseDogfoodArgs,
runParallelTasks,
} from "./dogfood.ts";
const root = fileURLToPath(new URL("..", import.meta.url));
describe("dogfood local, servers and all", () => {
it("parses local, servers and all arguments with Rust architecture names", () => {
expect(parseDogfoodArgs(["local"])).toEqual({ action: "local", buildOnly: false });
expect(parseDogfoodArgs(["local", "--build-only"])).toEqual({ action: "local", buildOnly: true });
expect(parseDogfoodArgs(["local", "--", "--build-only"])).toEqual({ action: "local", buildOnly: true });
expect(parseDogfoodArgs(["servers"])).toEqual({ action: "servers", architectures: ["x86_64", "aarch64"] });
expect(parseDogfoodArgs(["servers", "x86_64"])).toEqual({ action: "servers", architectures: ["x86_64"] });
expect(parseDogfoodArgs(["all", "--", "aarch64", "--build-only"])).toEqual({
action: "all",
buildOnly: true,
architectures: ["aarch64"],
});
expect(parseDogfoodArgs(["all"])).toEqual({
action: "all",
buildOnly: false,
architectures: ["x86_64", "aarch64"],
});
expect(() => parseDogfoodArgs([])).toThrow(/Expected 'local', 'servers' or 'all'/);
expect(() => parseDogfoodArgs(["build"])).toThrow(/Expected 'local', 'servers' or 'all'/);
expect(() => parseDogfoodArgs(["local", "my-remote"])).toThrow(/Unexpected option/);
expect(() => parseDogfoodArgs(["servers", "--build-only"])).toThrow(/Unexpected option/);
expect(() => parseDogfoodArgs(["all", "--restart"])).toThrow(/Unexpected option/);
// A remote name keeps building the server natively on that SSH host.
expect(parseDogfoodArgs(["all", "--", "my-remote", "--build-only", "--restart", "--ssh-opt", "Port=2222"])).toEqual(
{
action: "all",
buildOnly: true,
remoteArgs: ["my-remote", "--restart", "--ssh-opt", "Port=2222"],
remote: {
remoteName: "my-remote",
clean: false,
installRust: false,
restart: "always",
sshOpts: ["Port=2222"],
},
},
);
expect(deferRestartArgs(["my-remote", "--restart", "--ssh-opt", "Port=2222"])).toEqual([
"my-remote",
"--ssh-opt",
"Port=2222",
"--no-restart",
]);
expect(() => parseDogfoodArgs(["all", "my-remote", "--clean"])).toThrow(/dogfood:remote <remote_name> --clean/);
expect(() => parseDogfoodArgs(["all", "my-remote", "x86_64"])).toThrow(/Unexpected extra argument/);
expect(() => parseDogfoodArgs(["servers", "x86_64", "aarch64"])).toThrow(/Choose one/);
});
it("uses Rust target and cache names for local server builds", () => {
expect(DOGFOOD_SERVER_TARGETS).toEqual({
x86_64: "x86_64-unknown-linux-musl",
aarch64: "aarch64-unknown-linux-musl",
});
expect(dogfoodServerTargetDir(root, "x86_64")).toBe(join(root, "target", "dogfood-servers", "x86_64"));
expect(dogfoodServerTargetDir(root, "aarch64")).toBe(join(root, "target", "dogfood-servers", "aarch64"));
});
it("runs parallel tasks concurrently, prefixes lines, and terminates siblings on failure", async () => {
const out: string[] = [];
const err: string[] = [];
await runParallelTasks(
[
{
label: "local",
command: process.execPath,
args: ["-e", 'process.stdout.write("step 1\\nstep "); setTimeout(() => process.stdout.write("2"), 40);'],
},
{
label: "remote",
command: process.execPath,
args: ["-e", 'process.stderr.write("warn line\\n"); process.stdout.write("done\\n");'],
},
],
{ stdout: (line) => out.push(line), stderr: (line) => err.push(line) },
);
expect(out).toContain("[local] step 1\n");
expect(out).toContain("[local] step 2\n");
expect(out).toContain("[remote] done\n");
expect(err).toEqual(["[remote] warn line\n"]);
const started = Date.now();
await expect(
runParallelTasks(
[
{
label: "local",
command: process.execPath,
args: ["-e", "setTimeout(() => {}, 10_000)"],
},
{
label: "remote",
command: process.execPath,
args: ["-e", "setTimeout(() => process.exit(2), 50)"],
},
],
{ stdout: () => {}, stderr: () => {} },
),
).rejects.toThrow(/\[remote\] exited with status 2/);
expect(Date.now() - started).toBeLessThan(3_000);
});
it("keeps the vanilla app identifier so settings, MCP, Skills and agent sessions are shared, without updates", () => {
const baseConfig = JSON.parse(readFileSync(join(root, "src-tauri", "tauri.conf.json"), "utf8")) as {
identifier: string;
};
const override = dogfoodTauriConfig(DEFAULT_DOGFOOD_APP_NAME, ["../target/dogfood-icons/dogfood.icns"]);
expect(baseConfig.identifier).toBe("ai.l0ng.openidea");
expect("identifier" in override).toBe(false);
expect(override).toEqual({
productName: "openidea_dogfood",
bundle: {
createUpdaterArtifacts: false,
icon: ["../target/dogfood-icons/dogfood.icns"],
},
plugins: {
updater: { endpoints: [] },
},
});
});
it("uses fast release compilation defaults while respecting caller overrides", () => {
expect(dogfoodBuildEnv({})).toEqual({
CARGO_PROFILE_RELEASE_LTO: "off",
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16",
});
expect(dogfoodBuildEnv({ CARGO_PROFILE_RELEASE_LTO: "thin", CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "4" })).toEqual({
CARGO_PROFILE_RELEASE_LTO: "thin",
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "4",
});
});
it("resolves platform bundle arguments and install destinations", () => {
expect(dogfoodCliArgs("openidea_dogfood", ["icon.icns"], "darwin").slice(0, 3)).toEqual([
"build",
"--bundles",
"app",
]);
expect(dogfoodCliArgs("openidea_dogfood", ["icon.ico"], "win32").slice(0, 2)).toEqual(["build", "--no-bundle"]);
expect(dogfoodPaths(join("/repo", "target"), "openidea_dogfood", "darwin", {})).toEqual({
bundlePath: join("/repo", "target", "release", "bundle", "macos", "openidea_dogfood.app"),
installPath: join("/Applications", "openidea_dogfood.app"),
});
expect(
dogfoodPaths(join("/repo", "target"), "openidea_dogfood", "darwin", {
DOGFOOD_INSTALL_DIR: "/Users/me/Applications",
}),
).toEqual({
bundlePath: join("/repo", "target", "release", "bundle", "macos", "openidea_dogfood.app"),
installPath: join("/Users/me/Applications", "openidea_dogfood.app"),
});
});
it("keeps the dogfood source SVGs geometrically identical to the vanilla icons in grey", () => {
const sourceDir = join(root, "src-tauri", "icons", "source");
for (const [vanillaName, dogfoodName] of [
["openidea.svg", "openidea-dogfood.svg"],
["openidea-windows.svg", "openidea-dogfood-windows.svg"],
] as const) {
const vanilla = readFileSync(join(sourceDir, vanillaName), "utf8");
const dogfood = readFileSync(join(sourceDir, dogfoodName), "utf8");
const recolored = vanilla
.replaceAll('aria-label="OpenIDEA"', 'aria-label="OpenIDEA Dogfood"')
.replace(
"<image ",
'<defs><filter id="grey"><feColorMatrix type="saturate" values="0"/></filter></defs><image filter="url(#grey)" ',
);
expect(dogfood).toBe(recolored);
}
});
it("generates the dogfood icon bundle with a distinct macOS .icns name", () => {
const tempDir = mkdtempSync(join(tmpdir(), "openidea-dogfood-icons-"));
try {
const iconPaths = generateDogfoodIcons(root, tempDir, "darwin");
expect(iconPaths).toHaveLength(5);
expect(iconPaths.some((p) => p.endsWith("/dogfood.icns"))).toBe(true);
expect(existsSync(join(tempDir, "dogfood.icns"))).toBe(true);
expect(existsSync(join(tempDir, "[email protected]"))).toBe(true);
expect(existsSync(join(tempDir, "icon.ico"))).toBe(true);
const firstMtime = statSync(join(tempDir, "dogfood.icns")).mtimeMs;
expect(generateDogfoodIcons(root, tempDir, "darwin")).toEqual(iconPaths);
expect(statSync(join(tempDir, "dogfood.icns")).mtimeMs).toBe(firstMtime);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
}, 30_000);
it("installs a macOS .app bundle into DOGFOOD_INSTALL_DIR", () => {
if (process.platform !== "darwin") return;
const tempDir = mkdtempSync(join(tmpdir(), "openidea-dogfood-install-"));
const prevInstallDir = process.env.DOGFOOD_INSTALL_DIR;
try {
const fakeRoot = join(tempDir, "repo");
const fakeBundle = join(fakeRoot, "target", "release", "bundle", "macos", "openidea_dogfood.app");
mkdirSync(join(fakeBundle, "Contents", "MacOS"), { recursive: true });
writeFileSync(join(fakeBundle, "Contents", "MacOS", "openidea"), "#!/bin/sh\n");
const destDir = join(tempDir, "Applications");
process.env.DOGFOOD_INSTALL_DIR = destDir;
installDogfood(fakeRoot, "openidea_dogfood", "darwin", join(fakeRoot, "target"));
expect(existsSync(join(destDir, "openidea_dogfood.app", "Contents", "MacOS", "openidea"))).toBe(true);
} finally {
if (prevInstallDir === undefined) delete process.env.DOGFOOD_INSTALL_DIR;
else process.env.DOGFOOD_INSTALL_DIR = prevInstallDir;
rmSync(tempDir, { recursive: true, force: true });
}
});
});