Files
support_backend/tests/integration/support-org-capability-lookup.test.ts
T
saqib mirandClaude Sonnet 5 a0c1a9aa33 feat: implement support organization (006) — teams, agents, hierarchy, capability lookup
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>
2026-09-02 18:12:26 +05:30

139 lines
4.8 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 4 against a real Postgres. */
describe('Support organization — capability eligibility lookup (User Story 4)', () => {
let app: FastifyInstance;
const teamName = `Test Capability Team ${Date.now()}`;
const skillX = `skill_x_${Date.now()}`;
const skillY = `skill_y_${Date.now()}`;
const skillZ = `skill_z_${Date.now()}`;
let teamId: string;
let agentAId: string;
let agentBId: string;
const nodeIds: 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 agentA = await app.inject({
method: 'POST',
url: `/admin/teams/${teamId}/agents`,
payload: { name: 'Agent A' },
});
agentAId = agentA.json().data.id;
await app.inject({
method: 'PUT',
url: `/admin/agents/${agentAId}/skills/${skillX}`,
payload: { level: 3 },
});
const agentB = await app.inject({
method: 'POST',
url: `/admin/teams/${teamId}/agents`,
payload: { name: 'Agent B' },
});
agentBId = agentB.json().data.id;
await app.inject({
method: 'PUT',
url: `/admin/agents/${agentBId}/skills/${skillY}`,
payload: { level: 3 },
});
});
afterAll(async () => {
await prismaClient.hierarchyNode.deleteMany({ where: { id: { in: nodeIds } } });
await prismaClient.auditLog.deleteMany({
where: { entityType: 'HierarchyNode', entityId: { in: nodeIds } },
});
await prismaClient.agentSkill.deleteMany({ where: { agentId: { in: [agentAId, agentBId] } } });
await prismaClient.agentAvailability.deleteMany({
where: { agentId: { in: [agentAId, agentBId] } },
});
await prismaClient.agent.deleteMany({ where: { teamId } });
await prismaClient.team.deleteMany({ where: { id: teamId } });
await app.close();
});
it('Scenario 4: skill-only match, availability never filters, inactive excluded, empty on no match, hierarchy-scope composition', async () => {
const onlyX = await app.inject({
method: 'GET',
url: `/support-org/capability-eligibility?skills=${skillX}`,
});
const onlyXIds = onlyX.json().data.map((a: { id: string }) => a.id);
expect(onlyXIds).toContain(agentAId);
expect(onlyXIds).not.toContain(agentBId);
await app.inject({
method: 'PUT',
url: `/admin/agents/${agentAId}/availability`,
payload: { status: 'offline', workingHours: {} },
});
const stillOffline = await app.inject({
method: 'GET',
url: `/support-org/capability-eligibility?skills=${skillX}`,
});
expect(stillOffline.json().data.map((a: { id: string }) => a.id)).toContain(agentAId);
await app.inject({
method: 'PATCH',
url: `/admin/agents/${agentAId}`,
payload: { active: false },
});
const afterDeactivation = await app.inject({
method: 'GET',
url: `/support-org/capability-eligibility?skills=${skillX}`,
});
expect(afterDeactivation.json().data.map((a: { id: string }) => a.id)).not.toContain(agentAId);
const noMatch = await app.inject({
method: 'GET',
url: `/support-org/capability-eligibility?skills=skill_nobody_has_${Date.now()}`,
});
expect(noMatch.statusCode).toBe(200);
expect(noMatch.json().data).toEqual([]);
// Reactivate agent A and give it skillZ too, then create a scoped hierarchy node requiring
// skillZ for a given product — only an agent holding BOTH x and z should be eligible.
await app.inject({
method: 'PATCH',
url: `/admin/agents/${agentAId}`,
payload: { active: true },
});
await app.inject({
method: 'PUT',
url: `/admin/agents/${agentAId}/skills/${skillZ}`,
payload: { level: 2 },
});
const productId = `TEST_CAP_PRODUCT_${Date.now()}`;
const node = await app.inject({
method: 'POST',
url: '/admin/hierarchy-nodes',
payload: {
name: 'Capability Scope Node',
order: 0,
productScope: [productId],
skills: [skillZ],
assignmentStrategy: 'ROUND_ROBIN',
},
});
nodeIds.push(node.json().data.id);
const scoped = await app.inject({
method: 'GET',
url: `/support-org/capability-eligibility?skills=${skillX}&productId=${productId}`,
});
const scopedIds = scoped.json().data.map((a: { id: string }) => a.id);
expect(scopedIds).toContain(agentAId); // holds x and z
expect(scopedIds).not.toContain(agentBId); // holds y only, missing x and z
});
});