Files
support_backend/tests/integration/platform-reports/management-dashboard.test.ts
T
saqib mirandClaude Sonnet 5 d65683641a feat(015-reporting-dashboards): four real reporting/analytics endpoints
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>
2026-09-09 11:59:38 +05:30

218 lines
7.6 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 { ticketsRepository } from '@/modules/ticketing/tickets';
import { messagesService } from '@/modules/ticketing/messages';
import { resolutionRepository } from '@/modules/problem-management/resolutions';
/**
* Covers specs/015-reporting-dashboards/quickstart.md Scenario 1 against a real Postgres/Redis.
* Drives ticket-status transitions directly through ticketsRepository (not ticketsService) to
* avoid publishing TICKET_UPDATED — this test only needs the raw persisted state its own
* aggregation queries read, and publishing real domain events here risks the same kind of
* cross-file contamination 014-full-observability's own business-metrics.test.ts found and fixed
* (an unscoped HUMAN_ESCALATION triggering real auto-assignment against the shared agent pool).
*/
describe('Management dashboard (User Story 1)', () => {
let app: FastifyInstance;
let authToken: string;
const externalProductId = `TEST_MGMT_REPORT_PROD_${Date.now()}`;
let productId: string;
let secret: string;
const rangeFrom = new Date(Date.now() - 60 * 60 * 1000).toISOString();
const rangeTo = new Date(Date.now() + 60 * 60 * 1000).toISOString();
beforeAll(async () => {
app = await buildApp();
authToken = await loginAs(app, 'ADMIN');
const product = await prismaClient.product.create({
data: { externalProductId, name: 'Management 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: `Management report test ${Date.now()}-${Math.random()}`,
},
});
expect(created.statusCode).toBe(202);
return created.json().data.ticketId as string;
}
async function driveDirectly(ticketId: string, statuses: string[]): Promise<void> {
let ticket = await prismaClient.ticket.findUniqueOrThrow({ where: { id: ticketId } });
for (const status of statuses) {
const updated = await ticketsRepository.updateStatus(ticketId, status, ticket.version);
if (!updated) throw new Error(`Failed to transition ticket to ${status} in test setup.`);
ticket = updated;
}
}
it('reports real figures matching the actual created data', async () => {
// AI-resolved ticket.
const aiTicketId = await createTicket();
await driveDirectly(aiTicketId, [
'AI_ANALYZING',
'AI_TROUBLESHOOTING',
'AI_VERIFYING',
'AI_RESOLVED',
]);
await resolutionRepository.create({ ticketId: aiTicketId, outcome: 'fixed', resolvedBy: 'ai' });
await driveDirectly(aiTicketId, ['RESOLVED']);
// Human-resolved ticket, with a first agent response recorded.
const humanTicketId = await createTicket();
await messagesService.post(humanTicketId, 'agent-1', 'AGENT_MESSAGE', 'Looking into this.');
await driveDirectly(humanTicketId, [
'HUMAN_ESCALATION',
'IN_PROGRESS',
'RESOLUTION_PENDING_CUSTOMER',
]);
await resolutionRepository.create({
ticketId: humanTicketId,
outcome: 'fixed',
resolvedBy: 'agent-1',
});
await driveDirectly(humanTicketId, ['RESOLVED']);
// Still-open ticket.
await createTicket();
// SLA policy + one met, one breached run.
const policy = await prismaClient.sLAPolicy.create({
data: {
name: `Mgmt Report Policy ${Date.now()}`,
productId,
firstResponseMinutes: 30,
resolutionMinutes: 240,
},
});
const metTicketId = await createTicket();
await prismaClient.sLARun.create({
data: {
ticketId: metTicketId,
policyId: policy.id,
firstResponseDueAt: new Date(Date.now() + 30 * 60_000),
resolutionDueAt: new Date(Date.now() + 240 * 60_000),
status: 'completed',
completedAt: new Date(),
},
});
const breachedTicketId = await createTicket();
await prismaClient.sLARun.create({
data: {
ticketId: breachedTicketId,
policyId: policy.id,
firstResponseDueAt: new Date(Date.now() + 30 * 60_000),
resolutionDueAt: new Date(Date.now() - 60_000),
status: 'breached',
breachedAt: new Date(),
},
});
// One escalation event.
const escalatedTicketId = await createTicket();
await prismaClient.escalationEvent.create({
data: {
ticketId: escalatedTicketId,
ruleId: null,
fromNodeId: null,
toNodeId: null,
reason: 'management dashboard test',
triggeredBy: 'system',
},
});
const res = await app.inject({
method: 'GET',
url: `/admin/reports/management?from=${rangeFrom}&to=${rangeTo}`,
headers: authHeader(authToken),
});
expect(res.statusCode).toBe(200);
const data = res.json().data;
expect(data.totalCases).toBeGreaterThanOrEqual(6);
expect(data.aiResolved).toBeGreaterThanOrEqual(1);
expect(data.humanEscalated).toBeGreaterThanOrEqual(1);
expect(data.resolved).toBeGreaterThanOrEqual(2);
expect(data.open).toBeGreaterThanOrEqual(1);
expect(data.slaCompliance.met).toBeGreaterThanOrEqual(1);
expect(data.slaCompliance.breached).toBeGreaterThanOrEqual(1);
expect(data.slaCompliance.rate).not.toBeNull();
expect(data.escalationCount).toBeGreaterThanOrEqual(1);
expect(data.averageResponseSeconds).not.toBeNull();
expect(data.averageResolutionSeconds).not.toBeNull();
expect(data.range.from).toBeTruthy();
expect(data.range.to).toBeTruthy();
});
it('returns all-zero counts and all-null rates for a range with no activity', async () => {
const farPastFrom = new Date('2000-01-01').toISOString();
const farPastTo = new Date('2000-01-02').toISOString();
const res = await app.inject({
method: 'GET',
url: `/admin/reports/management?from=${farPastFrom}&to=${farPastTo}`,
headers: authHeader(authToken),
});
expect(res.statusCode).toBe(200);
const data = res.json().data;
expect(data.totalCases).toBe(0);
expect(data.aiResolved).toBe(0);
expect(data.humanEscalated).toBe(0);
expect(data.resolved).toBe(0);
expect(data.open).toBe(0);
expect(data.slaCompliance.rate).toBeNull();
expect(data.averageResponseSeconds).toBeNull();
expect(data.averageResolutionSeconds).toBeNull();
});
it('rejects a range where from is after to', async () => {
const res = await app.inject({
method: 'GET',
url: `/admin/reports/management?from=${rangeTo}&to=${rangeFrom}`,
headers: authHeader(authToken),
});
expect(res.statusCode).toBe(400);
});
});