Skip to content

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

13.4 KB
Raw Blame
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { createRequire } from "node:module";
import test from "node:test";

const require = createRequire(import.meta.url);

const packageJson = JSON.parse(
  await readFile(new URL("../package.json", import.meta.url), "utf8"),
);
const sharedPackageJson = JSON.parse(
  await readFile(new URL("../../../packages/shared/package.json", import.meta.url), "utf8"),
);
const dmgBackground = await readFile(
  new URL("../build/dmg-background.png", import.meta.url),
);
const dmgBackgroundRetina = await readFile(
  new URL("../build/[email protected]", import.meta.url),
);
const viteConfigSource = await readFile(
  new URL("../electron.vite.config.ts", import.meta.url),
  "utf8",
);
const preloadSource = await readFile(
  new URL("../electron/preload/index.ts", import.meta.url),
  "utf8",
);
const pluginPanelPreloadSource = await readFile(
  new URL("../electron/preload/plugin-panel.ts", import.meta.url),
  "utf8",
);

test("packaging keeps native voice modules as runtime dependencies", () => {
  assert.deepEqual(Object.keys(packageJson.dependencies).sort(), [
    "@picovoice/pvrecorder-node",
    "electron-updater",
    "transcribe-cpp",
  ]);

  for (const dependency of [
    "@pi-desktop/agent-runtime",
    "@pi-desktop/i18n",
    "@pi-desktop/plugin-sdk",
    "@pi-desktop/shared",
    "mermaid",
    "pinyin-pro",
    "react",
    "shiki",
  ]) {
    assert.ok(
      packageJson.devDependencies[dependency],
      `${dependency} must be available for bundling without shipping its package tree`,
    );
  }
});

test("renderer output keeps its size controls", async () => {
  // electron-vite's renderer preset hard-defaults minify to false, unlike plain
  // Vite, so leaving it implicit ships unminified chunks.
  assert.match(viteConfigSource, /minify:\s*"esbuild"/);

  // The bundled Chromium supports woff2 universally; woff/truetype src entries
  // would be emitted as assets and never served.
  assert.match(viteConfigSource, /dropLegacyFontFallbacks/);
  assert.match(viteConfigSource, /plugins:\s*\[[^\]]*dropLegacyFontFallbacks\(\)/);

  // build/*.png are electron-builder installer icons (1024px+). The renderer
  // must import the downscaled marks instead.
  const brandLogoSource = await readFile(
    new URL("../src/components/BrandLogo.tsx", import.meta.url),
    "utf8",
  );
  assert.match(brandLogoSource, /"\.\.\/assets\/brand\/logo-light\.png"/);
  assert.match(brandLogoSource, /"\.\.\/assets\/brand\/logo-dark\.png"/);
  assert.doesNotMatch(brandLogoSource, /\.\.\/\.\.\/build\//);
});

test("legacy font fallback stripping only removes redundant fallback sources", () => {
  // Mirror of the pi-drop-legacy-font-fallbacks regex in electron.vite.config.ts.
  const pattern = new RegExp(
    viteConfigSource.match(/code\.replace\(\s*(\/[^\n]+\/g)/)[1].slice(1, -2),
    "g",
  );
  const strip = (css) => css.replace(pattern, "");

  // A KaTeX-shaped face keeps only its woff2 source.
  assert.equal(
    strip('src:url(a.woff2) format("woff2"),url(a.woff) format("woff"),url(a.ttf) format("truetype");'),
    'src:url(a.woff2) format("woff2");',
  );

  // A variable face declaring only woff2-variations must survive untouched.
  const variations = 'src: url("../f.woff2") format("woff2-variations");';
  assert.equal(strip(variations), variations);

  // A face whose only source is a legacy format would otherwise lose every
  // source; the leading comma in the pattern is what protects it.
  for (const soleSource of [
    'src: url(only.woff) format("woff");',
    'src: url(only.ttf) format("truetype");',
  ]) {
    assert.equal(strip(soleSource), soleSource);
  }

  // Stripping must never leave a dangling comma or an empty declaration.
  for (const css of [
    'src:url(a.woff2) format("woff2"),url(a.woff) format("woff");',
    'src:url(a.woff2) format("woff2"),url("data:font/woff;base64,AA)BB") format("woff");',
  ]) {
    const out = strip(css);
    assert.doesNotMatch(out, /,\s*;/);
    assert.doesNotMatch(out, /src:\s*;/);
  }
});

test("main bundles JavaScript dependencies and externalizes runtime modules", () => {
  assert.doesNotMatch(viteConfigSource, /externalizeDepsPlugin\s*\(/);
  // jiti is listed so the trusted-extension loader's lazy import never
  // enters the main bundle; main itself never loads it (spec 16 §4.2).
  assert.match(viteConfigSource, /external:\s*\[/);
  for (const moduleName of ["@picovoice/pvrecorder-node", "transcribe-cpp"]) {
    assert.match(viteConfigSource, new RegExp(`"${moduleName.replaceAll("/", "\\/")}"`));
  }
  assert.doesNotMatch(viteConfigSource, /node-pty/);
  assert.doesNotMatch(JSON.stringify(packageJson.dependencies), /node-pty/);
});

test("sandbox preload entries use standalone shared subpath bundles", () => {
  const sharedExports = sharedPackageJson.exports ?? {};

  assert.match(preloadSource, /from "@pi-desktop\/shared\/protocol"/);
  assert.match(pluginPanelPreloadSource, /from "@pi-desktop\/shared\/theme"/);
  assert.ok(sharedExports["./protocol"], "protocol must be available as a shared subpath");
  assert.ok(sharedExports["./theme"], "theme must be available as a shared subpath");
  assert.doesNotMatch(preloadSource, /from "@pi-desktop\/shared"/);
  assert.doesNotMatch(pluginPanelPreloadSource, /from "@pi-desktop\/shared"/);
});

test("packaging keeps only shipped locales and excludes non-runtime artifacts", () => {
  assert.deepEqual(packageJson.build.electronLanguages, [
    "en-US",
    "zh-CN",
    // electron-builder uses underscore locale directories in macOS bundles.
    "zh_CN",
    "zh-TW",
    "zh_TW",
    "tr",
    "de",
    "es",
    "fr",
    "ko",
    "pt-BR",
    "pt_BR",
  ]);
  assert.ok(packageJson.build.files.includes("!**/*.map"));
  assert.ok(
    packageJson.build.files.includes(
      "!**/node_modules/*/{test,tests,__tests__,powered-test,example,examples}/**",
    ),
  );
  assert.ok(
    packageJson.build.files.includes(
      "!**/node_modules/**/*.{test,spec}.{js,cjs,mjs}",
    ),
  );
  assert.ok(
    packageJson.build.files.includes(
      "!**/node_modules/node-addon-api/tools/**",
    ),
  );
  assert.ok(
    packageJson.build.files.includes(
      "!**/node_modules/node-addon-api/*.{c,gyp,gypi,h,js,json}",
    ),
  );
  assert.ok(
    packageJson.build.files.includes(
      "!**/node_modules/node-addon-api/README.md",
    ),
  );
  assert.ok(
    !packageJson.build.files.includes("!**/node_modules/node-addon-api/**"),
    "node-addon-api license must not be removed with its build-only files",
  );
  assert.ok(
    packageJson.build.files.every(
      (pattern) => !/LICENSE|NOTICE|\*\.md/.test(pattern),
    ),
    "third-party license and notice files must remain packageable",
  );
  // The agent-runtime dist-bundle mapping must remain a directory copy: the
  // bundle emits dist-bundle/package.json with "type":"module" so the ESM
  // sidecar.js loads in packaged installs (issue #507). Contract tests live
  // in agent-runtime-bundle-package.test.mjs.
  assert.deepEqual(packageJson.build.extraResources, [
    {
      from: "build/icon.png",
      to: "tray-icon.png",
    },
    {
      from: "../../packages/agent-runtime/dist-bundle",
      to: "agent-runtime",
    },
    // Built-in skills stay outside the asar so they read as plain files.
    {
      from: "resources/skills",
      to: "skills",
    },
    // The inline visualization kit (stylesheet, runtime, pinned libraries)
    // is read from disk by the pi-genui: protocol handler.
    {
      from: "resources/gen-ui",
      to: "gen-ui",
    },
    // Bundled first-party plugins, for the same reason: host-core reads their
    // manifests from disk and the views are loaded as file:// pages (ADR 0105).
    {
      from: "resources/plugins",
      to: "plugins",
    },
    {
      from: "resources/models.dev",
      to: "models.dev",
    },
  ]);
  assert.doesNotMatch(JSON.stringify(packageJson.build), /node-pty/);
});

test("macOS targets follow the native architecture selected by the runner", () => {
  const macTargets = packageJson.build.mac.target;
  assert.deepEqual(
    macTargets.map((entry) => entry.target),
    ["dmg", "zip"],
  );
  assert.ok(
    macTargets.every((entry) => entry.arch === undefined),
    "macOS targets must not pin the package to Apple Silicon",
  );
  assert.doesNotMatch(packageJson.scripts["dist:mac"], /--(?:arm64|x64)/);
});

test("macOS DMG remains a two-icon install and artifacts omit first-launch guidance", () => {
  assert.equal(packageJson.build.mac.extraDistFiles, undefined);
  assert.doesNotMatch(
    JSON.stringify(packageJson.build),
    /PI-Desktop-macOS-open\.command|PI-Desktop-macOS-opening-help\.txt/,
    "macOS package configuration must not ship first-launch guidance assets",
  );
  assert.equal(packageJson.build.dmg.background, "build/dmg-background.png");
  assert.equal(packageJson.build.dmg.icon, "build/icon.icns");
  assert.deepEqual(packageJson.build.dmg.window, { width: 720, height: 440 });
  assert.equal(packageJson.build.dmg.iconSize, 128);
  assert.equal(packageJson.build.dmg.iconTextSize, 12);
  assert.deepEqual(packageJson.build.dmg.contents, [
    { x: 180, y: 196 },
    { x: 540, y: 196, type: "link", path: "/Applications" },
  ]);
  assert.doesNotMatch(
    JSON.stringify(packageJson.build.dmg.contents),
    /PI-Desktop-macOS-open\.command|Open PI-Desktop\.command|opening-help|If app won't open/,
    "the DMG must not expose first-launch guidance assets",
  );
  assert.deepEqual([...dmgBackground.subarray(0, 8)], [
    137, 80, 78, 71, 13, 10, 26, 10,
  ]);
  assert.equal(dmgBackground.readUInt32BE(16), 720);
  assert.equal(dmgBackground.readUInt32BE(20), 440);
  assert.deepEqual([...dmgBackgroundRetina.subarray(0, 8)], [
    137, 80, 78, 71, 13, 10, 26, 10,
  ]);
  assert.equal(dmgBackgroundRetina.readUInt32BE(16), 1440);
  assert.equal(dmgBackgroundRetina.readUInt32BE(20), 880);
});

test("packaging keeps voice native payloads unpacked and excludes removed PTY payloads", () => {
  assert.deepEqual(packageJson.build.asar, { smartUnpack: false });
  assert.deepEqual(packageJson.build.asarUnpack, [
    "node_modules/transcribe-cpp/**/*.node",
    "node_modules/transcribe-cpp/**/bin/**",
    // koffi hands the provider's library path straight to dlopen/LoadLibrary,
    // which cannot read inside app.asar (see transcribe-native-library.ts).
    "node_modules/@transcribe-cpp/**",
    "node_modules/@koromix/**/*.node",
    "node_modules/@picovoice/pvrecorder-node/**/*.node",
    "node_modules/@picovoice/pvrecorder-node/**/lib/**",
  ]);
  assert.doesNotMatch(JSON.stringify(packageJson.build.files), /node-pty/);
  assert.doesNotMatch(JSON.stringify(packageJson.build.extraResources), /node-pty/);
});

test("packaging ships only the target platform's voice native payloads", () => {
  // electron-builder merges platform-level `files` into a matcher that holds
  // only exclusions and then includes the whole app directory, so per-target
  // filtering lives in the root list through the `${os}` macro (mac | win |
  // linux). Patterns are evaluated in order, like electron-builder's filter.
  const builderRequire = createRequire(require.resolve("electron-builder"));
  const { Minimatch } = createRequire(builderRequire.resolve("app-builder-lib"))("minimatch");
  const recorderPatterns = packageJson.build.files.filter((pattern) =>
    pattern.startsWith("!**/node_modules/@picovoice/pvrecorder-node/") && pattern.includes("${os}"),
  );
  assert.equal(recorderPatterns.length, 2);
  const libDirs = ["mac/arm64", "mac/x86_64", "windows/amd64", "windows/arm64", "linux/x86_64",
    "raspberry-pi/cortex-a72-aarch64"];
  const shipped = (os) => {
    const matchers = ["**/*", ...recorderPatterns].map(
      (pattern) => new Minimatch(pattern.replaceAll("${os}", os), { dot: true }),
    );
    return libDirs.filter((dir) => {
      const file = `node_modules/@picovoice/pvrecorder-node/lib/${dir}/pv_recorder.node`;
      let match = false;
      for (const matcher of matchers) {
        if (match !== matcher.negate) continue;
        match = matcher.match(file);
      }
      return match;
    });
  };
  assert.deepEqual(shipped("mac"), ["mac/arm64", "mac/x86_64"]);
  assert.deepEqual(shipped("win"), ["windows/amd64", "windows/arm64"]);
  // Linux arm64 loads the Raspberry Pi builds (pvrecorder's own platform map).
  assert.deepEqual(shipped("linux"), ["linux/x86_64", "raspberry-pi/cortex-a72-aarch64"]);

  for (const platform of ["mac", "win", "linux"]) {
    assert.equal(packageJson.build[platform].files, undefined, `${platform}.files would ship src/`);
  }
  for (const pattern of [
    "!**/node_modules/koffi/{doc,vendor}/**",
    "!**/node_modules/@picovoice/pvrecorder-node/{src/**,copy.js,jest.config.js,tsconfig.json}",
  ]) {
    assert.ok(packageJson.build.files.includes(pattern), `${pattern} must stay excluded`);
  }
  // koffi's JS entry imports src/koffi/src/static.js at runtime.
  assert.ok(packageJson.build.files.every((pattern) => !pattern.includes("koffi/src")));
});

test("main output is minified without renaming and shares one Pi SDK copy", () => {
  const mainConfig = viteConfigSource.slice(
    viteConfigSource.indexOf("  main: {"),
    viteConfigSource.indexOf("  preload: {"),
  );
  assert.match(mainConfig, /minify:\s*"esbuild"/);
  // Packaged builds ship no source maps; identifiers keep stack traces readable.
  assert.match(mainConfig, /minifyIdentifiers:\s*false/);
  assert.match(mainConfig, /plugins:\s*\[dedupeMainPackages\(\)\]/);
  assert.match(
    viteConfigSource,
    /MAIN_SHARED_PACKAGES = \["@earendil-works\/pi-ai", "typebox"\]/,
  );
});