UNPKG

rubik-cipher

Version:

Simple rubik cube cipher

268 lines 13.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const RubikCipher_1 = __importDefault(require("./RubikCipher")); const rubikHelpers_1 = require("./helpers/rubikHelpers"); describe('rubikHelpers test', () => { it('chunkString', () => { chai_1.expect(rubikHelpers_1.chunkString('aaaa', 2)).to.eql(['aa', 'aa']); }); it('minimalSizeByText', () => { chai_1.expect(rubikHelpers_1.minimalSizeByText('testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttest')).to.equal(4); }); it('encodeScramble', () => { chai_1.expect(rubikHelpers_1.encodeScramble({ depth: 1, wide: false, target: 'B', rotation: 1, })).to.equal('B011'); chai_1.expect(rubikHelpers_1.encodeScramble({ depth: 2, wide: false, target: 'B', rotation: 1, })).to.equal('B021'); chai_1.expect(rubikHelpers_1.encodeScramble({ depth: 3, wide: true, target: 'B', rotation: 1, })).to.equal('B131'); chai_1.expect(rubikHelpers_1.encodeScramble({ depth: 1, wide: true, target: 'B', rotation: 1, })).to.equal('B111'); chai_1.expect(() => { rubikHelpers_1.encodeScramble({ depth: 1, wide: true, target: 'B', rotation: 4, }); }).to.throw(); }); it('decodeScramble', () => { chai_1.expect(rubikHelpers_1.decodeScramble('B011')).to.eql({ depth: 1, wide: false, target: 'B', rotation: 1 }); chai_1.expect(rubikHelpers_1.decodeScramble('B021')).to.eql({ depth: 2, wide: false, target: 'B', rotation: 1 }); chai_1.expect(rubikHelpers_1.decodeScramble('B131')).to.eql({ depth: 3, wide: true, target: 'B', rotation: 1 }); chai_1.expect(rubikHelpers_1.decodeScramble('B111')).to.eql({ depth: 1, wide: true, target: 'B', rotation: 1 }); chai_1.expect(() => { rubikHelpers_1.decodeScramble('B11'); }).to.throw(); }); it('isValidKey', () => { chai_1.expect(rubikHelpers_1.isValidKey('LoremIpsum', '+5+1+1+L011 L123')).to.be.equal(false); chai_1.expect(rubikHelpers_1.isValidKey('LoremIpsum', '1+L011')).to.be.equal(false); chai_1.expect(rubikHelpers_1.isValidKey('LoremIpsum', '1+1+1+0+L112 U112 B013 D111')).to.be.equal(false); chai_1.expect(rubikHelpers_1.isValidKey('LoremIpsum', '2+0+1+0+L112 U112 B013 D111')).to.be.equal(false); chai_1.expect(rubikHelpers_1.isValidKey('LoremIpsumLoremIpsumLoremIpsum', '2+1+1+0+L112 U112 B013 D111')).to.be.equal(false); }); it('getDataFromKey', () => { chai_1.expect(() => { rubikHelpers_1.getDataFromKey('LoremIpsum', '1+L011'); }).to.throw(); }); }); describe('Cube test', () => { it('Create cube without config', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test'); }).to.not.throw(); }); it('Create cube with empty text', () => { chai_1.expect(() => { const result = new RubikCipher_1.default(''); }).to.throw(); }); it('Create cube with config object', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', { cubeSize: 2, groupSize: 1, fillRest: true, fillWithRandom: false }); }).to.not.throw(); chai_1.expect(() => { const result = new RubikCipher_1.default('test', {}); }).to.not.throw(); }); it('Create cube with config that has invalid size', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', { cubeSize: 1 }); }).to.throw(); }); it('Create cube with config, that has text longer than specified size', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('testtesttesttesttesttesttesttest', { cubeSize: 2 }); }).to.throw(); }); it('Create cube with config that has invalid group size', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', { groupSize: 0 }); }).to.throw('Group size must be larger than 0'); }); it('Create cube with key config', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', '2+1+1+0+L112 U112 B013 D111'); }).to.not.throw(); }); it('Create cube with key config, that has invalid size', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', '1+1+1+0+L112 U112 B013 D111'); }).to.throw(); }); it('Create cube with key config, that has invalid group size', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', '2+0+1+0+L112 U112 B013 D111'); }).to.throw(); }); it('Create cube with key config, that has too long text', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttest', '2+1+1+0+F R- L'); }).to.throw(); }); it('Create cube with key config, that has invalid format', () => { chai_1.expect(() => { const result = new RubikCipher_1.default('test', '+2+1+1+0+L112 U112 B013 D111'); }).to.throw(); chai_1.expect(() => { const result = new RubikCipher_1.default('test', '2+1+aabb'); }).to.throw(); }); it('Scramble cube with no parameters, 2^3 times', () => { const result = new RubikCipher_1.default('test'); result.scramble(); chai_1.expect(result.Combination.split(' ').length).to.equal(2 ** 3); }); it('Scramble cube 5 times', () => { const result = new RubikCipher_1.default('test'); result.scramble(5); chai_1.expect(result.Combination.split(' ').length).to.equal(5); }); it('Scramle cube 5 times and add 5 scrambles more', () => { const result = new RubikCipher_1.default('test'); result.scramble(5); result.addScramble(5); chai_1.expect(result.Combination.split(' ').length).to.equal(10); }); it('Scramble cube 5 times and add 2^3 scrambles', () => { const result = new RubikCipher_1.default('test'); result.scramble(5); result.addScramble(); chai_1.expect(result.Combination.split(' ').length).to.equal(5 + (2 ** 3)); }); it('Turn cube by string combination', () => { const result = new RubikCipher_1.default('test'); result.turn('L112 U112 B013 D111'); chai_1.expect(result.Combination).to.equal('L112 U112 B013 D111'); }); it('Turn cube by object array combination', () => { const result = new RubikCipher_1.default('test'); result.turn([{ depth: 1, wide: false, target: 'U', rotation: 1 }]); chai_1.expect(result.Combination).to.equal('U011'); }); it('Turn cube with specific inverse, size 4', () => { const result = new RubikCipher_1.default('Testing is cool.', { cubeSize: 4 }); const baseIndexes = result.Indexes; result.turn('B111 L012 B113 D111 B013'); chai_1.expect(result.Combination).to.equal('B111 L012 B113 D111 B013'); result.turn('B011 D113 B111 L013 L013 B113'); chai_1.expect(result.Indexes).to.be.eql(baseIndexes); }); it('Turn cube with inverse function, size 5, random shuffle', () => { const result = new RubikCipher_1.default('Testing is cool.', { cubeSize: 5 }); const baseIndexes = result.Indexes; chai_1.expect(result.InverseCombination).to.be.eql([]); result.scramble(); chai_1.expect(result.Indexes).to.not.be.eql(baseIndexes); result.turn(result.InverseCombination); chai_1.expect(result.Indexes).to.be.eql(baseIndexes); }); it('Text from default cube (x filled)', () => { const result = new RubikCipher_1.default('test'); chai_1.expect(result.Text).to.equal('testxxxxxxxxxxxxxxxxxxxx'); }); it('Text from cube with random fill', () => { const word = 'test'; const result = new RubikCipher_1.default(word, { fillWithRandom: true }); const text = result.Text; chai_1.expect(text.substr(0, word.length)).to.equal(word); }); it('Text from cube with no fill', () => { const result = new RubikCipher_1.default('test', { fillRest: false }); const text = result.Text; chai_1.expect(text).to.equal('test'); }); // Todo: Add changeSettings to RubikCipher() it('Text from cube with groups by 2', () => { let cube = new RubikCipher_1.default('testandtest', { groupSize: 2 }); cube.scramble(5); chai_1.expect(cube.Text.length).to.equal((6 * (2 ** 2) * 2)); cube = new RubikCipher_1.default('testtest', { groupSize: 2 }); cube.scramble(5); chai_1.expect(cube.Text.length).to.equal((6 * (2 ** 2) * 2)); cube = new RubikCipher_1.default('testandtest', { groupSize: 2, fillWithRandom: true }); cube.scramble(5); chai_1.expect(cube.Text.length).to.equal((6 * (2 ** 2) * 2)); }); it('Text from cube with groups by 2, but with no fill', () => { let cube = new RubikCipher_1.default('testtest', { groupSize: 2, fillRest: false }); cube.scramble(5); chai_1.expect(cube.Text.length).to.equal(8); cube = new RubikCipher_1.default('testteste', { groupSize: 2, fillRest: false }); cube.scramble(5); chai_1.expect(cube.Text.length).to.equal(9); }); it('Text from cube, that has been scrambled and unscrabled', () => { const result = new RubikCipher_1.default('test'); chai_1.expect(result.Text).to.equal('testxxxxxxxxxxxxxxxxxxxx'); result.turn('B111 L012 B113 D111 B013'); chai_1.expect(result.Text).to.equal('xsxtxtxxxxxexxxxxxxxxxxx'); result.turn('B011 D113 B111 L013 L013 B113'); chai_1.expect(result.Text).to.equal('testxxxxxxxxxxxxxxxxxxxx'); }); it('Text from cube with large cube config', () => { chai_1.expect(() => { const cube = new RubikCipher_1.default('test object test', '5+1+1+1+B012 L022 D113 L013 F112'); }).to.not.throw(); }); // Todo: Add changeSettings to RubikCipher() it('Key from cube with specified config object', () => { let cube = new RubikCipher_1.default('test', { cubeSize: 2, groupSize: 1, fillRest: true, fillWithRandom: true, }); cube.turn('L112 U112 B013 D111'); chai_1.expect(cube.Key).to.be.equal('2+1+1+1+L112 U112 B013 D111'); cube = new RubikCipher_1.default('test', { cubeSize: 2, groupSize: 1, fillRest: false, fillWithRandom: false, }); cube.turn('L112 U112 B013 D111'); chai_1.expect(cube.Key).to.be.equal('2+1+0+0+L112 U112 B013 D111'); }); it('Decode dynamic cube from key, from size 2 to 10', () => { const testCount = 10; for (let i = 2; i < testCount; i++) { const cCube = new RubikCipher_1.default('HelloWorld!', { cubeSize: i }); const originalText = cCube.Text; cCube.scramble(); const ciphreKey = cCube.Key; const ciphreText = cCube.Text; const result = new RubikCipher_1.default(ciphreText, ciphreKey); chai_1.expect(result.Text).to.be.equal(originalText); } }); it('Decode dynamic cube from key but standart constructor, from size 2 to 10', () => { const testCount = 10; for (let i = 2; i < testCount; i++) { const cCube = new RubikCipher_1.default('HelloWorld!', { cubeSize: i }); const originalText = cCube.Text; cCube.scramble(); const cCombination = cCube.Combination; const cKey = cCube.Key; const ciphreText = cCube.Text; const config = rubikHelpers_1.getDataFromKey(ciphreText, cKey); chai_1.expect(cKey.split('+')[4]).to.be.equal(cCombination); const testCiphreCube = new RubikCipher_1.default(ciphreText, config); chai_1.expect(testCiphreCube.Text).to.be.equal(ciphreText); const inverse = rubikHelpers_1.getInverseCombination(cKey.split('+')[4].split(' ').map(rubikHelpers_1.decodeScramble)); chai_1.expect(inverse.map(rubikHelpers_1.encodeScramble).join(' ')).to.be.equal(cCube.InverseCombination.map(rubikHelpers_1.encodeScramble).join(' ')); chai_1.expect(inverse).to.be.eql(cCube.InverseCombination); testCiphreCube.turn(inverse); chai_1.expect(testCiphreCube.Text).to.be.equal(originalText); } }); }); //# sourceMappingURL=RubikCipher.test.js.map