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> = {}) { 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 }).updateWithVersion).not.toHaveBeenCalled(); }); });