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 { readTranscriptSource, readMainSource } from "./helpers/source-contracts.mjs";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

const read = (path) => readFile(new URL(path, import.meta.url), "utf8");

const [transcript, markdown, api, main, panel, protocol, hook] = await Promise.all([
  readTranscriptSource(),
  read("../src/components/Markdown.tsx"),
  read("../src/lib/api.ts"),
  readMainSource(),
  read("../../../packages/host-runtime/src/workspace-files.ts"),
  read("../../../packages/shared/src/protocol.ts"),
  read("../src/lib/use-referenced-image-data-url.ts"),
]);

test("in-chat image display has a contained renderer-to-main bridge", () => {
  assert.match(protocol, /fsReadImageDataUrl: "pi-desktop\/fs\/readImageDataUrl"/);
  assert.match(api, /fsReadImageDataUrl: \(ref: string, mimeType\?: string, sessionId\?: string\)/);
  assert.match(main, /IPC\.invoke\.fsReadImageDataUrl/);
  assert.match(main, /readOpenableImage\(/);
  assert.match(panel, /export async function readOpenableImage\(/);
  assert.match(panel, /isAttachmentBlobRef/);
  assert.match(panel, /ALLOWED_IMAGE_MIME/);
  assert.match(panel, /MAX_IMAGE_BYTES/);
  assert.doesNotMatch(panel, /info\.isFile\(\) \? trimmed : null/);
});

test("renderer hook loads referenced image data URLs with a scoped bounded cache", () => {
  assert.match(hook, /useReferencedImageDataUrl\(/);
  assert.match(hook, /dataUrlCache = new Map<string, string>\(\)/);
  assert.match(hook, /DATA_URL_CACHE_ENTRIES/);
  assert.match(hook, /DATA_URL_CACHE_MAX_BYTES/);
  assert.match(hook, /fsReadImageDataUrl\(normalizedRef, mimeType, sessionId\)/);
  assert.match(hook, /resolved\.key === requestedKey/);
  assert.match(hook, /result\.kind === "image" && result\.dataUrl/);
  assert.match(hook, /https\?\|data\|blob/);
});

test("user message image attachments render as chips with a hover card", () => {
  assert.match(transcript, /function MessageAttachmentImage\(/);
  assert.match(transcript, /useReferencedImageDataUrl\(attachment\.ref, attachment\.mimeType\)/);
  assert.match(transcript, /className="message-attachment-image-chip"/);
  assert.match(transcript, /role="listitem"/);
  assert.match(transcript, /onOpen=\{onOpenFile\}/);
  // One shared preview card serves the draft and the transcript.
  assert.match(
    transcript,
    /import \{ ImageHoverCard, type ImageHoverAnchor \} from "\.\.\/\.\.\/\.\.\/components\/ImageHoverCard";/,
  );
  assert.match(transcript, /<ImageHoverCard src=\{dataUrl\} anchor=\{anchor\} onDismiss=\{dismiss\} \/>/);
  assert.match(
    transcript,
    /attachment\.kind === "image" \?/,
  );
});

test("local markdown images render inline with a chip fallback", () => {
  assert.match(markdown, /useReferencedImageDataUrl\(isRemote \? null : localRef\)/);
  assert.match(markdown, /className="chat-image-local"/);
  assert.match(markdown, /className="chat-image-chip"/);
  assert.match(markdown, /attachmentRef/);
});