2026-07-27 15:17:14 +02:00
|
|
|
/**
|
|
|
|
|
* Tests for copyKey.
|
|
|
|
|
*
|
|
|
|
|
* inquirer and execa are mocked so nothing touches a TTY or the real
|
|
|
|
|
* clipboard; the key directories are real temp directories.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import os from 'node:os';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
|
|
2026-07-30 15:42:35 +02:00
|
|
|
const { execa, prompt } = vi.hoisted(() => ({ execa: vi.fn(), prompt: vi.fn() }));
|
2026-07-27 15:17:14 +02:00
|
|
|
|
|
|
|
|
vi.mock('execa', () => ({ execa }));
|
|
|
|
|
vi.mock('inquirer', () => ({ default: { prompt } }));
|
|
|
|
|
|
|
|
|
|
import { copyKey } from '../src/keyman.copy.js';
|
|
|
|
|
|
|
|
|
|
describe('copyKey', () => {
|
|
|
|
|
let root: string;
|
|
|
|
|
let sshDir: string;
|
|
|
|
|
let tmpDir: string;
|
|
|
|
|
let logSpy: ReturnType<typeof vi.spyOn>;
|
|
|
|
|
let errorSpy: ReturnType<typeof vi.spyOn>;
|
2026-07-30 15:42:35 +02:00
|
|
|
let warnSpy: ReturnType<typeof vi.spyOn>;
|
2026-07-27 15:17:14 +02:00
|
|
|
|
|
|
|
|
const touch = (dir: string, file: string, contents = '') => {
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
fs.writeFileSync(path.join(dir, file), contents);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const messages = (spy: ReturnType<typeof vi.spyOn>) =>
|
|
|
|
|
spy.mock.calls.map((c) => c.join(' ')).join('\n');
|
|
|
|
|
|
|
|
|
|
/** The choices offered by the last inquirer.prompt call. */
|
|
|
|
|
const choices = () => prompt.mock.calls.at(-1)?.[0][0].choices as string[];
|
|
|
|
|
|
2026-07-30 15:42:35 +02:00
|
|
|
/** What was piped into the clipboard command. */
|
|
|
|
|
const piped = () => (execa.mock.calls.at(-1)?.[2] as { input?: string } | undefined)?.input;
|
|
|
|
|
|
2026-07-27 15:17:14 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'keyman-copy-')));
|
|
|
|
|
sshDir = path.join(root, '.ssh');
|
|
|
|
|
tmpDir = path.join(root, 'tmp');
|
|
|
|
|
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
|
|
|
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
2026-07-30 15:42:35 +02:00
|
|
|
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
2026-07-27 15:17:14 +02:00
|
|
|
|
2026-07-30 15:42:35 +02:00
|
|
|
execa.mockResolvedValue({ stdout: '' });
|
2026-07-27 15:17:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('warns when neither directory exists', async () => {
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
expect(messages(logSpy)).toContain('No SSH keys found.');
|
|
|
|
|
expect(prompt).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('warns when the directories hold no private keys', async () => {
|
|
|
|
|
touch(sshDir, 'known_hosts');
|
|
|
|
|
touch(tmpDir, 'id_prod.pub');
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
expect(messages(logSpy)).toContain('No SSH keys found.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('offers the keys from both directories without duplicates', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
touch(sshDir, 'id_prod.pub');
|
|
|
|
|
touch(tmpDir, 'id_prod');
|
|
|
|
|
touch(tmpDir, 'id_stage');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
touch(tmpDir, 'id_prod.pub');
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
expect(choices()).toEqual(['id_prod', 'id_stage']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('copies the trimmed public key from tmp to the clipboard', async () => {
|
|
|
|
|
touch(tmpDir, 'id_prod');
|
|
|
|
|
touch(tmpDir, 'id_prod.pub', 'ssh-ed25519 AAAA tmp\n');
|
|
|
|
|
touch(sshDir, 'id_prod.pub', 'ssh-ed25519 AAAA ssh\n');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
2026-07-30 15:42:35 +02:00
|
|
|
expect(piped()).toBe('ssh-ed25519 AAAA tmp');
|
2026-07-27 15:17:14 +02:00
|
|
|
expect(messages(logSpy)).toContain('copied to clipboard');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('falls back to the public key in .ssh', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
touch(sshDir, 'id_prod.pub', 'ssh-ed25519 AAAA ssh\n');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
2026-07-30 15:42:35 +02:00
|
|
|
expect(piped()).toBe('ssh-ed25519 AAAA ssh');
|
2026-07-27 15:17:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('reports a missing public key without invoking the clipboard', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
expect(messages(errorSpy)).toContain('Public key not found for id_prod');
|
|
|
|
|
expect(execa).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('reports a clipboard failure instead of throwing', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
touch(sshDir, 'id_prod.pub', 'ssh-ed25519 AAAA ssh');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
2026-07-30 15:42:35 +02:00
|
|
|
execa.mockRejectedValue(Object.assign(new Error('refused'), { stderr: 'no display' }));
|
2026-07-27 15:17:14 +02:00
|
|
|
|
|
|
|
|
await expect(copyKey(sshDir, tmpDir)).resolves.toBeUndefined();
|
|
|
|
|
expect(messages(errorSpy)).toContain('Failed to copy to clipboard');
|
|
|
|
|
});
|
2026-07-30 15:42:35 +02:00
|
|
|
|
|
|
|
|
it('prints the key when no clipboard command exists at all', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
touch(sshDir, 'id_prod.pub', 'ssh-ed25519 AAAA ssh');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
execa.mockImplementation(async () => {
|
|
|
|
|
throw Object.assign(new Error('spawn ENOENT'), { code: 'ENOENT' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
// The operation is "give me this public key". Without a clipboard it is still
|
|
|
|
|
// answerable, and it used to be a dead end on every platform but macOS.
|
|
|
|
|
expect(messages(logSpy)).toContain('ssh-ed25519 AAAA ssh');
|
|
|
|
|
expect(messages(warnSpy)).toContain('No clipboard command found');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('names the private keys it cannot manage', async () => {
|
|
|
|
|
touch(sshDir, 'id_prod');
|
|
|
|
|
touch(sshDir, 'id_prod.pub', 'PUBLIC');
|
|
|
|
|
touch(sshDir, 'deploy_ed25519', '-----BEGIN OPENSSH PRIVATE KEY-----\nAAAA\n');
|
|
|
|
|
prompt.mockResolvedValue({ selectedKey: 'id_prod' });
|
|
|
|
|
|
|
|
|
|
await copyKey(sshDir, tmpDir);
|
|
|
|
|
|
|
|
|
|
expect(choices()).toEqual(['id_prod']);
|
|
|
|
|
expect(messages(logSpy)).toContain('deploy_ed25519');
|
|
|
|
|
expect(messages(logSpy)).toContain('not named id_*');
|
|
|
|
|
});
|
2026-07-27 15:17:14 +02:00
|
|
|
});
|