2 lines
2 KiB
JavaScript
2 lines
2 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';import {BankFixture} from './postbank-fixture.mjs';import {EVENT_SCHEMA,createRealmEvents} from '../node/jarvis-events.mjs';
|
|
test('event instruments keep scoped authority, replay-safe creation, optimistic edits and retained cancellation history',async()=>{const db=new BankFixture();try{for(const sql of EVENT_SCHEMA)await db.query(sql.replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4',''));const actor={id:randomUUID()},scopeId=randomUUID();let allowed=true;const api=createRealmEvents(null,db,{authenticate:async()=>actor,authorize:async(u,k,id)=>allowed&&id===scopeId,origin:'https://cp.dunerealms.com',clock:()=>1000}),input={action:'create',kind:'clan',scopeId,requestId:randomUUID(),title:'Desert gathering',description:'Join our expedition.',startsAt:5000,endsAt:9000,confirmed:true};const post=x=>api(new Request('https://cp.dunerealms.com/api/jarvis/events',{method:'POST',headers:{Origin:'https://cp.dunerealms.com','Content-Type':'application/json'},body:JSON.stringify(x)}));const response=await post(input);assert.equal(response.status,200);const {event}=await response.json();assert.match(event.reference,/^EVT-/);assert.equal((await(await post(input)).json()).replayed,true);assert.equal((await post({...input,title:'Changed with same request'})).status,409);allowed=false;assert.equal((await post({...input,action:'cancel',id:event.id,revision:1})).status,403);allowed=true;assert.equal((await post({...input,action:'cancel',id:event.id,revision:1})).status,200);assert.equal((await db.first('SELECT state FROM jarvis_events WHERE id=?',[event.id])).state,'cancelled');assert.equal((await db.first('SELECT COUNT(*) n FROM jarvis_event_history')).n,2);assert.equal((await post({...input,action:'update',id:event.id,revision:1})).status,409);assert.equal((await post({...input,requestId:randomUUID(),promotions:['discord']})).status,503);assert.equal((await db.first('SELECT COUNT(*) n FROM jarvis_events')).n,1);}finally{db.close();}});
|

Staff