Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

3.1 KB
Raw Blame
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { copyText } from '../src/ui/clipboard.ts';

function webview(t, clipboard, copied = true) {
  const calls = [];
  const input = { style: {}, value: '', setAttribute() {}, select() { calls.push(['select', this.value]); }, remove() { calls.push(['remove']); } };
  t.mock.getter(globalThis, 'navigator', () => ({ clipboard }));
  const previousDocument = Object.getOwnPropertyDescriptor(globalThis, 'document');
  Object.defineProperty(globalThis, 'document', { configurable: true, value: {
    activeElement: { focus() { calls.push(['focus']); } },
    createElement() { return input; }, body: { append() {} },
    execCommand(command) { calls.push([command, input.value]); return copied; },
  } });
  t.after(() => {
    if (previousDocument) Object.defineProperty(globalThis, 'document', previousDocument);
    else delete globalThis.document;
  });
  return calls;
}

function nativeClipboard(t, invoke) {
  for (const [key, value] of [['isTauri', true], ['window', { __TAURI_INTERNALS__: { invoke } }]]) {
    const original = Object.getOwnPropertyDescriptor(globalThis, key);
    Object.defineProperty(globalThis, key, { configurable: true, value });
    t.after(() => { if (original) Object.defineProperty(globalThis, key, original); else delete globalThis[key]; });
  }
}

test('native copying uses IPC with the exact text and does not require browser clipboard permission', async t => {
  const calls = [];
  nativeClipboard(t, async (command, args) => { calls.push({ command, text: args.text }); });
  const value = `CODEX_HOME='/path with spaces' codex --profile hiroute`;
  await copyText(value);
  assert.deepEqual(calls, [{ command: 'plugin:clipboard-manager|write_text', text: value }]);
});

test('native clipboard failure propagates to the caller for a manual-copy fallback', async t => {
  nativeClipboard(t, async () => { throw new Error('CLIPBOARD_BUSY'); });
  await assert.rejects(copyText('launch command'), /CLIPBOARD_BUSY/);
});

test('copy succeeds through the clipboard API without changing focus', async t => {
  let actual;
  const calls = webview(t, { async writeText(value) { actual = value; } });
  await copyText('launch command');
  assert.equal(actual, 'launch command');
  assert.deepEqual(calls, []);
});

test('native WebViews with a missing or rejected clipboard API still copy the exact command', async t => {
  for (const clipboard of [undefined, { async writeText() { throw new Error('NotAllowedError'); } }]) {
    await t.test(clipboard ? 'rejected' : 'missing', async sub => {
      const calls = webview(sub, clipboard);
      const command = `CODEX_HOME='/path with spaces' codex --profile hiroute`;
      await copyText(command);
      assert.deepEqual(calls, [['select', command], ['copy', command], ['remove'], ['focus']]);
    });
  }
});

test('failed copying reports failure and removes the temporary field', async t => {
  const calls = webview(t, undefined, false);
  await assert.rejects(copyText('launch command'), /CLIPBOARD_UNAVAILABLE/);
  assert.deepEqual(calls.slice(-2), [['remove'], ['focus']]);
});