156 lines
4.9 KiB
TypeScript
156 lines
4.9 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|||
|
|
import { buildApp } from '@/app';
|
||
|
|
import { prismaClient } from '@/infrastructure/database';
|
||
|
|
import { FastifyInstance } from 'fastify';
|
||
|
|
import { loginAs, authHeader } from '../../helpers/auth';
|
||
|
|
import {
|
||
|
|
encryptCredential,
|
||
|
|
generateCredentialSecret,
|
||
|
|
issueIntegrationToken,
|
||
|
|
} from '@/modules/catalog/products';
|
||
|
|
import { reportingConfig } from '@/config';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Covers specs/015-reporting-dashboards/quickstart.md Scenario 3 against a real Postgres/Redis.
|
||
|
|
* Assignment rows are created directly via Prisma (not through a real HUMAN_ESCALATION +
|
||
|
|
* default-strategy auto-assignment) — the same contamination avoidance
|
||
|
|
* management-dashboard.test.ts already documents: this test only needs the persisted
|
||
|
|
* Assignment/SLARun state its own aggregation queries read, not a live orchestration run.
|
||
|
|
*/
|
||
|
|
describe('Support dashboard (User Story 3)', () => {
|
||
|
|
let app: FastifyInstance;
|
||
|
|
let authToken: string;
|
||
|
|
const externalProductId = `TEST_SUPPORT_REPORT_PROD_${Date.now()}`;
|
||
|
|
let productId: string;
|
||
|
|
let secret: string;
|
||
|
|
|
||
|
|
beforeAll(async () => {
|
||
|
|
app = await buildApp();
|
||
|
|
authToken = await loginAs(app, 'ADMIN');
|
||
|
|
|
||
|
|
const product = await prismaClient.product.create({
|
||
|
|
data: { externalProductId, name: 'Support Report Test Product', status: 'active' },
|
||
|
|
});
|
||
|
|
productId = product.id;
|
||
|
|
secret = generateCredentialSecret();
|
||
|
|
await prismaClient.productIntegration.create({
|
||
|
|
data: {
|
||
|
|
productId,
|
||
|
|
credentialRef: encryptCredential(secret),
|
||
|
|
authMechanism: 'signed_token',
|
||
|
|
allowedScope: { tenantIds: ['tenant-1'] },
|
||
|
|
status: 'active',
|
||
|
|
rateLimitPerMinute: 1000,
|
||
|
|
rateLimitPerUserPerMinute: 1000,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
afterAll(async () => {
|
||
|
|
await app.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
async function createTicket(): Promise<string> {
|
||
|
|
const token = issueIntegrationToken(secret, {
|
||
|
|
externalProductId,
|
||
|
|
tenantId: 'tenant-1',
|
||
|
|
userId: 'user-1',
|
||
|
|
});
|
||
|
|
const created = await app.inject({
|
||
|
|
method: 'POST',
|
||
|
|
url: '/v1/support/requests',
|
||
|
|
headers: { authorization: `Bearer ${token}` },
|
||
|
|
payload: {
|
||
|
|
productId: externalProductId,
|
||
|
|
tenantId: 'tenant-1',
|
||
|
|
userId: 'user-1',
|
||
|
|
source: 'test',
|
||
|
|
problem: `Support report test ${Date.now()}-${Math.random()}`,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
expect(created.statusCode).toBe(202);
|
||
|
|
return created.json().data.ticketId as string;
|
||
|
|
}
|
||
|
|
|
||
|
|
it("reflects each agent's real current assignment workload", async () => {
|
||
|
|
const team = await app.inject({
|
||
|
|
method: 'POST',
|
||
|
|
url: '/admin/teams',
|
||
|
|
headers: authHeader(authToken),
|
||
|
|
payload: { name: `Support Report Team ${Date.now()}` },
|
||
|
|
});
|
||
|
|
const teamId = team.json().data.id as string;
|
||
|
|
const agent = await app.inject({
|
||
|
|
method: 'POST',
|
||
|
|
url: `/admin/teams/${teamId}/agents`,
|
||
|
|
headers: authHeader(authToken),
|
||
|
|
payload: { name: 'Support Report Agent' },
|
||
|
|
});
|
||
|
|
const agentId = agent.json().data.id as string;
|
||
|
|
|
||
|
|
const ticket1 = await createTicket();
|
||
|
|
const ticket2 = await createTicket();
|
||
|
|
await prismaClient.assignment.createMany({
|
||
|
|
data: [
|
||
|
|
{ ticketId: ticket1, agentId, strategy: 'MANUAL', isCurrent: true },
|
||
|
|
{ ticketId: ticket2, agentId, strategy: 'MANUAL', isCurrent: true },
|
||
|
|
],
|
||
|
|
});
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: 'GET',
|
||
|
|
url: '/admin/reports/support',
|
||
|
|
headers: authHeader(authToken),
|
||
|
|
});
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
const workload = res.json().data.workloadByAgent as Array<{
|
||
|
|
agentId: string;
|
||
|
|
openAssignments: number;
|
||
|
|
}>;
|
||
|
|
expect(workload.find((w) => w.agentId === agentId)).toMatchObject({ openAssignments: 2 });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('counts a near-due SLA run as at-risk, distinct from breached', async () => {
|
||
|
|
const policy = await prismaClient.sLAPolicy.create({
|
||
|
|
data: {
|
||
|
|
name: `Support Risk Policy ${Date.now()}`,
|
||
|
|
productId,
|
||
|
|
firstResponseMinutes: 30,
|
||
|
|
resolutionMinutes: 240,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const riskTicketId = await createTicket();
|
||
|
|
await prismaClient.sLARun.create({
|
||
|
|
data: {
|
||
|
|
ticketId: riskTicketId,
|
||
|
|
policyId: policy.id,
|
||
|
|
firstResponseDueAt: new Date(Date.now() + 30 * 60_000),
|
||
|
|
resolutionDueAt: new Date(
|
||
|
|
Date.now() + (reportingConfig.slaRiskThresholdMinutes - 1) * 60_000,
|
||
|
|
),
|
||
|
|
status: 'running',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const safeTicketId = await createTicket();
|
||
|
|
await prismaClient.sLARun.create({
|
||
|
|
data: {
|
||
|
|
ticketId: safeTicketId,
|
||
|
|
policyId: policy.id,
|
||
|
|
firstResponseDueAt: new Date(Date.now() + 30 * 60_000),
|
||
|
|
resolutionDueAt: new Date(Date.now() + 999 * 60_000),
|
||
|
|
status: 'running',
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: 'GET',
|
||
|
|
url: '/admin/reports/support',
|
||
|
|
headers: authHeader(authToken),
|
||
|
|
});
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
expect(res.json().data.slaAtRisk).toBeGreaterThanOrEqual(1);
|
||
|
|
});
|
||
|
|
});
|