Skip to content

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

7.8 KB
Raw Blame
import {
  readTranscriptModule,
  readTranscriptSource,
} 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,
  styles,
  hook,
  api,
  toolDetails,
  toolRow,
  filesTab,
  markdown,
] = await Promise.all([
  readTranscriptSource(),
  read("../src/styles/chat-links.css"),
  read("../src/hooks/use-preview-target.ts"),
  read("../src/lib/api.ts"),
  read("../src/components/ToolDetails.tsx"),
  readTranscriptModule("ToolRow.tsx"),
  read("../src/components/workpanel/FilesTab.tsx"),
  read("../src/components/Markdown.tsx"),
]);

test("sent user-message file refs render as composer-like chips", () => {
  assert.match(transcript, /className=\"composer-chip chat-file-chip\"/);
  assert.match(transcript, /function FileRefChip/);
  assert.match(transcript, /segment\.target\.kind === \"file\"/);
  assert.match(transcript, /useOpenChatFileRef/);
  assert.match(transcript, /composer-chip-name/);
  assert.match(styles, /\.chat-file-chip[\s\S]*?appearance: none/);
  assert.match(transcript, /mimeType=\{attachment\.mimeType\}/);
  assert.match(transcript, /onOpen\(path, undefined, mimeType, \{ line, column \}\)/);
});

test("a file chip is routed by where the reference resolved, never optimistically", () => {
  // Completion happens in the main process first, so the click can no longer
  // open a path that does not exist — the empty panel this used to produce is
  // replaced by a report.
  assert.match(hook, /api\.fsResolveRef\(/);
  assert.match(hook, /chat\.fileRefMissing/);
  // A project group can hold several folders (ADR 0249), so the address shape
  // follows the folder that answered: relative for the primary one, absolute
  // for its siblings, and the file view switches to the folder it is given.
  assert.match(hook, /match\.projectRoot \? match\.projectRoot\.primary : true/);
  // An isolated session's worktree is outside the plugin's project roots, so
  // the branch copy travels by its absolute path (issue #1747).
  assert.match(hook, /const isolated = Boolean\(session\?\.worktree\)/);
  assert.match(
    hook,
    /path: primary && !isolated \? match\.relativePath : match\.absolutePath/,
  );
  // Project and scratch HTML run at the exact path resolved by main.
  assert.match(hook, /resolved\.inProject \|\| resolved\.scratch/);
  assert.match(hook, /isHtmlFilePath\(resolved\.absolutePath\)/);
  assert.match(hook, /openUrl\(resolved\.absolutePath\)/);
  // Plain project files prefer the bundled file view. A path:line reference
  // uses the host file tab because the bundled view has no line-navigation API.
  assert.match(hook, /FILE_MANAGER_PLUGIN_TAB/);
  // The resolver may now be handed the caller's options (issue #2113 adds
  // allowDirectory), but resolution still precedes any file-manager load.
  const resolvedRefIndex = hook.indexOf("const resolved = await resolveRef(path, baseDir, options)");
  const livePluginViewIndex = hook.indexOf("ensureFileManagerView(workspacePath)", resolvedRefIndex);
  assert.ok(
    livePluginViewIndex > resolvedRefIndex,
    "actively ensure the bundled file view after async path resolution",
  );
  assert.match(hook, /api\.listPluginViews\(\)/);
  assert.match(hook, /api\.enablePlugin\(fileManager\.id\)/);
  assert.match(hook, /api\.reloadPlugin\(fileManager\.id\)/);
  assert.match(hook, /isActiveInProject\(/);
  assert.doesNotMatch(hook, /const pluginViews = useAppStore\(\(s\) => s\.pluginViews\)/);
  assert.match(hook, /const hasPosition = line !== undefined \|\| column !== undefined/);
  assert.match(hook, /!hasPosition[\s\S]*?isHtmlFilePath/);
  assert.match(hook, /!hasPosition[\s\S]*?ensureFileManagerView/);
  assert.match(hook, /fileManagerPluginTab\(resolved\.path\)/);
  assert.match(filesTab, /data-line=\{i \+ 1\}/);
  assert.match(filesTab, /viewerBodyRef\.current\?\.querySelector/);
  assert.match(filesTab, /if \(!file \|\| selectedLine == null\) return/);
  assert.match(filesTab, /data-line="\$\{selectedLine\}"/);
  assert.match(filesTab, /return \(\) => cancelAnimationFrame\(frame\)/);
  assert.match(filesTab, /selectedLine === undefined/);
  // Session scratch and attachment files live outside the plugin's project
  // roots, and positioned references need the host viewer's line navigation.
  assert.match(hook, /inProject: false/);
  assert.match(hook, /openFile\(resolved\.path, mimeType, \{ line, column \}\)/);
  // Only the explicit default-browser destination may request the OS handoff;
  // an ordinary transcript click keeps its resolved in-app destination.
  const defaultBrowserIndex = hook.indexOf(
    'if (destination === "default-browser")',
  );
  const defaultBrowserBranch = hook.slice(
    defaultBrowserIndex,
    hook.indexOf("const hasPosition", defaultBrowserIndex),
  );
  assert.match(
    defaultBrowserBranch,
    /await api\.fsOpen\(resolved\.absolutePath, mimeType, sessionId\)/,
  );
  assert.doesNotMatch(
    hook.slice(0, defaultBrowserIndex),
    /api\.fsOpen\(/,
  );
  assert.match(
    api,
    /fsOpen: \(path: string, mimeType\?: string, sessionId\?: string\) =>\s*invoke\(IPC\.invoke\.fsOpen, \{ path, mimeType, \.\.\.\(sessionId \? \{ sessionId \} : \{\}\) \}\)/,
  );
});

test("Markdown file anchors use the same project-file opener as inline references", () => {
  const anchorStart = markdown.indexOf("function Anchor(");
  const imageStart = markdown.indexOf("/**\n * Local image references", anchorStart);
  assert.ok(anchorStart >= 0 && imageStart > anchorStart);
  const anchor = markdown.slice(anchorStart, imageStart);
  assert.match(anchor, /handleMarkdownFileLinkClick\(e, href, root, baseDir, openFileRef\)/);
});

test("a tool row and a tool result row open a file where the message body does", () => {
  // One opener serves every transcript surface that names a file. It completes
  // the reference the same way a chat chip does instead of handing the raw path
  // to the host viewer, so a Read/Write/Edit row summary and a Glob/Grep result
  // row land in the bundled file view too (ADR 0262). The call this replaces is
  // the one that let those surfaces pick the destination themselves.
  assert.match(hook, /const openFileRef = useOpenChatFileRef\(\);/);
  assert.match(hook, /target\.kind === "file"[\s\S]*?openFileRef\(target\.path, undefined, undefined, \{/);
  assert.match(hook, /line: target\.line,[\s\S]*?column: target\.column/);
  assert.doesNotMatch(hook, /openFile\(target\.path\)/);
  // Both surfaces still call that opener, and neither reaches the host viewer's
  // store action directly: the tool row summary carries the call's own path,
  // and the result lists carry one entry per file and per matched file.
  assert.match(toolRow, /const openTarget = useOpenPreviewTarget\(\)/);
  assert.match(toolRow, /openTarget\(previewTarget\)/);
  assert.doesNotMatch(toolRow, /openFileInWorkPanel/);
  assert.equal(
    toolDetails.match(/openTarget\(\{ kind: "file", path: rel \}\)/g)?.length,
    2,
  );
  assert.doesNotMatch(toolDetails, /openFileInWorkPanel/);
});

test("user-message bare paths wait for fs/resolveRef before becoming chips", async () => {
  const [verified, files] = await Promise.all([
    read("../src/hooks/use-verified-chat-text.ts"),
    read("../src/lib/verified-chat-files.ts"),
  ]);
  assert.match(transcript, /useVerifiedChatText\(text, attachments\)/);
  assert.match(transcript, /attachments=\{message\.attachments\}/);
  assert.match(verified, /useAppStore\(\(s\) => s\.workspace\?\.path\)/);
  assert.doesNotMatch(verified, /useAppStore\(\(s\) => s\.workspace\)(?!\?)/);
  assert.match(verified, /api\.fsResolveRef\(/);
  assert.match(files, /MAX_MESSAGE_CANDIDATES = 32/);
  assert.match(files, /MAX_CONCURRENT_LOOKUPS = 4/);
  assert.match(files, /!segment\.text\.startsWith\("@\"\)/);
});