UNPKG

@amanwebdev/sudoku-generator

Version:
143 lines (142 loc) 6.37 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); describe('generateSudoku', () => { it('should generate a 9x9 grid', () => { const sudoku = (0, index_1.generateSudoku)(9, 'easy'); expect(sudoku.grid.length).toBe(9); expect(sudoku.grid[0].length).toBe(9); }); it('should generate a 4x4 grid', () => { const sudoku = (0, index_1.generateSudoku)(4, 'easy'); expect(sudoku.grid.length).toBe(4); expect(sudoku.grid[0].length).toBe(4); }); it('should generate a 6x6 grid', () => { const sudoku = (0, index_1.generateSudoku)(6, 'easy'); expect(sudoku.grid.length).toBe(6); expect(sudoku.grid[0].length).toBe(6); }); it('should have a valid number of empty cells for easy difficulty', () => { const sudoku = (0, index_1.generateSudoku)(9, 'easy'); const emptyCells = sudoku.grid.flat().filter((cell) => cell === 0).length; expect(emptyCells).toBeGreaterThan(0); }); }); describe('toPDF', () => { const testGrid = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9] ]; it('should generate a PDF with default options', () => __awaiter(void 0, void 0, void 0, function* () { const pdfBytes = yield (0, index_1.toPDF)(testGrid); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should generate a PDF with light theme', () => __awaiter(void 0, void 0, void 0, function* () { const pdfBytes = yield (0, index_1.toPDF)(testGrid, { theme: 'light' }); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should generate a PDF with dark theme', () => __awaiter(void 0, void 0, void 0, function* () { const pdfBytes = yield (0, index_1.toPDF)(testGrid, { theme: 'dark' }); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should generate a PDF with custom theme', () => __awaiter(void 0, void 0, void 0, function* () { const customTheme = { background: '#f0f0f0', gridColor: '#333333', textColor: '#000000', boxLineColor: '#666666' }; const pdfBytes = yield (0, index_1.toPDF)(testGrid, { theme: customTheme }); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should generate a PDF with custom metadata', () => __awaiter(void 0, void 0, void 0, function* () { const pdfBytes = yield (0, index_1.toPDF)(testGrid, { title: 'Custom Sudoku', author: 'Test Author', subject: 'Test Subject', keywords: 'test, sudoku' }); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should work with 4x4 grid', () => __awaiter(void 0, void 0, void 0, function* () { const smallGrid = [ [1, 0, 3, 0], [0, 3, 0, 1], [3, 0, 1, 0], [0, 1, 0, 3] ]; const pdfBytes = yield (0, index_1.toPDF)(smallGrid); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should work with 6x6 grid', () => __awaiter(void 0, void 0, void 0, function* () { const mediumGrid = [ [1, 0, 3, 0, 5, 0], [0, 3, 0, 1, 0, 5], [3, 0, 1, 0, 4, 0], [0, 1, 0, 3, 0, 2], [5, 0, 4, 0, 2, 0], [0, 2, 0, 5, 0, 1] ]; const pdfBytes = yield (0, index_1.toPDF)(mediumGrid); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should generate solution PDF when showSolution is true', () => __awaiter(void 0, void 0, void 0, function* () { const puzzle = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9] ]; const solution = [ [5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3, 4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7], [8, 5, 9, 7, 6, 1, 4, 2, 3], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 6, 1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8, 6, 1, 7, 9] ]; const pdfBytes = yield (0, index_1.toPDF)(puzzle, { showSolution: true, title: 'Sudoku Solution' }, solution); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); it('should work without solution parameter when showSolution is false', () => __awaiter(void 0, void 0, void 0, function* () { const pdfBytes = yield (0, index_1.toPDF)(testGrid, { showSolution: false }); expect(pdfBytes).toBeInstanceOf(Uint8Array); expect(pdfBytes.length).toBeGreaterThan(0); })); });