try to fix this spaghetti of a code

This commit is contained in:
Frederik Spirgi
2026-09-09 10:57:34 +02:00
parent 101116472a
commit 729de5dcc4
5 changed files with 102 additions and 29 deletions
+16
View File
@@ -0,0 +1,16 @@
const crypto = require('node:crypto');
const {createRepository} = require('../lib/repository');
const {createLobbyService} = require('../lib/lobbies');
(async () => {
const repo = await createRepository(process.argv[2]);
for (const username of ['a','b']) await repo.register(username,'unused-test-hash',null);
let now=1;
const lobbies=createLobbyService(repo,{ante:10,now:()=>now,intermissionMs:60000,turnMs:30000});
const command=(username,type,fields={})=>lobbies.command(username,{type,requestId:crypto.randomUUID(),...fields});
const {lobbyId}=await command('a','lobby:create');
await command('b','lobby:join',{lobbyId}); await command('a','poker:start',{lobbyId});
const game=lobbies.get(lobbyId).game; now=game.nextRoundStartsAt;
await command(null,'timer',{lobbyId,handId:game.id,phase:'intermission',deadline:now});
await command('b','poker:action',{lobbyId,action:'bet',payload:{amount:30}});
process.send({ready:true,totals:await repo.totals()});
})().catch(error=>{console.error(error);process.exit(1);});
+25
View File
@@ -0,0 +1,25 @@
const {test,expect} = require('@playwright/test');
const {fork} = require('node:child_process');
const {once} = require('node:events');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const {createRepository} = require('../lib/repository');
test('a killed process leaves durable escrow that the next owner refunds once',async()=>{
const directory=await fs.mkdtemp(path.join(os.tmpdir(),'gambling-crash-'));
const filename=path.join(directory,'test.sqlite');
const child=fork(path.join(__dirname,'crash-fixture.js'),[filename],{stdio:['ignore','ignore','pipe','ipc'],windowsHide:true});
try {
const [message]=await once(child,'message');
expect(message.totals).toEqual({users:350,escrow:50});
const exited=once(child,'exit'); child.kill('SIGKILL'); await exited;
const repository=await createRepository(filename);
try {await repository.recover();await repository.recover();expect(await repository.totals()).toEqual({users:400,escrow:0});}
finally {await repository.close();}
} finally {
if(child.exitCode===null && child.signalCode===null){const exited=once(child,'exit');child.kill('SIGKILL');await exited;}
const relative=path.relative(os.tmpdir(),directory);
if(relative.startsWith('..') || path.isAbsolute(relative) || !path.basename(directory).startsWith('gambling-crash-')) throw new Error('Unsafe cleanup path');
await fs.rm(directory,{recursive:true,force:true});
}
});