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 متوقف gateway, zero agents or فعال 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:'نامشخص',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('سرویس فهرست ثبتشده: پاسخگو (نه سلامت زنده Hermes)')).toBeInTheDocument();
screen.getAllByText(/زمان ثبت: 2020-01-01T00:00:00Z/).forEach(row=>expect(row).toHaveTextContent('قدیمی'));
fireEvent.change(screen.getByLabelText('پروفایل اجرای مجاز'),{target:{value:'dev'}});
expect(await screen.findByText(/عاملهای فعال: نامشخص/)).toBeInTheDocument();
expect(await screen.findByText(/پرچم تاریخی نمای ثبتشده: فعال; فعالیت زنده نامشخص است/)).toBeInTheDocument();
expect(screen.queryByText(/درگاه.*متوقف/)).not.toBeInTheDocument();
expect(screen.queryByText(/عاملهای فعال: 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 متوقف gateway, zero usage and profile-bound reads',async()=>{
render();expect(await screen.findByText('v1 · abc')).toBeInTheDocument();
fireEvent.change(await screen.findByLabelText('پروفایل اجرای مجاز'),{target:{value:'dev'}});
expect(await screen.findByText('degraded')).toBeInTheDocument();expect(await screen.findByText('نشستی دریافت نشد.')).toBeInTheDocument();
expect(screen.getByText('توکن ورودی: 0')).toBeInTheDocument();expect(screen.getByText(/هزینه تخمینی منبع: 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(/وضعیت اجرا در دسترس نیست/)).toBeInTheDocument();expect(screen.queryByText('اجرای احرازهویتشده health: healthy')).not.toBeInTheDocument();
});
it('rejects malformed health and never renders نامشخص 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(/وضعیت اجرا در دسترس نیست/)).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:'بازخوانی اطلاعات اجرا'}));await waitFor(()=>expect(vi.mocked(api.get).mock.calls.filter(([u])=>u.endsWith('/health'))).toHaveLength(2));
});