web/website/tests/company-integrity.test.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

8 lines
2.4 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {registerCompany,appointCompanyCeo} from '../node/company-registry.mjs';
const founder='11111111-1111-1111-1111-111111111111',actor='22222222-2222-2222-2222-222222222222',company='33333333-3333-3333-3333-333333333333',ceo='44444444-4444-4444-4444-444444444444';
function fixture({founderExists=true,companyExists=true,ceoExists=true,previous=null}={}){const queries=[];return {queries,transaction:async fn=>fn({first:async(sql)=>{queries.push(sql);if(sql.includes('corrino_companies'))return companyExists?{id:company,ceo_account_id:previous}:null;if(sql.includes('site_accounts'))return sql.includes('WHERE id=?')?(queries.some(q=>q.includes('corrino_companies'))?(ceoExists?{id:ceo}:null):(founderExists?{id:founder}:null)):null;},query:async(sql)=>{queries.push(sql);return {affectedRows:1};}})};}
test('company registration requires existing founder before writes',async()=>{const db=fixture({founderExists:false});await assert.rejects(registerCompany(db,{slug:'alpha',name:'Alpha',founderId:founder,actorId:actor,authorized:true}),/Founder account not found/);assert.ok(db.queries.every(q=>q.startsWith('SELECT')));});
test('valid registration writes company and audit in one transaction',async()=>{const db=fixture();const r=await registerCompany(db,{slug:'alpha',name:'Alpha',founderId:founder,actorId:actor,authorized:true});assert.equal(r.status,'pending');assert.equal(db.queries.filter(q=>q.startsWith('INSERT')).length,2);});
test('CEO appointment locks company and records audit',async()=>{const db=fixture();const r=await appointCompanyCeo(db,{companyId:company,ceoId:ceo,actorId:actor,authorized:true});assert.equal(r.changed,true);assert.ok(db.queries.some(q=>q.includes('FOR UPDATE')));assert.equal(db.queries.filter(q=>q.startsWith('INSERT')).length,1);});
test('repeated CEO appointment does not create duplicate event',async()=>{const db=fixture({previous:ceo});const r=await appointCompanyCeo(db,{companyId:company,ceoId:ceo,actorId:actor,authorized:true});assert.equal(r.changed,false);assert.ok(db.queries.every(q=>q.startsWith('SELECT')));});
test('missing CEO account prevents company mutation',async()=>{const db=fixture({ceoExists:false});await assert.rejects(appointCompanyCeo(db,{companyId:company,ceoId:ceo,actorId:actor,authorized:true}),/CEO account not found/);assert.ok(db.queries.every(q=>q.startsWith('SELECT')));});