Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,6 @@ describe('move dispatchers', () => {
expect(store.getState().G).toMatchObject({ moved: '0' });
});

test('with undefined playerID - multiplayer mode', () => {
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(
game.moveNames,
store,
undefined,
null,
true
);
api.B();
expect(store.getState().G).toMatchObject({ moved: undefined });
});
Comment thread
delucis marked this conversation as resolved.

test('with null playerID - singleplayer mode', () => {
const store = createStore(reducer, initialState);
const api = createMoveDispatchers(game.moveNames, store, null);
Expand Down
21 changes: 20 additions & 1 deletion src/core/game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('basic', () => {
C: {
move: () => 'C',
},
INVALID: () => {
class Foo {
a: number;
constructor(a: number) {
this.a = a;
}
}
return { a: new Foo(1) };
},
},

phases: {
Expand All @@ -40,7 +49,7 @@ describe('basic', () => {
});

test('sanity', () => {
expect(game.moveNames).toEqual(['A', 'B', 'C']);
expect(game.moveNames).toEqual(['A', 'B', 'C', 'INVALID']);
expect(typeof game.processMove).toEqual('function');
});

Expand All @@ -57,6 +66,16 @@ describe('basic', () => {
expect(game.processMove(state, { type: 'A' })).toEqual('PA.A');
});

test('invalid non-serializable state throws', () => {
const G = { test: true };
const ctx = { phase: '' };
const state = { G, ctx, plugins: {} };

expect(() => {
game.processMove(state, { type: 'INVALID' });
}).toThrow();
});

test('long-form move syntax', () => {
expect(
game.processMove({ ctx: { phase: '' }, plugins: {} }, { type: 'C' })
Expand Down
17 changes: 16 additions & 1 deletion src/core/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ function IsProcessed(game: Game | ProcessedGame): game is ProcessedGame {
return game.processMove !== undefined;
}

function checkSerializable(G: unknown) {
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'test') {
// Only check on test environment.
return G;
}
const assert = require('assert');
assert.deepStrictEqual(
G,
JSON.parse(JSON.stringify(G)),
'Resulting state from move is not serialiazble.'
);
return G;
}

/**
* Helper to generate the game move reducer. The returned
* reducer has the following signature:
Expand Down Expand Up @@ -95,7 +110,7 @@ export function ProcessGameConfig(game: Game | ProcessedGame): ProcessedGame {
if (action.args !== undefined) {
args = args.concat(action.args);
}
return fn(state.G, ctxWithAPI, ...args);
return checkSerializable(fn(state.G, ctxWithAPI, ...args));
}

logging.error(`invalid move object: ${action.type}`);
Expand Down