MC/website/tests/jarvis-key-validation.test.mjs
2026-11-27 23:43:05 +00:00

2 lines
1.5 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 {createKeyValidation,VALIDATION_CASE_SCHEMA} from '../node/jarvis-key-validation.mjs';
test('invalid user and staff checks create one redacted Dev incident per day and never consume keys',async()=>{const db=new BankFixture();await db.query(VALIDATION_CASE_SCHEMA.replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4',''));const user={id:randomUUID(),username:'Tester'},calls=[];let staff=false;const api=createKeyValidation(db,{inspect:async()=>({valid:false,state:'invalid'})},{authenticate:async()=>user,canStaff:async()=>staff,createTicket:async p=>{calls.push(p);return{ticketNumber:'DEV-1'};},clock:()=>1000});const check=(code,extra={})=>api(new Request('https://cp.dunerealms.com/api/jarvis/validate',{method:'POST',headers:{Origin:'https://cp.dunerealms.com','Content-Type':'application/json'},body:JSON.stringify({code,...extra})}));try{assert.equal((await check('CLAN-PRIVATE-INVALID',{staff:true})).status,403);const first=await(await check('CLAN-PRIVATE-INVALID')).json();assert.equal(first.support,'created');assert.equal((await(await check('OTHER-PRIVATE-INVALID')).json()).caseId,first.caseId);assert.equal(calls.length,1);assert.ok(!JSON.stringify(calls).includes('PRIVATE-INVALID'));assert.ok(!(await db.first('SELECT * FROM jarvis_validation_cases')).key_digest.includes('PRIVATE'));}finally{db.close();}});