Skip to content

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

2.6 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";

import { resolveComposerSmartStop } from "../src/lib/composer-smart-stop.ts";

const message = (role, content = "", thinking) => ({ role, content, thinking });
const draft = {
  text: "inspect these",
  fileReferences: [
    { path: "src/one/index.ts", name: "index.ts" },
    { path: "/tmp/session scratch/index.ts", name: "index.ts" },
  ],
};

test("unanswered stop restores the structured draft and removes its user row", () => {
  const previous = message("assistant", "previous answer");
  const sent = message(
    "user",
    'inspect these\n@src/one/index.ts @"/tmp/session scratch/index.ts"',
  );
  const result = resolveComposerSmartStop(
    [previous, sent],
    { messageCountBeforeSend: 1, draft },
  );

  assert.deepEqual(result, { kind: "restore", kept: [previous], draft });
});

test("a stop before user-message projection still restores only the new draft", () => {
  const messages = [message("user", "older prompt"), message("assistant", "done")];
  const result = resolveComposerSmartStop(messages, {
    messageCountBeforeSend: messages.length,
    draft,
  });

  assert.deepEqual(result, { kind: "restore", kept: messages, draft });
});

test("assistant text, thinking, or a tool row prevents draft restoration", () => {
  const prefix = [message("user", "new prompt")];
  for (const reply of [
    message("assistant", "partial"),
    message("assistant", "", "thinking"),
    message("tool", ""),
  ]) {
    assert.deepEqual(
      resolveComposerSmartStop([...prefix, reply], {
        messageCountBeforeSend: 0,
        draft,
      }),
      { kind: "settle" },
    );
  }
});

test("legacy sends without a snapshot keep the text-only fallback", () => {
  assert.deepEqual(resolveComposerSmartStop([message("user", "plain prompt")]), {
    kind: "restore",
    kept: [],
    draft: { text: "plain prompt", fileReferences: [] },
  });
});

test("a stop without a local snapshot restores the typed form of a rewritten prompt", () => {
  // An `@agent` (or template / Skill) turn stores the model instruction in
  // `content`; the composer must get back what the user typed.
  const sent = {
    role: "user",
    content: "The user explicitly delegated this request with @mentions. Call the `Task` tool…\n\n@explorer find it",
    command: "@explorer find it",
  };
  const result = resolveComposerSmartStop([sent]);
  assert.equal(result.kind, "restore");
  assert.equal(result.draft.text, "@explorer find it");
  assert.equal(resolveComposerSmartStop([message("user", "plain")]).draft.text, "plain");
});