forked from unimatrix/web
Replace monthly community goals with JARVIS server projects, staff review, scoped funding and automatic closure
This commit is contained in:
parent
bf3ae53053
commit
3fab6cd650
2 changed files with 293 additions and 22 deletions
17
website/tests/server-project-api.test.mjs
Normal file
17
website/tests/server-project-api.test.mjs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
||||
import {createServerProjectAPI} from '../node/server-project-api.mjs';
|
||||
function fixture(){let user={id:randomUUID()};const calls=[];const service={list:async actor=>({projects:[],accountId:actor.id}),submit:async(actor,body)=>{calls.push({actor,body});return {replayed:false};},review:async()=>{calls.push('review');return{};}};
|
||||
const api=createServerProjectAPI({},service,{authenticate:async()=>user});
|
||||
return{calls,logout:()=>{user=null;},call:(path='',body,origin='https://cp.dunerealms.com')=>api(new Request('https://cp.dunerealms.com/api/projects'+path,body?{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)}:{}))};}
|
||||
test('project API scopes private requests to the authenticated account and rejects browser overrides',async()=>{
|
||||
const f=fixture();assert.equal((await f.call()).status,200);assert.equal((await f.call('?accountId=other')).status,400);
|
||||
const submit={requestId:randomUUID(),title:'Arena',proposal:'A proposal for a new public arena.',requestedMinor:25000,confirmed:true};
|
||||
assert.equal((await f.call('/submit',{...submit,applicantId:randomUUID()})).status,400);assert.equal((await f.call('/submit',submit,'https://elsewhere.example')).status,403);assert.equal(f.calls.length,0);
|
||||
assert.equal((await f.call('/submit',submit)).status,200);assert.equal(f.calls.length,1);assert.equal(typeof f.calls[0].actor.revalidate,'function');
|
||||
f.logout();assert.equal((await f.call()).status,401);
|
||||
});
|
||||
test('review instructions require a boolean decision and cannot inject bank accounts or payment destinations',async()=>{
|
||||
const f=fixture(),review={projectId:randomUUID(),requestId:randomUUID(),version:1,approve:true,approvedMinor:100,note:'Approved.',confirmed:true};
|
||||
assert.equal((await f.call('/review',{...review,approve:'true'})).status,400);assert.equal((await f.call('/review',{...review,targetNumber:'any-bank-account'})).status,400);assert.equal(f.calls.length,0);
|
||||
assert.equal((await f.call('/review',review)).status,200);
|
||||
});
|
||||
54
website/tests/server-projects.test.mjs
Normal file
54
website/tests/server-projects.test.mjs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
||||
import {companyFixture} from './company-deed-fixture.mjs';
|
||||
import {createServerProjects,initializeServerProjects} from '../node/server-projects.mjs';
|
||||
async function fixture(){
|
||||
const f=await companyFixture();await initializeServerProjects(f.db);const payments=new Set(),news=new Set();let failPayment=false,canReview=true;
|
||||
const banking={fund:async(_actor,input)=>{if(failPayment)throw Error('Tax unavailable');payments.add(input.requestId);},close:async()=>({closed:true})};
|
||||
const service=createServerProjects(f.db,f.keys,{clock:()=>1000,authorizePlayer:async actor=>Object.values(f.people).some(p=>p.id===actor.id),authorizeReview:async actor=>canReview&&actor.id===f.people.Owner.id,banking,announce:async p=>news.add(p.id)});
|
||||
const submit={requestId:randomUUID(),title:'Public arena',proposal:'A new public arena for organised community events.',requestedMinor:25000};
|
||||
return{...f,service,submit,payments,news,banking,failPayment:value=>{failPayment=value;},revokeReview:()=>{canReview=false;}};
|
||||
}
|
||||
test('project applications are private, reviewed through a bound instrument and fund exactly once',async()=>{
|
||||
const f=await fixture();try{
|
||||
await f.service.submit(f.people.Member,f.submit);assert.equal((await f.service.submit(f.people.Member,f.submit)).replayed,true);
|
||||
const id=f.submit.requestId;assert.equal((await f.service.list(f.people.Other)).projects.length,0);assert.equal((await f.service.list(f.people.Owner)).projects.length,1);
|
||||
const review={projectId:id,requestId:randomUUID(),version:1,approve:true,approvedMinor:20000,note:'Approved server project.'};
|
||||
await assert.rejects(f.service.review(f.people.Other,review),/review authority/);
|
||||
await f.service.review(f.people.Owner,review);assert.equal((await f.service.review(f.people.Owner,review)).replayed,true);
|
||||
assert.equal((await f.service.list(f.people.Member)).projects[0].state,'funding-pending');
|
||||
assert.deepEqual(await f.service.drain(),{processed:1,failed:0});assert.equal(f.payments.size,1);assert.equal(f.news.size,0);
|
||||
await f.service.drain();await f.service.drain();assert.equal(f.news.size,1);assert.equal(f.payments.size,1);
|
||||
const p=(await f.service.list(f.people.Member)).projects[0];assert.equal(p.state,'active');
|
||||
await f.service.complete(f.people.Member,{projectId:id,requestId:randomUUID(),version:p.version});await f.service.drain();
|
||||
assert.equal((await f.service.list(f.people.Member)).projects[0].state,'completed');
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
test('unfunded approvals stay pending and payment retries reconcile a committed bank result',async()=>{
|
||||
const f=await fixture();try{
|
||||
await f.service.submit(f.people.Member,f.submit);await f.service.review(f.people.Owner,{projectId:f.submit.requestId,requestId:randomUUID(),version:1,approve:true,approvedMinor:25000,note:'Approved budget.'});
|
||||
f.failPayment(true);assert.equal((await f.service.drain()).failed,1);assert.equal(f.payments.size,0);assert.equal(f.news.size,0);
|
||||
f.failPayment(false);f.db.sqlite.exec("CREATE TRIGGER fail_project_activation BEFORE UPDATE ON jarvis_server_projects WHEN NEW.state='active' BEGIN SELECT RAISE(ABORT,'temporary central failure');END;");
|
||||
assert.equal((await f.service.drain()).failed,1);assert.equal(f.payments.size,1);assert.equal((await f.service.list(f.people.Member)).projects[0].state,'funding-pending');
|
||||
f.db.sqlite.exec('DROP TRIGGER fail_project_activation');await f.service.drain();await f.service.drain();assert.equal(f.payments.size,1);assert.equal(f.news.size,1);
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
test('changed project budgets, stale versions and withdrawn staff authority cannot approve requests',async()=>{
|
||||
const f=await fixture();try{
|
||||
await f.service.submit(f.people.Member,f.submit);await assert.rejects(f.service.submit(f.people.Member,{...f.submit,requestedMinor:50000}),/reference conflict/);
|
||||
const review={projectId:f.submit.requestId,requestId:randomUUID(),version:2,approve:true,approvedMinor:25000,note:'Approved budget.'};
|
||||
await assert.rejects(f.service.review(f.people.Owner,review),/version/);
|
||||
await assert.rejects(f.service.review(f.people.Owner,{...review,requestId:randomUUID(),version:1,approvedMinor:50000}),/within the requested/);
|
||||
f.revokeReview();await assert.rejects(f.service.review(f.people.Owner,{...review,requestId:randomUUID(),version:1}),/review authority/);
|
||||
assert.equal((await f.service.list(f.people.Member)).projects[0].state,'submitted');assert.equal(f.payments.size,0);
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
test('only funded, exhausted projects enter automatic closure and retries do not create extra jobs',async()=>{
|
||||
const f=await fixture();try{
|
||||
f.banking.exhausted=async()=>[{project_id:f.submit.requestId}];
|
||||
await f.service.submit(f.people.Member,f.submit);await f.service.drain();assert.equal((await f.service.list(f.people.Member)).projects[0].state,'submitted');
|
||||
await f.service.review(f.people.Owner,{projectId:f.submit.requestId,requestId:randomUUID(),version:1,approve:true,approvedMinor:25000,note:'Approved budget.'});
|
||||
await f.service.drain();assert.equal((await f.service.list(f.people.Member)).projects[0].state,'active');
|
||||
await f.service.drain();await f.service.drain();assert.equal((await f.service.list(f.people.Member)).projects[0].state,'completed');
|
||||
assert.equal((await f.db.query("SELECT id FROM jarvis_project_jobs WHERE kind='close-empty'")).length,1);
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
Loading…
Reference in a new issue

Staff