Select Git revision
controller.js
Forked from
SOFT Core / SOFT 260 / Boost Board Game
Source project has a limited visibility.
-
Brady James Garvin authoredBrady James Garvin authored
controller.test.js 13.88 KiB
/* eslint-disable no-magic-numbers */
import { jest, describe, test, expect } from '@jest/globals';
import { callsTo, anyUnexpectedCalls } from '../testing/awaitCalls.js';
import Controller from './controller.js';
function controllerWithMocks(engineURL, engineThreadURL, gameIdentifier) {
globalThis.Worker = jest.fn().mockName('Worker').mockReturnValue({
addEventListener: jest.fn().mockName('addEventListener'),
postMessage: jest.fn().mockName('postMessage'),
});
const result = new Controller(
engineURL,
engineThreadURL,
gameIdentifier,
jest.fn().mockName('propertyHandler'),
jest.fn().mockName('moveHandler'),
);
return result;
}
describe('the controller\'s automatic behaviors', () => {
test('connect as needed when setting strength', async() => {
const controller = controllerWithMocks('abc', 'def', 'ghi');
controller.setStrength(999);
controller.setStrength(9999);
await callsTo(controller.worker.postMessage);
expect(controller.worker.postMessage.mock.calls).toEqual([
['bei def'],
['isready'],
]);
controller.receiveMessage('protocol 1.0.0');
controller.receiveMessage('beiok');
controller.receiveMessage('readyok');
await callsTo(controller.worker.postMessage);
expect(controller.worker.postMessage.mock.calls).toEqual([
['bei def'],
['isready'],
['setoption strength 999'],
['isready'],
]);
controller.receiveMessage('readyok');
await callsTo(controller.worker.postMessage);
expect(controller.worker.postMessage.mock.calls).toEqual([
['bei def'],
['isready'],
['setoption strength 999'],
['isready'],
['setoption strength 9999'],
['isready'],
]);
});
test('connect and start a game as needed when setting a line', async() => {
const controller = controllerWithMocks('abc', 'def', 'ghi');
controller.setLine({ serialization: 'jkl' }, [{ serialization: 'mno' }, { serialization: 'pqr' }]);
controller.setLine({ serialization: 'stu' }, []);
await callsTo(controller.worker.postMessage);
expect(controller.worker.postMessage.mock.calls).toEqual([
['bei def'],
['isready'],
]);
controller.receiveMessage('protocol 1.0.0');
controller.receiveMessage('beiok');
controller.receiveMessage('readyok');
await callsTo(controller.worker.postMessage);
expect(controller.worker.postMessage.mock.calls).toEqual([
['bei def'],
['isready'],