Files
ai-coding-workflow-labs/superpowers/tests/game/board.test.js
T
tiennm99 51a4b90163 refactor: migrate quiz-project, gstack, and superpowers to JS+JSDoc
Convert all 41 TypeScript source/config files in cc4e-course/quiz-project
(Next.js), gstack (Astro), and superpowers (Vite/React/Phaser) to plain
JavaScript with JSDoc type annotations, replacing tsconfig.json with
jsconfig.json (strict + checkJs) in each subproject. No behavior changes:
lint, typecheck, test, and build gates match their TypeScript baselines
exactly in all three subprojects. Update stack references in the affected
READMEs accordingly.
2026-08-18 09:49:42 +07:00

94 lines
2.6 KiB
JavaScript

import { describe, it, expect } from "vitest";
import { createBoard, getRemainingTiles, shuffleBoard } from "../../src/game/board";
describe("createBoard", () => {
it("creates a board with correct dimensions for easy", () => {
const board = createBoard("easy");
expect(board.length).toBe(4);
expect(board[0].length).toBe(6);
});
it("creates a board with correct dimensions for medium", () => {
const board = createBoard("medium");
expect(board.length).toBe(6);
expect(board[0].length).toBe(8);
});
it("creates a board with correct dimensions for hard", () => {
const board = createBoard("hard");
expect(board.length).toBe(8);
expect(board[0].length).toBe(10);
});
it("every tile has a matching pair", () => {
const board = createBoard("easy");
/** @type {Map<string, number>} */
const emojiCounts = new Map();
for (const row of board) {
for (const cell of row) {
if (cell) {
emojiCounts.set(cell.emoji, (emojiCounts.get(cell.emoji) ?? 0) + 1);
}
}
}
for (const [, count] of emojiCounts) {
expect(count).toBe(2);
}
});
it("all tiles have unique ids", () => {
const board = createBoard("medium");
/** @type {Set<number>} */
const ids = new Set();
for (const row of board) {
for (const cell of row) {
if (cell) {
expect(ids.has(cell.id)).toBe(false);
ids.add(cell.id);
}
}
}
});
});
describe("getRemainingTiles", () => {
it("returns all tiles from a full board", () => {
const board = createBoard("easy");
const remaining = getRemainingTiles(board);
expect(remaining.length).toBe(24);
});
it("excludes null cells", () => {
const board = createBoard("easy");
board[0][0] = null;
board[0][1] = null;
const remaining = getRemainingTiles(board);
expect(remaining.length).toBe(22);
});
});
describe("shuffleBoard", () => {
it("preserves tile count after shuffle", () => {
const board = createBoard("easy");
board[0][0] = null;
board[0][1] = null;
const shuffled = shuffleBoard(board);
const remaining = getRemainingTiles(shuffled);
expect(remaining.length).toBe(22);
});
it("keeps null positions as occupied cells and vice versa", () => {
const board = createBoard("easy");
board[0][0] = null;
board[1][2] = null;
const shuffled = shuffleBoard(board);
let nullCount = 0;
for (const row of shuffled) {
for (const cell of row) {
if (cell === null) nullCount++;
}
}
expect(nullCount).toBe(2);
});
});