import test from 'node:test';import assert from 'node:assert/strict'; import {alphaResetPlan} from '../node/alpha-reset-plan.mjs'; // An in-memory domain simulation: no network, live DB, or secret key issuance. function fixture(){return { clans:[{id:'house-official',official:true,ownerId:'office'},{id:'sietch-official',official:true,ownerId:'naib'},{id:'uni-house',ownerId:'uni'},{id:'test-house',ownerId:'player'}], companies:[{id:'order',official:true,ownerId:'patrick'},{id:'unicorp',ownerId:'uni'}], listings:[],ownerId:'uni', members:[{clan:'house-official',user:'official-member'},{clan:'sietch-official',user:'fremen-member'},{clan:'uni-house',user:'uni'},{clan:'uni-house',user:'blue'},{clan:'test-house',user:'tester'}], history:[],completed:new Set() };} function simulate(state,{failBeforeCommit=false}={}){ const resetId='alpha-reset-test'; if(state.completed.has(resetId))return {replayed:true}; const plan=alphaResetPlan(state); const nextClans=state.clans.filter(c=>!plan.deleteClanIds.includes(c.id)); const nextMembers=state.members.filter(m=>!plan.deleteClanIds.includes(m.clan)); if(failBeforeCommit)throw Error('Injected domain failure'); state.clans=nextClans;state.members=nextMembers; state.history.push({resetId,deleted:plan.deleteClanIds});state.completed.add(resetId); return {replayed:false,plan}; } test('clan-reset simulation preserves official houses, sietches and every member of the owner clan',()=>{ const state=fixture(),before=structuredClone(state);simulate(state); assert.deepEqual(state.clans.map(c=>c.id),['house-official','sietch-official','uni-house']); assert.deepEqual(state.members,before.members.filter(m=>m.clan!=='test-house')); assert.deepEqual(state.companies,before.companies); assert.equal(state.members.some(m=>m.user==='tester'),false); }); test('retry does not execute clan reset twice or add duplicate history',()=>{ const state=fixture();simulate(state);assert.deepEqual(simulate(state),{replayed:true});assert.equal(state.history.length,1); }); test('a failed simulated transaction changes no clan, membership or reset marker',()=>{ const state=fixture(),before=structuredClone(state); assert.throws(()=>simulate(state,{failBeforeCommit:true}),/Injected/);assert.deepEqual(state,before); simulate(state);assert.equal(state.history.length,1); });