web/website/tests/jarvis-management.test.mjs

13 lines
3.7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {BankFixture} from './postbank-fixture.mjs';
import {createJarvisManagement} from '../node/jarvis-management.mjs';
import {organisationKeySchema} from '../node/organisation-keys.mjs';
import {JARVIS_LISTING_SCHEMA} from '../node/jarvis-listings.mjs';
const normalize=sql=>sql.replace(/ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4/g,'').replace(/,INDEX \w+\([^)]*\)/g,'').replace(/UNIQUE KEY \w+\(/g,'UNIQUE(');
async function setup(){const db=new BankFixture();for(const sql of [...organisationKeySchema,...JARVIS_LISTING_SCHEMA])await db.query(normalize(sql));await db.query('CREATE TABLE site_accounts(id TEXT PRIMARY KEY,username TEXT)');await db.query("INSERT INTO site_accounts VALUES('owner','Citizen')");await db.query("INSERT INTO organisation_keys(id,account_id,key_type,scope_id,code_hash,code_sealed,created_at) VALUES('key','owner','clan-founding','one','secret-hash','SECRET-SEALED-CODE',1)");const central={prepare(sql){return{bind(...args){return{async all(){return{results:await db.query(sql,args)};}};}};}};let signed=true,permitted=true;const api=createJarvisManagement(central,db,{}, {authenticate:async()=>signed?{id:'owner'}:null,authorize:async()=>permitted,edition:async()=>({stage:'alpha',version:'1.0.1.1',season:'S01'})});return{db,api,signed:v=>signed=v,permitted:v=>permitted=v,call:(query='',method='GET')=>api(new Request('https://cp.dunerealms.com/api/jarvis/management'+query,{method}))};}
test('management register requires authenticated management authority and rejects writes and identity overrides',async()=>{const f=await setup();try{f.signed(false);assert.equal((await f.call()).status,401);f.signed(true);f.permitted(false);assert.equal((await f.call()).status,403);f.permitted(true);assert.equal((await f.call('','POST')).status,405);for(const query of ['?section=keys&userId=other','?section=keys&section=history','?page=0','?page=1.5','?page=10001'])assert.equal((await f.call(query)).status,400);}finally{f.db.close();}});
test('key register exposes explicit safe metadata only, with bounded pagination',async()=>{const f=await setup();try{const response=await f.call('?section=keys&page=1');assert.equal(response.status,200);const data=await response.json();assert.equal(data.records[0].recipient,'Citizen');assert.equal(data.records[0].type,'clan-founding');assert.equal(data.hasMore,false);const serialized=JSON.stringify(data);assert.ok(!serialized.includes('SECRET'));assert.ok(!serialized.includes('secret-hash'));assert.ok(!serialized.includes('code_sealed'));assert.equal((await f.call('?section=does-not-exist')).status,404);}finally{f.db.close();}});
test('missing section schema reports unavailable instead of claiming empty success; seasons contains only public edition',async()=>{const f=await setup();try{assert.equal((await f.call('?section=deeds')).status,503);const data=await (await f.call('?section=seasons')).json();assert.deepEqual(data.edition,{stage:'alpha',version:'1.0.1.1',season:'S01'});assert.equal((await f.call('?section=listings')).status,200);}finally{f.db.close();}});
test('deed register retains public faction metadata so shared House office types remain distinguishable',async()=>{const f=await setup();try{await f.db.query('CREATE TABLE political_office_terms(id TEXT PRIMARY KEY,office TEXT,faction TEXT,user_id TEXT,starts_at INTEGER,expires_at INTEGER,outcome TEXT)');await f.db.query("INSERT INTO political_office_terms VALUES('term','house_leader','atreides','owner',1,2,'winner')");const response=await f.call('?section=deeds');assert.equal(response.status,200);const data=await response.json();assert.equal(data.records[0].faction,'atreides');assert.equal(data.records[0].holder,'Citizen');assert.equal(data.records[0].type,'house_leader');}finally{f.db.close();}});