Skip to content

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

7.0 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";
import { register } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";

const here = dirname(fileURLToPath(import.meta.url));
register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));

const {
  archFromUname,
  checksumMatches,
  isPublishedTarget,
  normalizeChecksum,
  parseChecksumFile,
  parseUnameOutput,
  piHostArtifactName,
  piHostArtifactUrls,
  piHostBundleDir,
  platformFromUname,
  resolveTarget,
  targetKey,
  versionsMatch,
} = await import("../electron/main/remote/pi-host-release.ts");

const VERSION = "0.15.1-beta.5";
const LINUX_X64 = { platform: "linux", arch: "x64" };
const ARTIFACT_NAME = "pi-host-0.15.1-beta.5-linux-x64.tar.gz";
const RELEASE_BASE = "https://github.com/vastsa/PI-Desktop/releases/download/v0.15.1-beta.5";
/** 64 hex characters, so it passes `normalizeChecksum`. */
const DIGEST = "0123456789abcdef".repeat(4);
/** Same length as {@link DIGEST}, one nibble different: a tampered download. */
const TAMPERED = `${DIGEST.slice(0, -1)}${DIGEST.endsWith("f") ? "e" : "f"}`;

test("artifact name and URLs follow the release tag layout the desktop pins", () => {
  assert.equal(piHostBundleDir(VERSION, LINUX_X64), "pi-host-0.15.1-beta.5-linux-x64");
  assert.equal(piHostArtifactName(VERSION, LINUX_X64), ARTIFACT_NAME);
  const urls = piHostArtifactUrls(VERSION, LINUX_X64);
  // Both files are uploaded to the same v-tagged release by `release.yml`, so
  // the desktop can derive them without an API call.
  assert.equal(urls.tarball, `${RELEASE_BASE}/${ARTIFACT_NAME}`);
  assert.equal(urls.checksum, `${RELEASE_BASE}/${ARTIFACT_NAME}.sha256`);
});

test("resolveTarget maps uname output to a release target", () => {
  assert.deepEqual(parseUnameOutput("Linux\nx86_64\n"), { systemName: "Linux", machine: "x86_64" });
  // The remote probe answers `uname -s && uname -m`; anything past the second
  // line (a login banner, a stray echo) must not change the target.
  assert.deepEqual(resolveTarget("Linux\nx86_64\nextra noise"), { platform: "linux", arch: "x64" });
  assert.deepEqual(resolveTarget(" Darwin \n aarch64 \n"), { platform: "darwin", arch: "arm64" });
  assert.deepEqual(resolveTarget("darwin\narm64"), { platform: "darwin", arch: "arm64" });
  // `amd64` is what Linux uname reports for the same architecture as x86_64.
  assert.deepEqual(resolveTarget("linux\namd64"), { platform: "linux", arch: "x64" });
});

test("resolveTarget refuses a platform or architecture the release cannot serve", () => {
  assert.equal(platformFromUname("FreeBSD"), null);
  assert.equal(archFromUname("riscv64"), null);
  // A host that answered only half the probe must fail closed rather than
  // falling back to the desktop's own target.
  assert.equal(resolveTarget("Linux\nriscv64\n"), null);
  assert.equal(resolveTarget("FreeBSD\nx86_64\n"), null);
  assert.equal(resolveTarget("Linux"), null);
  assert.equal(resolveTarget(""), null);
});

test("isPublishedTarget matches the release matrix's Linux architectures", () => {
  assert.equal(targetKey(LINUX_X64), "linux-x64");
  assert.equal(isPublishedTarget(LINUX_X64), true);
  // The desktop matrix publishes Linux x64 and arm64, and release.yml's
  // pi-host-bundle job builds one bundle per lane.
  assert.equal(isPublishedTarget({ platform: "linux", arch: "arm64" }), true);
  // Publishing parity: a 404 halfway through a download is what this guard
  // exists to prevent, so every unpublished cell must be refused here.
  assert.equal(isPublishedTarget({ platform: "darwin", arch: "x64" }), false);
  assert.equal(isPublishedTarget({ platform: "darwin", arch: "arm64" }), false);
});

test("parseChecksumFile reads the digest of its own artifact from a sha256sum file", () => {
  assert.equal(parseChecksumFile(`${DIGEST}  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME), DIGEST);
  // Upper-case digests appear in hand-pasted files; they are the same digest.
  assert.equal(parseChecksumFile(`${DIGEST.toUpperCase()}  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME), DIGEST);
  // Binary mode marks the name with `*`, and some tools write a path.
  assert.equal(parseChecksumFile(`${DIGEST} *${ARTIFACT_NAME}\n`, ARTIFACT_NAME), DIGEST);
  assert.equal(parseChecksumFile(`${DIGEST}  /tmp/build/${ARTIFACT_NAME}\n`, ARTIFACT_NAME), DIGEST);
  // A digest published for a different artifact (or a different version) must
  // never be trusted for this download.
  assert.equal(parseChecksumFile(`${DIGEST}  pi-host-0.15.0-linux-x64.tar.gz\n`, ARTIFACT_NAME), null);
  // Garbage before the real line is tolerated; the digest is still found.
  assert.equal(parseChecksumFile(`# generated by release.sh\n\n${DIGEST}  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME), DIGEST);
});

test("parseChecksumFile rejects anything that is not a full sha256 digest", () => {
  assert.equal(parseChecksumFile(`${DIGEST.slice(0, 63)}  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME), null, "63 hex chars");
  assert.equal(parseChecksumFile(`${DIGEST}0  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME), null, "65 hex chars");
  assert.equal(
    parseChecksumFile(`${DIGEST.slice(0, 60)}zzzz  ${ARTIFACT_NAME}\n`, ARTIFACT_NAME),
    null,
    "non-hex characters",
  );
  assert.equal(parseChecksumFile("", ARTIFACT_NAME), null);
  assert.equal(parseChecksumFile("\n\n", ARTIFACT_NAME), null);
});

test("normalizeChecksum accepts only a trimmed, lower-case 64-hex digest", () => {
  assert.equal(normalizeChecksum(`  ${DIGEST.toUpperCase()}\n`), DIGEST);
  assert.equal(normalizeChecksum(DIGEST.slice(0, 63)), null);
  assert.equal(normalizeChecksum(`${DIGEST}0`), null);
  assert.equal(normalizeChecksum(`${DIGEST.slice(0, 63)}g`), null);
  assert.equal(normalizeChecksum(""), null);
});

test("checksumMatches compares digests case-insensitively and refuses junk", () => {
  assert.equal(checksumMatches(DIGEST, DIGEST), true);
  assert.equal(checksumMatches(DIGEST.toUpperCase(), DIGEST), true);
  assert.equal(checksumMatches(DIGEST, DIGEST.toUpperCase()), true);
  // One differing nibble, a length mismatch, and a non-hex value are all false
  // rather than a throw: the caller turns false into a typed bootstrap failure.
  assert.equal(checksumMatches(TAMPERED, DIGEST), false);
  assert.equal(checksumMatches(DIGEST.slice(0, 63), DIGEST), false);
  assert.equal(checksumMatches("not-a-digest", DIGEST), false);
  assert.equal(checksumMatches("", ""), false);
});

test("versionsMatch requires the same release, tolerating a v prefix on either side", () => {
  assert.equal(versionsMatch("0.15.1-beta.5", "0.15.1-beta.5"), true);
  // The tag carries the `v` and `APP_VERSION` does not; either side may have it.
  assert.equal(versionsMatch("v0.15.1-beta.5", "0.15.1-beta.5"), true);
  assert.equal(versionsMatch("0.15.1-beta.5", "v0.15.1-beta.5"), true);
  assert.equal(versionsMatch(" v0.15.1-beta.5 ", "0.15.1-beta.5"), true);
  assert.equal(versionsMatch("0.15.2", "0.15.1"), false);
  assert.equal(versionsMatch("0.15.1-beta.4", "0.15.1-beta.5"), false);
  assert.equal(versionsMatch("", ""), false);
  assert.equal(versionsMatch("", "0.15.1"), false);
  assert.equal(versionsMatch("v", ""), false);
});