Files
NODEDC_MISSION_CORE/apps/control-station/test/commandRuntime.test.mjs
T

73 lines
5.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import test from 'node:test';
import ts from 'typescript';
const code=ts.transpileModule(readFileSync(new URL('../src/core/fleet/commandRuntime.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText;
const {mergeCommands,commandPending,commandTracked,commandStatus,commandTone,commandDate,commandDuration,commandReceipt,commandEntryStatus}=await import('data:text/javascript;base64,'+Buffer.from(code).toString('base64'));
test('runtime retains loaded history and merges a changing operation by immutable id',()=>{
const a={id:'a',sequence:10,state:'waiting'},b={id:'b',sequence:9,state:'device_confirmed'};
const entries=mergeCommands([a,b],[{...a,state:'dispatched'},{id:'c',sequence:11,state:'queued'}]);
assert.deepEqual(entries.map(x=>[x.id,x.state]),[['c','queued'],['a','dispatched'],['b','device_confirmed']]);
assert.equal(a.state,'waiting');
});
test('a delayed polling response cannot roll a completed command back to pending',()=>{
const current={id:'a',sequence:10,state:'device_confirmed',updated_at:20};
assert.equal(mergeCommands([current],[{...current,state:'dispatched',updated_at:10}])[0].state,'device_confirmed');
});
test('runtime makes expiry, no confirmation and receipt distinct; none claim actuator execution',()=>{
for(const state of ['waiting','submitting','queued','dispatched'])assert.equal(commandPending(state),true);
for(const state of ['expired','cancelled','unconfirmed','uncertain','device_confirmed'])assert.equal(commandPending(state),false);
assert.equal(commandTone('device_confirmed'),'success');
assert.equal(commandTone('unconfirmed'),'warning');
assert.notEqual(commandStatus('expired'),commandStatus('unconfirmed'));
assert.doesNotMatch(commandStatus('device_confirmed'),/исполнено|выполнено/i);
assert.equal(commandDuration(undefined),'—');assert.equal(commandDuration(0.01234),'0.012 с');assert.equal(commandDate(undefined),'—');
});
test('unresolved results remain tracked after timeout without blocking the command queue',()=>{
const unknown={id:'a',sequence:1,state:'uncertain',reconcile_pending:true,updated_at:10};
assert.equal(commandPending(unknown.state),false);
assert.equal(commandTracked(unknown),true);
const final={...unknown,state:'device_confirmed',reconcile_pending:false,updated_at:20};
assert.equal(commandTracked(final),false);
assert.deepEqual(mergeCommands([unknown],[final]),[final]);
});
test('tracker ACK is visible without pretending that execution finished',()=>{
const ack={state:'dispatched',device_code:6,device_accepted_at:10};
assert.equal(commandPending(ack.state),true);
assert.equal(commandEntryStatus(ack),'Принято B2, выполняется');
assert.equal(commandReceipt(ack),'B2 принял команду; требуется время на выполнение');
assert.equal(commandReceipt({device_code:0,kind:'restart'}),'B2 принял команду перезапуска');
assert.equal(commandEntryStatus({...ack,state:'device_confirmed'}),'Подтверждено B2');
assert.equal(commandReceipt({device_code:5}),'B2 сообщил об ошибке выполнения');
assert.equal(commandReceipt({device_code:7}),'Очередь команд B2 заполнена');
});
test('durable jobs show recovery independently of a single attempt and stop request',()=>{
const entry={state:'queued',durable:true,device_accepted_at:10};
assert.equal(commandEntryStatus({...entry,runtime_phase:'retry-wait'}),'Ожидает следующей попытки');
assert.equal(commandEntryStatus({...entry,runtime_phase:'reconciling'}),'Уточнение результата у VPS');
assert.equal(commandEntryStatus({...entry,stop_requested:true}),'Остановка повторов');
assert.equal(commandPending('accepted'),false);
assert.equal(commandPending('stopped'),false);
});
test('text output receipt does not falsely claim no response when it cannot correlate SERV OK',()=>{
const text={kind:'pulse-output',state:'unconfirmed',description:{encoding:'INTERNAL · FC'}};
assert.match(commandReceipt(text),/не содержит идентификатора команды/);
assert.doesNotMatch(commandReceipt(text),/Ответ трекера не получен/);
assert.equal(commandStatus('unconfirmed'),'Результат не подтверждён');
});
test('text transport completion releases queue without claiming execution',()=>{
const delivered={kind:'set-output',state:'delivered',delivered_at:100,delivery_seconds:1.5,description:{encoding:'INTERNAL · FC'}};
assert.equal(commandPending(delivered.state),false);
assert.equal(commandStatus(delivered.state),'Доставлено');
assert.equal(commandTone(delivered.state),'neutral');
assert.match(commandReceipt(delivered),/Доставка команды подтверждена/);
assert.match(commandReceipt(delivered),/не подтверждение переключения/);
});