MC/website/tests/kitset-grants.test.mjs
2026-11-27 23:43:05 +00:00

21 lines
3.8 KiB
JavaScript

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {createHash,createHmac} from 'node:crypto';
import {prepareKitsetGrant,kitGrantReceipt} from '../node/kitset-grants.mjs';
import {defaultItemDesign} from '../control-panel/public/item-design-model.mjs';
const actorId='f3ffda37-01e4-41ea-b89d-fbeda82e453e',kitsetId='f8f7f370-de8d-4f48-bf7b-ee15310f3a81',requestId='1fa03954-62cf-440a-b762-8b45924c3061',player='49d2fe76-7977-4d49-bd39-e48fc8c648b8',secret='b'.repeat(64);
function fixture(){
const design={version:1,name:'Test',owner:{kind:'office',id:'emperor'},items:[{id:'8d38f674-2155-44ba-b5dd-d1430f5c584a',quantity:1,design:defaultItemDesign('dr_s1_legendary_mk14_helmet')}]},raw=JSON.stringify(design);
const row={key_id:'8d38f674-2155-44ba-b5dd-d1430f5c584b',revision:1,design_json:raw,fingerprint:createHash('sha256').update(raw).digest('hex'),revoked_at:null,expires_at:null,key_type:'kit-definition'};
const db={async query(sql){assert.match(sql,/^SELECT /);if(sql.includes('jarvis_kitset_listings'))return [row];return [{id:actorId,username:'unimatrix999',role:'owner'}];}};
return {db,row,input:{actorId,kitsetId,requestId,recipients:[player],serverKey:'lobby'},settings:{GAME_LINK_SECRET:secret},options:{clock:()=>1000,allowed:async()=>true}};
}
test('prepares only a signed immutable definition, without writes or a bank charge',async()=>{const f=fixture(),r=await prepareKitsetGrant(f.db,f.input,f.settings,f.options);assert.equal(r.audit.chargedMinor,0);assert.equal(r.audit.state,'prepared');const d=JSON.parse(Buffer.from(r.envelope.payload,'base64'));assert.equal(d.kitName,'Test');assert.equal(d.items[0].design.equipmentMark,14);assert.equal(d.recipients[0].receiptId,kitGrantReceipt(requestId,player));assert.equal(r.envelope.signature,createHmac('sha256',secret).update(r.envelope.payload).digest('hex'));});
test('rejects missing current authority',async()=>{const f=fixture();await assert.rejects(prepareKitsetGrant(f.db,f.input,f.settings,{...f.options,allowed:async()=>false}),/authority/);});
test('rejects revoked definitions',async()=>{const f=fixture();f.row.revoked_at=1;await assert.rejects(prepareKitsetGrant(f.db,f.input,f.settings,f.options),/unavailable/);});
test('rejects edited published snapshots',async()=>{const f=fixture();f.row.design_json=f.row.design_json.replace('Test','Changed');await assert.rejects(prepareKitsetGrant(f.db,f.input,f.settings,f.options),/changed/);});
test('rejects duplicate or too many recipients',async()=>{const f=fixture();for(const recipients of [[player,player],[player,player,player]])await assert.rejects(prepareKitsetGrant(f.db,{...f.input,recipients},f.settings,f.options),/recipients/);});
test('rejects missing or ambiguous game links',async()=>{const f=fixture(),query=f.db.query;f.db.query=sql=>sql.includes('site_game_links')?Promise.resolve([]):query(sql);await assert.rejects(prepareKitsetGrant(f.db,f.input,f.settings,f.options),/linked personal/);});
test('receipt identities are stable and recipient-bound',()=>{assert.equal(kitGrantReceipt(requestId,player),kitGrantReceipt(requestId,player));assert.notEqual(kitGrantReceipt(requestId,player),kitGrantReceipt(requestId,actorId));});
test('published copper kit entries reach the upgraded native engine instead of a legacy website veto',async()=>{const f=fixture(),kit=JSON.parse(f.row.design_json);kit.items[0].design=defaultItemDesign('dr_s1_spice_shovel_mk5');f.row.design_json=JSON.stringify(kit);f.row.fingerprint=createHash('sha256').update(f.row.design_json).digest('hex');const grant=await prepareKitsetGrant(f.db,f.input,f.settings,f.options),payload=JSON.parse(Buffer.from(grant.envelope.payload,'base64'));assert.equal(payload.items[0].template.material,'copper_shovel');assert.equal(payload.items[0].design.quality,'rare');assert.equal(payload.items[0].quantity,1);});