MC/website/tests/character-register.test.mjs

14 lines
3.5 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {LocalD1} from './d1-adapter.mjs';import {CHARACTER_SCHEMA,createCharacterChoice} from '../control-panel/character-choice.mjs';import {createClanKeyReceipts} from '../control-panel/clan-key-receipts.mjs';
function fixture(){const db=new LocalD1(`CREATE TABLE site_accounts(id TEXT PRIMARY KEY);CREATE TABLE site_game_links(user_id TEXT,minecraft_uuid TEXT);CREATE TABLE account_audit(id TEXT,actor_id TEXT,action TEXT,details TEXT,created_at INT);${CHARACTER_SCHEMA};`);db.sqlite.exec("INSERT INTO site_accounts VALUES('u'),('other');INSERT INTO site_game_links VALUES('u','mc');");const app=createCharacterChoice(db,{authenticate:async()=>({id:'u'}),access:async()=>({nodes:['web.menu.game']})});const post=(body,origin='https://cp.dunerealms.com')=>app(new Request('https://cp.dunerealms.com/api/account/character',{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)}));return {db,app,post};}
test('faction is saved centrally with optimistic versioning and an audit, without game writes',async()=>{const f=fixture();try{
assert.equal((await f.post({faction:'atreides',version:0,confirmed:true})).status,200);
const data=await (await f.app(new Request('https://cp.dunerealms.com/api/account/character'))).json();assert.equal(data.selection,'atreides');assert.equal(data.gameSync,'not_connected');assert.equal(data.version,1);
assert.equal((await f.post({faction:'ordos',version:0,confirmed:true})).status,409);assert.equal(f.db.sqlite.prepare('SELECT COUNT(*) n FROM account_audit').get().n,1);
assert.equal((await f.post({faction:'bene',version:1,confirmed:true})).status,400);assert.equal((await f.post({faction:'ordos',version:1,confirmed:true},'https://evil.invalid')).status,403);
f.db.sqlite.exec("UPDATE site_game_links SET minecraft_uuid='new-mc'");assert.equal((await (await f.app(new Request('https://cp.dunerealms.com/api/account/character'))).json()).selection,null);
}finally{f.db.close();}});
test('failed faction audit rolls back saved selection',async()=>{const f=fixture();try{f.db.sqlite.exec("CREATE TRIGGER fail_audit BEFORE INSERT ON account_audit BEGIN SELECT RAISE(ABORT,'audit unavailable');END;");await assert.rejects(f.post({faction:'atreides',version:0,confirmed:true}),/audit unavailable/);assert.equal(f.db.sqlite.prepare('SELECT COUNT(*) n FROM site_character_choices').get().n,0);}finally{f.db.close();}});
test('private charter receipts are account-scoped and never expose secrets',async()=>{const f=fixture();try{
f.db.sqlite.exec(`CREATE TABLE organisation_keys(id TEXT,account_id TEXT,key_type TEXT,created_at INT,used_at INT,target_id TEXT,expires_at INT,revoked_at INT);CREATE TABLE site_clans(id TEXT,name TEXT,tag TEXT);CREATE TABLE site_clan_charters(clan_id TEXT,charter_kind TEXT);INSERT INTO organisation_keys VALUES('own','u','clan-founding',1,2,'c',NULL,NULL),('private','other','political-appointment',1,2,'c',NULL,NULL);INSERT INTO site_clans VALUES('c','Test','TEST');INSERT INTO site_clan_charters VALUES('c','royal');`);
const api=createClanKeyReceipts(f.db,{authenticate:async()=>({id:'u'}),access:async()=>({nodes:['web.menu.game']})}),res=await api(new Request('https://cp.dunerealms.com/api/organisation-keys')),data=await res.json();assert.equal(data.keys.length,1);assert.equal(data.keys[0].id,'own');assert.equal(data.keys[0].status,'Applied');assert(!JSON.stringify(data).includes('code_hash'));assert.equal((await api(new Request('https://cp.dunerealms.com/api/organisation-keys?user=other'))).status,400);
}finally{f.db.close();}});