import { describe, it, expect, vi, beforeEach } from 'vitest'; import { listActionsTool, proposeActionTool } from '../../../../lib/ai/agent/tools/blue/actions.js'; import { infraAuditTool } from '../../../../lib/ai/agent/tools/blue/infra_audit.js'; beforeEach(() => { process.env.VOID_API_URL = 'http://127.0.0.1:3000'; process.env.VOID_AGENT_TOKEN = 'blue-tok'; }); describe('blue action tools', () => { it('list_actions GETs the whitelist with the agent bearer', async () => { const fetchMock = vi.fn(async () => ({ ok: true, json: async () => ({ actions: [{ id: 'restart-caddy-ct100', tier: 'safe' }] }) })); const out = await listActionsTool.handler({}, {}, { fetchImpl: fetchMock }); expect(out.actions[0].id).toBe('restart-caddy-ct100'); const [url, opts] = fetchMock.mock.calls[0]; expect(url).toBe('http://127.0.0.1:3000/api/actions'); expect(opts.headers.Authorization).toBe('Bearer blue-tok'); }); it('propose_action POSTs run and returns the queued/executed result', async () => { const fetchMock = vi.fn(async () => ({ ok: true, json: async () => ({ queued: true, action_row_id: 'r1' }) })); const out = await proposeActionTool.handler({ action_id: 'stop-ct107' }, {}, { fetchImpl: fetchMock }); expect(out.queued).toBe(true); expect(fetchMock.mock.calls[0][0]).toBe('http://127.0.0.1:3000/api/actions/stop-ct107/run'); expect(fetchMock.mock.calls[0][1].method).toBe('POST'); }); it('infra_audit GETs the read-only audit with the agent bearer', async () => { const fetchMock = vi.fn(async () => ({ ok: true, json: async () => ({ ok: false, docs: { summary: { unreachable: 1 } } }) })); const out = await infraAuditTool.handler({}, {}, { fetchImpl: fetchMock }); expect(out.ok).toBe(false); const [url, opts] = fetchMock.mock.calls[0]; expect(url).toBe('http://127.0.0.1:3000/api/infra/audit'); expect(opts.headers.Authorization).toBe('Bearer blue-tok'); }); });