web/website/tests/jarvis-api.test.mjs
2026-11-21 20:22:37 +01:00

16 lines
3.9 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {jarvisRequestFixture} from './jarvis-request-fixture.mjs';import {createJarvisServices} from '../node/jarvis-api.mjs';import {createClans} from '../control-panel/clans.mjs';
const origin='https://cp.dunerealms.com';
async function fixture(){const f=await jarvisRequestFixture();let actor=f.people.Owner,services;const authenticate=async()=>actor,access=async()=>({nodes:['web.menu.game']});services=createJarvisServices(f.db,f.bank,{CP_ORIGIN:origin,POSTBANK_ENABLED:'true'},{authenticate,access,bankAuthority:async()=>({entities:[]}),edition:async()=>({stage:'alpha',version:'1.0.1.1',season:'S01'}),invokeClans:r=>createClans(f.db,{origin,authenticate,access,requestIntegration:services.requests})(r)});return {...f,services,actor:name=>actor=name?f.people[name]:null,call:(path,body,from=origin)=>services.api(new Request(origin+'/api/jarvis/'+path,body?{method:'POST',headers:{origin:from,'Content-Type':'application/json'},body:JSON.stringify(body)}:{}))};}
test('public J.A.R.V.I.S. listing metadata is separate from private organisation context and application messages',async()=>{const f=await fixture();try{
let response=await f.call('context?kind=clan');assert.equal(response.status,200);const context=await response.json();assert.equal(context.id,f.clanId);assert.equal(context.recruitment.unitPriceMinor,0);assert.equal(context.canPublish,true);
f.actor(null);assert.equal((await f.call('context?kind=clan')).status,401);response=await f.call('listings');assert.equal(response.status,200);const listings=(await response.json()).listings;assert.equal(listings.length,1);assert.equal('actor_id' in listings[0],false);assert.equal('feeMinor' in listings[0],false);
f.actor('Alpha');assert.equal((await f.call('publish',{requestId:randomUUID(),kind:'clan',organisationId:f.clanId,title:'Forged',message:'',expectedPriceMinor:0,confirmed:true})).status,403);assert.equal((await f.call('apply',{listingId:f.listing.id,confirmed:true,message:'Only the clan may read this'})).status,200);assert.equal((await f.call('apply',{listingId:f.listing.id,confirmed:true})).status,200);assert.ok(!JSON.stringify(await(await f.call('listings')).json()).includes('Only the clan'));
f.actor('Owner');response=await f.call('context?kind=clan');assert.equal((await response.json()).applications[0].message,'Only the clan may read this');
}finally{f.close();}});
test('J.A.R.V.I.S. review delegates to the existing membership engine and immediately removes the filled advertisement',async()=>{const f=await fixture();try{
f.actor('Alpha');assert.equal((await f.call('apply',{listingId:f.listing.id,confirmed:true})).status,200);f.actor('Owner');const context=await(await f.call('context?kind=clan')).json();assert.equal((await f.call('review',{organisationId:f.clanId,version:context.clanVersion,target:'Alpha',decision:'accept'})).status,200);assert.equal((await(await f.call('listings')).json()).listings.length,0);f.actor('Alpha');const own=await(await f.call('application?listing='+f.listing.id)).json();assert.equal(own.application,'accepted');assert.equal(own.canApply,false);assert.equal(f.db.sqlite.prepare('SELECT COUNT(*) n FROM site_clan_members WHERE user_id=?').get(f.people.Alpha.id).n,1);
}finally{f.close();}});
test('origin, scope and malformed context filters cannot publish or reveal another organisation workspace',async()=>{const f=await fixture();try{
const body={requestId:randomUUID(),kind:'clan',organisationId:f.clanId,title:'Safe',message:'',expectedPriceMinor:0,confirmed:true};assert.equal((await f.call('publish',body,'https://attacker.example')).status,403);assert.equal((await f.call('context?kind=clan&kind=company')).status,400);assert.equal((await f.call('context?kind=clan&id='+randomUUID())).status,403);assert.equal((await f.call('publish',{...body,audience:'clan-only'})).status,400);assert.equal((await f.bank.first('SELECT COUNT(*) n FROM jarvis_listings')).n,1);
}finally{f.close();}});