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

12 lines
2.8 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {bootstrapAppointments} from '../scripts/bootstrap-organisation-appointments.mjs';
import {MIGRATION_APPOINTMENTS} from '../node/organisation-appointments.mjs';
const owner='f3ffda37-01e4-41ea-b89d-fbeda82e453e';
function fixture({conflict=false,auditFail=false}={}){
const accounts=[...new Set(MIGRATION_APPOINTMENTS.map(p=>p.username))].map((username,i)=>({username,id:username==='unimatrix999'?owner:'id-'+i,game_link_exempt:0}));const assignments=[],events=[];
const tx={async first(sql,args){if(sql.includes('org_control_principals'))return {principal_kind:'owner'};if(sql.includes('org_scope_versions'))return {version:1};if(sql.includes('site_accounts'))return accounts.find(a=>a.id===args[0]&&a.username===args[1]);throw Error(sql);},async query(sql,args){if(sql.startsWith('SELECT user_id,role_key')){const list=assignments.filter(a=>a.type===args[0]&&a.scope===args[1]);return conflict?[...list,{user_id:'other',role_key:'branch_lead'}]:list;}if(sql.startsWith('INSERT INTO org_assignments'))assignments.push({user_id:args[1],type:args[2],scope:args[3],role_key:args[4]});if(sql.startsWith('INSERT INTO org_assignment_events')){if(auditFail)throw Error('audit failed');events.push(args);}}};
const db={prepare(){return {bind(){return {all:async()=>({results:accounts})};}};},async transaction(fn){const a=assignments.length,e=events.length;try{return await fn(tx);}catch(error){assignments.length=a;events.length=e;throw error;}}};return {db,accounts,assignments,events};
}
test('Approved roster resolves 14 existing people and 26 functions without changing access',async()=>{const f=fixture();const plan=await bootstrapAppointments(f.db);assert.equal(plan.accounts,14);assert.equal(plan.assignments.length,26);assert.equal(plan.permissionsChanged,false);assert.equal(f.assignments.length,0);});
test('Appointments are audited, idempotent and use the existing Owner identity',async()=>{const f=fixture();const result=await bootstrapAppointments(f.db,{actorId:owner,apply:true});assert.equal(result.created,26);assert.equal(f.events.length,26);const again=await bootstrapAppointments(f.db,{actorId:owner,apply:true});assert.equal(again.created,0);assert.equal(again.retained,26);assert.equal(f.events.length,26);});
test('A conflicting leader, identity mismatch or audit failure prevents a partial migration',async()=>{const f=fixture({conflict:true});await assert.rejects(bootstrapAppointments(f.db,{actorId:owner,apply:true}),/another holder/);assert.equal(f.assignments.length,0);const bad=fixture();bad.accounts[0].id='wrong';await assert.rejects(bootstrapAppointments(bad.db),/identities/);const fail=fixture({auditFail:true});await assert.rejects(bootstrapAppointments(fail.db,{actorId:owner,apply:true}),/audit/);assert.equal(fail.assignments.length,0);});