Initial commit: ssessesse ki battle
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { createGameSessionStore } from '../src/server/game-session-store.js';
|
||||
|
||||
const uri = process.env.TEST_MONGODB_URI;
|
||||
test('Standalone session counters apply concurrent and late results exactly once', { skip: !uri }, async () => {
|
||||
const client = await new MongoClient(uri!, { serverSelectionTimeoutMS: 5000 }).connect();
|
||||
const db = client.db(`dragon_ball_test_p2p_records_${randomUUID().replaceAll('-', '')}`);
|
||||
try {
|
||||
let now = Date.now();
|
||||
const store = await createGameSessionStore(db, () => now);
|
||||
const creationKey = randomBytes(32).toString('base64url');
|
||||
const session = await store.create(creationKey);
|
||||
assert.deepEqual(await store.create(creationKey), session);
|
||||
const auth = { gameSessionId: session.gameSessionId, token: session.token };
|
||||
const registration = { requestId: randomUUID(), matchId: randomUUID(), mode: 'bot' as const, expectedPreviousSeq: 0 };
|
||||
const active = await store.register(auth, registration);
|
||||
assert.deepEqual(await store.register(auth, registration), active);
|
||||
const win = { ...active, outcome: 'win' as const, reason: 'hit' as const };
|
||||
const replies = await Promise.all(Array.from({ length: 20 }, () => store.record(auth, win)));
|
||||
assert.equal(replies.filter(r => r.status === 'applied').length, 1);
|
||||
assert.equal((await store.get(auth)).stats.bot.wins, 1);
|
||||
assert.equal((await store.get(auth)).stats.bot.current, 1);
|
||||
assert.equal((await store.record(auth, win)).status, 'duplicate'); // lost response retry
|
||||
await assert.rejects(store.record(auth, { ...win, outcome: 'loss' }), /RESULT_CONFLICT/);
|
||||
const second = await store.register(auth, { ...registration, requestId: randomUUID(), matchId: randomUUID(), expectedPreviousSeq: 1 });
|
||||
await store.record(auth, { ...second, outcome: 'loss', reason: 'hit' });
|
||||
assert.equal((await store.record(auth, win)).status, 'stale');
|
||||
assert.deepEqual((await store.get(auth)).stats.bot, { wins: 1, losses: 1, draws: 0, current: 0, best: 1 });
|
||||
const registrations = await Promise.allSettled([0, 1].map(() => store.register(auth, { ...registration, requestId: randomUUID(), matchId: randomUUID(), expectedPreviousSeq: 2 })));
|
||||
assert.equal(registrations.filter(r => r.status === 'fulfilled').length, 1);
|
||||
const third = (await store.get(auth)).activeMatch!;
|
||||
await store.record(auth, { ...third, outcome: 'aborted', reason: 'connection_failed' });
|
||||
assert.deepEqual((await store.get(auth)).stats.bot, { wins: 1, losses: 1, draws: 0, current: 0, best: 1 });
|
||||
await assert.rejects(store.record(auth, { ...third, seq: 9, matchId: randomUUID(), outcome: 'win', reason: 'hit' }), /NO_ACTIVE_MATCH/);
|
||||
await assert.rejects(store.get({ ...auth, token: randomBytes(32).toString('base64url') }), /INVALID_SESSION/);
|
||||
const fourth = await store.register(auth, { ...registration, requestId: randomUUID(), matchId: randomUUID(), expectedPreviousSeq: 3 });
|
||||
await store.record(auth, { ...fourth, outcome: 'draw', reason: 'double_timeout' });
|
||||
assert.equal((await store.get(auth)).stats.bot.draws, 1);
|
||||
now += 91 * 86400000;
|
||||
await assert.rejects(store.get(auth), /INVALID_SESSION/);
|
||||
} finally { try { await db.dropDatabase(); } finally { await client.close(); } }
|
||||
});
|
||||
test('friend registration requires membership, but expired room cannot prevent recording', { skip: !uri }, async () => {
|
||||
const client = await new MongoClient(uri!, { serverSelectionTimeoutMS: 5000 }).connect();
|
||||
const db = client.db(`dragon_ball_test_p2p_member_${randomUUID().replaceAll('-', '')}`);
|
||||
try {
|
||||
let now = Date.now(); const store = await createGameSessionStore(db, () => now);
|
||||
const session = await store.create(randomBytes(32).toString('base64url'));
|
||||
const other = await store.create(randomBytes(32).toString('base64url'));
|
||||
const request = { requestId: randomUUID(), matchId: randomUUID(), roomId: randomUUID(), mode: 'friend' as const, expectedPreviousSeq: 0 };
|
||||
await assert.rejects(store.register(session, request), /ROOM_NOT_FOUND/);
|
||||
await db.collection<any>('rooms').insertOne({ _id: request.roomId, hostGameSessionId: session.gameSessionId, guestGameSessionId: other.gameSessionId, status: 'paired', currentMatchId: request.matchId, expiresAt: new Date(now + 1000) });
|
||||
const active = await store.register(session, request); now += 2000;
|
||||
await store.record(session, { ...active, outcome: 'win', reason: 'hit' });
|
||||
assert.equal((await store.get(session)).stats.friend.current, 1);
|
||||
assert.equal((await store.get(session)).stats.bot.wins, 0);
|
||||
} finally { try { await db.dropDatabase(); } finally { await client.close(); } }
|
||||
});
|
||||
Reference in New Issue
Block a user