Files
Void-Homelab/tests/ai/agent/tools/propose_change.test.js
2026-06-01 18:16:53 +10:00

44 lines
1.9 KiB
JavaScript

import { describe, it, expect, beforeAll } from 'vitest';
import { pool } from '../../../../lib/db/pool.js';
import { resetDb } from '../../../helpers/db.js';
import { migrateUp } from '../../../../lib/db/migrate.js';
import { proposeChangeTool } from '../../../../lib/ai/agent/tools/propose_change.js';
let spaceId, agentId;
beforeAll(async () => {
await resetDb(); await migrateUp();
({ rows: [{ id: spaceId }] } = await pool.query(
`INSERT INTO spaces(slug,name) VALUES('s','S') RETURNING id`));
({ rows: [{ id: agentId }] } = await pool.query(`SELECT id FROM agents WHERE slug='companion'`));
});
const suggestAgent = (id) => ({ kind: 'agent', id, capabilities: { read: true, suggest: true, write: false }, scopes: {} });
describe('propose_change tool', () => {
it('writes a pending_changes row and never applies', async () => {
const ctx = { agent: suggestAgent(agentId), space_id: spaceId };
const out = await proposeChangeTool.handler(
{ entity_type: 'task', action: 'create', payload: { space_id: spaceId, title: 'Validate CSV' }, reason: 'tracking' },
ctx
);
expect(out.pending_change_id).toBeTruthy();
expect(out.applied).toBe(false);
const { rows } = await pool.query(`SELECT * FROM pending_changes WHERE id=$1`, [out.pending_change_id]);
expect(rows[0].status).toBe('pending');
expect(rows[0].agent_id).toBe(agentId);
const { rows: tasks } = await pool.query(`SELECT * FROM tasks WHERE title='Validate CSV'`);
expect(tasks).toHaveLength(0); // not applied
});
it('refuses when the agent cannot even suggest', async () => {
const denied = { kind: 'agent', id: agentId, capabilities: { read: true, suggest: false, write: false }, scopes: {} };
const out = await proposeChangeTool.handler(
{ entity_type: 'task', action: 'create', payload: { title: 'x' } },
{ agent: denied, space_id: spaceId }
);
expect(out.error).toMatch(/not permitted/i);
});
});