Files
2026-09-30 19:35:18 +03:00

51 lines
4.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { normalizeDeviceManagementCommand } from '../src/management-command.mjs';
import { applyLifecycleManagementCommand, authorizeLifecycleManagementReplay } from '../src/lifecycle-repository.mjs';
import { applyOntologyManagementCommand } from '../src/ontology-repository.mjs';
const projectId='11111111-1111-4111-8111-111111111111', entityId='22222222-2222-4222-8222-222222222222';
const actor={ userRef:'user:operator', hubRole:'owner', groupRefs:[], ownerScopes:[] };
function db({role='owner', entityProject=projectId, active=false}={}) {
const writes=[];
return { writes, async query(sql, values) {
if (sql.includes('from device_projects')) return {rows:[{id:projectId,owner_scope_id:'owner',scope_kind:'company',owner_ref:'company:test',lifecycle_state:'active',owner_lifecycle_state:'active'}]};
if (sql.includes('from device_project_grants')) return {rows:[{id:'grant',principal_kind:'user',principal_ref:actor.userRef,project_role:role,capability_allow:[],capability_deny:[],lifecycle_state:'active'}]};
if (sql.includes('from device_instances')) return {rows:[{id:entityId,project_id:entityProject,lifecycle_state:'claimed'}]};
if (sql.includes('from device_sessions')) return {rows:[{active}]};
if (sql.startsWith('update device_infrastructure_hosts')) { writes.push({sql,values}); return {rows:entityProject === projectId ? [{id:entityId,lifecycle_state:values[2]}] : []}; }
if (sql.startsWith('update') || sql.includes('insert into device_audit_events')) {writes.push({sql,values});return {rows:[]};}
throw Error('Unexpected query '+sql);
}};
}
for (const [kind, ref, apply] of [['device.update','deviceRef',applyLifecycleManagementCommand],['infrastructure_host.ensure','hostRef',applyOntologyManagementCommand]]) {
test(`${kind} archive and restore preserve identity and private bindings`,async()=>{
for (const archived of [true,false]) {
const command=normalizeDeviceManagementCommand(kind,{projectRef:`project:${projectId}`,[ref]:`${ref==='hostRef'?'host':'device'}:${entityId}`,archived});
const client=db(); const result=await apply(client,{actor,commandKind:kind,command});
assert.equal(result.archived,archived);
const update=client.writes.find((write)=>write.sql.startsWith('update'));
assert.match(update.sql,/where id\s*=\s*\$1 and project_id\s*=\s*\$2/);
assert.equal(update.sql.includes('management_credential_ref'),false);
assert.equal(update.sql.includes('display_name'),false);
assert.equal(update.values[2],archived?'suspended':ref==='hostRef'?'active':'claimed');
assert.ok(client.writes.some((write)=>write.sql.includes('device_audit_events')));
}
});
test(`${kind} rejects unauthorized and cross-project archive`,async()=>{
const command=normalizeDeviceManagementCommand(kind,{projectRef:`project:${projectId}`,[ref]:`${ref==='hostRef'?'host':'device'}:${entityId}`,archived:true});
for (const options of [{role:'viewer'},{entityProject:'different'}]) {
const client=db(options); await assert.rejects(apply(client,{actor,commandKind:kind,command}));
if (options.role) assert.equal(client.writes.length,0);
}
assert.throws(()=>normalizeDeviceManagementCommand(kind,{projectRef:`project:${projectId}`,[ref]:`${ref==='hostRef'?'host':'device'}:${entityId}`,archived:'true'}));
assert.throws(()=>normalizeDeviceManagementCommand(kind,{projectRef:`project:${projectId}`,[ref]:`${ref==='hostRef'?'host':'device'}:${entityId}`,archived:true,displayName:'unexpected'}));
});
}
test('archive cannot interrupt an active device and replay rechecks its project',async()=>{
const command=normalizeDeviceManagementCommand('device.update',{projectRef:`project:${projectId}`,deviceRef:`device:${entityId}`,archived:true});
const client=db({active:true});
await assert.rejects(applyLifecycleManagementCommand(client,{actor,commandKind:'device.update',command}),/device_archive_active_session/);
assert.equal(client.writes.length,0);
await assert.rejects(authorizeLifecycleManagementReplay(db({entityProject:'different'}),{actor,commandKind:'device.update',command}),/project_mismatch/);
});