import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createElement} from 'react'; import {renderToStaticMarkup} from 'react-dom/server'; import {createServer} from 'vite'; let server,Detail,presentation,enrolledDevice; before(async()=>{ server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}}); ({K1Detail:Detail}=await server.ssrLoadModule('@xgrids-k1/frontend/sensors/K1Detail.tsx')); presentation=await server.ssrLoadModule('@xgrids-k1/frontend/sensors/presentation.ts'); ({enrolledDevice}=await server.ssrLoadModule('../../packages/sensor-ui/src/contracts.ts')); }); after(async()=>{await server?.close();}); const device={id:'synthetic',kind:'k1',name:'K1 test',online:true,verified:true,prepared:true, control:{can_start:true,can_stop:false,can_verify:true,generation:1,revision:1,acquisition_id:null}, snapshot:{context:{session_id:'exact-session',execution:{node_id:'test'}},acquisition:'idle'}}; const render=(value,enabled=true)=>renderToStaticMarkup(createElement(Detail,{device:value,enabled, transport:{},back:()=>{},refresh:async()=>{},failure:()=>{},createRerunHost:()=>assert.fail('SSR must not start a viewer or hardware')})); test('manual K1 surface admits one START only after current control proof',()=>{ const markup=render(device); assert.match(markup,/Инициировать запуск/); const start=(markup.match(//g)??[]).filter(button=>button.replace(/<[^>]+>/g,'')==='Инициировать запуск'); assert.equal(start.length,1); assert.match(start[0],/]*disabled=""/); for(const label of ['Тип установки / носитель','Режим GNSS','Название проекта'])assert.ok(markup.includes(label)); assert.match(markup,/Настройки устройства/); assert.doesNotMatch(markup,/Остановить устройство|Обновить просмотр|sensor-live-layout/); const waiting={...device,online:false,verified:false,control:{...device.control,can_start:false,network_applied:true,reason_code:'application_authority_unavailable'}}; const pending=render(waiting); assert.doesNotMatch(pending,/Инициировать запуск|Настройки устройства|sensor-live-layout/); assert.match(pending,/Проверить состояние K1/); assert.match(pending,/авторизовать/); assert.equal(presentation.k1Status(waiting,true).label,'Нет связи с K1'); assert.equal(presentation.k1ManualState(device,false).canStart,false); }); test('active acquisition exposes STOP and Rerun without another START',()=>{ const active={...device,control:{...device.control,can_start:false,can_stop:true},snapshot:{...device.snapshot,acquisition:'streaming'}}; const markup=render(active); assert.match(markup,/Остановить устройство/); assert.match(markup,/rerun-viewport__canvas/); assert.doesNotMatch(markup,/>СтатусПространственная сцена{ const starting={...device,control:{...device.control,can_start:false},snapshot:{...device.snapshot,acquisition:'starting'}}; const markup=render(starting); const start=(markup.match(//g)??[]).filter(button=>button.replace(/<[^>]+>/g,'')==='Запускаем устройство'); assert.equal(start.length,1); assert.match(start[0],/]*aria-busy="true"/); assert.equal((start[0].match(/class="nodedc-activity-indicator"/g)??[]).length,1); assert.match(markup,/K1 калибруется и готовит облако точек/); assert.doesNotMatch(markup,/Обновить просмотр/); }); test('connection loss is visible independently of completed STOP',()=>{ const stopped={...device,online:false,verified:false,control:{...device.control,phase:'completed',can_start:false,network_applied:true}}; const markup=render(stopped); assert.match(markup,/Нет связи с K1/); assert.match(markup,/Проверить состояние K1/); assert.doesNotMatch(markup,/Инициировать запуск|нет управления/); }); test('enrollment handoff opens only the verified current session',()=>{ const inventory={fresh:true,items:[device],operations:[]}; assert.equal(enrolledDevice(inventory,'exact-session'),device); assert.equal(enrolledDevice(inventory,'other-session'),null); assert.equal(enrolledDevice({...inventory,fresh:false},'exact-session'),null); assert.equal(enrolledDevice({...inventory,items:[{...device,verified:false}]},'exact-session'),null); assert.equal(enrolledDevice({...inventory,items:[device,device]},'exact-session'),null); }); test('completed scan with healthy control remains connected and permits a named next scan',()=>{ const stopped={...device,control:{...device.control,phase:'completed'}}; const markup=render(stopped); assert.equal(presentation.k1Status(stopped,true).label,'Подключён'); assert.equal(presentation.k1ManualState(stopped,true).canStart,true); assert.match(markup,/Инициировать запуск/); assert.doesNotMatch(markup,/Проверить состояние K1|Подключить устройство|Устройство остановлено/); assert.equal(presentation.k1Status(stopped,false).tone,'neutral'); });