summaryrefslogtreecommitdiff
path: root/web/test/state.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/test/state.test.ts')
-rw-r--r--web/test/state.test.ts97
1 files changed, 87 insertions, 10 deletions
diff --git a/web/test/state.test.ts b/web/test/state.test.ts
index c422810..d1f9b99 100644
--- a/web/test/state.test.ts
+++ b/web/test/state.test.ts
@@ -8,9 +8,10 @@ import {
} from 'vitest';
import { get } from 'svelte/store';
import { GameSession, ownedIds } from '$lib/state/session';
-import type { GameState } from '$lib/protocol/types';
+import type { GameState, UpdateMessage } from '$lib/protocol/types';
+import { serverSettings } from '$lib/settings';
import { SocketSequence } from './fake-socket';
-import { emptyGameState } from './fixtures';
+import { emptyGameState, emptySettings } from './fixtures';
function makeState(overrides: Partial<GameState> = {}): GameState {
return emptyGameState(overrides);
@@ -30,9 +31,31 @@ function welcome(
role,
index,
description: 'last action',
+ historyLength: index + 1,
+ redoDescription: null,
+ undoDescription: 'last action',
event: null,
state,
- seats
+ seats,
+ settings: emptySettings()
+ };
+}
+
+function update(
+ state: GameState,
+ index = 8,
+ overrides: Partial<UpdateMessage> = {}
+): UpdateMessage {
+ return {
+ t: 'update',
+ index,
+ description: 'an action',
+ historyLength: index + 1,
+ redoDescription: null,
+ undoDescription: 'an action',
+ event: { type: 'none' },
+ state,
+ ...overrides
};
}
@@ -42,6 +65,7 @@ describe('GameSession', () => {
beforeEach(() => {
vi.useFakeTimers();
+ serverSettings.set(null);
sockets = new SocketSequence();
session = new GameSession({
transport: sockets.factory,
@@ -81,17 +105,32 @@ describe('GameSession', () => {
expect(get(session.status)).toBe('open');
});
+ it('applies a welcome settings payload and history metadata', () => {
+ welcomeWith(makeState({ round: 3 }), 'sess1', 'player', { B: 'sess1' });
+ expect(get(serverSettings)?.locale).toBe('en');
+ expect(get(serverSettings)?.saves).toEqual([]);
+ expect(get(session.historyLength)).toBe(8);
+ expect(get(session.undoDescription)).toBe('last action');
+ expect(get(session.redoDescription)).toBeNull();
+ });
+
it('replaces state and index on an update', () => {
welcomeWith(makeState({ round: 4 }));
- sockets.current.serverSend({
- t: 'update',
- index: 8,
- description: 'x',
- event: { type: 'none' },
- state: makeState({ round: 5 })
- });
+ sockets.current.serverSend(update(makeState({ round: 5 }), 8));
expect(get(session.state)?.round).toBe(5);
expect(get(session.index)).toBe(8);
+ expect(get(session.historyLength)).toBe(9);
+ expect(get(session.undoDescription)).toBe('an action');
+ });
+
+ it('applies a settings broadcast', () => {
+ welcomeWith(makeState());
+ sockets.current.serverSend({
+ t: 'settings',
+ settings: emptySettings({ locale: 'de', saves: ['alpha'] })
+ });
+ expect(get(serverSettings)?.locale).toBe('de');
+ expect(get(serverSettings)?.saves).toEqual(['alpha']);
});
it('owns() is true only for the session\'s characters', () => {
@@ -148,6 +187,44 @@ describe('GameSession', () => {
characterId: 'Berserker'
});
});
+
+ it('sends the Phase 6 settings/save messages', () => {
+ welcomeWith(makeState());
+ session.setSetting('noInit', false);
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'setSetting',
+ name: 'noInit',
+ value: false
+ });
+ session.setSetting('style', 0);
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'setSetting',
+ name: 'style',
+ value: 0
+ });
+ session.save('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'save', name: 'alpha' });
+ session.loadSave('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'loadSave', name: 'alpha' });
+ session.deleteSave('alpha');
+ expect(sockets.current.lastJson()).toEqual({ t: 'deleteSave', name: 'alpha' });
+ session.saveCharacter('Blinky 2', 'Blinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'saveCharacter',
+ name: 'Blinky 2',
+ characterId: 'Blinkblade'
+ });
+ session.loadCharacterSave('Blinky 2\nBlinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'loadCharacterSave',
+ saveId: 'Blinky 2\nBlinkblade'
+ });
+ session.deleteCharacterSave('Blinky 2\nBlinkblade');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'deleteCharacterSave',
+ saveId: 'Blinky 2\nBlinkblade'
+ });
+ });
});
describe('ownedIds', () => {