feat(repos): agents (+ tokens + caps), conversations, messages

This commit is contained in:
root
2026-05-31 10:36:40 +10:00
parent 5e094f347e
commit 1d799105ac
5 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
import * as agents from '../../lib/db/repos/agents.js';
const owner = { kind: 'user', id: null };
beforeEach(async () => { await resetDb(); await migrateUp(); });
describe('agents repo', () => {
it('creates agent with default capabilities', async () => {
const a = await agents.create({ slug: 'mercy', name: 'Mercy', kind: 'claude' }, owner);
expect(a.capabilities.read).toBe(true);
expect(a.capabilities.write).toBe(false);
});
it('createToken returns plaintext, verifyToken finds the agent', async () => {
const a = await agents.create({ slug: 'mercy', name: 'Mercy', kind: 'claude' }, owner);
const { token, id } = await agents.createToken(a.id, 'default');
expect(token).toMatch(/^vk_/);
const found = await agents.verifyToken(token);
expect(found?.id).toBe(a.id);
});
it('revoked token does not verify', async () => {
const a = await agents.create({ slug: 'mercy', name: 'Mercy', kind: 'claude' }, owner);
const { token, id } = await agents.createToken(a.id, 'default');
await agents.revokeToken(id);
expect(await agents.verifyToken(token)).toBeNull();
});
it('setCapabilities updates the jsonb', async () => {
const a = await agents.create({ slug: 'mercy', name: 'Mercy', kind: 'claude' }, owner);
const u = await agents.setCapabilities(a.id, { read: true, suggest: true, write: true }, { pages: true });
expect(u.capabilities.write).toBe(true);
expect(u.scopes.pages).toBe(true);
});
});

View File

@@ -0,0 +1,21 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
import * as agents from '../../lib/db/repos/agents.js';
import * as conversations from '../../lib/db/repos/conversations.js';
import * as messages from '../../lib/db/repos/messages.js';
const owner = { kind: 'user', id: null };
beforeEach(async () => { await resetDb(); await migrateUp(); });
describe('conversations + messages', () => {
it('creates a conversation and appends messages', async () => {
const a = await agents.create({ slug: 'mercy', name: 'Mercy', kind: 'claude' }, owner);
const c = await conversations.create({ title: 'hi', agent_id: a.id }, owner);
const m1 = await messages.append(c.id, { role: 'user', body: 'hello' });
const m2 = await messages.append(c.id, { role: 'assistant', body: 'hi', agent_id: a.id });
const list = await messages.listByConversation(c.id);
expect(list).toHaveLength(2);
expect(list[0].id).toBe(m1.id);
});
});