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>
60 lines
2.1 KiB
TypeScript
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();
|
|
});
|
|
});
|