import test from 'node:test'; import assert from 'node:assert/strict'; import {LocalD1} from './d1-adapter.mjs'; import {createClaimOverview,projectClaimRegistry} from '../control-panel/claim-overview.mjs'; import {CLAIM_REGISTRY_SCHEMA,readClaimRegistry} from '../node/claim-registry.mjs'; const id='11111111-1111-4111-8111-111111111111',other='22222222-2222-4222-8222-222222222222',owner='33333333-3333-4333-8333-333333333333'; const snapshot={organisationId:id,tier:'BC_T1',usedChunks:1,homeBlock:{id:other,worldUuid:owner,worldName:'Kaladam',position:{x:0,y:64,z:-16}},estates:[{id:other,name:'Capital',worldName:'Kaladam',chunks:1,state:'active'}]}; function fixture(){const db=new LocalD1('CREATE TABLE site_clans(id TEXT PRIMARY KEY,name TEXT,tag TEXT,home_world TEXT,active INTEGER);CREATE TABLE site_clan_members(user_id TEXT,clan_id TEXT,clan_role TEXT);CREATE TABLE site_clan_institutions(clan_id TEXT,institution_key TEXT);');db.sqlite.prepare('INSERT INTO site_clans VALUES(?,?,?,?,1)').run(id,'House One','ONE','kaladam');db.sqlite.prepare('INSERT INTO site_clans VALUES(?,?,?,?,1)').run(other,'Private House','TWO','giedermark');db.sqlite.prepare('INSERT INTO site_clan_members VALUES(?,?,?)').run(owner,id,'owner');return db;} const request=path=>new Request('https://cp.dunerealms.com/api/v1/claims/overview'+path); test('registry projection uses real registered counts and tariff values, never simulated funds',()=>{ const view=projectClaimRegistry({id},snapshot);assert.equal(view.capacity,2);assert.equal(view.availableChunks,1);assert.equal(view.monthlyUpkeep,'2.00 DM');assert.equal(view.homeBlock.position.z,-16);assert.equal(view.testCreditMinor,undefined); assert.equal(projectClaimRegistry({id},null).usedChunks,null); assert.throws(()=>projectClaimRegistry({id:other},snapshot)); assert.throws(()=>projectClaimRegistry({id},{...snapshot,usedChunks:2})); }); test('current members can read their clan; unrelated clan ids and query overrides do not grant access',async()=>{ const db=fixture();try{const api=createClaimOverview(db,{}, {authenticate:async()=>({id:owner}),access:async()=>({nodes:['web.menu.game']}),readRegistry:async()=>snapshot}); const own=await(await api(request(''))).json();assert.equal(own.clan.id,id);assert.equal(own.simulation,false);assert.equal(own.claims.usedChunks,1); assert.equal((await api(request('?clanId='+other))).status,404); assert.equal((await api(request('?clanId=bad'))).status,400); assert.equal((await api(request('?clanId='+id+'&clanId='+other))).status,400); assert.equal((await api(request('?actorId='+owner))).status,400); db.sqlite.prepare('DELETE FROM site_clan_members').run();assert.equal((await api(request('?clanId='+id))).status,404); }finally{db.close();} }); test('subhouses are independently authorised and broken claim providers do not leak snapshots',async()=>{ const db=fixture();try{let allowed=false;const api=createClaimOverview(db,{}, {authenticate:async()=>({id:owner}),access:async()=>({nodes:['web.menu.game']}),organisationAuthority:async(_,clanId)=>({canView:allowed&&clanId===other}),readRegistry:async()=>{throw Error('Private database failure');},readSubhouses:async()=>[{id:other,name:'Private House'}]}); let result=await(await api(request(''))).json();assert.equal(result.claims.state,'unavailable');assert.deepEqual(result.subhouses.items,[]);assert.equal(result.claims.usedChunks,null); allowed=true;result=await(await api(request(''))).json();assert.deepEqual(result.subhouses.items,[{id:other,name:'Private House'}]);assert.equal((await api(request('?clanId='+other))).status,200); }finally{db.close();} }); test('sign-in, game permission and read-only methods remain required',async()=>{ const db=fixture();try{let user=null;const api=createClaimOverview(db,{}, {authenticate:async()=>user,access:async()=>({nodes:[]})});assert.equal((await api(request(''))).status,401);user={id:owner};assert.equal((await api(request(''))).status,403);assert.equal((await api(new Request(request('').url,{method:'POST'}))).status,405);}finally{db.close();} }); test('persistent projection keeps empty and simulated registries separate from live claims',async()=>{ const db=new LocalD1(CLAIM_REGISTRY_SCHEMA);try{ assert.equal(await readClaimRegistry(db,'clan',id),null); db.sqlite.prepare('INSERT INTO site_claim_registry VALUES(?,?,?,?,?)').run('clan',id,JSON.stringify(snapshot),1,100); assert.deepEqual(await readClaimRegistry(db,'clan',id),snapshot); assert.equal(await readClaimRegistry(db,'company',id),null); db.sqlite.prepare('UPDATE site_claim_registry SET snapshot_json=?').run(JSON.stringify({...snapshot,simulation:true})); await assert.rejects(readClaimRegistry(db,'clan',id),/real organisation/); }finally{db.close();} });