Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

56.7 KB
Raw Blame
const test = require("node:test");
const assert = require("node:assert/strict");
const Module = require("node:module");
const { EventEmitter } = require("node:events");
const childProcess = require("node:child_process");

const selectionManagerPath = require.resolve("../../src/helpers/selectionManager");
// selectionManager requires "./debugLogger" at load; the stub records every
// line so tests can assert what a declined paste leaves in the debug log.
const logged = [];
const debugLoggerStub = {
  debug: (message, meta, scope) => logged.push({ level: "debug", message, meta, scope }),
  info: (message, meta, scope) => logged.push({ level: "info", message, meta, scope }),
  warn: (message, meta, scope) => logged.push({ level: "warn", message, meta, scope }),
  trace: () => {},
  log: () => {},
  error: () => {},
};
const declines = () =>
  logged.filter((entry) => entry.message === "Assistant response paste declined");

const originalLoad = Module._load;

function loadSelectionManager({ spawn, execFile } = {}) {
  delete require.cache[selectionManagerPath];
  Module._load = function loadWithElectronMock(request, parent, isMain) {
    if (request === "electron") {
      return { clipboard: { readText: () => "", writeText: () => {} } };
    }
    if (request === "./debugLogger") {
      return debugLoggerStub;
    }
    if (request === "child_process" && (spawn || execFile)) {
      return { ...childProcess, ...(spawn && { spawn }), ...(execFile && { execFile }) };
    }
    return originalLoad.call(this, request, parent, isMain);
  };
  try {
    return require("../../src/helpers/selectionManager");
  } finally {
    Module._load = originalLoad;
  }
}

const SelectionManager = loadSelectionManager();

function makeHarness({
  selections = ["original"],
  now = () => 1000,
  pasteResult = { restoreComplete: Promise.resolve() },
} = {}) {
  const reads = [...selections];
  const pastes = [];
  const textEditMonitor = {
    lastTargetPid: 42,
    activatePid: async (pid) => pid === 42,
    getSelectedText: async () => {
      const value = reads.shift();
      if (value === undefined) return { state: "unavailable" };
      if (value === null) return { state: "none" };
      if (typeof value === "object") return value;
      return { state: "selected", text: value };
    },
  };
  const clipboardManager = {
    runClipboardOperation: (operation) => operation(),
    _pasteText: async (text, options) => {
      pastes.push({ text, options });
      return pasteResult;
    },
  };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor,
    platform: "darwin",
    now,
  });
  return { manager, pastes };
}

test("captures an exact selection in an opaque session", async () => {
  const { manager } = makeHarness({ selections: ["first\nsecond 😀"] });
  const result = await manager.captureSelectedText();

  assert.equal(result.status, "selected");
  assert.equal(result.text, "first\nsecond 😀");
  assert.equal(result.characterCount, 14);
  assert.ok(result.sessionId);
});

test("captures a verified writable caret in an opaque delivery session", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  const result = await manager.captureSelectedText({ probeEditable: true });

  assert.equal(result.status, "editable");
  assert.ok(result.sessionId);
});

test("the editable probe only runs when the capture requests it", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  manager._isTerminalPid = async () => {
    throw new Error("must not resolve terminal identity without probeEditable");
  };

  assert.equal((await manager.captureSelectedText()).status, "none");
});

test("pastes an assistant response only while the captured caret target is still editable", async () => {
  const { manager, pastes } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "none", editable: true },
    ],
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });
  const result = await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response", {
    restoreClipboard: true,
  });

  assert.deepEqual(result, { success: true });
  assert.equal(pastes.length, 1);
  assert.equal(pastes[0].text, "Agent response");
  assert.equal(pastes[0].options.restoreClipboard, true);
});

test("does not paste an assistant response after the captured caret target changes", async () => {
  const { manager, pastes } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "selected", text: "new selection" },
    ],
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });

  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: false,
    code: "target_changed",
  });
  assert.equal(pastes.length, 0);
});

// Generated text pasted into a shell executes on its embedded newlines, so a
// terminal's empty prompt must never read as a writable caret.
test("a macOS terminal's empty prompt never becomes a caret delivery target", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  manager.clipboardManager.isTerminalSignature = (signature) =>
    signature.toLowerCase().includes("iterm");
  manager._readExecutablePath = async () => "/Applications/iTerm.app/Contents/MacOS/iTerm2";

  assert.equal((await manager.captureSelectedText({ probeEditable: true })).status, "none");
});

test("a terminal-flagged target is refused as a caret destination without probing", async () => {
  const probes = [];
  const manager = new SelectionManager({
    clipboardManager: {
      isTerminalSignature: (signature) => signature.toLowerCase().includes("terminal"),
    },
    textEditMonitor: {
      isFocusedEditable: async (target) => {
        probes.push(target);
        return "editable";
      },
    },
    platform: "win32",
    now: () => 1000,
  });
  const flagged = { kind: "win-hwnd", id: "1A", isTerminal: true };
  const byExeName = { kind: "win-hwnd", id: "2B", exeName: "WindowsTerminal.exe" };
  const editor = { kind: "win-hwnd", id: "3C", exeName: "notepad.exe" };

  assert.equal(
    (await manager._markEditableCaret({ status: "none", target: flagged }, flagged, true)).status,
    "none"
  );
  assert.equal(
    (await manager._markEditableCaret({ status: "none", target: byExeName }, byExeName, true))
      .status,
    "none"
  );
  assert.equal(probes.length, 0);
  assert.equal(
    (await manager._markEditableCaret({ status: "none", target: editor }, editor, true)).status,
    "editable"
  );
  assert.equal(probes.length, 1);
});

// AT-SPI targets carry only a pid — no window class or exe name for the
// signature check — so the executable name must be resolved before a Wayland
// terminal's empty prompt can read as a writable caret.
test("a Linux AT-SPI terminal pid never becomes a caret delivery target", async () => {
  const probes = [];
  const executableByPid = { 4321: "gnome-terminal-", 8765: "gedit" };
  const manager = new SelectionManager({
    clipboardManager: {
      isTerminalSignature: (signature) => signature.toLowerCase().includes("gnome-terminal"),
    },
    textEditMonitor: {
      isFocusedEditable: async (target) => {
        probes.push(target);
        return "editable";
      },
    },
    platform: "linux",
    now: () => 1000,
  });
  manager._readExecutablePath = async (pid) => executableByPid[pid] ?? "";
  const terminal = { kind: "atspi-pid", id: "4321" };
  const editor = { kind: "atspi-pid", id: "8765" };

  assert.equal(
    (await manager._markEditableCaret({ status: "none", target: terminal }, terminal, true)).status,
    "none"
  );
  assert.equal(probes.length, 0, "a terminal pid must be refused without probing");
  assert.equal(
    (await manager._markEditableCaret({ status: "none", target: editor }, editor, true)).status,
    "editable"
  );
  assert.equal(probes.length, 1);
});

test("a Linux caret probe is bound to the target's process", async () => {
  const probes = [];
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {
      isFocusedEditable: async (target) => {
        probes.push(target);
        return target.pid ? "editable" : "not_editable";
      },
    },
    platform: "linux",
    now: () => 1000,
  });
  const windowPids = { "xdotool 7": "111", "kdotool {kde}": "222" };
  manager._readWindowPid = async (tool, id) => windowPids[`${tool} ${id}`] ?? null;
  const mark = (target) => manager._markEditableCaret({ status: "none", target }, target, true);

  const cases = [
    [{ kind: "atspi-pid", id: "333" }, "333"],
    [{ kind: "x11-window", id: "7", windowClass: "gedit" }, "111"],
    [{ kind: "kde-window", id: "{kde}", windowClass: "kate" }, "222"],
    [{ kind: "hyprland-window", id: "0xabc", windowClass: "gedit", pid: 444 }, 444],
  ];
  for (const [target, pid] of cases) {
    const result = await mark(target);
    assert.equal(result.status, "editable", target.kind);
    assert.deepEqual(result.target, target, "the stored target is unchanged");
    assert.equal(probes.at(-1).pid, pid, target.kind);
  }

  // A window whose process can't be read is never a caret.
  for (const target of [
    { kind: "x11-window", id: "8", windowClass: "gedit" },
    { kind: "hyprland-window", id: "0xdef", windowClass: "gedit", pid: null },
  ]) {
    assert.equal((await mark(target)).status, "none", target.kind);
    assert.ok(!probes.at(-1).pid, target.kind);
  }
});

// Answers keyed by the command line; anything else fails like a missing tool.
function loadWithExecFile(outputs) {
  return loadSelectionManager({
    execFile: (command, args, options, callback) => {
      const stdout = outputs[[command, ...args].join(" ")];
      callback(stdout === undefined ? new Error("failed") : null, stdout ?? "", "");
    },
  });
}

test("a Linux window's pid is read by its window id", async () => {
  const Manager = loadWithExecFile({
    "xdotool getwindowpid 7": "111\n",
    "kdotool getwindowpid {kde}": "222\n",
    "xdotool getwindowpid 9": "0\n",
  });
  const manager = new Manager({ clipboardManager: {}, platform: "linux", now: () => 1000 });

  assert.equal(await manager._readWindowPid("xdotool", "7"), "111");
  assert.equal(await manager._readWindowPid("kdotool", "{kde}"), "222");
  assert.equal(await manager._readWindowPid("xdotool", "9"), null);
  assert.equal(await manager._readWindowPid("xdotool", "8"), null);
});

test("a Hyprland target carries the pid hyprctl reports", async () => {
  const activeWindow = (fields) => ({
    "hyprctl activewindow -j": JSON.stringify({ address: "0xabc", class: "Gedit", ...fields }),
  });
  const read = (outputs) =>
    new (loadWithExecFile(outputs))({
      clipboardManager: { commandExists: (command) => command === "hyprctl" },
      platform: "linux",
      now: () => 1000,
    })._getLinuxTarget();

  assert.deepEqual(await read(activeWindow({ pid: 444 })), {
    kind: "hyprland-window",
    id: "0xabc",
    windowClass: "gedit",
    pid: 444,
  });
  assert.equal((await read(activeWindow({}))).pid, null);
  assert.equal((await read(activeWindow({ pid: -1 }))).pid, null);
});

test("treats a clipboard-only fallback as a failed targeted paste", async () => {
  const { manager, pastes } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "none", editable: true },
    ],
    pasteResult: { restoreComplete: Promise.resolve(), pasted: false },
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });

  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: false,
    code: "paste_failed",
  });
  assert.equal(pastes.length, 1);
});

test("replaces only when target and exact selection still match", async () => {
  const { manager, pastes } = makeHarness({ selections: ["original", "original"] });
  const capture = await manager.captureSelectedText();
  const result = await manager.replaceSelectedText(capture.sessionId, "improved", {
    restoreClipboard: true,
  });

  assert.deepEqual(result, { success: true });
  assert.equal(pastes.length, 1);
  assert.equal(pastes[0].text, "improved");
  assert.equal(pastes[0].options.restoreClipboard, true);
});

test("does not paste when the selection changed", async () => {
  const { manager, pastes } = makeHarness({ selections: ["original", "different"] });
  const capture = await manager.captureSelectedText();
  const result = await manager.replaceSelectedText(capture.sessionId, "improved");

  assert.deepEqual(result, { success: false, code: "selection_changed" });
  assert.equal(pastes.length, 0);
});

// Keys still held past the modifier wait (#2113) block the edit at two points.
// Both must say so: "selection_unavailable" sends the user to permissions
// settings and "paste_failed" hides that the edit is on the clipboard.
test("a replacement blocked by held modifiers at revalidation reports modifiers_held", async () => {
  const { manager, pastes } = makeHarness({ selections: ["original"] });
  const capture = await manager.captureSelectedText();
  manager._readCurrentSelection = async () => ({ status: "unavailable", code: "modifiers_held" });

  assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, "improved"), {
    success: false,
    code: "modifiers_held",
  });
  assert.equal(pastes.length, 0);
});

test("a replacement whose paste was held back for modifiers reports modifiers_held", async () => {
  const { manager, pastes } = makeHarness({
    selections: ["original", "original"],
    pasteResult: { restoreComplete: Promise.resolve(), pasted: false, reason: "modifiers-held" },
  });
  const capture = await manager.captureSelectedText();

  assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, "improved"), {
    success: false,
    code: "modifiers_held",
  });
  assert.equal(pastes.length, 1);
});

// No Wayland clipboard written on wlroots, Hyprland or niri (#2621): the edit says
// why rather than "could not replace".
for (const [reason, code] of [
  ["wl-copy-missing", "wl_copy_missing"],
  ["wl-copy-failed", "wl_copy_failed"],
]) {
  test(`a replacement held back for ${reason} reports ${code}`, async () => {
    const { manager, pastes } = makeHarness({
      selections: ["original", "original"],
      pasteResult: { restoreComplete: Promise.resolve(), pasted: false, reason },
    });
    const capture = await manager.captureSelectedText();

    assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, "improved"), {
      success: false,
      code,
    });
    assert.equal(pastes.length, 1);
  });
}

// The assistant's caret paste is blocked the same two ways. The renderer only
// reads `success`, so this code is for logs and support rather than UI.
test("an assistant caret paste held back by modifier keys reports modifiers_held", async () => {
  const { manager } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "none", editable: true },
    ],
    pasteResult: { restoreComplete: Promise.resolve(), pasted: false, reason: "modifiers-held" },
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });

  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: false,
    code: "modifiers_held",
  });
});

test("a caret re-read blocked by held modifier keys reports modifiers_held", async () => {
  const { manager, pastes } = makeHarness({ selections: [{ state: "none", editable: true }] });
  const capture = await manager.captureSelectedText({ probeEditable: true });
  manager._readCurrentSelection = async () => ({ status: "unavailable", code: "modifiers_held" });

  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: false,
    code: "modifiers_held",
  });
  assert.equal(pastes.length, 0);
});

test("selection sessions are single-use", async () => {
  const { manager } = makeHarness({ selections: ["original", "original", "original"] });
  const capture = await manager.captureSelectedText();
  assert.equal((await manager.replaceSelectedText(capture.sessionId, "first")).success, true);
  assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, "second"), {
    success: false,
    code: "session_expired",
  });
});

test("expired sessions fail without reading or replacing the selection", async () => {
  let currentTime = 1000;
  const { manager, pastes } = makeHarness({
    selections: ["original", "original"],
    now: () => currentTime,
  });
  const capture = await manager.captureSelectedText();
  currentTime += 5 * 60 * 1000 + 1;

  assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, "improved"), {
    success: false,
    code: "session_expired",
  });
  assert.equal(pastes.length, 0);
});

test("oversized selections are rejected before creating a session", async () => {
  const { manager } = makeHarness({ selections: ["x".repeat(6001)] });
  assert.deepEqual(await manager.captureSelectedText(), {
    status: "too_large",
    characterCount: 6001,
    maxCharacters: 6000,
  });
  assert.equal(manager.sessions.size, 0);
});

// Exercises the clipboard-sentinel path (Windows/Linux) with injected
// clipboard reads, covering the KDE desync guard: a clipboard side the
// sentinel write never reached must not be mistaken for the copied selection.
function makeCaptureHarness({ readClipboard }) {
  const writes = [];
  const clipboardManager = {
    runClipboardOperation: (operation) => operation(),
    _saveClipboard: () => ({ type: "text", data: "user clipboard" }),
    _restoreClipboard: () => {},
    _writeClipboardTextAll: (text) => writes.push(text),
    _readClipboardTextAll: () => readClipboard(writes),
  };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  return { manager, writes };
}

const CAPTURE_TARGET = { kind: "x11-window", id: "7" };

test("stale text on a desynced clipboard side is never treated as the selection", async () => {
  const { manager, writes } = makeCaptureHarness({
    readClipboard: (written) => ["stale text", written[0]],
  });
  const result = await manager._captureViaClipboard(
    async () => ({ success: true, target: CAPTURE_TARGET }),
    null
  );

  assert.equal(result.status, "none");
  assert.equal(writes.at(-1), "user clipboard");
});

test("a copy that replaces the sentinel is captured and the clipboard restored", async () => {
  let polled = false;
  const { manager, writes } = makeCaptureHarness({
    readClipboard: (written) => {
      if (written.length === 0) return ["user clipboard"];
      if (!polled) {
        polled = true;
        return [written[0]];
      }
      return ["copied selection", written[0]];
    },
  });
  const result = await manager._captureViaClipboard(
    async () => ({ success: true, target: CAPTURE_TARGET }),
    null
  );

  assert.equal(result.status, "selected");
  assert.equal(result.text, "copied selection");
  assert.equal(writes.at(-1), "user clipboard");
});

// A line copy (one line + trailing terminator) from an empty-selection Ctrl+C
// in editors like VS Code must never be mistaken for a real selection.
const VSCODE_TARGET = { kind: "x11-window", id: "7", windowClass: "code" };

function makeLineCopyHarness(copiedText) {
  let polled = false;
  return makeCaptureHarness({
    readClipboard: (written) => {
      if (written.length === 0) return ["user clipboard"];
      if (!polled) {
        polled = true;
        return [written[0]];
      }
      return [copiedText, written[0]];
    },
  });
}

test("a line copy from an empty selection in a line-copy editor reads as no selection", async () => {
  const { manager, writes } = makeLineCopyHarness("const x = 1;\r\n");
  const result = await manager._captureViaClipboard(
    async () => ({ success: true, target: VSCODE_TARGET }),
    VSCODE_TARGET
  );

  assert.equal(result.status, "none");
  assert.equal(writes.at(-1), "user clipboard");
});

test("a multi-line selection in a line-copy editor is still captured", async () => {
  const { manager } = makeLineCopyHarness("first line\nsecond line\n");
  const result = await manager._captureViaClipboard(
    async () => ({ success: true, target: VSCODE_TARGET }),
    VSCODE_TARGET
  );

  assert.equal(result.status, "selected");
  assert.equal(result.text, "first line\nsecond line\n");
});

test("a trailing-newline selection outside line-copy editors is still captured", async () => {
  const { manager } = makeLineCopyHarness("whole line\n");
  const firefoxTarget = { kind: "x11-window", id: "7", windowClass: "org.mozilla.firefox" };
  const result = await manager._captureViaClipboard(
    async () => ({ success: true, target: firefoxTarget }),
    firefoxTarget
  );

  assert.equal(result.status, "selected");
  assert.equal(result.text, "whole line\n");
});

test("does not overwrite a clipboard value copied while capture is in flight", async () => {
  let userCopied = false;
  const { manager, writes } = makeCaptureHarness({
    readClipboard: (written) => {
      if (written.length === 0) return ["user clipboard"];
      return userCopied ? ["new user clipboard", written[0]] : ["user clipboard", written[0]];
    },
  });

  const result = await manager._captureViaClipboard(async () => {
    userCopied = true;
    return { success: false };
  }, null);

  assert.deepEqual(result, { status: "unavailable", code: "copy_failed" });
  assert.equal(writes.at(-1), "new user clipboard");
});

// captureTarget() fires on every toggle press, including stop, and the AT-SPI
// probe can outlive fast cloud transcription. Reading lastTarget before the
// stop-press probe lands must wait for it instead of failing closed.
test("captureSelectedText awaits an in-flight target probe before reading lastTarget", async () => {
  const clipboardManager = { runClipboardOperation: (operation) => operation() };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  let resolveProbe;
  manager._getLinuxTarget = () => new Promise((resolve) => (resolveProbe = resolve));
  manager._readCurrentSelection = async (expectedTarget) =>
    expectedTarget
      ? { status: "selected", text: "picked", target: expectedTarget }
      : { status: "unavailable", code: "target_unavailable" };

  const probe = manager.captureTarget();
  const read = manager.captureSelectedText();
  resolveProbe({ kind: "atspi-pid", id: "2524568" });
  await probe;

  const result = await read;
  assert.equal(result.status, "selected");
  assert.equal(result.text, "picked");
});

test("start target captures share in-flight and recent work", async () => {
  let now = 1000;
  let calls = 0;
  const resolvers = [];
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {},
    platform: "linux",
    now: () => now,
  });
  manager._probeTarget = () => {
    calls += 1;
    return new Promise((resolve) => resolvers.push(resolve));
  };

  const first = manager.captureTarget();
  const joined = manager.captureTarget();
  assert.equal(calls, 1);
  resolvers[0]({ kind: "atspi-pid", id: "1" });
  await Promise.all([first, joined]);

  const reused = manager.captureTarget();
  assert.equal(calls, 1);
  await reused;

  now += 250;
  const refreshed = manager.captureTarget();
  assert.equal(calls, 2);
  resolvers[1]({ kind: "atspi-pid", id: "2" });
  await refreshed;
});

test("a failed target probe is retried rather than reused", async () => {
  let calls = 0;
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  manager._probeTarget = async () => {
    calls += 1;
    return null;
  };

  await manager.captureTarget();
  await manager.captureTarget();
  assert.equal(calls, 2);
});

test("a forced probe stays fresh and older work cannot overwrite it", async () => {
  const clipboardManager = { runClipboardOperation: (operation) => operation() };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  const resolvers = [];
  manager._getLinuxTarget = () => new Promise((resolve) => resolvers.push(resolve));

  const first = manager.captureTarget();
  const second = manager.captureTarget({ force: true });
  resolvers[1]({ kind: "atspi-pid", id: "2" });
  await second;
  resolvers[0]({ kind: "atspi-pid", id: "1" });
  await first;

  assert.deepEqual(manager.lastTarget, { kind: "atspi-pid", id: "2" });
});

test("Wayland target lookup attempts AT-SPI once", async () => {
  const spawnCalls = [];
  const SpawningSelectionManager = loadSelectionManager({
    spawn: (command, args) => {
      spawnCalls.push({ command, args });
      const child = new EventEmitter();
      child.stdout = new EventEmitter();
      child.stderr = new EventEmitter();
      child.kill = () => {};
      process.nextTick(() => child.emit("close", 1));
      return child;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: {
      _isWayland: () => true,
      commandExists: () => false,
      resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
    },
    platform: "linux",
    now: () => 1000,
  });

  assert.equal(await manager._getLinuxTarget(), null);
  assert.equal(await manager._getLinuxTarget(), null);
  assert.deepEqual(spawnCalls, [
    { command: "/tmp/linux-fast-paste", args: ["--atspi-target"] },
    { command: "/tmp/linux-fast-paste", args: ["--atspi-target"] },
  ]);
});

test("an AT-SPI timeout opens a cooldown and retries after it expires", async (t) => {
  t.mock.timers.enable({ apis: ["setTimeout"] });
  let now = 1000;
  let spawns = 0;
  let kills = 0;
  const SpawningSelectionManager = loadSelectionManager({
    spawn: () => {
      spawns += 1;
      const child = new EventEmitter();
      child.stdout = new EventEmitter();
      child.stderr = new EventEmitter();
      child.kill = () => {
        kills += 1;
      };
      return child;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: {
      resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
    },
    platform: "linux",
    now: () => now,
  });

  const first = manager._getLinuxAtspiTarget();
  t.mock.timers.tick(2000);
  assert.equal(await first, null);
  assert.equal(spawns, 1);
  assert.equal(kills, 1);

  const cooledTarget = manager._getLinuxAtspiTarget();
  assert.equal(spawns, 1, "a target probe must not spawn during the cooldown");
  assert.equal(await cooledTarget, null);
  const cooledSelection = manager._readLinuxAtspiSelection("/tmp/linux-fast-paste", {
    kind: "atspi-pid",
    id: "42",
  });
  assert.equal(spawns, 1, "a selection read must not spawn during the cooldown");
  assert.equal((await cooledSelection).code, "accessibility_unavailable");

  now += 2000;
  const retry = manager._getLinuxAtspiTarget();
  t.mock.timers.tick(2000);
  assert.equal(await retry, null);
  assert.equal(spawns, 2);
  assert.equal(kills, 2);

  now += 2000;
  const selection = manager._readLinuxAtspiSelection("/tmp/linux-fast-paste", {
    kind: "atspi-pid",
    id: "42",
  });
  t.mock.timers.tick(1200);
  assert.equal((await selection).status, "unavailable");
  assert.equal(spawns, 3);
  assert.equal(kills, 3);
  const cooledBySelection = manager._getLinuxAtspiTarget();
  assert.equal(spawns, 3, "a selection timeout must cool down target probes too");
  assert.equal(await cooledBySelection, null);
});

test("older AT-SPI timeouts cannot cool down a newer successful probe", async (t) => {
  t.mock.timers.enable({ apis: ["setTimeout"] });
  let spawns = 0;
  const SpawningSelectionManager = loadSelectionManager({
    spawn: () => {
      spawns += 1;
      const child = new EventEmitter();
      child.stdout = new EventEmitter();
      child.stderr = new EventEmitter();
      child.kill = () => {};
      if (spawns >= 3) {
        process.nextTick(() => {
          child.stdout.emit("data", "TARGET ATSPI 42\n");
          child.emit("close", 0);
        });
      }
      return child;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: {
      resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
    },
    platform: "linux",
    now: () => 1000,
  });

  const olderTarget = manager._getLinuxAtspiTarget();
  const olderSelection = manager._readLinuxAtspiSelection("/tmp/linux-fast-paste", {
    kind: "atspi-pid",
    id: "42",
  });
  const newer = manager._getLinuxAtspiTarget();
  assert.deepEqual(await newer, { kind: "atspi-pid", id: "42" });
  t.mock.timers.tick(2000);
  assert.equal(await olderTarget, null);
  assert.equal((await olderSelection).status, "unavailable");

  const next = manager._getLinuxAtspiTarget();
  assert.equal(spawns, 4, "neither older timeout may open a cooldown");
  assert.deepEqual(await next, { kind: "atspi-pid", id: "42" });
});

// The Windows paste path restores the window captured at record start (#859).
// getWinTarget hands the paste that HWND exactly as --detect-only printed
// it ("TARGET %p", hex) so the binary's base-16 --restore-window parse round-trips.
test("getWinTarget returns the hex HWND a win32 probe captured (#859)", async () => {
  const spawnCalls = [];
  const SpawningSelectionManager = loadSelectionManager({
    spawn: (command, args) => {
      spawnCalls.push({ command, args });
      const probe = new EventEmitter();
      probe.stdout = new EventEmitter();
      probe.stderr = new EventEmitter();
      process.nextTick(() => {
        probe.stdout.emit(
          "data",
          "TARGET 00001A2B\nWINDOW_CLASS Chrome_WidgetWin_1\nIS_TERMINAL false\n"
        );
        probe.emit("close", 0);
      });
      return probe;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: { resolveWindowsFastPasteBinary: () => "/tmp/windows-fast-paste.exe" },
    textEditMonitor: {},
    platform: "win32",
    now: () => 1000,
  });

  await manager.captureTarget();

  assert.deepEqual(spawnCalls, [
    { command: "/tmp/windows-fast-paste.exe", args: ["--detect-only"] },
  ]);
  assert.equal((await manager.getWinTarget())?.id, "00001A2B");
});

test("the app context of a win32 target carries its exe, class and window title", async () => {
  const SpawningSelectionManager = loadSelectionManager({
    spawn: () => {
      const probe = new EventEmitter();
      probe.stdout = new EventEmitter();
      probe.stderr = new EventEmitter();
      process.nextTick(() => {
        probe.stdout.emit(
          "data",
          "TARGET 00001A2B\nWINDOW_CLASS Chrome_WidgetWin_1\nEXE_NAME chrome.exe\n" +
            "WINDOW_TITLE Inbox - Gmail - Google Chrome\nIS_TERMINAL false\n"
        );
        probe.emit("close", 0);
      });
      return probe;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: { resolveWindowsFastPasteBinary: () => "/tmp/windows-fast-paste.exe" },
    textEditMonitor: {},
    platform: "win32",
    now: () => 1000,
  });

  const capture = manager.captureTarget();
  const context = manager.getTargetAppContext();
  await capture;

  assert.deepEqual(await context, {
    appSignature: "chrome.exe Chrome_WidgetWin_1",
    windowTitle: "Inbox - Gmail - Google Chrome",
  });
  // The title stays in the main process: the target itself never carries it.
  assert.equal(JSON.stringify(manager.lastTarget).includes("Inbox"), false);
});

test("the app context of a macOS target names the app from its pid", async () => {
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {
      lastTargetPid: 42,
      getAppContext: async (pid) =>
        pid === 42
          ? { appName: "Google Chrome", bundleId: "com.google.Chrome", urlHost: "mail.google.com" }
          : null,
    },
    platform: "darwin",
    now: () => 1000,
  });

  assert.deepEqual(await manager.getTargetAppContext(), {
    appSignature: "Google Chrome com.google.Chrome",
    windowTitle: undefined,
    urlHost: "mail.google.com",
  });
});

test("an X11 target's window title is read only when the app context is asked for", async () => {
  const calls = [];
  const ExecSelectionManager = loadSelectionManager({
    execFile: (command, args, _options, callback) => {
      calls.push([command, ...args]);
      callback(null, "Inbox - Gmail - Mozilla Firefox\n", "");
    },
  });
  const manager = new ExecSelectionManager({
    clipboardManager: {},
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  manager.lastTarget = { kind: "x11-window", id: "7", windowClass: "firefox" };

  assert.deepEqual(await manager.getTargetAppContext(), {
    appSignature: "firefox",
    windowTitle: "Inbox - Gmail - Mozilla Firefox",
  });
  assert.deepEqual(calls, [["xdotool", "getwindowname", "7"]]);
});

test("an AT-SPI target is named from its pid and titled from its window", async () => {
  let stdout = "";
  const SpawningSelectionManager = loadSelectionManager({
    spawn: () => {
      const child = new EventEmitter();
      child.stdout = new EventEmitter();
      child.stderr = new EventEmitter();
      child.kill = () => {};
      process.nextTick(() => {
        child.stdout.emit("data", stdout);
        child.emit("close", 0);
      });
      return child;
    },
  });
  const manager = new SpawningSelectionManager({
    clipboardManager: { resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste" },
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  manager._readTargetNames = async (pid) => (pid === "42" ? "chrome" : "");
  const title = "Inbox - Gmail - Google Chrome";

  stdout = `TARGET ATSPI 42\nWINDOW_TITLE_B64 ${Buffer.from(title).toString("base64")}\n`;
  manager.lastTarget = await manager._getLinuxAtspiTarget();
  assert.deepEqual(manager.lastTarget, { kind: "atspi-pid", id: "42" });
  assert.deepEqual(await manager.getTargetAppContext(), {
    appSignature: "chrome",
    windowTitle: title,
  });

  // An older helper prints no title.
  stdout = "TARGET ATSPI 42\n";
  manager.lastTarget = await manager._getLinuxAtspiTarget();
  assert.deepEqual(await manager.getTargetAppContext(), {
    appSignature: "chrome",
    windowTitle: null,
  });
  assert.equal(
    await new SelectionManager({ platform: "linux", textEditMonitor: {} }).getTargetAppContext(),
    null
  );
});

// captureTarget() nulls lastTarget while its probe runs; a paste racing the
// stop-press probe must wait for the answer instead of restoring nothing.
test("getWinTarget waits for an in-flight probe before answering", async () => {
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {},
    platform: "win32",
    now: () => 1000,
  });
  let resolveProbe;
  manager._probeTarget = () => new Promise((resolve) => (resolveProbe = resolve));

  const probe = manager.captureTarget();
  const pending = manager.getWinTarget();
  resolveProbe({ kind: "win-hwnd", id: "0000F00D" });
  await probe;

  assert.equal((await pending)?.id, "0000F00D");
});

test("getWinTarget is null without a capture or with a non-Windows target", async () => {
  const manager = new SelectionManager({
    clipboardManager: {},
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  assert.equal(await manager.getWinTarget(), null);

  manager.lastTarget = { kind: "x11-window", id: "7" };
  assert.equal(await manager.getWinTarget(), null);
});

test("caret delivery pins the captured Windows HWND into the paste helper", async () => {
  const pastes = [];
  const manager = new SelectionManager({
    clipboardManager: {
      runClipboardOperation: (operation) => operation(),
      _pasteText: async (text, options) => {
        pastes.push({ text, options });
        return { restoreComplete: Promise.resolve() };
      },
    },
    textEditMonitor: {},
    platform: "win32",
    now: () => 1000,
  });
  manager.sessions.set("caret-session", {
    kind: "caret",
    target: { kind: "win-hwnd", id: "00001A2B" },
    expiresAt: 2000,
  });
  manager._readCurrentSelection = async () => ({ status: "editable" });

  assert.deepEqual(await manager.pasteAtCapturedTarget("caret-session", "Agent response"), {
    success: true,
  });
  assert.equal(pastes[0].options.targetWindow, "00001A2B");
});

// Replacement text typed into a shell executes on its embedded newlines, so a
// terminal target must read as no selection (standalone dictation), not as an
// editable selection.
test("a terminal target reads as no selection", async () => {
  const clipboardManager = {
    runClipboardOperation: (operation) => operation(),
    isLinuxTerminalWindowClass: (windowClass) => windowClass === "konsole",
  };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor: { isFocusedEditable: async () => "editable" },
    platform: "linux",
    now: () => 1000,
  });
  const terminalTarget = { kind: "kde-window", id: "9", windowClass: "konsole" };
  manager._getLinuxTarget = async () => terminalTarget;

  const result = await manager._readLinuxSelection(null);
  assert.equal(result.status, "none");
  assert.deepEqual(result.target, terminalTarget);
});

// Capture runs right after the voice assistant hotkey press, while its keys are
// often still down. A Ctrl+C sent into them copies nothing, so capture waits for
// the release and fails closed when the keys stay held.
for (const [modifiers, expectCopy] of [
  ["held", false],
  ["released", true],
  ["unknown", true],
]) {
  test(`Linux selection capture ${expectCopy ? "copies" : "sends no copy"} when modifiers are ${modifiers}`, async () => {
    let copyAttempts = 0;
    const manager = new SelectionManager({
      clipboardManager: {
        runClipboardOperation: (operation) => operation(),
        isLinuxTerminalWindowClass: () => false,
        resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
        _awaitModifierRelease: async () => ({ state: modifiers, waitedMs: 0 }),
      },
      textEditMonitor: {},
      platform: "linux",
      now: () => 1000,
    });
    const target = { kind: "x11-window", id: "7", windowClass: "org.gnome.texteditor" };
    manager._getLinuxTarget = async () => target;
    manager._captureViaClipboard = async () => {
      copyAttempts += 1;
      return { status: "none", target };
    };

    const result = await manager._readLinuxSelection(null);

    assert.equal(copyAttempts, expectCopy ? 1 : 0);
    assert.deepEqual(
      result,
      expectCopy ? { status: "none", target } : { status: "unavailable", code: "modifiers_held" }
    );
  });
}

// The target was classified before the wait. If focus moved while the keys were
// held (say, to a terminal), a copy chord would reach a window nobody checked:
// a plain Ctrl+C there interrupts whatever is running.
for (const [waitedMs, focusMoved, expectCopy] of [
  [400, true, false],
  [400, false, true],
  [0, true, true],
]) {
  test(`Linux selection capture ${expectCopy ? "copies" : "sends no copy"} after a ${waitedMs} ms wait when focus ${focusMoved ? "moved" : "stayed"}`, async () => {
    let copyAttempts = 0;
    let targetReads = 0;
    const manager = new SelectionManager({
      clipboardManager: {
        runClipboardOperation: (operation) => operation(),
        isLinuxTerminalWindowClass: (windowClass) => windowClass === "konsole",
        resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
        _awaitModifierRelease: async () => ({ state: "released", waitedMs }),
      },
      textEditMonitor: {},
      platform: "linux",
      now: () => 1000,
    });
    const target = { kind: "kde-window", id: "7", windowClass: "kate" };
    const terminal = { kind: "kde-window", id: "9", windowClass: "konsole" };
    manager._getLinuxTarget = async () => {
      targetReads += 1;
      return targetReads > 1 && focusMoved ? terminal : target;
    };
    manager._captureViaClipboard = async () => {
      copyAttempts += 1;
      return { status: "none", target };
    };

    const result = await manager._readLinuxSelection(null);

    assert.equal(copyAttempts, expectCopy ? 1 : 0);
    assert.equal(targetReads, waitedMs > 0 ? 2 : 1, "only a real wait pays for a second lookup");
    if (!expectCopy) assert.deepEqual(result, { status: "target_changed", code: "focus_moved" });
  });
}

// A capture that saw focus move runs the command on its own (nothing was
// checked), but a session being revalidated has a real target to protect: the
// selection edit or caret delivery is declined as a changed target.
test("a Linux session revalidation declines when focus moved during the modifier wait", async () => {
  let targetReads = 0;
  let pastes = 0;
  const manager = new SelectionManager({
    clipboardManager: {
      runClipboardOperation: (operation) => operation(),
      isLinuxTerminalWindowClass: () => false,
      resolveLinuxFastPasteBinary: () => "/tmp/linux-fast-paste",
      _awaitModifierRelease: async () => ({ state: "released", waitedMs: 400 }),
      _pasteText: async () => {
        pastes += 1;
        return { pasted: true };
      },
    },
    textEditMonitor: {},
    platform: "linux",
    now: () => 1000,
  });
  const target = { kind: "kde-window", id: "7", windowClass: "kate" };
  const other = { kind: "kde-window", id: "9", windowClass: "konsole" };
  manager._getLinuxTarget = async () => (++targetReads % 2 === 0 ? other : target);
  manager.sessions.set("edit", { kind: "selection", text: "old", target, expiresAt: 2000 });
  manager.sessions.set("caret", { kind: "caret", target, expiresAt: 2000 });

  assert.deepEqual(await manager.replaceSelectedText("edit", "new"), {
    success: false,
    code: "target_changed",
  });
  assert.deepEqual(await manager.pasteAtCapturedTarget("caret", "answer"), {
    success: false,
    code: "target_changed",
  });
  assert.equal(pastes, 0);
});

// macOS accessibility never resolves a focused element in Chromium browsers, so
// a synthetic ⌘C is the only way to tell a real selection from an empty field.
function makeMacClipboardHarness({ copyOutput = "COPY_OK 42 Dia", copied = null } = {}) {
  const writes = [];
  // The copied text only becomes visible after the helper runs; anything on the
  // clipboard beforehand is baselined as stale.
  let copySent = false;
  const clipboardManager = {
    runClipboardOperation: (operation) => operation(),
    resolveFastPasteBinary: () => "/bin/macos-fast-paste",
    isTerminalSignature: (signature) => /ghostty|iterm|terminal/i.test(signature || ""),
    _saveClipboard: () => ({ type: "text", data: "user clipboard" }),
    _restoreClipboard: () => {},
    _writeClipboardTextAll: (text) => writes.push(text),
    _readClipboardTextAll: () => {
      if (writes.length === 0) return ["user clipboard"];
      return copySent && copied !== null ? [copied, writes[0]] : [writes[0]];
    },
  };
  const manager = new SelectionManager({
    clipboardManager,
    textEditMonitor: { lastTargetPid: 42, getSelectedText: async () => ({ state: "unavailable" }) },
    platform: "darwin",
    now: () => 1000,
  });
  manager._runCopyHelper = async () => {
    copySent = true;
    return { success: true, stdout: copyOutput, stderr: "" };
  };
  return { manager, writes };
}

test("an inaccessible macOS target falls back to a synthetic copy", async () => {
  const { manager, writes } = makeMacClipboardHarness({ copied: "the selected paragraph" });
  const result = await manager.captureSelectedText();

  assert.equal(result.status, "selected");
  assert.equal(result.text, "the selected paragraph");
  assert.equal(writes.at(-1), "user clipboard");
});

test("nothing selected in an inaccessible macOS target reads as no selection", async () => {
  const { manager } = makeMacClipboardHarness({ copied: null });
  assert.equal((await manager.captureSelectedText()).status, "none");
});

test("a copy landing in a different app than expected reports the target changed", async () => {
  const { manager } = makeMacClipboardHarness({
    copyOutput: "COPY_OK 99 Some Other App",
    copied: "text from the wrong app",
  });
  assert.equal((await manager.captureSelectedText()).status, "target_changed");
});

// Replacement text typed into a shell executes on its embedded newlines, and
// terminals without an accessibility tree reach editing only via this path.
test("a macOS terminal selection reads as no selection", async () => {
  const { manager } = makeMacClipboardHarness({
    copyOutput: "COPY_OK 42 Ghostty",
    copied: "rm -rf important",
  });
  assert.equal((await manager.captureSelectedText()).status, "none");
});

// A bare caret in VS Code copies the whole line, which must never be mistaken
// for a selection and silently rewritten.
test("a macOS line copy in a line-copy editor reads as no selection", async () => {
  const { manager } = makeMacClipboardHarness({
    copyOutput: "COPY_OK 42 Code",
    copied: "const x = 1;\n",
  });
  assert.equal((await manager.captureSelectedText()).status, "none");
});

test("an unverifiable macOS field with no selection stays on the panel route", async () => {
  const { manager } = makeMacClipboardHarness({ copied: null });
  manager.textEditMonitor.isFocusedEditable = async () => "unknown";

  const result = await manager.captureSelectedText({ probeEditable: true });
  assert.equal(result.status, "none");
  assert.equal(result.sessionId, undefined);
});

test("a confirmed non-editable macOS focus stays on the panel route", async () => {
  const { manager } = makeMacClipboardHarness({ copied: null });
  manager.textEditMonitor.isFocusedEditable = async () => "not_editable";

  assert.equal((await manager.captureSelectedText({ probeEditable: true })).status, "none");
});

test("an unknown verdict in a terminal is still refused as a caret", async () => {
  const { manager } = makeMacClipboardHarness({ copyOutput: "COPY_OK 42 Ghostty", copied: null });
  manager.textEditMonitor.isFocusedEditable = async () => "unknown";

  assert.equal((await manager.captureSelectedText({ probeEditable: true })).status, "none");
});

// COPY_OK reports the app name best-effort; when it is missing, the executable
// must be resolved before an unnamed terminal can be trusted as a caret.
test("an unnamed macOS target resolves the executable before trusting a caret", async () => {
  const { manager } = makeMacClipboardHarness({ copyOutput: "COPY_OK 42", copied: null });
  manager.textEditMonitor.isFocusedEditable = async () => "editable";
  manager._readExecutablePath = async () => "/Applications/Ghostty.app/Contents/MacOS/ghostty";

  assert.equal((await manager.captureSelectedText({ probeEditable: true })).status, "none");
});

test("a caret verified after clipboard capture delivers the assistant response", async () => {
  const { manager } = makeMacClipboardHarness({ copied: null });
  const pastes = [];
  manager.textEditMonitor.isFocusedEditable = async () => "editable";
  manager.clipboardManager._pasteText = async (text, options) => {
    pastes.push({ text, options });
    return { restoreComplete: Promise.resolve() };
  };

  const capture = await manager.captureSelectedText({ probeEditable: true });
  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: true,
  });
  assert.equal(pastes[0].text, "Agent response");
});

for (const { name, appName, copied } of [
  { name: "selection matches the clipboard", appName: "Dia", copied: "user clipboard" },
  { name: "selection is a full code line", appName: "Code", copied: "const value = 1;\n" },
  { name: "focus moves to an integrated terminal", appName: "Code", copied: null },
  { name: "focus moves to a browser page", appName: "Chrome", copied: null },
]) {
  test(`a verified caret stops delivery when AX becomes unknown and ${name}`, async () => {
    const { manager } = makeMacClipboardHarness({ copyOutput: `COPY_OK 42 ${appName}`, copied });
    const pastes = [];
    manager.textEditMonitor.getSelectedText = async () => ({ state: "none", editable: true });
    manager._readExecutablePath = async () => appName;
    manager.clipboardManager._pasteText = async (text) => {
      pastes.push(text);
      return { restoreComplete: Promise.resolve() };
    };
    const capture = await manager.captureSelectedText({ probeEditable: true });
    assert.equal(capture.status, "editable");

    // The app/PID is unchanged, but the focused field is no longer inspectable.
    manager.textEditMonitor.getSelectedText = async () => ({ state: "unknown" });
    manager.textEditMonitor.isFocusedEditable = async () => "unknown";

    assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
      success: false,
      code: "target_changed",
    });
    assert.deepEqual(pastes, []);
  });
}

test("an assistant answer remains available when a dormant browser has no input", async () => {
  const { createAssistantResponseDelivery, deliverAssistantResponse } =
    await import("../../src/helpers/assistantResponseDelivery.ts");
  const { manager } = makeMacClipboardHarness({ copyOutput: "COPY_OK 42 Chrome" });
  const pastes = [];
  const writes = [];
  manager.textEditMonitor.getSelectedText = async () => ({ state: "unknown" });
  manager.textEditMonitor.isFocusedEditable = async () => "unknown";
  // Posting Cmd+V succeeds even when there is no field to receive it.
  manager.clipboardManager._pasteText = async (text) => {
    pastes.push(text);
    return { restoreComplete: Promise.resolve() };
  };

  const capture = await manager.captureSelectedText({ probeEditable: true });
  const delivery = createAssistantResponseDelivery({
    autoPasteEnabled: true,
    deliverySessionId: capture.sessionId,
    restoreClipboard: true,
    allowClipboardFallback: false,
  });
  const result = await deliverAssistantResponse(delivery, "Agent response", {
    electronAPI: {
      pasteAtCapturedTarget: (...args) => manager.pasteAtCapturedTarget(...args),
      writeClipboard: async (text) => {
        writes.push(text);
        return { success: true };
      },
    },
    clipboard: {},
  });

  assert.deepEqual(result, { pasted: false, copied: true });
  assert.deepEqual(pastes, []);
  assert.deepEqual(writes, ["Agent response"]);
});

test("a failed macOS copy stays non-fatal so the command still runs", async () => {
  const { manager } = makeMacClipboardHarness({ copied: null });
  manager._runCopyHelper = async () => ({ success: false, stdout: "", stderr: "" });

  assert.deepEqual(await manager.captureSelectedText(), {
    status: "unavailable",
    code: "accessibility_unavailable",
  });
});

test("a missing copy helper stays non-fatal so the command still runs", async () => {
  const { manager } = makeMacClipboardHarness();
  manager.clipboardManager.resolveFastPasteBinary = () => null;

  assert.deepEqual(await manager.captureSelectedText(), {
    status: "unavailable",
    code: "accessibility_unavailable",
  });
});

// The clipboard fallback is macOS-only; Windows and Linux keep their own capture
// paths untouched.
test("a win32 read never reaches the macOS clipboard fallback", async () => {
  let macCalled = false;
  const manager = new SelectionManager({
    clipboardManager: {
      runClipboardOperation: (operation) => operation(),
      resolveWindowsFastPasteBinary: () => null,
    },
    textEditMonitor: { lastTargetPid: 42 },
    platform: "win32",
    now: () => 1000,
  });
  manager._readMacSelectionViaClipboard = async () => {
    macCalled = true;
    return { status: "selected", text: "wrong platform" };
  };

  const result = await manager._readCurrentSelection({ kind: "win-hwnd", id: "7" });

  assert.equal(macCalled, false);
  assert.deepEqual(result, { status: "unavailable", code: "copy_helper_unavailable" });
});

test("empty replacement output is rejected without consuming a paste", async () => {
  const { manager, pastes } = makeHarness({ selections: ["original"] });
  const capture = await manager.captureSelectedText();
  assert.deepEqual(await manager.replaceSelectedText(capture.sessionId, ""), {
    success: false,
    code: "invalid_replacement",
  });
  assert.equal(pastes.length, 0);
});

test("a caret in a markdown-native macOS app is reported as accepting markdown", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  manager._readExecutablePath = async () => "/Applications/Obsidian.app/Contents/MacOS/Obsidian";

  const result = await manager.captureSelectedText({ probeEditable: true });

  assert.equal(result.status, "editable");
  assert.equal(result.acceptsMarkdown, true);
});

test("a caret in a plain-text macOS app is reported as wanting plain text", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  manager._readExecutablePath = async () =>
    "/System/Applications/TextEdit.app/Contents/MacOS/TextEdit";

  const result = await manager.captureSelectedText({ probeEditable: true });

  assert.equal(result.status, "editable");
  assert.equal(result.acceptsMarkdown, false);
});

test("an unreadable pid defaults the caret to plain text", async () => {
  const { manager } = makeHarness({ selections: [{ state: "none", editable: true }] });
  manager._readExecutablePath = async () => "";

  const result = await manager.captureSelectedText({ probeEditable: true });

  assert.equal(result.status, "editable");
  assert.equal(result.acceptsMarkdown, false);
});

test("Windows and Linux caret targets are judged by the identity they already carry", async () => {
  const { manager } = makeHarness();
  manager._readExecutablePath = async () => {
    throw new Error("must not spawn ps for a target that names its app");
  };

  assert.equal(
    await manager._targetAcceptsMarkdown({
      kind: "win-hwnd",
      id: "00001A2B",
      exeName: "Obsidian.exe",
      windowClass: "Chrome_WidgetWin_1",
    }),
    true
  );
  assert.equal(
    await manager._targetAcceptsMarkdown({
      kind: "win-hwnd",
      id: "00001A2B",
      exeName: "WINWORD.EXE",
    }),
    false
  );
  assert.equal(
    await manager._targetAcceptsMarkdown({
      kind: "x11-window",
      id: "0x1",
      windowClass: "md.obsidian.obsidian",
    }),
    true
  );
  assert.equal(await manager._targetAcceptsMarkdown(null), false);
});

test("a Linux AT-SPI target resolves its executable like the terminal check does", async () => {
  const { manager } = makeHarness();
  manager._readExecutablePath = async (pid) => (pid === 77 ? "obsidian" : "");

  assert.equal(await manager._targetAcceptsMarkdown({ kind: "atspi-pid", id: 77 }), true);
  assert.equal(await manager._targetAcceptsMarkdown({ kind: "atspi-pid", id: 78 }), false);
});

test("a paste declined by a changed target logs the code and the probe verdict", async () => {
  logged.length = 0;
  const { manager } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "selected", text: "new selection" },
    ],
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });
  await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response");

  assert.equal(declines().length, 1);
  const [entry] = declines();
  assert.equal(entry.level, "info");
  assert.equal(entry.scope, "clipboard");
  assert.equal(entry.meta.code, "target_changed");
  assert.equal(entry.meta.platform, "darwin");
  assert.equal(entry.meta.sessionFound, true);
  assert.equal(entry.meta.sessionKind, "caret");
  assert.equal(entry.meta.probeStatus, "selected");
});

test("a paste against a missing or expired session logs session_expired", async () => {
  logged.length = 0;
  const { manager, pastes } = makeHarness();

  assert.deepEqual(await manager.pasteAtCapturedTarget("missing-session", "Agent response"), {
    success: false,
    code: "session_expired",
  });
  assert.equal(pastes.length, 0);
  const [entry] = declines();
  assert.equal(entry.meta.code, "session_expired");
  assert.equal(entry.meta.sessionFound, false);
});

test("a selection session offered as a caret target logs its kind", async () => {
  logged.length = 0;
  const { manager } = makeHarness({ selections: ["some selected text"] });
  const capture = await manager.captureSelectedText();

  assert.equal(
    (await manager.pasteAtCapturedTarget(capture.sessionId, "x")).code,
    "session_expired"
  );
  const [entry] = declines();
  assert.equal(entry.meta.sessionFound, true);
  assert.equal(entry.meta.sessionKind, "selection");
});

test("empty text logs invalid_replacement before any clipboard work", async () => {
  logged.length = 0;
  const { manager, pastes } = makeHarness();

  assert.deepEqual(await manager.pasteAtCapturedTarget("any-session", ""), {
    success: false,
    code: "invalid_replacement",
  });
  assert.equal(pastes.length, 0);
  assert.equal(declines()[0].meta.code, "invalid_replacement");
});

test("a paste the clipboard helper reports as not pasted logs paste_failed", async () => {
  logged.length = 0;
  const { manager } = makeHarness({
    selections: [
      { state: "none", editable: true },
      { state: "none", editable: true },
    ],
    pasteResult: { pasted: false, restoreComplete: Promise.resolve() },
  });
  const capture = await manager.captureSelectedText({ probeEditable: true });

  assert.deepEqual(await manager.pasteAtCapturedTarget(capture.sessionId, "Agent response"), {
    success: false,
    code: "paste_failed",
  });
  const [entry] = declines();
  assert.equal(entry.meta.code, "paste_failed");
  assert.equal(entry.meta.probeStatus, "editable");
});