import {render,screen,fireEvent,waitFor} from '@testing-library/react';
import {beforeEach,it,expect,vi} from 'vitest';
import api from '../services/api';
import RuntimeReadPanel from './RuntimeReadPanel';
const provenance={source:'snapshot',capturedAt:'2020-01-01T00:00:00Z',ageSeconds:5000,freshness:'stale',liveProcess:'unknown'};
it('labels snapshot inventory without presenting stopped gateway, zero agents or active sessions as live',async()=>{
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/health')?{ok:true,version:'v1',commit:'abc',auth_required:true,provenance}:url.endsWith('/profiles')?[{name:'dev',is_default:false,gateway_running:false,liveGatewayState:'unknown',provenance}]:url.endsWith('/status')?{version:'v1',gateway_running:false,overall:'unknown',active_agents:0,gateway_state:null,liveGatewayState:'unknown',liveActiveAgents:null,provenance}:url.endsWith('/sessions')?{total:1,sessions:[{id:'session-1',profile:'dev',source:'cli',is_active:true}],provenance}:{period_days:30,totals:{total_input:1,total_output:2,total_sessions:1,total_estimated_cost:0},provenance}}));
render();
expect(await screen.findByText('Inventory service: responsive (not live Hermes health)')).toBeInTheDocument();
screen.getAllByText(/Captured at: 2020-01-01T00:00:00Z/).forEach(row=>expect(row).toHaveTextContent('stale'));
fireEvent.change(screen.getByLabelText('Authorized runtime profile'),{target:{value:'dev'}});
expect(await screen.findByText(/Active agents: unknown/)).toBeInTheDocument();
expect(await screen.findByText(/historical snapshot flag: active; live activity unknown/)).toBeInTheDocument();
expect(screen.queryByText(/Gateway.*stopped/)).not.toBeInTheDocument();
expect(screen.queryByText(/Active agents: 0/)).not.toBeInTheDocument();
expect(screen.queryByText(/runtime health: healthy/)).not.toBeInTheDocument();
});
vi.mock('../services/api',()=>({default:{get:vi.fn()}}));
beforeEach(()=>{vi.resetAllMocks();vi.mocked(api.get).mockImplementation(async url=>{
if(url.endsWith('/health'))return {data:{ok:true,version:'v1',commit:'abc',auth_required:true}};
if(url.endsWith('/profiles'))return {data:[{name:'dev',is_default:false,gateway_running:false}]};
if(url.endsWith('/status'))return {data:{version:'v1',gateway_running:false,overall:'degraded',active_agents:0,gateway_state:null}};
if(url.endsWith('/sessions'))return {data:{total:0,sessions:[]}};
if(url.endsWith('/usage'))return {data:{period_days:30,totals:{total_input:0,total_output:0,total_sessions:0,total_estimated_cost:0}}};
throw new Error('Runtime unavailable');
});});
it('shows actual health identity, explicit stopped gateway, zero usage and profile-bound reads',async()=>{
render();expect(await screen.findByText('v1 · abc')).toBeInTheDocument();
fireEvent.change(await screen.findByLabelText('Authorized runtime profile'),{target:{value:'dev'}});
expect(await screen.findByText('degraded')).toBeInTheDocument();expect(await screen.findByText('No sessions returned.')).toBeInTheDocument();
expect(screen.getByText('Input tokens: 0')).toBeInTheDocument();expect(screen.getByText(/Upstream estimated cost: 0/)).toBeInTheDocument();
expect(api.get).toHaveBeenCalledWith('/admin/orchestration/runtime/sessions',{params:{profile:'dev',limit:20,offset:0}});
});
it('shows unavailable rather than healthy when runtime cannot be authenticated',async()=>{
vi.mocked(api.get).mockRejectedValue(new Error('Runtime unavailable'));
render();expect(await screen.findByText(/Health unavailable/)).toBeInTheDocument();expect(screen.queryByText('Authenticated runtime health: healthy')).not.toBeInTheDocument();
});
it('rejects malformed health and never renders unknown secret fields',async()=>{
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/health')?{ok:true,version:'v1',commit:'abc',auth_required:false,password:'secret-value'}:[]}));
render();expect(await screen.findByText(/Health unavailable/)).toBeInTheDocument();expect(screen.queryByText(/secret-value/)).not.toBeInTheDocument();
});
it('refreshes the authenticated runtime reads without a browser runtime connection',async()=>{
render();await screen.findByText('v1 · abc');fireEvent.click(screen.getByRole('button',{name:'Refresh runtime'}));await waitFor(()=>expect(vi.mocked(api.get).mock.calls.filter(([u])=>u.endsWith('/health'))).toHaveLength(2));
});