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.ts183
1 files changed, 183 insertions, 0 deletions
diff --git a/web/test/state.test.ts b/web/test/state.test.ts
new file mode 100644
index 0000000..3dada4f
--- /dev/null
+++ b/web/test/state.test.ts
@@ -0,0 +1,183 @@
+import {
+ describe,
+ it,
+ expect,
+ beforeEach,
+ afterEach,
+ vi
+} from 'vitest';
+import { get } from 'svelte/store';
+import { GameSession, ownedIds } from '$lib/state/session';
+import type { GameState } from '$lib/protocol/types';
+import { SocketSequence } from './fake-socket';
+
+function makeState(overrides: Partial<GameState> = {}): GameState {
+ return {
+ level: 1,
+ solo: false,
+ autoScenarioLevel: false,
+ difficulty: 1,
+ roundState: 0,
+ round: 1,
+ totalRounds: 1,
+ scenario: 'custom',
+ toastMessage: '',
+ scenarioSpecialRules: [],
+ scenarioSectionsAdded: [],
+ currentCampaign: 'Jaws of the Lion',
+ currentList: [],
+ currentAbilityDecks: [],
+ sanctuaryDeck: {},
+ modifierDeck: {},
+ modifierDeckAllies: {},
+ lootDeck: {},
+ unlockedClasses: [],
+ showAllyDeck: false,
+ allyDeckInOGGloom: true,
+ elementState: {},
+ ...overrides
+ };
+}
+
+function welcome(
+ state: GameState,
+ session = 'sess1',
+ role: 'player' | 'spectator' = 'spectator',
+ seats: Record<string, string | null> = {},
+ index = 7
+) {
+ return {
+ t: 'welcome',
+ v: 1,
+ session,
+ role,
+ index,
+ description: 'last action',
+ event: null,
+ state,
+ seats
+ };
+}
+
+describe('GameSession', () => {
+ let session: GameSession;
+ let sockets: SocketSequence;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ sockets = new SocketSequence();
+ session = new GameSession({
+ transport: sockets.factory,
+ url: 'ws://test/ws',
+ reconnectBaseMs: 100,
+ pingIntervalMs: 60_000
+ });
+ session.connect();
+ sockets.current.open();
+ });
+
+ afterEach(() => vi.useRealTimers());
+
+ function welcomeWith(
+ state: GameState,
+ sessionId = 'sess1',
+ role: 'player' | 'spectator' = 'player',
+ seats: Record<string, string | null> = {}
+ ) {
+ sockets.current.serverSend(
+ welcome(state, sessionId, role, seats)
+ );
+ }
+
+ it('starts empty and connecting', () => {
+ expect(get(session.state)).toBeNull();
+ expect(get(session.session)).toBeNull();
+ expect(get(session.index)).toBe(-1);
+ });
+
+ it('applies a welcome to every store', () => {
+ welcomeWith(makeState({ round: 3 }), 'sess1', 'player', { B: 'sess1' });
+ expect(get(session.session)).toEqual({ id: 'sess1', role: 'player' });
+ expect(get(session.state)?.round).toBe(3);
+ expect(get(session.seats)).toEqual({ B: 'sess1' });
+ expect(get(session.index)).toBe(7);
+ expect(get(session.status)).toBe('open');
+ });
+
+ 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 })
+ });
+ expect(get(session.state)?.round).toBe(5);
+ expect(get(session.index)).toBe(8);
+ });
+
+ it('owns() is true only for the session\'s characters', () => {
+ welcomeWith(makeState(), 'sess1', 'player', {
+ A: 'sess1',
+ B: 'sess2',
+ C: null
+ });
+ expect(session.owns('A')).toBe(true);
+ expect(session.owns('B')).toBe(false);
+ expect(session.owns('C')).toBe(false);
+ });
+
+ it('reports spectator when no seat is claimed', () => {
+ welcomeWith(makeState(), 'sess1', 'spectator', { A: 'sess2' });
+ expect(session.isSpectator()).toBe(true);
+ expect(session.ownedCharacterIds()).toEqual([]);
+ });
+
+ it('lists owned character ids', () => {
+ welcomeWith(makeState(), 'sess1', 'player', {
+ A: 'sess1',
+ B: 'sess1',
+ C: 'sess2'
+ });
+ expect(session.ownedCharacterIds().sort()).toEqual(['A', 'B']);
+ });
+
+ it('surfaces protocol errors and clears them', () => {
+ sockets.current.serverSend({
+ t: 'error',
+ code: 'forbidden',
+ message: 'claim a character first'
+ });
+ expect(get(session.lastError)).toEqual({
+ code: 'forbidden',
+ message: 'claim a character first'
+ });
+ session.clearError();
+ expect(get(session.lastError)).toBeNull();
+ });
+
+ it('sends commands and claims through the socket', () => {
+ welcomeWith(makeState());
+ session.command('draw');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'cmd',
+ name: 'draw',
+ args: {}
+ });
+ session.claim('Berserker');
+ expect(sockets.current.lastJson()).toEqual({
+ t: 'claim',
+ characterId: 'Berserker'
+ });
+ });
+});
+
+describe('ownedIds', () => {
+ it('returns ids owned by the session', () => {
+ expect(ownedIds('s', { a: 's', b: 't', c: null })).toEqual(['a']);
+ });
+ it('returns empty for a null session', () => {
+ expect(ownedIds(null, { a: 's' })).toEqual([]);
+ });
+});