the-game-domain
Version:
Complete domain layer for 'The Game' card game with rich business entities, automatic persistence, action tracking, and advanced game mechanics. Features Game, Player, Card, Deck, Pile entities with repository pattern, turn management, victory/defeat dete
110 lines • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const InMemoryGameRepository_1 = require("./InMemoryGameRepository");
const Game_1 = require("../entities/Game");
describe('InMemoryGameRepository', () => {
let repository;
beforeEach(() => {
repository = new InMemoryGameRepository_1.InMemoryGameRepository();
});
describe('save', () => {
it('should save a game successfully', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
expect(repository.size).toBe(1);
expect(repository.has(game.id)).toBe(true);
});
it('should update an existing game', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
const player = game.players[0];
if (player) {
const card = player.cards[0];
if (card) {
await game.moveCardToPile(player.id, card.value, 1);
}
}
await repository.save(game);
expect(repository.size).toBe(1);
const savedGame = await repository.findById(game.id);
expect(savedGame).toBeDefined();
if (savedGame) {
const savedPlayer = savedGame.players[0];
expect(savedPlayer?.cards.length).toBe(5);
}
});
});
describe('findById', () => {
it('should return null for non-existent game', async () => {
const result = await repository.findById('non-existent-id');
expect(result).toBeNull();
});
it('should return the correct game', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
const result = await repository.findById(game.id);
expect(result).toBeDefined();
expect(result?.id).toBe(game.id);
});
});
describe('findAll', () => {
it('should return empty array when no games exist', async () => {
const result = await repository.findAll();
expect(result).toEqual([]);
});
it('should return all saved games', async () => {
const game1 = new Game_1.Game(2);
const game2 = new Game_1.Game(3);
await repository.save(game1);
await repository.save(game2);
const result = await repository.findAll();
expect(result).toHaveLength(2);
expect(result.map(g => g.id)).toContain(game1.id);
expect(result.map(g => g.id)).toContain(game2.id);
});
});
describe('delete', () => {
it('should delete an existing game', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
await repository.delete(game.id);
expect(repository.size).toBe(0);
expect(repository.has(game.id)).toBe(false);
});
it('should not throw error when deleting non-existent game', async () => {
await expect(repository.delete('non-existent-id')).resolves.not.toThrow();
});
});
describe('clear', () => {
it('should remove all games', async () => {
const game1 = new Game_1.Game(2);
const game2 = new Game_1.Game(3);
await repository.save(game1);
await repository.save(game2);
repository.clear();
expect(repository.size).toBe(0);
expect(await repository.findAll()).toEqual([]);
});
});
describe('size', () => {
it('should return correct size', () => {
expect(repository.size).toBe(0);
});
it('should update size when games are added', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
expect(repository.size).toBe(1);
});
});
describe('has', () => {
it('should return false for non-existent game', () => {
expect(repository.has('non-existent-id')).toBe(false);
});
it('should return true for existing game', async () => {
const game = new Game_1.Game(2);
await repository.save(game);
expect(repository.has(game.id)).toBe(true);
});
});
});
//# sourceMappingURL=InMemoryGameRepository.test.js.map