web/website/tests/entity-relations.test.mjs
2026-11-27 23:43:05 +00:00

41 lines
3.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {randomUUID} from 'node:crypto';
import {companyFixture} from './company-deed-fixture.mjs';
import {entityControllers,currentOfficeController} from '../node/entity-relations.mjs';
test('parent company control requires a current eligible deed and owner membership',async()=>{
const f=await companyFixture();try{
const parent=await f.create(),child=randomUUID();
await f.db.query("UPDATE site_company_deeds SET charter_kind='royal' WHERE company_id=?",[parent]);
await f.db.query('INSERT INTO jarvis_entity_relations VALUES(?,?,?,?,?,?,?,?,?)',['company',child,'company',parent,'staff',randomUUID(),randomUUID(),f.people.Owner.id,Date.now()]);
assert.deepEqual(await entityControllers(f.db,'company',child),[f.people.Owner.id]);
await f.db.query('DELETE FROM site_company_members WHERE company_id=?',[parent]);
await assert.rejects(entityControllers(f.db,'company',child),/requires review/);
}finally{f.db.close();}
});
test('political parent authority follows the current unambiguous office term and expires',async()=>{
const f=await companyFixture();try{
f.db.sqlite.exec('ALTER TABLE political_office_terms ADD COLUMN id TEXT;ALTER TABLE political_office_terms ADD COLUMN kind TEXT;');
const now=Date.now(),term=randomUUID();
await f.db.query("INSERT INTO political_office_terms(id,kind,user_id,office,faction,starts_at,expires_at,outcome) VALUES(?,'imperial',?,'emperor','imperium',?,?,'winner')",[term,f.people.Owner.id,now-1000,now+1000]);
assert.equal((await currentOfficeController(f.db,'emperor',now)).userId,f.people.Owner.id);
assert.equal(await currentOfficeController(f.db,'emperor',now+1000),null);
await f.db.query("INSERT INTO political_office_terms(id,kind,user_id,office,faction,starts_at,expires_at,outcome) VALUES(?,'imperial',?,'emperor','imperium',?,?,'winner')",[randomUUID(),f.people.Other.id,now-1000,now+1000]);
assert.equal(await currentOfficeController(f.db,'emperor',now),null);
}finally{f.db.close();}
});
import {setEntityParent} from '../node/entity-relations.mjs';
test('parent changes serialize, reject cycles, preserve ownership and replay one instruction',async()=>{
const f=await companyFixture();try{
f.db.sqlite.exec('CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INTEGER)');
const child=await f.create(),parent=randomUUID(),now=Date.now();
await f.db.query("INSERT INTO corrino_companies(id,slug,name,description,founded_by,status,created_at,updated_at) VALUES(?,'parent','Parent','Parent',?,'active',?,?)",[parent,f.people.Other.id,now,now]);
await f.db.query("INSERT INTO site_company_deeds(company_id,owner_account_id,charter_kind,version,created_at,updated_at) VALUES(?,?,'royal',1,?,?)",[parent,f.people.Other.id,now,now]);
const instruction={childKind:'company',childId:child,parentKind:'company',parentId:parent,origin:'staff',keyId:randomUUID(),requestId:randomUUID(),actorId:f.people.Owner.id};
assert.equal((await f.db.transaction(tx=>setEntityParent(tx,instruction))).replayed,false);
assert.equal((await f.db.transaction(tx=>setEntityParent(tx,instruction))).replayed,true);
await f.db.query("UPDATE site_company_deeds SET charter_kind='royal' WHERE company_id=?",[child]);
await assert.rejects(f.db.transaction(tx=>setEntityParent(tx,{...instruction,childId:parent,parentId:child,requestId:randomUUID()})),/cycle/);
assert.equal((await f.db.first('SELECT owner_account_id FROM site_company_deeds WHERE company_id=?',[child])).owner_account_id,f.people.Owner.id);
}finally{f.db.close();}
});