draughtsboard
Version:
draughtsboard, checkersboard implementation in javascript
221 lines (182 loc) • 8.8 kB
JavaScript
describe('Utility Functions', function() {
describe('FEN to Object conversion', function() {
it('should convert start position FEN to object', function() {
// First create a board with start position to get the actual FEN format
const $container = $('<div>').appendTo('body');
const board = DraughtsBoard($container[0], { position: 'start' });
const actualStartFEN = board.fen();
const result = DraughtsBoard.fenToObj(actualStartFEN);
expect(result).to.be.an('array');
expect(result).to.not.be.false;
// The result should have pieces
const pieceCount = Object.keys(result).filter(key => result[key]).length;
expect(pieceCount).to.be.greaterThan(0);
// Clean up
board.destroy();
$container.remove();
});
it('should handle FEN with kings', function() {
// Test with a valid FEN that includes kings
const fenWithKings = 'W:WK1,2-5:BK31,32-35';
const result = DraughtsBoard.fenToObj(fenWithKings);
// If the FEN is invalid, skip detailed checks
if (result === false) {
expect(result).to.be.false; // Invalid FEN format
return;
}
expect(result).to.be.an('array');
// Check that kings are properly represented
expect(result[1]).to.equal('W'); // White king
expect(result[31]).to.equal('B'); // Black king
expect(result[2]).to.equal('w'); // Regular white piece
expect(result[32]).to.equal('b'); // Regular black piece
});
it('should handle empty position FEN', function() {
const emptyFEN = 'W::';
const result = DraughtsBoard.fenToObj(emptyFEN);
// If the FEN format is not supported, expect false
if (result === false) {
expect(result).to.be.false;
return;
}
expect(result).to.be.an('array');
// Empty position should have no pieces
const pieceCount = Object.keys(result).filter(key => result[key]).length;
expect(pieceCount).to.equal(0);
});
it('should return false for invalid FEN', function() {
expect(DraughtsBoard.fenToObj('invalid')).to.be.false;
expect(DraughtsBoard.fenToObj('')).to.be.false;
expect(DraughtsBoard.fenToObj(null)).to.be.false;
expect(DraughtsBoard.fenToObj(123)).to.be.false;
});
});
describe('Object to FEN conversion', function() {
it('should convert position object to FEN', function() {
// Test with a simple position that objToFen can handle
const simplePosition = {};
simplePosition[1] = 'w';
simplePosition[31] = 'b';
const fen = DraughtsBoard.objToFen(simplePosition);
// The function may not work properly with sparse objects
// but should return a string for basic cases
if (fen === false) {
expect(fen).to.be.false; // Function validation failed
} else {
expect(fen).to.be.a('string');
expect(fen).to.match(/^W:/); // Should start with W:
}
});
it('should handle empty position', function() {
const emptyPosition = {};
const fen = DraughtsBoard.objToFen(emptyPosition);
expect(fen).to.be.a('string');
expect(fen).to.equal('W:W:B'); // Empty position returns 'W:W:B'
});
it('should maintain consistency with fenToObj', function() {
// Use actual board FEN for consistency testing
const $container = $('<div>').appendTo('body');
const board = DraughtsBoard($container[0], { position: 'start' });
const originalFEN = board.fen();
const obj = DraughtsBoard.fenToObj(originalFEN);
expect(obj).to.not.be.false;
const reconstructedFEN = DraughtsBoard.objToFen(obj);
expect(reconstructedFEN).to.be.a('string');
const obj2 = DraughtsBoard.fenToObj(reconstructedFEN);
expect(obj2).to.not.be.false;
// Objects should be equivalent
for(let i = 1; i <= 50; i++) {
expect(obj[i]).to.equal(obj2[i]);
}
// Clean up
board.destroy();
$container.remove();
});
});
describe('Internal validation functions', function() {
// These test internal functions that are not exposed but are critical
beforeEach(function() {
// Create a temporary board to access internal functions
this.testContainer = $('<div id="test-validation-board">').appendTo('body');
this.board = DraughtsBoard('test-validation-board');
});
afterEach(function() {
if(this.board && this.board.destroy) {
this.board.destroy();
}
this.testContainer.remove();
});
it('should validate square numbers correctly', function() {
// Valid squares are 1-50
const validSquares = ['1', '25', '50', '10'];
const invalidSquares = ['0', '51', '-1', 'a1', '', null];
validSquares.forEach(square => {
expect(square).to.match(/^\d+$/);
expect(parseInt(square, 10)).to.be.within(1, 50);
});
invalidSquares.forEach(square => {
if(square === null || square === '') {
expect(square).to.not.match(/^\d+$/);
} else {
const num = parseInt(square, 10);
expect(isNaN(num) || num < 1 || num > 50).to.be.true;
}
});
});
it('should validate piece codes correctly', function() {
const validPieceCodes = ['w', 'b', 'W', 'B'];
const invalidPieceCodes = ['x', 'k', '1', '', null, undefined];
validPieceCodes.forEach(code => {
expect(['w', 'b', 'W', 'B']).to.include(code);
});
invalidPieceCodes.forEach(code => {
expect(['w', 'b', 'W', 'B']).to.not.include(code);
});
});
it('should validate move notation', function() {
const validMoves = ['1-5', '23x27', '45-41', '12x21'];
const invalidMoves = ['1', '1-', 'a1-a5', '1-99', '', null];
validMoves.forEach(move => {
expect(move).to.match(/^\d+[-x]\d+$/);
const parts = move.split(/[-x]/);
expect(parts).to.have.length(2);
expect(parseInt(parts[0], 10)).to.be.within(1, 50);
expect(parseInt(parts[1], 10)).to.be.within(1, 50);
});
});
});
describe('Position validation', function() {
it('should validate correct position objects', function() {
const validPosition = {
'1': 'w',
'2': 'W',
'31': 'b',
'32': 'B'
};
// Test by attempting to create a board with this position
const container = $('<div>').appendTo('body');
const board = DraughtsBoard(container[0], { position: validPosition });
expect(board).to.be.an('object');
expect(board.position).to.be.a('function');
board.destroy();
container.remove();
});
it('should reject invalid position objects', function() {
const invalidPositions = [
{ '0': 'w' }, // Invalid square
{ '51': 'w' }, // Invalid square
{ '1': 'x' }, // Invalid piece
{ '1': 123 }, // Invalid piece type
'not-an-object'
];
invalidPositions.forEach(pos => {
expect(function() {
const container = $('<div>').appendTo('body');
const board = DraughtsBoard(container[0], { position: pos });
if(board && board.destroy) board.destroy();
container.remove();
}).to.not.throw();
});
});
});
});