Files
Netcatty/infrastructure/ai/vaultAgentBridgeClient.test.ts
T

697 lines
21 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import type { Host, Snippet, VaultNote } from '../../domain/models';
import { handleVaultAgentOp, runSerializedVaultAgentRequest, type VaultAgentApiDeps } from './vaultAgentBridgeClient';
type DepsSeed = {
hosts?: Host[];
notes?: VaultNote[];
snippets?: Snippet[];
customGroups?: string[];
};
function createDeps(
overrides: Partial<VaultAgentApiDeps> & DepsSeed = {},
): VaultAgentApiDeps {
let hosts = overrides.hosts ?? [];
let notes = overrides.notes ?? [];
let snippets = overrides.snippets ?? [];
let customGroups = overrides.customGroups ?? [];
const base: VaultAgentApiDeps = {
getHosts: () => hosts,
getNotes: () => notes,
getCustomGroups: () => customGroups,
snippets,
portForwardingRules: [],
keys: [],
identities: [],
resolveEffectiveHost: (host) => host,
updateHostNotes: () => {},
updateCustomGroups: (groups) => {
customGroups = groups;
},
updateHosts: (nextHosts) => {
hosts = nextHosts;
},
updateNotes: (nextNotes) => {
notes = nextNotes;
},
updateSnippets: (nextSnippets) => {
snippets = nextSnippets;
},
startTunnel: async () => ({ success: true }),
stopTunnel: async () => ({ success: true }),
openHost: (hostId) => {
const host = hosts.find((entry) => entry.id === hostId);
if (!host) return { ok: false, error: `Host "${hostId}" was not found.` };
return { ok: true, sessionId: `session-${hostId}`, host };
},
};
return {
...base,
...overrides,
get snippets() {
return snippets;
},
getHosts: overrides.getHosts ?? base.getHosts,
getNotes: overrides.getNotes ?? base.getNotes,
getCustomGroups: overrides.getCustomGroups ?? base.getCustomGroups,
updateHosts: (nextHosts) => {
base.updateHosts(nextHosts);
overrides.updateHosts?.(nextHosts);
},
updateNotes: (nextNotes) => {
base.updateNotes(nextNotes);
overrides.updateNotes?.(nextNotes);
},
updateSnippets: (nextSnippets) => {
base.updateSnippets(nextSnippets);
overrides.updateSnippets?.(nextSnippets);
},
updateCustomGroups: (groups) => {
base.updateCustomGroups(groups);
overrides.updateCustomGroups?.(groups);
},
};
}
describe('handleVaultAgentOp vault notes', () => {
it('note.create persists to updateNotes and returns the new note', async () => {
const updated: VaultNote[][] = [];
const deps = createDeps({
notes: [],
updateNotes: (nextNotes) => {
updated.push(nextNotes);
},
});
const result = await handleVaultAgentOp(
'note.create',
{ title: 'Deploy runbook', content: '# Steps\n1. Connect' },
deps,
);
assert.equal(result.ok, true);
assert.equal(updated.length, 1);
assert.equal(updated[0]?.length, 1);
assert.equal(updated[0]?.[0]?.title, 'Deploy runbook');
assert.equal(updated[0]?.[0]?.content, '# Steps\n1. Connect');
assert.equal((result as { note?: VaultNote }).note?.id, updated[0]?.[0]?.id);
});
it('note.list returns summaries without full content', async () => {
const note: VaultNote = {
id: 'note-1',
title: 'Existing',
content: 'secret body',
createdAt: 1,
updatedAt: 2,
};
const result = await handleVaultAgentOp('note.list', {}, createDeps({ notes: [note] }));
assert.equal(result.ok, true);
const listed = (result as { notes?: Array<{ id: string; contentLength: number; title: string }> }).notes;
assert.equal(listed?.length, 1);
assert.equal(listed?.[0]?.title, 'Existing');
assert.equal(listed?.[0]?.contentLength, 'secret body'.length);
assert.equal('content' in (listed?.[0] ?? {}), false);
});
it('note.update replaces content and bumps updatedAt', async () => {
const existing: VaultNote = {
id: 'note-1',
title: 'Old title',
content: 'old',
createdAt: 100,
updatedAt: 100,
};
const updated: VaultNote[][] = [];
const deps = createDeps({
notes: [existing],
updateNotes: (nextNotes) => {
updated.push(nextNotes);
},
});
const result = await handleVaultAgentOp(
'note.update',
{ noteId: 'note-1', title: 'New title', content: 'new body' },
deps,
);
assert.equal(result.ok, true);
assert.equal(updated[0]?.[0]?.title, 'New title');
assert.equal(updated[0]?.[0]?.content, 'new body');
assert.ok((updated[0]?.[0]?.updatedAt ?? 0) >= 100);
});
it('sequential note.create calls accumulate instead of overwriting prior notes', async () => {
const deps = createDeps({ notes: [] });
const first = await handleVaultAgentOp(
'note.create',
{ title: 'First', content: 'one' },
deps,
);
const second = await handleVaultAgentOp(
'note.create',
{ title: 'Second', content: 'two' },
deps,
);
assert.equal(first.ok, true);
assert.equal(second.ok, true);
assert.equal(deps.getNotes().length, 2);
assert.deepEqual(deps.getNotes().map((note) => note.title), ['First', 'Second']);
});
it('host.notes.set still updates host metadata separately from vault notes', async () => {
const host: Host = {
id: 'host-1',
label: 'prod',
hostname: '10.0.0.1',
username: 'root',
notes: '',
};
let hostNotes = '';
const deps = createDeps({
hosts: [host],
updateHostNotes: (hostId, notes) => {
assert.equal(hostId, 'host-1');
hostNotes = notes;
},
});
const result = await handleVaultAgentOp(
'host.notes.set',
{ hostId: 'host-1', notes: 'host detail memo' },
deps,
);
assert.equal(result.ok, true);
assert.equal(hostNotes, 'host detail memo');
});
});
describe('handleVaultAgentOp vault hosts', () => {
const csvText = [
'Label,Hostname,Port,Username,Groups',
'web-1,10.0.0.10,22,deploy,prod/web',
'db-1,10.0.0.20,22,root,prod/db',
].join('\n');
it('host.list returns metadata without passwords', async () => {
const host: Host = {
id: 'host-1',
label: 'prod',
hostname: '10.0.0.1',
username: 'root',
password: 'secret',
port: 22,
};
const result = await handleVaultAgentOp('host.list', {}, createDeps({ hosts: [host] }));
assert.equal(result.ok, true);
const hosts = (result as { hosts?: Array<Record<string, unknown>> }).hosts;
assert.equal(hosts?.length, 1);
assert.equal(hosts?.[0]?.hostname, '10.0.0.1');
assert.equal('password' in (hosts?.[0] ?? {}), false);
});
it('host.open creates a terminal session for a vault host', async () => {
const host: Host = {
id: 'host-open-1',
label: 'edge',
hostname: 'edge.example.com',
username: 'ops',
port: 22,
};
const result = await handleVaultAgentOp(
'host.open',
{ hostId: 'host-open-1', chatSessionId: 'chat-1' },
createDeps({ hosts: [host] }),
);
assert.equal(result.ok, true);
assert.equal((result as { sessionId?: string }).sessionId, 'session-host-open-1');
assert.equal((result as { hostId?: string }).hostId, 'host-open-1');
assert.equal((result as { status?: string }).status, 'connecting');
assert.equal((result as { host?: { hostname?: string } }).host?.hostname, 'edge.example.com');
});
it('host.open fails for missing host ids', async () => {
const result = await handleVaultAgentOp(
'host.open',
{ hostId: 'missing' },
createDeps({ hosts: [] }),
);
assert.equal(result.ok, false);
assert.match(String((result as { error?: string }).error), /not found/i);
});
it('host.open requires hostId', async () => {
const result = await handleVaultAgentOp('host.open', {}, createDeps({ hosts: [] }));
assert.equal(result.ok, false);
assert.match(String((result as { error?: string }).error), /hostId/i);
});
it('hosts.create maps structured JSON from arbitrary text into vault hosts', async () => {
const updatedHosts: Host[][] = [];
const unstructuredMapped = JSON.stringify([
{ label: 'Prod API', hostname: 'api.example.com', username: 'deploy', port: 22, group: 'prod/api' },
{ label: 'Staging DB', hostname: '10.20.0.5', username: 'postgres', tags: ['db', 'staging'] },
]);
const result = await handleVaultAgentOp(
'hosts.create',
{ hosts: unstructuredMapped },
createDeps({
updateHosts: (hosts) => {
updatedHosts.push(hosts);
},
updateCustomGroups: () => {},
}),
);
assert.equal(result.ok, true);
assert.equal(updatedHosts[0]?.length, 2);
assert.equal((result as { addedCount?: number }).addedCount, 2);
});
it('hosts.create stores a referenced key path without private key content', async () => {
const deps = createDeps({ hosts: [] });
const result = await handleVaultAgentOp(
'hosts.create',
{
hosts: JSON.stringify([
{ hostname: 'key.example.com', username: 'deploy', keyPath: '~/.ssh/id_ed25519' },
]),
},
deps,
);
assert.equal(result.ok, true);
assert.deepEqual(deps.getHosts()[0]?.identityFilePaths, ['~/.ssh/id_ed25519']);
assert.equal(deps.getHosts()[0]?.authMethod, 'key');
const preview = (result as { previewHosts?: Array<Record<string, unknown>> }).previewHosts?.[0];
assert.equal('privateKey' in (preview ?? {}), false);
});
it('sequential hosts.create calls accumulate instead of dropping prior hosts', async () => {
const deps = createDeps({ hosts: [], customGroups: [] });
const first = await handleVaultAgentOp(
'hosts.create',
{ hosts: JSON.stringify([{ hostname: '10.0.0.1', username: 'root', label: 'first' }]) },
deps,
);
const second = await handleVaultAgentOp(
'hosts.create',
{ hosts: JSON.stringify([{ hostname: '10.0.0.2', username: 'root', label: 'second' }]) },
deps,
);
assert.equal(first.ok, true);
assert.equal(second.ok, true);
assert.equal(deps.getHosts().length, 2);
assert.deepEqual(deps.getHosts().map((host) => host.hostname), ['10.0.0.1', '10.0.0.2']);
});
it('host.update changes selected fields and preserves unrelated host data', async () => {
const deps = createDeps({
hosts: [{
id: 'host-1',
label: 'old',
hostname: '10.0.0.1',
username: 'root',
port: 22,
tags: ['keep'],
os: 'linux',
}],
customGroups: [],
});
const result = await handleVaultAgentOp(
'host.update',
{ hostId: 'host-1', name: 'new', ip: '10.0.0.2', group: 'prod' },
deps,
);
assert.equal(result.ok, true);
assert.equal(deps.getHosts()[0]?.label, 'new');
assert.equal(deps.getHosts()[0]?.hostname, '10.0.0.2');
assert.equal(deps.getHosts()[0]?.username, 'root');
assert.deepEqual(deps.getHosts()[0]?.tags, ['keep']);
assert.ok(deps.getCustomGroups().includes('prod'));
});
it('host.update applies effective inherited credential settings', async () => {
const host: Host = {
id: 'host-1',
label: 'inherited',
hostname: '10.0.0.1',
username: 'root',
port: 22,
tags: [],
os: 'linux',
};
const passwordDeps = createDeps({
hosts: [host],
resolveEffectiveHost: (current) => ({ ...current, savePassword: false }),
});
const identityDeps = createDeps({
hosts: [host],
resolveEffectiveHost: (current) => ({ ...current, identityId: 'identity-from-group' }),
});
const passwordResult = await handleVaultAgentOp(
'host.update',
{ hostId: host.id, password: 'do-not-persist' },
passwordDeps,
);
const usernameResult = await handleVaultAgentOp(
'host.update',
{ hostId: host.id, username: 'deploy' },
identityDeps,
);
assert.equal(passwordResult.ok, false);
assert.equal(passwordDeps.getHosts()[0]?.password, undefined);
assert.equal(usernameResult.ok, true);
assert.equal(identityDeps.getHosts()[0]?.username, 'deploy');
assert.equal(identityDeps.getHosts()[0]?.identityId, '');
});
it('host.update aligns managed-source ownership when moving groups', async () => {
const deps = createDeps({
hosts: [{
id: 'host-1',
label: 'managed host',
hostname: '10.0.0.1',
username: 'root',
group: 'managed',
tags: [],
os: 'linux',
managedSourceId: 'source-1',
}],
managedSources: [{
id: 'source-1',
type: 'ssh_config',
filePath: '~/.ssh/config',
groupName: 'managed',
lastSyncedAt: 1,
}],
});
const result = await handleVaultAgentOp(
'host.update',
{ hostId: 'host-1', group: '' },
deps,
);
assert.equal(result.ok, true);
assert.equal(deps.getHosts()[0]?.group, undefined);
assert.equal(deps.getHosts()[0]?.managedSourceId, undefined);
});
it('host.delete removes the requested host', async () => {
const deps = createDeps({
hosts: [
{ id: 'host-1', label: 'one', hostname: 'one', username: 'root', tags: [], os: 'linux' },
{ id: 'host-2', label: 'two', hostname: 'two', username: 'root', tags: [], os: 'linux' },
],
});
const result = await handleVaultAgentOp('host.delete', { hostId: 'host-1' }, deps);
assert.equal(result.ok, true);
assert.deepEqual(deps.getHosts().map((host) => host.id), ['host-2']);
assert.equal((result as { deletedHost?: { id?: string } }).deletedHost?.id, 'host-1');
});
it('host.update and host.delete never return nested proxy passwords', async () => {
const host: Host = {
id: 'host-1',
label: 'proxied',
hostname: '10.0.0.1',
username: 'root',
tags: [],
os: 'linux',
proxyConfig: {
type: 'http',
host: 'proxy.example.com',
port: 8080,
username: 'proxy-user',
password: 'proxy-secret',
},
};
const updateDeps = createDeps({ hosts: [host] });
const deleteDeps = createDeps({ hosts: [host] });
const updateResult = await handleVaultAgentOp(
'host.update',
{ hostId: host.id, notes: 'updated' },
updateDeps,
);
const deleteResult = await handleVaultAgentOp(
'host.delete',
{ hostId: host.id },
deleteDeps,
);
assert.equal(updateResult.ok, true);
assert.equal(deleteResult.ok, true);
const updatedProxy = (updateResult as {
host?: { proxyConfig?: Record<string, unknown> };
}).host?.proxyConfig;
const deletedProxy = (deleteResult as {
deletedHost?: { proxyConfig?: Record<string, unknown> };
}).deletedHost?.proxyConfig;
assert.equal(updatedProxy?.host, 'proxy.example.com');
assert.equal(deletedProxy?.host, 'proxy.example.com');
assert.equal('password' in (updatedProxy ?? {}), false);
assert.equal('password' in (deletedProxy ?? {}), false);
});
it('host.import dryRun previews parsed hosts without writing', async () => {
const updatedHosts: Host[][] = [];
const result = await handleVaultAgentOp(
'host.import',
{ format: 'csv', text: csvText, dryRun: 'true' },
createDeps({
updateHosts: (hosts) => {
updatedHosts.push(hosts);
},
}),
);
assert.equal(result.ok, true);
assert.equal((result as { dryRun?: boolean }).dryRun, true);
assert.equal(updatedHosts.length, 0);
assert.equal((result as { previewHosts?: unknown[] }).previewHosts?.length, 2);
});
it('host.import applies hosts to the vault', async () => {
const updatedHosts: Host[][] = [];
const updatedGroups: string[][] = [];
const result = await handleVaultAgentOp(
'host.import',
{ format: 'auto', text: csvText },
createDeps({
updateHosts: (hosts) => {
updatedHosts.push(hosts);
},
updateCustomGroups: (groups) => {
updatedGroups.push(groups);
},
}),
);
assert.equal(result.ok, true);
assert.equal(updatedHosts[0]?.length, 2);
assert.ok(updatedGroups[0]?.includes('prod/web'));
assert.equal((result as { addedCount?: number }).addedCount, 2);
});
});
describe('handleVaultAgentOp snippets and scripts', () => {
it('snippets.create persists script metadata via updateSnippets', async () => {
const updated: Snippet[][] = [];
const deps = createDeps({
snippets: [],
updateSnippets: (nextSnippets) => {
updated.push(nextSnippets);
},
});
const result = await handleVaultAgentOp(
'snippets.create',
{
label: 'Deploy',
content: 'await nct.screen.sendLine("echo ok");',
kind: 'script',
trigger: 'manual',
},
deps,
);
assert.equal(result.ok, true);
assert.equal(updated.length, 1);
assert.equal(updated[0]?.[0]?.kind, 'script');
assert.equal(updated[0]?.[0]?.label, 'Deploy');
});
it('snippets.create persists multi-line run mode', async () => {
const updated: Snippet[][] = [];
const deps = createDeps({
snippets: [],
updateSnippets: (nextSnippets) => {
updated.push(nextSnippets);
},
});
const result = await handleVaultAgentOp(
'snippets.create',
{
label: 'Login',
content: 'admin\npassword',
multiLineRunMode: 'lineDelay',
},
deps,
);
assert.equal(result.ok, true);
assert.equal(updated[0]?.[0]?.multiLineRunMode, 'lineDelay');
});
it('snippets.update persists multi-line run mode', async () => {
const updated: Snippet[][] = [];
const deps = createDeps({
snippets: [{ id: 'snippet-1', label: 'Login', command: 'admin\npassword', kind: 'snippet' }],
updateSnippets: (nextSnippets) => {
updated.push(nextSnippets);
},
});
const result = await handleVaultAgentOp(
'snippets.update',
{
snippetId: 'snippet-1',
multiLineRunMode: 'lineDelay',
},
deps,
);
assert.equal(result.ok, true);
assert.equal(updated[0]?.[0]?.multiLineRunMode, 'lineDelay');
});
it('snippets.list returns kind and trigger fields', async () => {
const deps = createDeps({
snippets: [{
id: 's1',
label: 'Auto',
command: 'nct.log(1)',
kind: 'script',
trigger: 'onConnect',
targetsAllHosts: true,
}],
});
const result = await handleVaultAgentOp('snippets.list', {}, deps);
assert.equal(result.ok, true);
const listed = (result as { snippets?: Array<{ kind?: string; trigger?: string; targetsAllHosts?: boolean }> }).snippets;
assert.equal(listed?.[0]?.kind, 'script');
assert.equal(listed?.[0]?.trigger, 'onConnect');
assert.equal(listed?.[0]?.targetsAllHosts, true);
});
it('scripts.reference returns nct syntax reference', async () => {
const result = await handleVaultAgentOp('scripts.reference', {}, createDeps());
assert.equal(result.ok, true);
const reference = (result as { reference?: string }).reference ?? '';
assert.match(reference, /nct\.screen\.waitForPrompt/);
});
it('host.connectScripts.set updates host queue', async () => {
const host: Host = {
id: 'host-a',
label: 'A',
hostname: 'a.example',
username: 'root',
os: 'linux',
protocol: 'ssh',
tags: [],
};
const snippets: Snippet[] = [{
id: 'run',
label: 'Run',
command: 'nct.log(1)',
kind: 'script',
trigger: 'onConnect',
targets: ['host-a'],
}];
const deps = createDeps({ hosts: [host], snippets });
const result = await handleVaultAgentOp(
'host.connectScripts.set',
{ hostId: 'host-a', scriptIds: JSON.stringify(['run']) },
deps,
);
assert.equal(result.ok, true);
assert.deepEqual(deps.getHosts()[0]?.connectScriptIds, ['run']);
});
it('scripts.delete removes script from vault', async () => {
const snippets: Snippet[] = [{
id: 'run',
label: 'Run',
command: 'nct.log(1)',
kind: 'script',
}];
const updated: Snippet[][] = [];
const deps = createDeps({
snippets,
updateSnippets: (next) => {
updated.push(next);
},
});
const result = await handleVaultAgentOp('scripts.delete', { scriptId: 'run' }, deps);
assert.equal(result.ok, true);
assert.equal(updated[0]?.length, 0);
});
it('scripts.delete rejects text snippets', async () => {
const deps = createDeps({
snippets: [{ id: 'cmd', label: 'Cmd', command: 'ls', kind: 'snippet' }],
});
const result = await handleVaultAgentOp('scripts.delete', { scriptId: 'cmd' }, deps);
assert.equal(result.ok, false);
});
});
describe('runSerializedVaultAgentRequest', () => {
it('runs concurrent tasks sequentially without losing updates', async () => {
let value = 0;
const results = await Promise.all([
runSerializedVaultAgentRequest(async () => {
const snapshot = value;
await new Promise((resolve) => setTimeout(resolve, 5));
value = snapshot + 1;
return value;
}),
runSerializedVaultAgentRequest(async () => {
const snapshot = value;
await new Promise((resolve) => setTimeout(resolve, 1));
value = snapshot + 1;
return value;
}),
]);
assert.deepEqual(results, [1, 2]);
assert.equal(value, 2);
});
});