Wires the pre-scaffolded, unused platform/reports module (ReportsService
.generateSummaryReport previously returned {}) into four real, admin-
gated dashboards matching docs/09-testing-observability-cicd.md's own
table:
- GET /admin/reports/management: total cases, AI-resolved, human-
escalated, resolved/open, SLA compliance/breaches, escalation count,
average response/resolution time.
- GET /admin/reports/product/:externalProductId: support volume,
problem-category breakdown, recurring problems, AI-resolution/human-
escalation rate, top error codes.
- GET /admin/reports/support: current per-agent workload, SLA at-risk/
breached counts, escalation count, response/resolution performance.
- GET /admin/reports/ai: AI resolution/human-handoff rate, failed-
troubleshooting-then-escalated rate, knowledge-match rate, confidence
distribution (reusing 005-ai-support's own decideConfidenceBand),
tool invocation success/failure.
Every rate/average is number|null -- null means no qualifying data in
range, never a computed NaN or a misleading 0. Adds one new durable
table, ErrorCodeLookup, since 014-full-observability's own equivalent
metric is a process-lifetime Prometheus counter unusable for a
historical "top errors" report.
Verified end-to-end against real Postgres/Redis: every figure checked
against hand-computed expected values, including a no-activity range
(all-zero counts, all-null rates) and cross-product isolation.
Also fixes a real regression the new ErrorCodeLookup FK caused in the
pre-existing known-issues.test.ts (its afterAll deleted ErrorCode rows
before the now-referencing lookup rows).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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);
|
|
});
|
|
});
|