import test from 'node:test'; import assert from 'node:assert/strict'; import {randomUUID} from 'node:crypto'; import {companyFixture} from './company-deed-fixture.mjs'; import {entityParent,setEntityParent} from '../node/entity-relations.mjs'; import {attachWarSupermother,endWarSupermother} from '../node/war-affiliations.mjs'; async function rig(){ const f=await companyFixture();f.db.sqlite.exec('CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INTEGER)'); const mother=await f.create();await f.db.query("UPDATE site_company_deeds SET charter_kind='royal' WHERE company_id=?",[mother]); async function organisation(){const id=randomUUID(),now=Date.now();await f.db.query("INSERT INTO corrino_companies(id,slug,name,description,founded_by,status,created_at,updated_at) VALUES(?,?,?,'A chartered company',?,'active',?,?)",[id,id,'Company '+id,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,?,?)",[id,f.people.Other.id,now,now]);return id;} const military=await organisation(),client=await organisation(),other=await organisation(); const link=(child,parent,origin='staff')=>f.db.transaction(tx=>setEntityParent(tx,{childKind:'company',childId:child,parentKind:'company',parentId:parent,origin,keyId:randomUUID(),requestId:randomUUID(),actorId:f.people.Owner.id})); await link(client,mother); const instruction={operationId:randomUUID(),childKind:'company',childId:mother,parentKind:'company',parentId:military,keyId:randomUUID(),requestId:randomUUID(),actorId:f.people.Owner.id}; const attach=()=>f.db.transaction(tx=>attachWarSupermother(tx,instruction)),end=()=>f.db.transaction(tx=>endWarSupermother(tx,instruction)); return {...f,mother,military,client,other,instruction,link,attach,end}; } test('military supermother preserves owners and the entire existing client branch; ordinary end removes only its link',async()=>{ const f=await rig();try{ const owner=await f.db.first('SELECT * FROM site_company_deeds WHERE company_id=?',[f.mother]),client=await entityParent(f.db,'company',f.client); assert.equal((await f.attach()).state,'active');assert.equal((await f.attach()).replayed,true); assert.equal((await entityParent(f.db,'company',f.mother)).parent_id,f.military); assert.deepEqual(await entityParent(f.db,'company',f.client),client); assert.equal((await f.db.first('SELECT owner_account_id FROM site_company_deeds WHERE company_id=?',[f.mother])).owner_account_id,owner.owner_account_id); assert.equal((await f.end()).state,'restored');assert.equal(await entityParent(f.db,'company',f.mother),null); assert.equal(await f.db.first('SELECT * FROM site_company_relations WHERE company_id=?',[f.mother]),null); assert.deepEqual(await entityParent(f.db,'company',f.client),client);assert.equal((await f.end()).replayed,true); }finally{f.db.close();} }); test('an original mother relationship is restored without flattening its descendants',async()=>{ const f=await rig();try{ await f.link(f.mother,f.other);await f.attach();await f.end(); assert.equal((await entityParent(f.db,'company',f.mother)).parent_id,f.other); assert.equal((await entityParent(f.db,'company',f.client)).parent_id,f.mother); }finally{f.db.close();} }); test('war cleanup never reverses a later settled conquest or purchase',async()=>{ const f=await rig();try{ await f.attach();await f.link(f.mother,f.other,'conquest');const conquest=await entityParent(f.db,'company',f.mother); assert.equal((await f.end()).state,'superseded');assert.deepEqual(await entityParent(f.db,'company',f.mother),conquest); assert.equal((await entityParent(f.db,'company',f.client)).parent_id,f.mother); }finally{f.db.close();} }); test('altered retries and nested temporary affiliations cannot replace a current military link',async()=>{ const f=await rig();try{ await f.attach();const current=await entityParent(f.db,'company',f.mother); await assert.rejects(f.db.transaction(tx=>attachWarSupermother(tx,{...f.instruction,parentId:f.other})),/reference conflict/); await assert.rejects(f.db.transaction(tx=>attachWarSupermother(tx,{...f.instruction,operationId:randomUUID(),requestId:randomUUID(),parentId:f.other})),/End the existing/); assert.deepEqual(await entityParent(f.db,'company',f.mother),current); }finally{f.db.close();} }); test('clan supermother also retains nested clients and the original owner through cleanup',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);CREATE TABLE site_clan_charters(clan_id TEXT PRIMARY KEY,charter_kind TEXT);CREATE TABLE site_clan_deeds(clan_id TEXT PRIMARY KEY,parent_clan_id TEXT,version INTEGER,updated_at INTEGER)'); const [mother,military,client]=[randomUUID(),randomUUID(),randomUUID()]; for(const id of [mother,military,client]){await f.db.query('INSERT INTO site_clans(id,name,owner_id,active) VALUES(?,?,?,1)',[id,id,f.people.Owner.id]);await f.db.query("INSERT INTO site_clan_charters VALUES(?,'royal')",[id]);await f.db.query('INSERT INTO site_clan_deeds VALUES(?,NULL,1,0)',[id]);} const base={childKind:'clan',parentKind:'clan',keyId:randomUUID(),requestId:randomUUID(),actorId:f.people.Owner.id}; await f.db.transaction(tx=>setEntityParent(tx,{...base,childId:client,parentId:mother,origin:'created'})); const instruction={...base,requestId:randomUUID(),operationId:randomUUID(),childId:mother,parentId:military}; await f.db.transaction(tx=>attachWarSupermother(tx,instruction));assert.equal((await entityParent(f.db,'clan',client)).parent_id,mother); await f.db.transaction(tx=>endWarSupermother(tx,instruction));assert.equal(await entityParent(f.db,'clan',mother),null); assert.equal((await f.db.first('SELECT owner_id FROM site_clans WHERE id=?',[mother])).owner_id,f.people.Owner.id); assert.equal((await f.db.first('SELECT parent_clan_id FROM site_clan_deeds WHERE clan_id=?',[client])).parent_clan_id,mother); }finally{f.db.close();} });