@mnbroatch/boardgame.io 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +102 -0
- package/ai/package.json +7 -0
- package/client/package.json +7 -0
- package/core/package.json +7 -0
- package/debug/package.json +7 -0
- package/dist/boardgameio.es.js +14238 -0
- package/dist/boardgameio.js +14277 -0
- package/dist/boardgameio.min.js +16 -0
- package/dist/cjs/Debug-9d141c06.js +9586 -0
- package/dist/cjs/ai-e0e8a768.js +377 -0
- package/dist/cjs/ai.js +20 -0
- package/dist/cjs/client-76dec77b.js +258 -0
- package/dist/cjs/client-a22d7500.js +524 -0
- package/dist/cjs/client.js +26 -0
- package/dist/cjs/core.js +52 -0
- package/dist/cjs/debug.js +18 -0
- package/dist/cjs/filter-player-view-bb02e2f6.js +89 -0
- package/dist/cjs/initialize-267fcd69.js +61 -0
- package/dist/cjs/internal.js +25 -0
- package/dist/cjs/master-2904879d.js +320 -0
- package/dist/cjs/master.js +18 -0
- package/dist/cjs/multiplayer.js +23 -0
- package/dist/cjs/plugin-random-7425844d.js +229 -0
- package/dist/cjs/plugins.js +59 -0
- package/dist/cjs/react-native.js +182 -0
- package/dist/cjs/react.js +727 -0
- package/dist/cjs/reducer-16eec232.js +1203 -0
- package/dist/cjs/server.js +4087 -0
- package/dist/cjs/socketio-7a0837eb.js +478 -0
- package/dist/cjs/testing.js +30 -0
- package/dist/cjs/transport-b1874dfa.js +37 -0
- package/dist/cjs/turn-order-b2ff8740.js +1136 -0
- package/dist/cjs/util-fcfd8fb8.js +140 -0
- package/dist/esm/Debug-0141fe2d.js +9577 -0
- package/dist/esm/ai-5c06e761.js +371 -0
- package/dist/esm/ai.js +8 -0
- package/dist/esm/client-2e653027.js +522 -0
- package/dist/esm/client-5f57c3f2.js +255 -0
- package/dist/esm/client.js +16 -0
- package/dist/esm/core.js +40 -0
- package/dist/esm/debug.js +10 -0
- package/dist/esm/filter-player-view-2c6cc96f.js +87 -0
- package/dist/esm/initialize-11d626ca.js +59 -0
- package/dist/esm/internal.js +10 -0
- package/dist/esm/master-fa8f2e43.js +318 -0
- package/dist/esm/master.js +10 -0
- package/dist/esm/multiplayer.js +14 -0
- package/dist/esm/plugin-random-087f861e.js +226 -0
- package/dist/esm/plugins.js +55 -0
- package/dist/esm/react-native.js +173 -0
- package/dist/esm/react.js +716 -0
- package/dist/esm/reducer-c46da7e5.js +1198 -0
- package/dist/esm/socketio-c22ffa65.js +455 -0
- package/dist/esm/testing.js +26 -0
- package/dist/esm/transport-ce07b771.js +35 -0
- package/dist/esm/turn-order-376d315e.js +1091 -0
- package/dist/esm/util-b6147cef.js +135 -0
- package/dist/types/packages/ai.d.ts +5 -0
- package/dist/types/packages/client.d.ts +3 -0
- package/dist/types/packages/core.d.ts +5 -0
- package/dist/types/packages/debug.d.ts +2 -0
- package/dist/types/packages/internal.d.ts +8 -0
- package/dist/types/packages/master.d.ts +2 -0
- package/dist/types/packages/multiplayer.d.ts +3 -0
- package/dist/types/packages/plugins.d.ts +3 -0
- package/dist/types/packages/react-native.d.ts +2 -0
- package/dist/types/packages/react.d.ts +3 -0
- package/dist/types/packages/server.d.ts +6 -0
- package/dist/types/packages/testing.d.ts +1 -0
- package/dist/types/src/ai/ai.d.ts +53 -0
- package/dist/types/src/ai/ai.test.d.ts +1 -0
- package/dist/types/src/ai/bot.d.ts +40 -0
- package/dist/types/src/ai/mcts-bot.d.ts +60 -0
- package/dist/types/src/ai/random-bot.d.ts +27 -0
- package/dist/types/src/client/client.d.ts +104 -0
- package/dist/types/src/client/client.test.d.ts +1 -0
- package/dist/types/src/client/debug/tests/debug.test.d.ts +1 -0
- package/dist/types/src/client/manager.d.ts +61 -0
- package/dist/types/src/client/react.d.ts +75 -0
- package/dist/types/src/client/react.ssr.test.d.ts +4 -0
- package/dist/types/src/client/react.test.d.ts +1 -0
- package/dist/types/src/client/transport/dummy.d.ts +18 -0
- package/dist/types/src/client/transport/local.d.ts +59 -0
- package/dist/types/src/client/transport/local.test.d.ts +1 -0
- package/dist/types/src/client/transport/socketio.d.ts +45 -0
- package/dist/types/src/client/transport/socketio.test.d.ts +1 -0
- package/dist/types/src/client/transport/transport.d.ts +50 -0
- package/dist/types/src/client/transport/transport.test.d.ts +1 -0
- package/dist/types/src/core/action-creators.d.ts +144 -0
- package/dist/types/src/core/action-types.d.ts +10 -0
- package/dist/types/src/core/backwards-compatibility.d.ts +12 -0
- package/dist/types/src/core/constants.d.ts +6 -0
- package/dist/types/src/core/errors.d.ts +15 -0
- package/dist/types/src/core/flow.d.ts +28 -0
- package/dist/types/src/core/flow.test.d.ts +1 -0
- package/dist/types/src/core/game-methods.d.ts +9 -0
- package/dist/types/src/core/game.d.ts +26 -0
- package/dist/types/src/core/game.test.d.ts +1 -0
- package/dist/types/src/core/initialize.d.ts +9 -0
- package/dist/types/src/core/logger.d.ts +2 -0
- package/dist/types/src/core/player-view.d.ts +7 -0
- package/dist/types/src/core/player-view.test.d.ts +1 -0
- package/dist/types/src/core/reducer.d.ts +155 -0
- package/dist/types/src/core/reducer.test.d.ts +1 -0
- package/dist/types/src/core/turn-order.d.ts +179 -0
- package/dist/types/src/core/turn-order.test.d.ts +8 -0
- package/dist/types/src/lobby/client.d.ts +194 -0
- package/dist/types/src/lobby/client.test.d.ts +1 -0
- package/dist/types/src/lobby/connection.d.ts +44 -0
- package/dist/types/src/lobby/connection.test.d.ts +1 -0
- package/dist/types/src/lobby/create-match-form.d.ts +26 -0
- package/dist/types/src/lobby/login-form.d.ts +23 -0
- package/dist/types/src/lobby/match-instance.d.ts +31 -0
- package/dist/types/src/lobby/react.d.ts +113 -0
- package/dist/types/src/lobby/react.ssr.test.d.ts +4 -0
- package/dist/types/src/lobby/react.test.d.ts +1 -0
- package/dist/types/src/master/filter-player-view.d.ts +96 -0
- package/dist/types/src/master/filter-player-view.test.d.ts +1 -0
- package/dist/types/src/master/master.d.ts +94 -0
- package/dist/types/src/master/master.test.d.ts +1 -0
- package/dist/types/src/plugins/events/events.d.ts +54 -0
- package/dist/types/src/plugins/events/events.test.d.ts +1 -0
- package/dist/types/src/plugins/main.d.ts +75 -0
- package/dist/types/src/plugins/main.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-events.d.ts +5 -0
- package/dist/types/src/plugins/plugin-immer.d.ts +7 -0
- package/dist/types/src/plugins/plugin-immer.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-log.d.ts +14 -0
- package/dist/types/src/plugins/plugin-log.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-player.d.ts +29 -0
- package/dist/types/src/plugins/plugin-player.test.d.ts +1 -0
- package/dist/types/src/plugins/plugin-random.d.ts +4 -0
- package/dist/types/src/plugins/plugin-serializable.d.ts +7 -0
- package/dist/types/src/plugins/plugin-serializable.test.d.ts +1 -0
- package/dist/types/src/plugins/random/random.alea.d.ts +19 -0
- package/dist/types/src/plugins/random/random.d.ts +54 -0
- package/dist/types/src/plugins/random/random.test.d.ts +1 -0
- package/dist/types/src/server/api.d.ts +13 -0
- package/dist/types/src/server/api.test.d.ts +1 -0
- package/dist/types/src/server/auth.d.ts +38 -0
- package/dist/types/src/server/auth.test.d.ts +1 -0
- package/dist/types/src/server/cors.d.ts +4 -0
- package/dist/types/src/server/cors.test.d.ts +1 -0
- package/dist/types/src/server/db/base.d.ts +192 -0
- package/dist/types/src/server/db/flatfile.d.ts +44 -0
- package/dist/types/src/server/db/flatfile.test.d.ts +1 -0
- package/dist/types/src/server/db/index.d.ts +4 -0
- package/dist/types/src/server/db/index.test.d.ts +1 -0
- package/dist/types/src/server/db/inmemory.d.ts +43 -0
- package/dist/types/src/server/db/inmemory.test.d.ts +1 -0
- package/dist/types/src/server/db/localstorage.d.ts +7 -0
- package/dist/types/src/server/db/localstorage.test.d.ts +1 -0
- package/dist/types/src/server/index.d.ts +68 -0
- package/dist/types/src/server/index.test.d.ts +1 -0
- package/dist/types/src/server/transport/pubsub/generic-pub-sub.d.ts +6 -0
- package/dist/types/src/server/transport/pubsub/in-memory-pub-sub.d.ts +7 -0
- package/dist/types/src/server/transport/pubsub/in-memory-pub-sub.test.d.ts +1 -0
- package/dist/types/src/server/transport/socketio-simultaneous.test.d.ts +1 -0
- package/dist/types/src/server/transport/socketio.d.ts +65 -0
- package/dist/types/src/server/transport/socketio.test.d.ts +1 -0
- package/dist/types/src/server/util.d.ts +35 -0
- package/dist/types/src/testing/mock-random.d.ts +15 -0
- package/dist/types/src/testing/mock-random.test.d.ts +1 -0
- package/dist/types/src/types.d.ts +387 -0
- package/internal/package.json +7 -0
- package/master/package.json +7 -0
- package/multiplayer/package.json +7 -0
- package/package.json +211 -0
- package/plugins/package.json +7 -0
- package/react/package.json +7 -0
- package/react-native/package.json +7 -0
- package/server/package.json +6 -0
- package/src/ai/ai.test.ts +433 -0
- package/src/ai/ai.ts +84 -0
- package/src/ai/bot.ts +122 -0
- package/src/ai/mcts-bot.ts +331 -0
- package/src/ai/random-bot.ts +20 -0
- package/src/client/client.test.ts +993 -0
- package/src/client/client.ts +588 -0
- package/src/client/debug/Debug.svelte +239 -0
- package/src/client/debug/Menu.svelte +65 -0
- package/src/client/debug/ai/AI.svelte +215 -0
- package/src/client/debug/ai/Options.svelte +48 -0
- package/src/client/debug/info/Info.svelte +22 -0
- package/src/client/debug/info/Item.svelte +24 -0
- package/src/client/debug/log/Log.svelte +157 -0
- package/src/client/debug/log/LogEvent.svelte +149 -0
- package/src/client/debug/log/LogMetadata.svelte +7 -0
- package/src/client/debug/log/PhaseMarker.svelte +27 -0
- package/src/client/debug/log/TurnMarker.svelte +23 -0
- package/src/client/debug/main/ClientSwitcher.svelte +59 -0
- package/src/client/debug/main/Controls.svelte +58 -0
- package/src/client/debug/main/Hotkey.svelte +84 -0
- package/src/client/debug/main/InteractiveFunction.svelte +85 -0
- package/src/client/debug/main/Main.svelte +121 -0
- package/src/client/debug/main/Move.svelte +68 -0
- package/src/client/debug/main/PlayerInfo.svelte +70 -0
- package/src/client/debug/mcts/Action.svelte +22 -0
- package/src/client/debug/mcts/MCTS.svelte +78 -0
- package/src/client/debug/mcts/Table.svelte +98 -0
- package/src/client/debug/tests/JSONTree.mock.svelte +3 -0
- package/src/client/debug/tests/debug.test.ts +183 -0
- package/src/client/debug/utils/shortcuts.js +50 -0
- package/src/client/debug/utils/shortcuts.test.js +49 -0
- package/src/client/manager.ts +177 -0
- package/src/client/react-native.js +136 -0
- package/src/client/react-native.test.js +229 -0
- package/src/client/react.ssr.test.tsx +24 -0
- package/src/client/react.test.tsx +213 -0
- package/src/client/react.tsx +192 -0
- package/src/client/transport/dummy.ts +19 -0
- package/src/client/transport/local.test.ts +353 -0
- package/src/client/transport/local.ts +230 -0
- package/src/client/transport/socketio.test.ts +328 -0
- package/src/client/transport/socketio.ts +210 -0
- package/src/client/transport/transport.test.ts +27 -0
- package/src/client/transport/transport.ts +95 -0
- package/src/core/action-creators.ts +159 -0
- package/src/core/action-types.ts +18 -0
- package/src/core/backwards-compatibility.ts +23 -0
- package/src/core/constants.ts +6 -0
- package/src/core/errors.ts +35 -0
- package/src/core/flow.test.ts +2433 -0
- package/src/core/flow.ts +897 -0
- package/src/core/game-methods.ts +9 -0
- package/src/core/game.test.ts +286 -0
- package/src/core/game.ts +114 -0
- package/src/core/initialize.ts +77 -0
- package/src/core/logger.test.js +90 -0
- package/src/core/logger.ts +18 -0
- package/src/core/player-view.test.ts +50 -0
- package/src/core/player-view.ts +39 -0
- package/src/core/reducer.test.ts +991 -0
- package/src/core/reducer.ts +532 -0
- package/src/core/turn-order.test.ts +1123 -0
- package/src/core/turn-order.ts +473 -0
- package/src/lobby/client.test.ts +385 -0
- package/src/lobby/client.ts +358 -0
- package/src/lobby/connection.test.ts +207 -0
- package/src/lobby/connection.ts +162 -0
- package/src/lobby/create-match-form.tsx +122 -0
- package/src/lobby/login-form.tsx +75 -0
- package/src/lobby/match-instance.tsx +135 -0
- package/src/lobby/react.ssr.test.tsx +22 -0
- package/src/lobby/react.test.tsx +594 -0
- package/src/lobby/react.tsx +402 -0
- package/src/master/filter-player-view.test.ts +381 -0
- package/src/master/filter-player-view.ts +102 -0
- package/src/master/master.test.ts +1068 -0
- package/src/master/master.ts +492 -0
- package/src/plugins/events/events.test.ts +108 -0
- package/src/plugins/events/events.ts +209 -0
- package/src/plugins/main.test.ts +411 -0
- package/src/plugins/main.ts +314 -0
- package/src/plugins/plugin-events.ts +40 -0
- package/src/plugins/plugin-immer.test.ts +86 -0
- package/src/plugins/plugin-immer.ts +37 -0
- package/src/plugins/plugin-log.test.ts +37 -0
- package/src/plugins/plugin-log.ts +40 -0
- package/src/plugins/plugin-player.test.ts +172 -0
- package/src/plugins/plugin-player.ts +100 -0
- package/src/plugins/plugin-random.ts +40 -0
- package/src/plugins/plugin-serializable.test.ts +40 -0
- package/src/plugins/plugin-serializable.ts +55 -0
- package/src/plugins/random/random.alea.ts +109 -0
- package/src/plugins/random/random.test.ts +167 -0
- package/src/plugins/random/random.ts +198 -0
- package/src/server/api.test.ts +1699 -0
- package/src/server/api.ts +527 -0
- package/src/server/auth.test.ts +275 -0
- package/src/server/auth.ts +89 -0
- package/src/server/cors.test.ts +121 -0
- package/src/server/cors.ts +7 -0
- package/src/server/db/base.ts +296 -0
- package/src/server/db/flatfile.test.ts +221 -0
- package/src/server/db/flatfile.ts +228 -0
- package/src/server/db/index.test.ts +8 -0
- package/src/server/db/index.ts +12 -0
- package/src/server/db/inmemory.test.ts +143 -0
- package/src/server/db/inmemory.ts +143 -0
- package/src/server/db/localstorage.test.ts +73 -0
- package/src/server/db/localstorage.ts +44 -0
- package/src/server/index.test.ts +265 -0
- package/src/server/index.ts +175 -0
- package/src/server/transport/pubsub/generic-pub-sub.ts +11 -0
- package/src/server/transport/pubsub/in-memory-pub-sub.test.ts +47 -0
- package/src/server/transport/pubsub/in-memory-pub-sub.ts +28 -0
- package/src/server/transport/socketio-simultaneous.test.ts +603 -0
- package/src/server/transport/socketio.test.ts +303 -0
- package/src/server/transport/socketio.ts +279 -0
- package/src/server/util.ts +85 -0
- package/src/testing/mock-random.test.ts +45 -0
- package/src/testing/mock-random.ts +27 -0
- package/src/types.ts +511 -0
- package/testing/package.json +7 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
// Tell ESLint about additional assertion methods.
|
|
2
|
+
/*
|
|
3
|
+
eslint jest/expect-expect: [
|
|
4
|
+
"warn",
|
|
5
|
+
{ "assertFunctionNames": ["expect", "throwsWith*", "testBasicBody"] }
|
|
6
|
+
]
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { LobbyClient } from './client';
|
|
10
|
+
|
|
11
|
+
const throwsWithoutBody = (fn: (...args: any) => Promise<any>) => async () => {
|
|
12
|
+
await expect(fn('tic-tac-toe')).rejects.toThrow(
|
|
13
|
+
`Expected body, got “undefined”.`
|
|
14
|
+
);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const testStringValidation =
|
|
18
|
+
(fn: (arg: any) => Promise<any>, label: string) => async () => {
|
|
19
|
+
await expect(fn(undefined)).rejects.toThrow(
|
|
20
|
+
`Expected ${label} string, got "undefined".`
|
|
21
|
+
);
|
|
22
|
+
await expect(fn(2)).rejects.toThrow(`Expected ${label} string, got "2".`);
|
|
23
|
+
await expect(fn('')).rejects.toThrow(`Expected ${label} string, got "".`);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const throwsWithInvalidGameName = (fn: (...args: any) => Promise<any>) =>
|
|
27
|
+
testStringValidation(fn, 'game name');
|
|
28
|
+
|
|
29
|
+
const throwsWithInvalidMatchID = (fn: (...args: any) => Promise<any>) =>
|
|
30
|
+
testStringValidation((matchID: string) => fn('chess', matchID), 'match ID');
|
|
31
|
+
|
|
32
|
+
const testBasicBody = (fn: (...args: any) => Promise<any>) => async () => {
|
|
33
|
+
await expect(
|
|
34
|
+
fn('chess', '1', { playerID: undefined, credentials: 'pwd' })
|
|
35
|
+
).rejects.toThrow(
|
|
36
|
+
'Expected body.playerID to be of type string, got “undefined”.'
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
await expect(
|
|
40
|
+
fn('chess', '2', { playerID: '0', credentials: 0 as unknown as string })
|
|
41
|
+
).rejects.toThrow('Expected body.credentials to be of type string, got “0”.');
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe('LobbyClient', () => {
|
|
45
|
+
let client = new LobbyClient();
|
|
46
|
+
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
(global as any).fetch = jest.fn(async () => ({
|
|
49
|
+
ok: true,
|
|
50
|
+
status: 200,
|
|
51
|
+
json: async () => {},
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('construction', () => {
|
|
56
|
+
test('basic', async () => {
|
|
57
|
+
await client.listGames();
|
|
58
|
+
expect(fetch).toBeCalledWith(`/games`, undefined);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('with server address', async () => {
|
|
62
|
+
const client = new LobbyClient({ server: 'http://api.io' });
|
|
63
|
+
await client.listGames();
|
|
64
|
+
expect(fetch).toBeCalledWith(`http://api.io/games`, undefined);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('with server address with trailing slash', async () => {
|
|
68
|
+
const client = new LobbyClient({ server: 'http://api.io/' });
|
|
69
|
+
await client.listGames();
|
|
70
|
+
expect(fetch).toBeCalledWith(`http://api.io/games`, undefined);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('status errors', () => {
|
|
75
|
+
beforeEach(async () => {
|
|
76
|
+
client = new LobbyClient();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('404 throws an error', async () => {
|
|
80
|
+
(global as any).fetch = jest.fn(async () => ({
|
|
81
|
+
ok: false,
|
|
82
|
+
status: 404,
|
|
83
|
+
json: async () => {},
|
|
84
|
+
}));
|
|
85
|
+
|
|
86
|
+
await expect(client.listGames()).rejects.toThrow('HTTP status 404');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('404 throws an error with json details', async () => {
|
|
90
|
+
(global as any).fetch = jest.fn(async () => ({
|
|
91
|
+
ok: false,
|
|
92
|
+
status: 404,
|
|
93
|
+
clone: () => ({
|
|
94
|
+
json: async () => ({ moreInformation: 'some helpful details' }),
|
|
95
|
+
}),
|
|
96
|
+
}));
|
|
97
|
+
|
|
98
|
+
await expect(client.listGames()).rejects.toThrow(
|
|
99
|
+
expect.objectContaining({
|
|
100
|
+
message: 'HTTP status 404',
|
|
101
|
+
details: {
|
|
102
|
+
moreInformation: 'some helpful details',
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('404 throws an error with text details', async () => {
|
|
109
|
+
(global as any).fetch = jest.fn(async () => ({
|
|
110
|
+
ok: false,
|
|
111
|
+
status: 404,
|
|
112
|
+
json: async () => {
|
|
113
|
+
throw new Error('impossible to parse json');
|
|
114
|
+
},
|
|
115
|
+
text: async () =>
|
|
116
|
+
'<moreInformation>some helpful details</moreInformation>',
|
|
117
|
+
}));
|
|
118
|
+
|
|
119
|
+
await expect(client.listGames()).rejects.toThrow(
|
|
120
|
+
expect.objectContaining({
|
|
121
|
+
message: 'HTTP status 404',
|
|
122
|
+
details: '<moreInformation>some helpful details</moreInformation>',
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('404 throws an error without details', async () => {
|
|
128
|
+
(global as any).fetch = jest.fn(async () => ({
|
|
129
|
+
ok: false,
|
|
130
|
+
status: 404,
|
|
131
|
+
json: async () => {
|
|
132
|
+
throw new Error('impossible to parse json');
|
|
133
|
+
},
|
|
134
|
+
text: async () => {
|
|
135
|
+
throw new Error('something went wrong in the connection');
|
|
136
|
+
},
|
|
137
|
+
}));
|
|
138
|
+
|
|
139
|
+
await expect(client.listGames()).rejects.toThrow(
|
|
140
|
+
expect.objectContaining({
|
|
141
|
+
message: 'HTTP status 404',
|
|
142
|
+
details: 'something went wrong in the connection',
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('listGames', () => {
|
|
149
|
+
test('calls `/games`', async () => {
|
|
150
|
+
await client.listGames();
|
|
151
|
+
expect(fetch).toBeCalledWith('/games', undefined);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('init can be customized', async () => {
|
|
155
|
+
await client.listGames({ headers: { Authorization: 'pwd' } });
|
|
156
|
+
expect(fetch).toBeCalledWith('/games', {
|
|
157
|
+
headers: { Authorization: 'pwd' },
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('listMatches', () => {
|
|
163
|
+
test('calls `/games/:name`', async () => {
|
|
164
|
+
await client.listMatches('tic-tac-toe');
|
|
165
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe`, undefined);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('validates gameName', throwsWithInvalidGameName(client.listMatches));
|
|
169
|
+
|
|
170
|
+
describe('builds filter queries', () => {
|
|
171
|
+
test('kitchen sink', async () => {
|
|
172
|
+
await client.listMatches('chess', {
|
|
173
|
+
isGameover: false,
|
|
174
|
+
updatedBefore: 3000,
|
|
175
|
+
updatedAfter: 1000,
|
|
176
|
+
});
|
|
177
|
+
expect(fetch).toBeCalledWith(
|
|
178
|
+
'/games/chess?isGameover=false&updatedBefore=3000&updatedAfter=1000',
|
|
179
|
+
undefined
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('isGameover', async () => {
|
|
184
|
+
await client.listMatches('chess', { isGameover: undefined });
|
|
185
|
+
expect(fetch).toBeCalledWith('/games/chess', undefined);
|
|
186
|
+
await client.listMatches('chess', { isGameover: false });
|
|
187
|
+
expect(fetch).toBeCalledWith(
|
|
188
|
+
'/games/chess?isGameover=false',
|
|
189
|
+
undefined
|
|
190
|
+
);
|
|
191
|
+
await client.listMatches('chess', { isGameover: true });
|
|
192
|
+
expect(fetch).toBeCalledWith('/games/chess?isGameover=true', undefined);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('updatedBefore', async () => {
|
|
196
|
+
const updatedBefore = 1989;
|
|
197
|
+
await client.listMatches('chess', { updatedBefore });
|
|
198
|
+
expect(fetch).toBeCalledWith(
|
|
199
|
+
'/games/chess?updatedBefore=1989',
|
|
200
|
+
undefined
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('updatedAfter', async () => {
|
|
205
|
+
const updatedAfter = 1970;
|
|
206
|
+
await client.listMatches('chess', { updatedAfter });
|
|
207
|
+
expect(fetch).toBeCalledWith(
|
|
208
|
+
'/games/chess?updatedAfter=1970',
|
|
209
|
+
undefined
|
|
210
|
+
);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('getMatch', () => {
|
|
216
|
+
test('calls `/games/:name/:id`', async () => {
|
|
217
|
+
await client.getMatch('tic-tac-toe', 'xyz');
|
|
218
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/xyz`, undefined);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('validates gameName', throwsWithInvalidGameName(client.getMatch));
|
|
222
|
+
test('validates matchID', throwsWithInvalidMatchID(client.getMatch));
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('createMatch', () => {
|
|
226
|
+
test('calls `/games/:name/create`', async () => {
|
|
227
|
+
await client.createMatch('tic-tac-toe', { numPlayers: 2 });
|
|
228
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/create`, {
|
|
229
|
+
method: 'post',
|
|
230
|
+
body: '{"numPlayers":2}',
|
|
231
|
+
headers: { 'Content-Type': 'application/json' },
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('validates gameName', throwsWithInvalidGameName(client.createMatch));
|
|
236
|
+
|
|
237
|
+
test('throws without body', throwsWithoutBody(client.createMatch));
|
|
238
|
+
|
|
239
|
+
test('validates body', async () => {
|
|
240
|
+
await expect(
|
|
241
|
+
client.createMatch('tic-tac-toe', {
|
|
242
|
+
numPlayers: '12' as unknown as number,
|
|
243
|
+
})
|
|
244
|
+
).rejects.toThrow(
|
|
245
|
+
'Expected body.numPlayers to be of type number, got “12”.'
|
|
246
|
+
);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('init can be customized', async () => {
|
|
250
|
+
await client.createMatch(
|
|
251
|
+
'chess',
|
|
252
|
+
{ numPlayers: 2 },
|
|
253
|
+
{ headers: { Authorization: 'pwd' } }
|
|
254
|
+
);
|
|
255
|
+
expect(fetch).toBeCalledWith(`/games/chess/create`, {
|
|
256
|
+
method: 'post',
|
|
257
|
+
body: '{"numPlayers":2}',
|
|
258
|
+
headers: {
|
|
259
|
+
'Content-Type': 'application/json',
|
|
260
|
+
Authorization: 'pwd',
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('joinMatch', () => {
|
|
267
|
+
test('calls `/games/:name/:id/join`', async () => {
|
|
268
|
+
await client.joinMatch('tic-tac-toe', 'xyz', {
|
|
269
|
+
playerID: '0',
|
|
270
|
+
playerName: 'Alice',
|
|
271
|
+
});
|
|
272
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/xyz/join`, {
|
|
273
|
+
method: 'post',
|
|
274
|
+
body: '{"playerID":"0","playerName":"Alice"}',
|
|
275
|
+
headers: { 'Content-Type': 'application/json' },
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test('validates gameName', throwsWithInvalidGameName(client.joinMatch));
|
|
280
|
+
test('validates matchID', throwsWithInvalidMatchID(client.joinMatch));
|
|
281
|
+
|
|
282
|
+
test(
|
|
283
|
+
'throws without body',
|
|
284
|
+
throwsWithoutBody(() => client.joinMatch('chess', 'id', undefined))
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
test('validates body', async () => {
|
|
288
|
+
await expect(
|
|
289
|
+
client.joinMatch('tic-tac-toe', 'xyz', {
|
|
290
|
+
playerID: 0 as unknown as string,
|
|
291
|
+
playerName: 'Bob',
|
|
292
|
+
})
|
|
293
|
+
).rejects.toThrow(
|
|
294
|
+
'Expected body.playerID to be of type string|undefined, got “0”.'
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
await expect(
|
|
298
|
+
client.joinMatch('tic-tac-toe', 'xyz', {
|
|
299
|
+
playerID: '0',
|
|
300
|
+
playerName: undefined,
|
|
301
|
+
})
|
|
302
|
+
).rejects.toThrow(
|
|
303
|
+
'Expected body.playerName to be of type string, got “undefined”.'
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
// Allows requests that don’t specify `playerID`.
|
|
307
|
+
await expect(
|
|
308
|
+
client.joinMatch('tic-tac-toe', 'xyz', { playerName: 'Bob' })
|
|
309
|
+
).resolves.not.toThrow();
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
describe('leaveMatch', () => {
|
|
314
|
+
test('calls `/games/:name/:id/leave`', async () => {
|
|
315
|
+
await client.leaveMatch('tic-tac-toe', 'xyz', {
|
|
316
|
+
playerID: '0',
|
|
317
|
+
credentials: 'pwd',
|
|
318
|
+
});
|
|
319
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/xyz/leave`, {
|
|
320
|
+
method: 'post',
|
|
321
|
+
body: '{"playerID":"0","credentials":"pwd"}',
|
|
322
|
+
headers: { 'Content-Type': 'application/json' },
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('validates gameName', throwsWithInvalidGameName(client.leaveMatch));
|
|
327
|
+
test('validates matchID', throwsWithInvalidMatchID(client.leaveMatch));
|
|
328
|
+
|
|
329
|
+
test(
|
|
330
|
+
'throws without body',
|
|
331
|
+
throwsWithoutBody(() => client.leaveMatch('chess', 'id', undefined))
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
test('validates body', testBasicBody(client.leaveMatch));
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
describe('updatePlayer', () => {
|
|
338
|
+
test('calls `/games/:name/:id/update`', async () => {
|
|
339
|
+
await client.updatePlayer('tic-tac-toe', 'xyz', {
|
|
340
|
+
playerID: '0',
|
|
341
|
+
credentials: 'pwd',
|
|
342
|
+
newName: 'Al',
|
|
343
|
+
});
|
|
344
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/xyz/update`, {
|
|
345
|
+
method: 'post',
|
|
346
|
+
body: '{"playerID":"0","credentials":"pwd","newName":"Al"}',
|
|
347
|
+
headers: { 'Content-Type': 'application/json' },
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test('validates gameName', throwsWithInvalidGameName(client.updatePlayer));
|
|
352
|
+
test('validates matchID', throwsWithInvalidMatchID(client.updatePlayer));
|
|
353
|
+
|
|
354
|
+
test(
|
|
355
|
+
'throws without body',
|
|
356
|
+
throwsWithoutBody(() => client.updatePlayer('chess', 'id', undefined))
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
test('validates body', testBasicBody(client.updatePlayer));
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('playAgain', () => {
|
|
363
|
+
test('calls `/games/:name/:id/playAgain`', async () => {
|
|
364
|
+
await client.playAgain('tic-tac-toe', 'xyz', {
|
|
365
|
+
playerID: '0',
|
|
366
|
+
credentials: 'pwd',
|
|
367
|
+
});
|
|
368
|
+
expect(fetch).toBeCalledWith(`/games/tic-tac-toe/xyz/playAgain`, {
|
|
369
|
+
method: 'post',
|
|
370
|
+
body: '{"playerID":"0","credentials":"pwd"}',
|
|
371
|
+
headers: { 'Content-Type': 'application/json' },
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('validates gameName', throwsWithInvalidGameName(client.playAgain));
|
|
376
|
+
test('validates matchID', throwsWithInvalidMatchID(client.playAgain));
|
|
377
|
+
|
|
378
|
+
test(
|
|
379
|
+
'throws without body',
|
|
380
|
+
throwsWithoutBody(() => client.playAgain('chess', 'id', undefined))
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
test('validates body', testBasicBody(client.playAgain));
|
|
384
|
+
});
|
|
385
|
+
});
|