Phase 6 of the roadmap. Populates the identity/teams and orchestration/hierarchy module directories (previously empty or near-empty scaffold stubs) and replaces identity/agents' pre-existing placeholder, which queried a generic User/UserRole model unrelated to this system's real architecture (nothing since 002-saas-integration's CustomerReference has used it). Teams and agents: active/inactive CRUD, never hard deletion; team deactivation never cascades to its agents. Skills: compound-unique upsert on (agentId, skillTag), never duplicates. Availability: a single current record per agent, deliberately last-write-wins rather than optimistic-locked — operational telemetry, not a durable business record. The dynamic hierarchy: a nestable, orderable HierarchyNode tree with every field (scope, assignment strategy reference, entry/exit conditions) stored as opaque admin-set data; cycle detection runs only on reparenting edits (a new node can't form a cycle); every create/edit/activate/deactivate is audited via AuditLog, reusing 002's existing writer pattern. A capability-eligibility read path composes hierarchy scope with caller-supplied skills for the future orchestration/assignment phase (Phase 7) to call, deliberately excluding availability per doc 05's own capability-before-availability ordering. Found and fixed a real bug before it reached tests: the initial availability upsert reset currentLoad to 0 on every update, not just creation. Adds 11 unit tests (cycle detection, capability matching) and 4 integration test files covering all four user stories. Full regression (every pre-existing 002-005 integration test plus all new ones) run against real Postgres/Redis/MinIO: 107 passed, 9 skipped (005's AI-key-gated tests, unrelated), 0 failed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
93 lines
3.2 KiB
TypeScript
93 lines
3.2 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { buildApp } from '@/app';
|
|
import { prismaClient } from '@/infrastructure/database';
|
|
import { FastifyInstance } from 'fastify';
|
|
|
|
/** Covers specs/006-support-organization/quickstart.md Scenario 2 against a real Postgres. */
|
|
describe('Support organization — agent skills and availability (User Story 2)', () => {
|
|
let app: FastifyInstance;
|
|
const teamName = `Test Skills Team ${Date.now()}`;
|
|
let teamId: string;
|
|
let agentId: string;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildApp();
|
|
const team = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/teams',
|
|
payload: { name: teamName },
|
|
});
|
|
teamId = team.json().data.id;
|
|
const agent = await app.inject({
|
|
method: 'POST',
|
|
url: `/admin/teams/${teamId}/agents`,
|
|
payload: { name: 'Skilled Agent' },
|
|
});
|
|
agentId = agent.json().data.id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await prismaClient.agentSkill.deleteMany({ where: { agentId } });
|
|
await prismaClient.agentAvailability.deleteMany({ where: { agentId } });
|
|
await prismaClient.agent.deleteMany({ where: { teamId } });
|
|
await prismaClient.team.deleteMany({ where: { id: teamId } });
|
|
await app.close();
|
|
});
|
|
|
|
it('Scenario 2: skill upsert never duplicates, availability upsert stays single-record', async () => {
|
|
const addSkill = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/agents/${agentId}/skills/pdf_conversion`,
|
|
payload: { level: 3 },
|
|
});
|
|
expect(addSkill.statusCode).toBe(200);
|
|
|
|
const updateSkill = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/agents/${agentId}/skills/pdf_conversion`,
|
|
payload: { level: 5 },
|
|
});
|
|
expect(updateSkill.statusCode).toBe(200);
|
|
|
|
const skills = await app.inject({ method: 'GET', url: `/admin/agents/${agentId}/skills` });
|
|
const pdfSkills = skills
|
|
.json()
|
|
.data.filter((s: { skillTag: string }) => s.skillTag === 'pdf_conversion');
|
|
expect(pdfSkills).toHaveLength(1);
|
|
expect(pdfSkills[0].level).toBe(5);
|
|
|
|
const setAvailability = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/agents/${agentId}/availability`,
|
|
payload: { status: 'busy', workingHours: { mon: '9-17' } },
|
|
});
|
|
expect(setAvailability.statusCode).toBe(200);
|
|
expect(setAvailability.json().data.currentLoad).toBe(0);
|
|
|
|
const updateAvailability = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/agents/${agentId}/availability`,
|
|
payload: { status: 'available', workingHours: { mon: '9-17' } },
|
|
});
|
|
expect(updateAvailability.statusCode).toBe(200);
|
|
|
|
const current = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/agents/${agentId}/availability`,
|
|
});
|
|
expect(current.json().data.status).toBe('available');
|
|
|
|
const allRecords = await prismaClient.agentAvailability.findMany({ where: { agentId } });
|
|
expect(allRecords).toHaveLength(1);
|
|
});
|
|
|
|
it('rejects an invalid availability status', async () => {
|
|
const response = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/agents/${agentId}/availability`,
|
|
payload: { status: 'not_a_real_status', workingHours: {} },
|
|
});
|
|
expect(response.statusCode).toBe(400);
|
|
});
|
|
});
|