Files
support_backend/tests/unit/observability/sla-compliance-metric.test.ts
T
saqib mirandClaude Sonnet 5 93d6fe8b94 fix(016-load-concurrency-testing): US3 — finish escalation idempotency plumbing
Completes the escalation-event repository/service changes from the prior
commit: normalizes the exactOptionalPropertyTypes mismatch in the
findFirst fallback lookup, and updates every existing unit test
(sla-pause-resume, sla-breach-detection, sla-compliance-metric,
escalation-rule-match) to the new SlaRunRepository.updateWithVersion and
EscalationEventRepository.create({event, wasNewlyCreated}) signatures.
Full typecheck/lint/architecture-check clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-10 11:13:46 +05:30

60 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SlaService } from '@/modules/orchestration/sla/service/sla.service';
import * as observability from '@/infrastructure/observability';
function fakeRun(overrides: Partial<Record<string, unknown>> = {}) {
return {
id: 'run-1',
ticketId: 'ticket-1',
status: 'running',
version: 1,
...overrides,
};
}
describe('SLA compliance metric (014-full-observability data-model.md #6)', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('counts a run that completes while still running as met', async () => {
const incSpy = vi.spyOn(observability.slaRunOutcomesCounter, 'inc');
const runs = {
findByTicketId: vi.fn().mockResolvedValue(fakeRun({ status: 'running' })),
updateWithVersion: vi.fn().mockResolvedValue(fakeRun({ status: 'completed' })),
} as never;
const service = new SlaService(undefined, runs);
await service.complete('ticket-1');
expect(incSpy).toHaveBeenCalledWith({ outcome: 'met' });
});
it('does not double-count a run that was already breached before it resolved', async () => {
const incSpy = vi.spyOn(observability.slaRunOutcomesCounter, 'inc');
const runs = {
findByTicketId: vi.fn().mockResolvedValue(fakeRun({ status: 'breached' })),
updateWithVersion: vi.fn().mockResolvedValue(fakeRun({ status: 'completed' })),
} as never;
const service = new SlaService(undefined, runs);
await service.complete('ticket-1');
expect(incSpy).not.toHaveBeenCalledWith({ outcome: 'met' });
});
it('does not count anything for a run already completed', async () => {
const incSpy = vi.spyOn(observability.slaRunOutcomesCounter, 'inc');
const runs = {
findByTicketId: vi.fn().mockResolvedValue(fakeRun({ status: 'completed' })),
updateWithVersion: vi.fn().mockResolvedValue(undefined),
} as never;
const service = new SlaService(undefined, runs);
await service.complete('ticket-1');
expect(incSpy).not.toHaveBeenCalled();
expect((runs as unknown as { updateWithVersion: ReturnType<typeof vi.fn> }).updateWithVersion).not.toHaveBeenCalled();
});
});