Skip to content

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

5.7 KB
Raw Blame
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { readComposerSource } from "./helpers/composer-source.mjs";

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

const [autocomplete, autocompleteHook, autocompleteStyles, composer, composerStyles] =
  await Promise.all([
    read("../src/components/ComposerAutocomplete.tsx"),
    read("../src/hooks/use-composer-autocomplete.ts"),
    read("../src/styles/composer-autocomplete.css"),
    readComposerSource(),
    read("../src/styles/composer.css"),
  ]);

test("file autocomplete rows show the leaf name with the full relative path", () => {
  assert.match(
    autocomplete,
    /const displayName = `\$\{name\}\$\{isDir \? "\/" : ""\}`/,
  );
  assert.match(autocomplete, /className="composer-ac-name">\{displayName\}/);
  // The trailing path span always carries the entry's full workspace-relative path.
  assert.match(autocomplete, /className="composer-ac-path">\{item\.entry\.path\}/);
  assert.match(autocompleteStyles, /\.composer-ac-path\s*\{[^{}]*flex: 1 1 0/);
  assert.match(
    autocompleteStyles,
    /\.composer-ac-path\s*\{[^{}]*text-overflow: ellipsis/,
  );
});

test("accepted files become compact references while directories keep completion", () => {
  assert.match(autocomplete, /title=\{item\.entry\.path\}/);
  assert.match(autocomplete, /aria-label=\{item\.entry\.path\}/);
  assert.match(
    autocompleteHook,
    /item\.kind === "path" && item\.entry\.kind === "file"/,
  );
  assert.match(autocompleteHook, /\.\.\.applyCompletion\(value, trigger, ""\)/);
  assert.match(autocompleteHook, /path: item\.entry\.path/);
  assert.match(
    autocompleteHook,
    /formatFileInsert\(item\.entry\.path, item\.entry\.kind\)/,
  );
  // Enter/Tab must splice a sentinel into the draft. A token-less reference
  // no longer paints after chips moved inline, so the @ token just vanished.
  assert.match(
    composer,
    /const token = nextChipToken\(\);[\s\S]*?result\.value\.slice\(0, result\.cursor\) \+ token/,
  );
  assert.match(
    composer,
    /createFileReference\(\s*acceptedFileReference\.path,[\s\S]*?token,/,
  );
  assert.match(composer, /applyEditorDraft\(\s*nextText,/);
  // Workspace switches still drop relative `@` chips, not every token-backed
  // chip — paste/scratch paths are absolute, plugin marks carry no path, and a
  // session reference names a conversation, so all three must survive.
  assert.match(composer, /function isPersistedScratchReference\(path: string\)/);
  assert.match(
    composer,
    /Boolean\(fileReference\.plugin\) \|\|[\s\S]*?fileReference\.kind === "session" \|\|[\s\S]*?isPersistedScratchReference\(fileReference\.path\)/,
  );
  assert.match(composer, /kept = current\.filter\(survives\)/);
  assert.doesNotMatch(
    composer,
    /current\.filter\(\(fileReference\) => Boolean\(fileReference\.token\)\)/,
  );
});

test("composer renders atomic inline chips and serializes paths on send", () => {
  // Chips are atomic non-editable elements inside the contenteditable draft,
  // one per sentinel token, with an ellipsized leaf name.
  assert.match(composer, /className = "composer-chip"/);
  assert.match(composer, /chip\.contentEditable = "false"/);
  assert.match(composer, /chip\.dataset\.token = token/);
  assert.match(composer, /composer-chip-name/);
  assert.match(composer, /const label = reference\.lineRange \? fileLineLabel\(reference\.name, reference\.lineRange\) : reference\.name/);
  assert.match(composer, /nameSpan\.textContent = label/);
  // A file chip's title is its path; a plugin mark's is its plugin.
  assert.match(
    composer,
    /isPluginMark\(reference\)\s*\? \(reference\.plugin\?\.pluginId \?\? ""\)\s*: composerMentionText\(reference\) \?\? reference\.path/,
  );
  assert.match(composer, /chip\.title = origin/);
  assert.match(
    composer,
    /serializeComposerFileReferences\(rawText, activeFileReferences\)/,
  );
  assert.match(
    composer,
    /sendPrompt\(\s*inlineContent,\s*submittedDraft,\s*activeSessionId \?\? undefined,\s*captureAcceptedSession,\s*\)/,
  );
  assert.match(composer, /serializeInlineComposerFileReferences\(/);
  assert.match(composer, /current\.filter\(/);
  assert.match(
    composerStyles,
    /\.composer-chip-name[\s\S]*?text-overflow: ellipsis/,
  );
});

test("text file chips expand into editable draft text", () => {
  assert.match(
    composer,
    /function isEditableTextReference\(reference: ComposerFileReference\)/,
  );
  assert.match(composer, /\? \(\) => onExpandText\(token\)/);
  assert.match(composer, /event\.key !== "Enter" && event\.key !== " "/);
  assert.match(composer, /const result = await api\.fsRead\(reference\.path, reference\.mimeType, sourceSessionId\)/);
  assert.match(composer, /result\.kind !== "text" \|\| result\.content === undefined/);
  assert.match(
    composer,
    /source\.slice\(0, index\) \+ result\.content \+ source\.slice\(index \+ token\.length\)/,
  );
  assert.match(
    composer,
    /fileReferencesRef\.current\.filter\([\s\S]*?fileReference\.token !== token/,
  );
  assert.match(composer, /applyEditorDraft\(nextText, nextReferences, index \+ result\.content\.length\)/);
  assert.match(
    composerStyles,
    /composer-chip\[data-action="expand-text-reference"\]:focus-visible/,
  );
});

test("unanswered stop restores compact references instead of serialized paths", () => {
  assert.match(composer, /setValue\(prefilled\.text\)/);
  assert.match(composer, /composerPrefill\.fileReferences\.map/);
  assert.match(composer, /composerPrefill\.sessionId !== activeSessionId/);
  assert.match(
    composer,
    /createFileReferenceFromSnapshot\(fileReference,\s*composerPrefill\.sessionId\)/,
  );
  assert.doesNotMatch(composer, /setValue\(composerPrefill\);/);
});