mancala/apps/backend/tests/game/fakes.ts
Halit Aksoy 85e3fb60f6 feat(backend): add game history API and live game previews
- GET /games/me/active and /games/me (Bearer token), /games/popular (live
  games, or the most spectated finished ones), and /game/:id now also
  returns saved records of finished games
- Lists are cached in Valkey for 10 seconds, cleared when a game is saved,
  and empty instead of failing while the database is down
- Live game previews over WebSocket: subscribers get the top 5 on
  subscribe and on change, at most once per second, without counting as
  spectators; ids are public ids
- RTMT supports several connection listeners
2026-10-16 00:21:09 +03:00

83 lines
3.0 KiB
TypeScript

import { FinishedGameSummary, GameRecord } from "@mancala/core";
import { GameHistoryRepository } from "../../src/history/GameHistoryRepository";
import { LiveGameStore } from "../../src/live/LiveGameStore";
import { Cache } from "../../src/cache/Cache";
export class InMemoryLiveGameStore implements LiveGameStore {
readonly games = new Map<string, unknown>();
readonly pending = new Map<string, GameRecord>();
failing = false;
private check() {
if (this.failing) return Promise.reject(new Error("Valkey is down"));
return Promise.resolve();
}
async saveGame(gameId: string, snapshot: unknown) {
await this.check();
this.games.set(gameId, JSON.parse(JSON.stringify(snapshot)));
}
async removeGame(gameId: string) {
await this.check();
this.games.delete(gameId);
}
async loadGames() {
await this.check();
return [...this.games.values()];
}
async savePendingRecord(record: GameRecord) {
await this.check();
this.pending.set(record.id, record);
}
async removePendingRecord(gameId: string) {
await this.check();
this.pending.delete(gameId);
}
async loadPendingRecords() {
await this.check();
return [...this.pending.values()];
}
}
// Lists leave out the moves, like the Postgres repository.
const toSummary = ({ moves, totalSpectators, ...summary }: GameRecord): FinishedGameSummary => summary;
export class InMemoryGameHistoryRepository implements GameHistoryRepository {
readonly records = new Map<string, GameRecord>();
failing = false;
async save(record: GameRecord) {
if (this.failing) throw new Error("database is down");
if (!this.records.has(record.id)) this.records.set(record.id, record);
}
async get(id: string) {
if (this.failing) throw new Error("database is down");
return this.records.get(id);
}
async getRecentForUser(userId: string, limit: number): Promise<FinishedGameSummary[]> {
return [...this.records.values()]
.filter((r) => r.player1.userId === userId || r.player2.userId === userId)
.sort((a, b) => b.endedAt - a.endedAt).slice(0, limit).map(toSummary);
}
async getPopular(limit: number): Promise<FinishedGameSummary[]> {
return [...this.records.values()]
.sort((a, b) => b.peakSpectators - a.peakSpectators || b.endedAt - a.endedAt).slice(0, limit).map(toSummary);
}
}
// Lets fire-and-forget promises (saving to Valkey or the database) settle.
export const flushPromises = () => new Promise((resolve) => setImmediate(resolve));
export class InMemoryCache implements Cache {
readonly values = new Map<string, unknown>();
async getJson<T>(key: string) {
return this.values.get(key) as T | undefined;
}
async setJson(key: string, value: unknown) {
this.values.set(key, JSON.parse(JSON.stringify(value)));
}
async delete(...keys: string[]) {
keys.forEach((key) => this.values.delete(key));
}
}