import test from 'node:test'; import assert from 'node:assert/strict'; import {companyFixture} from './company-deed-fixture.mjs'; import {initializeSystemCharacters,createSystemCharacters} from '../node/system-characters.mjs'; import {SYSTEM_CHARACTERS} from '../node/system-character-catalogue.mjs'; import {accessFor} from '../node/permissions.mjs'; import {directoryFilters} from '../node/citizen-directory.mjs'; async function fixture(){ const f=await companyFixture(); f.db.sqlite.exec("ALTER TABLE site_accounts ADD COLUMN password_salt TEXT;ALTER TABLE site_accounts ADD COLUMN created_at BIGINT;ALTER TABLE site_game_links ADD COLUMN minecraft_name TEXT;ALTER TABLE site_game_links ADD COLUMN linked_at BIGINT;CREATE TABLE site_game_identities(minecraft_uuid TEXT PRIMARY KEY,first_linked_at BIGINT,last_name TEXT);CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at BIGINT);"); await initializeSystemCharacters(f.db);let allowed=true; return {...f,deny(){allowed=false;},service:createSystemCharacters(f.db,f.keys,{authorizeOperator:async actor=>allowed&&actor.id===f.people.Owner.id})}; } test('approved characters create stable system keys and distinct synthetic identities without ordinary access',async()=>{ const f=await fixture();try{ const identities=[]; for(const c of SYSTEM_CHARACTERS){const one=await f.service.provision(f.people.Owner,c.key);assert.equal(one.created,true);identities.push(one);assert.equal((await f.service.provision(f.people.Owner,c.key)).keyId,one.keyId);assert.equal(await f.service.permits(one.id,'quest.issue'),true);assert.equal(await f.service.permits(one.id,'web.bank.staff.manage'),false);const account=await f.db.first('SELECT * FROM site_accounts WHERE id=?',[one.id]);assert.equal(account.password_hash,'!system');assert.deepEqual((await accessFor(f.db,account)).nodes,[]);} assert.equal(new Set(identities.map(i=>i.minecraftUuid)).size,SYSTEM_CHARACTERS.length); assert.equal((await f.db.query('SELECT * FROM jarvis_system_identities')).length,SYSTEM_CHARACTERS.length); assert.equal((await f.db.query('SELECT * FROM account_audit')).length,SYSTEM_CHARACTERS.length); assert.match(JSON.stringify(directoryFilters(new URL('https://cp.dunerealms.com/api/admin/citizens'))),/system_account/); }finally{f.db.close();} }); test('operators cannot overwrite real accounts, introduce unapproved characters, or continue after authority changes',async()=>{ const f=await fixture();try{ await f.db.query('UPDATE site_accounts SET username=? WHERE id=?',['paul_atreides',f.people.Member.id]); await assert.rejects(f.service.provision(f.people.Owner,'paul_atreides'),/another account/); assert.equal((await f.db.first('SELECT password_hash FROM site_accounts WHERE id=?',[f.people.Member.id])).password_hash,'hash'); await assert.rejects(f.service.provision(f.people.Member,'thufir_hawat'),/authority/); await assert.rejects(f.service.provision(f.people.Owner,'arbitrary_admin'),/authority/); f.deny();await assert.rejects(f.service.provision(f.people.Owner,'thufir_hawat'),/authority/); assert.equal((await f.db.query('SELECT * FROM jarvis_system_identities')).length,0); }finally{f.db.close();} }); test('revoked or modified identity keys fail closed without restoring capabilities',async()=>{ const f=await fixture();try{ const one=await f.service.provision(f.people.Owner,'thufir_hawat'); await f.db.query('UPDATE organisation_keys SET revoked_at=1 WHERE id=?',[one.keyId]); await assert.rejects(f.service.permits(one.id,'quest.issue'),/review/); await assert.rejects(f.service.provision(f.people.Owner,'thufir_hawat'),/review/); assert.equal((await f.db.query('SELECT * FROM jarvis_system_identities')).length,1); }finally{f.db.close();} }); // Production exposes query/prepare on its root handle, first only on transactions. test('operator provisioning works with the production root database interface',async()=>{ const f=await fixture();try{ f.db.sqlite.exec('CREATE TABLE org_control_principals(user_id TEXT PRIMARY KEY,principal_kind TEXT)'); await f.db.query("INSERT INTO org_control_principals VALUES(?,'owner')",[f.people.Owner.id]); const {provisionCharacters}=await import('../scripts/provision-system-characters.mjs'); const root={query:f.db.query.bind(f.db),transaction:f.db.transaction.bind(f.db)}; const result=await provisionCharacters(root,f.keys,{actorId:f.people.Owner.id,apply:true}); assert.equal(result.count,SYSTEM_CHARACTERS.length);assert.ok(result.accounts.every(a=>a.created)); assert.ok((await provisionCharacters(root,f.keys,{actorId:f.people.Owner.id})).accounts.every(a=>a.existing)); await f.db.query('DELETE FROM org_control_principals'); await assert.rejects(provisionCharacters(root,f.keys,{actorId:f.people.Owner.id,apply:true}),/authority/); }finally{f.db.close();} });