Files
canina/frontend/admin-panel/src/services/runtime.test.ts
T

22 lines
2.1 KiB
TypeScript

import {it,expect,vi} from 'vitest';
import api from './api';
import {runtime} from './runtime';
vi.mock('./api',()=>({default:{get:vi.fn()}}));
const provenance={source:'snapshot',capturedAt:'2020-01-01T00:00:00Z',ageSeconds:100,freshness:'stale',liveProcess:'unknown'};
it.each([{source:'live'},{capturedAt:'not-a-date'},{capturedAt:'2999-01-01T00:00:00Z'},{ageSeconds:-1},{ageSeconds:0.5},{freshness:'live'},{liveProcess:'running'}])('rejects invalid provenance %j',async invalid=>{
vi.mocked(api.get).mockResolvedValue({data:{ok:true,version:'1',commit:'x',auth_required:true,provenance:{...provenance,...invalid}}});
await expect(runtime.health()).rejects.toThrow('Invalid snapshot provenance');
});
it('retains fresh snapshots as inventory with unknown process state',async()=>{
vi.mocked(api.get).mockResolvedValue({data:{ok:true,version:'1',commit:'x',auth_required:true,provenance:{...provenance,freshness:'fresh'}}});
expect(await runtime.health()).toMatchObject({provenance:{freshness:'fresh',liveProcess:'unknown'}});
});
it('retains validated snapshot provenance and unknown live telemetry across inventory reads',async()=>{
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/health')?{ok:true,version:'1',commit:'x',auth_required:true,provenance}:url.endsWith('/profiles')?[{name:'dev',is_default:false,gateway_running:false,liveGatewayState:'unknown',provenance}]:url.endsWith('/status')?{version:'1',overall:'unknown',gateway_running:false,active_agents:0,gateway_state:null,liveGatewayState:'unknown',liveActiveAgents:null,provenance}:url.endsWith('/sessions')?{total:0,sessions:[],provenance}:{period_days:30,totals:{total_input:0,total_output:0,total_sessions:0,total_estimated_cost:0},provenance}}));
expect(await runtime.health()).toMatchObject({provenance});
expect(await runtime.profiles()).toMatchObject([{provenance,liveGatewayState:'unknown'}]);
expect(await runtime.status('dev')).toMatchObject({provenance,liveGatewayState:'unknown',liveActiveAgents:null});
expect(await runtime.sessions('dev')).toMatchObject({provenance});
expect(await runtime.usage('dev')).toMatchObject({provenance});
});