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>
94 lines
3.2 KiB
TypeScript
94 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 1 against a real Postgres. */
|
|
describe('Support organization — teams and agents (User Story 1)', () => {
|
|
let app: FastifyInstance;
|
|
const teamName = `Test Team ${Date.now()}`;
|
|
let teamId: string;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildApp();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await prismaClient.agentSkill.deleteMany({ where: { agent: { team: { name: teamName } } } });
|
|
await prismaClient.agentAvailability.deleteMany({
|
|
where: { agent: { team: { name: teamName } } },
|
|
});
|
|
await prismaClient.agent.deleteMany({ where: { team: { name: teamName } } });
|
|
await prismaClient.team.deleteMany({ where: { name: teamName } });
|
|
await app.close();
|
|
});
|
|
|
|
it('Scenario 1: create, roster, deactivate/reactivate, team deactivation does not cascade', async () => {
|
|
const createTeam = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/teams',
|
|
payload: { name: teamName },
|
|
});
|
|
expect(createTeam.statusCode).toBe(201);
|
|
expect(createTeam.json().data.active).toBe(true);
|
|
teamId = createTeam.json().data.id;
|
|
|
|
const createAgent = await app.inject({
|
|
method: 'POST',
|
|
url: `/admin/teams/${teamId}/agents`,
|
|
payload: { name: 'Agent A' },
|
|
});
|
|
expect(createAgent.statusCode).toBe(201);
|
|
const agentId = createAgent.json().data.id;
|
|
|
|
const roster = await app.inject({ method: 'GET', url: `/admin/teams/${teamId}` });
|
|
expect(roster.json().data.agents.map((a: { id: string }) => a.id)).toContain(agentId);
|
|
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/agents/${agentId}`,
|
|
payload: { active: false },
|
|
});
|
|
const activeListing = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/agents?active=true&teamId=${teamId}`,
|
|
});
|
|
expect(activeListing.json().data.map((a: { id: string }) => a.id)).not.toContain(agentId);
|
|
|
|
const directFetch = await app.inject({ method: 'GET', url: `/admin/agents/${agentId}` });
|
|
expect(directFetch.statusCode).toBe(200);
|
|
expect(directFetch.json().data.active).toBe(false);
|
|
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/agents/${agentId}`,
|
|
payload: { active: true },
|
|
});
|
|
const reactivatedListing = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/agents?active=true&teamId=${teamId}`,
|
|
});
|
|
expect(reactivatedListing.json().data.map((a: { id: string }) => a.id)).toContain(agentId);
|
|
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/teams/${teamId}`,
|
|
payload: { active: false },
|
|
});
|
|
const agentAfterTeamDeactivation = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/agents/${agentId}`,
|
|
});
|
|
expect(agentAfterTeamDeactivation.json().data.active).toBe(true);
|
|
});
|
|
|
|
it('404s creating an agent on a nonexistent team', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/teams/nonexistent-team-id/agents',
|
|
payload: { name: 'Ghost Agent' },
|
|
});
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
});
|