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

20 lines
2.5 KiB
JavaScript

import {test} from 'node:test';import assert from 'node:assert/strict';import {BankFixture} from './postbank-fixture.mjs';
import {personnelSchema,ensurePersonnelNumber,DIRECTORY_OWNER_ID} from '../node/personnel-numbers.mjs';
async function fixture(){const db=new BankFixture();db.sqlite.exec('CREATE TABLE site_accounts(id TEXT PRIMARY KEY,username TEXT,role TEXT,password_hash TEXT);CREATE TABLE site_account_policies(user_id TEXT PRIMARY KEY,game_link_exempt INTEGER)');for(const sql of personnelSchema)db.sqlite.exec(sql.replace(/ ENGINE=InnoDB DEFAULT CHARSET=utf8mb4/g,''));for(const [id,name,role]of [[DIRECTORY_OWNER_ID,'unimatrix999','owners'],['user-a','alice','user'],['user-b','bob','user'],['official','dunerealms','official_account'],['service','dev','user']])await db.query('INSERT INTO site_accounts VALUES(?,?,?,?)',[id,name,role,'hash']);await db.query('INSERT INTO site_account_policies VALUES(?,1)',['service']);const transaction=db.transaction.bind(db);db.transaction=work=>transaction(tx=>work({...tx,first:(sql,args)=>tx.first(sql.replace(/ FOR SHARE/g,''),args)}));return db;}
test('one immutable number per person, owner 0001 reserved and duplicate calls cannot allocate twice',async()=>{
const db=await fixture();try{
assert.equal(await db.transaction(tx=>ensurePersonnelNumber(tx,'user-a')),'DR-0002');
assert.equal(await db.transaction(tx=>ensurePersonnelNumber(tx,DIRECTORY_OWNER_ID)),'DR-0001');
const result=await Promise.all([db.transaction(tx=>ensurePersonnelNumber(tx,'user-b')),db.transaction(tx=>ensurePersonnelNumber(tx,'user-b'))]);assert.deepEqual(result,['DR-0003','DR-0003']);
await db.query("UPDATE site_accounts SET username='renamed',role='staff' WHERE id='user-a'");assert.equal(await db.transaction(tx=>ensurePersonnelNumber(tx,'user-a')),'DR-0002');
assert.equal((await db.first('SELECT COUNT(*) n FROM directory_personnel')).n,3);
}finally{db.close();}
});
test('official and technical identities do not get extra person numbers; failed appointments roll back allocation',async()=>{
const db=await fixture();try{
for(const id of ['official','service'])assert.equal(await db.transaction(tx=>ensurePersonnelNumber(tx,id)),null);
await assert.rejects(db.transaction(async tx=>{await ensurePersonnelNumber(tx,'user-a');throw Error('Appointment audit failed');}));
assert.equal((await db.first('SELECT COUNT(*) n FROM directory_personnel')).n,0);
assert.equal(await db.transaction(tx=>ensurePersonnelNumber(tx,'user-b')),'DR-0002');
}finally{db.close();}
});