MC/website/tests/owner-homeblock-release.test.mjs

47 lines
7.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 {GENERATOR_SCHEMA} from '../node/jarvis-master-generator.mjs';
import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
import {ownerHomeBlockPlan,applyOwnerHomeBlockRelease} from '../node/owner-homeblock-release.mjs';
import {createStaffHomeBlockKeys} from '../node/staff-homeblock-keys.mjs';
import {createGameOrganisations,GAME_ORGANISATION_SCHEMA} from '../node/game-organisations.mjs';
async function schema(f){
await f.db.query(GENERATOR_SCHEMA.replace(/ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4/g,''));
f.db.sqlite.exec('CREATE TABLE account_audit(id TEXT PRIMARY KEY,actor_id TEXT,action TEXT,details TEXT CHECK(length(details)<=500),created_at INT);CREATE TABLE org_control_principals(user_id TEXT PRIMARY KEY,principal_kind TEXT);');
}
async function ownerFixture(){
const f=await companyFixture();await schema(f);const id=OFFICIAL_ACCOUNT.ownerId,clan=randomUUID();
await f.db.query("INSERT INTO site_accounts(id,username,role) VALUES(?,'unimatrix999','user')",[id]);await f.db.query("INSERT INTO org_control_principals VALUES(?,'owner')",[id]);await f.db.query("INSERT INTO site_clans VALUES(?,'Artreides',?,1)",[clan,id]);
const snapshot={organisationId:clan,source:'minecraft',tier:'KC_T4',usedChunks:16,homeBlock:{id:randomUUID(),worldUuid:randomUUID(),worldName:'arrakis',position:{x:594,y:58,z:-2053}},estates:[{id:randomUUID(),chunks:16,worldguard:{regionId:'old-region'}}]};
await f.db.query("INSERT INTO site_claim_registry VALUES('clan',?,?,4,1)",[clan,JSON.stringify(snapshot)]);return {...f,clan,snapshot};
}
test('Owner free release consumes original staff key, preserves estate and tier, bounds audit, and replays without clearing a later Home Block',async()=>{
const f=await ownerFixture();try{
const p=await ownerHomeBlockPlan(f.db),requestId=randomUUID(),r=await applyOwnerHomeBlockRelease(f.db,f.keys,p,{requestId});assert.equal(r.released,true);
const row=await f.db.first('SELECT * FROM site_claim_registry WHERE organisation_id=?',[f.clan]);assert.deepEqual(JSON.parse(row.snapshot_json),{...f.snapshot,homeBlock:null});assert.equal(row.revision,5);
const instruction=await f.db.first('SELECT * FROM jarvis_generated_instructions WHERE request_id=?',[requestId]);assert.equal(instruction.state,'applied');assert.deepEqual(JSON.parse(instruction.manifest_json).previousSnapshot,f.snapshot);
assert.equal((await f.db.first('SELECT used_at FROM organisation_keys WHERE id=?',[r.keyId])).used_at>0,true);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,0);
const newer={...f.snapshot,homeBlock:{...f.snapshot.homeBlock,id:randomUUID()}};await f.db.query('UPDATE site_claim_registry SET snapshot_json=?,revision=6 WHERE organisation_id=?',[JSON.stringify(newer),f.clan]);await applyOwnerHomeBlockRelease(f.db,f.keys,p,{requestId});
assert.deepEqual(JSON.parse((await f.db.first('SELECT snapshot_json FROM site_claim_registry WHERE organisation_id=?',[f.clan])).snapshot_json),newer);assert.equal((await f.db.first('SELECT COUNT(*) n FROM account_audit')).n,1);
}finally{f.db.close();}
});
for(const mode of ['authority','owner','revision','same-revision-snapshot'])test('release refuses changed '+mode+' without removing the marker',async()=>{
const f=await ownerFixture();try{const p=await ownerHomeBlockPlan(f.db);if(mode==='authority')await f.db.query('DELETE FROM org_control_principals');if(mode==='owner')await f.db.query('UPDATE site_clans SET owner_id=?',[randomUUID()]);if(mode==='revision')await f.db.query('UPDATE site_claim_registry SET revision=5');if(mode==='same-revision-snapshot')await f.db.query('UPDATE site_claim_registry SET snapshot_json=?',[JSON.stringify({...f.snapshot,usedChunks:17})]);await assert.rejects(applyOwnerHomeBlockRelease(f.db,f.keys,p,{requestId:randomUUID()}));assert.ok(JSON.parse((await f.db.first('SELECT snapshot_json FROM site_claim_registry')).snapshot_json).homeBlock);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,0);}finally{f.db.close();}
});
test('consumed staff release authorises one free upgraded-estate re-registration, retains purchased tier and cannot bypass another relocation',async()=>{
const f=await companyFixture();try{
await schema(f);await f.db.query(GAME_ORGANISATION_SCHEMA);const id=await f.create(),uuid=randomUUID();await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[uuid,f.people.Owner.id]);
const snapshot={organisationId:id,source:'minecraft',tier:'RCO_T3',usedChunks:16,homeBlock:{id:randomUUID(),worldUuid:randomUUID(),worldName:'caladan',position:{x:8,y:64,z:8}},estates:[{id:randomUUID(),chunks:16}]};await f.db.query("INSERT INTO site_claim_registry VALUES('company',?,?,1,1)",[id,JSON.stringify(snapshot)]);
const service=createGameOrganisations(f.db,f.keys),input={action:'set-home',kind:'company',id,uuid,requestId:randomUUID(),authorityVersion:1,expectedRevision:1,confirmed:true,location:{server:'caladan',worldName:'caladan',worldUuid:snapshot.homeBlock.worldUuid,position:{x:1000,y:64,z:1000}}};
await assert.rejects(service.execute(input),/paid Home Block relocation/);
// A manually absent marker is insufficient authority.
await f.db.query('UPDATE site_claim_registry SET snapshot_json=? WHERE organisation_id=?',[JSON.stringify({...snapshot,homeBlock:null}),id]);await assert.rejects(service.execute({...input,requestId:randomUUID()}),/scoped relocation/);await f.db.query('UPDATE site_claim_registry SET snapshot_json=? WHERE organisation_id=?',[JSON.stringify(snapshot),id]);
const staff=createStaffHomeBlockKeys(f.db,f.keys,{authorizeStaff:async()=>true}),key=await staff.issue(f.people.Owner,{kind:'company',id,expectedRevision:1,ticketId:'owner-test',reason:'Owner requested a free Home Block reset',requestId:randomUUID()});await staff.apply(f.people.Owner,{keyId:key.keyId,requestId:randomUUID()});
const released=(await f.db.first('SELECT snapshot_json FROM site_claim_registry WHERE organisation_id=?',[id])).snapshot_json;
await f.db.query('UPDATE site_claim_registry SET snapshot_json=? WHERE organisation_id=?',[JSON.stringify({...JSON.parse(released),usedChunks:17}),id]);await assert.rejects(service.execute({...input,requestId:randomUUID(),expectedRevision:2}),/scoped relocation/);await f.db.query('UPDATE site_claim_registry SET snapshot_json=? WHERE organisation_id=?',[released,id]);
const r=await service.execute({...input,requestId:randomUUID(),expectedRevision:2});assert.equal(r.homeBlock.revision,3);const saved=JSON.parse((await f.db.first('SELECT snapshot_json FROM site_claim_registry WHERE organisation_id=?',[id])).snapshot_json);assert.equal(saved.usedChunks,1);assert.equal(saved.tier,'RCO_T3');assert.equal(saved.estates.length,1);assert.deepEqual(saved.homeBlock.position,input.location.position);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,0);
await assert.rejects(service.execute({...input,requestId:randomUUID(),expectedRevision:3,location:{...input.location,position:{x:2000,y:64,z:2000}}}),/paid Home Block relocation/);
}finally{f.db.close();}
});