Restructure ARNavi contour UX and expose project-scoped VPS inventory
Device Core qualification / qualify (push) Successful in 7m42s

This commit is contained in:
DCCONSTRUCTIONS
2026-09-30 19:35:18 +03:00
parent 1bf3ee6d41
commit c0487ca17b
17 changed files with 508 additions and 388 deletions
@@ -0,0 +1,50 @@
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/);
});
@@ -156,3 +156,42 @@ test('TLS pin is verified before any HTTP credential reaches the peer', async ()
await rm(root, { recursive: true, force: true });
}
});
test('project inventory releases DB before networking and never exposes other projects or credentials', async () => {
const contour = 'c'.repeat(32); let released = false; const calls = [];
const repo = repository({ role: 'viewer' });
const db = await repo.pool.connect(); const original = db.query;
db.release = () => { released = true; };
db.query = async (sql, values) => {
if (sql.includes('from device_b2_hosts')) {
assert.match(sql, /v.project_id=\$1 and h.lifecycle_state='active'/);
assert.match(sql, /l.project_id=v.project_id/);
assert.deepEqual(values, [projectId]);
return { rows: [{ ...source, host_id: hostId, display_name: 'VPS', contour_ids: [contour] }] };
}
return original(sql, values);
};
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path) {
assert.equal(released, true); calls.push(path);
return path === '/v2/identity' ? { host_id: pkg.host_id } : { items: [{ id: 'device', imei: '123456789012345', number: 1, name: 'B2', active: true }] };
} }) });
const result = await service.execute({ ...actor, hubRole: 'member' }, { projectRef: `project:${projectId}`, action: 'inventory' });
assert.deepEqual(calls, ['/v2/identity', `/v2/contours/${contour}/devices`]);
assert.equal(result.items[0].identifier.masked, '…12345');
assert.equal(result.hosts[0].reachable, true);
assert.equal(result.hosts[0].devices, 1);
const json = JSON.stringify(result);
for (const secret of [pkg.client_secret, source.secret_ref, '123456789012345', otherProject]) assert.equal(json.includes(secret), false);
assert.equal('online' in result.items[0], false);
});
test('unreachable or mismatched host is reported as unavailable, not an empty healthy fleet', async () => {
for (const fail of [false, true]) {
const repo = repository(); const db = await repo.pool.connect(); const original = db.query;
db.query = (sql, values) => sql.includes('from device_b2_hosts') ? { rows: [{ ...source, host_id: hostId, display_name: 'VPS', contour_ids: [] }] } : original(sql, values);
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call() { if (fail) throw Error('contains-private-details'); return { host_id: 'wrong-host' }; } }) });
const result = await service.execute(actor, { projectRef: `project:${projectId}`, action: 'inventory' });
assert.equal(result.hosts[0].reachable, false); assert.equal(result.hosts[0].devices, null);
assert.deepEqual(result.items, []); assert.equal(JSON.stringify(result).includes('private-details'), false);
}
});