MC/website/tests/home-relocation.test.mjs
2026-11-27 23:43:05 +00:00

22 lines
5.1 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 {createHomeRelocation,relocationFee} from '../node/home-relocation.mjs';
import {createGameOrganisations,GAME_ORGANISATION_SCHEMA} from '../node/game-organisations.mjs';
import {PostbankService} from '../node/postbank-service.mjs';import {entityId} from '../node/postbank-authority.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';
test('relocation fee uses the complete eligible treasury and rounds once in minor units',()=>{assert.equal(relocationFee([{balance_minor:'1001'},{balance_minor:'999'},{balance_minor:'-10'}]),200n);assert.equal(relocationFee([{balance_minor:'1'}]),1n);});
test('paid relocation commits one key/payment, hides the old home, waits for source cleanup and permits destination placement',async()=>{
const f=await companyFixture();try{
const id=await f.create(),uuid=randomUUID(),user=f.people.Owner;await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[uuid,user.id]);await f.db.query(GAME_ORGANISATION_SCHEMA);f.db.sqlite.exec('CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT,created_at INT);CREATE TABLE jarvis_generated_instructions(key_id TEXT,action_id TEXT,created_at INT);');
const person={id:entityId('person',user.id),kind:'person',key:user.id,name:'Owner',canView:true,canActivate:true,canPay:true};const authority=async()=>({entities:[person]});
await new PostbankService(f.db,authority).activate(user,{entityId:person.id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});const source=await f.db.first("SELECT * FROM pb_accounts WHERE type_code='102'");await f.db.query('UPDATE pb_accounts SET balance_minor=100000 WHERE id=?',[source.id]);await f.db.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,state,created_at) VALUES('sys_tax_treasury','979','Tax',0,'active',1)");
const game=createGameOrganisations(f.db,f.keys),loc={server:'caladan',worldName:'caladan',worldUuid:randomUUID(),position:{x:8,y:64,z:8}};await game.execute({action:'set-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1,expectedRevision:0,confirmed:true,location:loc});
const reserved=createHomeRelocation(f.db,f.db,f.keys,{}, {verifyStorage:async()=>null,authority,extraWars:async()=>[{attackerId:id,defenderId:randomUUID(),active:false,reserved:true}]});await assert.rejects(reserved.quote(user,{kind:'company',id,worldKey:'arrakduin'}),/scheduled or active/);assert.equal((await f.db.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[source.id])).balance_minor,100000);
const service=createHomeRelocation(f.db,f.db,f.keys,{}, {verifyStorage:async()=>null,authority});const input={kind:'company',id,worldKey:'arrakduin'};const q=await service.quote(user,input);assert.equal(q.feeMinor,'10000');const move={...input,requestId:randomUUID(),sourceAccount:String(source.id),quoteHash:q.quoteHash,confirmed:true};
f.db.sqlite.exec("CREATE TRIGGER fail_relocation_audit BEFORE INSERT ON account_audit BEGIN SELECT RAISE(ABORT,'audit unavailable');END;");await assert.rejects(service.accept(user,move),/audit unavailable/);assert.equal((await f.db.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[source.id])).balance_minor,100000);assert.equal((await f.db.first("SELECT COUNT(*) n FROM jarvis_game_organisation_actions WHERE action_id='home-relocate'")).n,0);f.db.sqlite.exec('DROP TRIGGER fail_relocation_audit');
const receipt=await service.accept(user,move);assert.equal(receipt.phase,'cleanup');assert.equal((await service.accept(user,move)).replayed,true);assert.equal((await f.db.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[source.id])).balance_minor,90000);assert.equal((await f.db.first("SELECT COUNT(*) n FROM pb_orders WHERE id=? AND mode='system'",[move.requestId])).n,1);
let row=await f.db.first('SELECT * FROM site_claim_registry WHERE organisation_id=?',[id]);assert.equal(JSON.parse(row.snapshot_json).homeBlock,null);assert.equal((await service.quote(user,input)).phase,'cleanup');
await assert.rejects(service.acknowledge({server:'arrakis',requestId:move.requestId,keyId:receipt.keyId}),/another server/);await service.acknowledge({server:'caladan',requestId:move.requestId,keyId:receipt.keyId});assert.equal((await service.acknowledge({server:'caladan',requestId:move.requestId,keyId:receipt.keyId})).replayed,true);
row=await f.db.first('SELECT * FROM site_claim_registry WHERE organisation_id=?',[id]);const place={action:'set-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1,expectedRevision:Number(row.revision),confirmed:true,location:{...loc,server:'arrakis',worldName:'arrakis',worldUuid:randomUUID()}};
await assert.rejects(game.execute({...place,requestId:randomUUID(),location:loc}),/confirmed relocation world/);await game.execute(place);row=await f.db.first('SELECT * FROM site_claim_registry WHERE organisation_id=?',[id]);assert.equal(JSON.parse(row.snapshot_json).relocation.phase,'completed');assert.equal(JSON.parse(row.snapshot_json).homeBlock.worldName,'arrakis');
}finally{f.db.close();}
});