Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

2.9 KB
Raw Blame
import assert from "node:assert/strict";
import { execFileSync, spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";

const script = fileURLToPath(new URL("./reuse-worktree-deps.mjs", import.meta.url));
test("reuse refuses incompatible or existing installs and resolves internal packages locally", t => {
  const tmp = mkdtempSync(join(tmpdir(), "pi-reuse-"));
  t.after(() => rmSync(tmp, { recursive: true, force: true }));
  const main = join(tmp, "main");
  const wt = join(tmp, "task");
  mkdirSync(join(main, "packages/shared"), { recursive: true });
  const git = (...args) => execFileSync("git", ["-C", main, ...args], { stdio: "pipe" });
  const run = cwd => spawnSync(process.execPath, [script], { cwd, encoding: "utf8" });
  const manifest = '{"name":"@pi-desktop/shared"}';
  writeFileSync(join(main, "packages/shared/package.json"), manifest);
  writeFileSync(join(main, "pnpm-lock.yaml"), "lock");
  git("init");
  git("add", "packages/shared/package.json", "pnpm-lock.yaml");
  git("-c", "user.name=Test", "-c", "[email protected]", "-c", "commit.gpgsign=false", "commit", "-m", "Fixture");
  git("worktree", "add", "--detach", wt, "HEAD");
  for (const path of [".pnpm/react", "@pi-desktop", ".bin"]) mkdirSync(join(main, "node_modules", path), { recursive: true });
  writeFileSync(join(main, "node_modules/.bin/tool"), "original shim");
  writeFileSync(join(main, "node_modules/.pnpm/lock.yaml"), "lock");
  const kind = process.platform === "win32" ? "junction" : "dir";
  symlinkSync(join(main, "packages/shared"), join(main, "node_modules/@pi-desktop/shared"), kind);
  symlinkSync(join(main, "node_modules/.pnpm/react"), join(main, "node_modules/react"), kind);
  assert.match(run(main).stderr, /dedicated worktree/);
  writeFileSync(join(wt, "pnpm-lock.yaml"), "different");
  assert.match(run(wt).stderr, /Different pnpm-lock/);
  writeFileSync(join(wt, "pnpm-lock.yaml"), "lock");
  writeFileSync(join(main, "node_modules/.pnpm/lock.yaml"), "old");
  assert.match(run(wt).stderr, /Installed dependencies are stale/);
  writeFileSync(join(main, "node_modules/.pnpm/lock.yaml"), "lock");
  const result = run(wt);
  assert.equal(result.status, 0, result.stderr);
  assert.equal(realpathSync.native(join(wt, "node_modules/react")), realpathSync.native(join(main, "node_modules/react")));
  assert.equal(realpathSync.native(join(wt, "node_modules/@pi-desktop/shared")), realpathSync.native(join(wt, "packages/shared")));
  const localShim = join(wt, "node_modules/.bin/tool");
  assert.equal(readFileSync(localShim, "utf8"), "original shim");
  writeFileSync(localShim, "regenerated shim");
  assert.equal(readFileSync(join(main, "node_modules/.bin/tool"), "utf8"), "original shim");
  assert.match(run(wt).stderr, /Refusing to overwrite/);
});