import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { expandHome, shellQuote, sshBaseArgs, sshTarget } from '../src/ssh.js'; import { normalizeMachine, pickMachine } from '../src/machines.js'; describe('SSH remote machine config', () => { it('expands ~ in identityFile and quotes remote commands', () => { assert.equal(expandHome('~/foo'), `${os.homedir()}/foo`); assert.equal(shellQuote("a'b"), `'a'"'"'b'`); }); it('builds ssh argv with user, host, port, and key path (not key bytes)', () => { const key = path.join(os.tmpdir(), 'verae-fleet-test-id'); fs.writeFileSync(key, 'not-a-real-key\n', { mode: 0o600 }); const m = normalizeMachine({ id: 'ns1', kind: 'ssh', host: '70.88.205.138', user: 'marchon', sshPort: 22, identityFile: key, }); assert.equal(m.kind, 'ssh'); assert.equal(sshTarget(m), 'marchon@70.88.205.138'); const args = sshBaseArgs(m); assert.ok(args.includes('-i')); assert.ok(args.includes(key)); assert.ok(!args.some((a) => a.includes('BEGIN'))); assert.equal(m.user, 'marchon'); fs.unlinkSync(key); }); it('places load on ssh hosts when they have free capacity', () => { const machines = [ normalizeMachine({ id: 'local', kind: 'local', host: '127.0.0.1', capacity: 2, roles: ['*'] }), normalizeMachine({ id: 'ns1', kind: 'ssh', host: '70.88.205.138', user: 'marchon', identityFile: '~/.ssh/id_ed25519', capacity: 8, roles: ['tree-node'], }), ]; const instances = [ { machine: 'local', pid: 1 }, { machine: 'local', pid: 2 }, ]; const pick = pickMachine(machines, instances, 'tree-node', 'tree-node'); assert.equal(pick.id, 'ns1'); }); });