import test from 'node:test'; import assert from 'node:assert/strict'; import {LocalD1} from './d1-adapter.mjs'; import {initializeClans,createClans} from '../control-panel/clans.mjs'; import {validateMotd} from '../node/organisation-motd.mjs'; const id='11111111-1111-4111-8111-111111111111'; async function fixture(){ const db=new LocalD1('CREATE TABLE site_accounts(id TEXT PRIMARY KEY,username TEXT,display_name TEXT,role TEXT);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 INTEGER);'); await initializeClans(db,{mysql:false}); db.sqlite.exec("INSERT INTO site_accounts(id,username,role) VALUES('owner','Leader','player'),('officer','Officer','player'),('outsider','Outsider','player');INSERT INTO site_game_links VALUES('owner','owner-mc'),('officer','officer-mc'),('outsider','outsider-mc');"); db.sqlite.prepare("INSERT INTO site_clans(id,name,name_key,tag,description,owner_id,created_at,updated_at) VALUES(?,'Test House','test house','TEST','Original description','owner',1,1)").run(id); for(const role of ['owner','officer'])db.sqlite.prepare('INSERT INTO site_clan_members VALUES(?,?,?,?,1)').run(role,role+'-mc',id,role); const app=actor=>createClans(db,{authenticate:async()=>({id:actor,username:actor}),access:async()=>({nodes:['web.menu.game']})}); return {db,get:async(actor)=>(await app(actor)(new Request('https://cp.dunerealms.com/api/clans/'+id))).json(),post:(actor,message,version=1,extra={})=>app(actor)(new Request('https://cp.dunerealms.com/api/clans',{method:'POST',headers:{origin:'https://cp.dunerealms.com','Content-Type':'application/json'},body:JSON.stringify({action:'motd',clanId:id,version,message,...extra})}))}; } test('owner publishes and clears a separate member-visible MOTD',async()=>{ const f=await fixture();try{ assert.equal((await f.post('owner',' Welcome!\r\nGather at dusk. ')).status,200); const member=await f.get('officer');assert.equal(member.clan.motd,'Welcome!\nGather at dusk.');assert.equal(member.clan.description,'Original description');assert.equal(member.clan.version,2); assert.equal((await f.get('outsider')).clan.motd,null); assert.equal(member.activity[0].title,'Message of the day'); assert.equal((await f.post('owner','',2)).status,200);assert.equal((await f.get('owner')).clan.motd,''); }finally{f.db.close();} }); test('officers and outsiders cannot impersonate owner or change the message',async()=>{ const f=await fixture();try{for(const actor of ['officer','outsider'])assert.equal((await f.post(actor,'Forged',1,{role:'owner',owner_id:'owner'})).status,403);assert.equal((await f.get('owner')).clan.motd,'');}finally{f.db.close();} }); test('stale version cannot overwrite a newer message',async()=>{ const f=await fixture();try{assert.equal((await f.post('owner','First')).status,200);assert.equal((await f.post('owner','Stale')).status,409);assert.equal((await f.get('owner')).clan.motd,'First');}finally{f.db.close();} }); test('validation rejects excess and control characters without mutating the clan',async()=>{ const f=await fixture();try{for(const value of ['x'.repeat(1001),'bad\0message',null])assert.equal((await f.post('owner',value)).status,400);assert.equal((await f.get('owner')).clan.version,1);assert.equal(validateMotd('x'.repeat(1000)).length,1000);}finally{f.db.close();} }); test('audit failure rolls back both MOTD and clan version',async()=>{ const f=await fixture();try{f.db.sqlite.exec("CREATE TRIGGER fail_motd_audit BEFORE INSERT ON account_audit BEGIN SELECT RAISE(ABORT,'audit unavailable');END;");await assert.rejects(f.post('owner','Must roll back'),/audit unavailable/);const data=await f.get('owner');assert.equal(data.clan.motd,'');assert.equal(data.clan.version,1);}finally{f.db.close();} });