refactor redux data model

This commit is contained in:
seavor 2026-04-15 21:48:03 -05:00
parent ae1bc3da38
commit 0ff391491d
243 changed files with 5212 additions and 5963 deletions

View file

@ -1,20 +1,21 @@
vi.mock('../../WebClient', () => ({
__esModule: true,
default: { protobuf: { sendRoomCommand: vi.fn() } },
}));
vi.mock('../../persistence', () => ({
RoomPersistence: {
gameCreated: vi.fn(),
joinedGame: vi.fn(),
leaveRoom: vi.fn(),
WebClient: {
instance: {
protobuf: { sendRoomCommand: vi.fn() },
response: {
room: {
gameCreated: vi.fn(),
joinedGame: vi.fn(),
leaveRoom: vi.fn(),
},
},
},
},
}));
import { makeCallbackHelpers } from '../../__mocks__/callbackHelpers';
import webClient from '../../WebClient';
import { WebClient } from '../../WebClient';
import { Data } from '@app/types';
import { RoomPersistence } from '../../persistence';
import { createGame } from './createGame';
import { joinGame } from './joinGame';
@ -24,7 +25,7 @@ import { create } from '@bufbuild/protobuf';
import { Mock } from 'vitest';
const { invokeOnSuccess } = makeCallbackHelpers(
webClient.protobuf.sendRoomCommand as Mock,
WebClient.instance.protobuf.sendRoomCommand as Mock,
// sendRoomCommand(roomId, ext, value, options) — options at index 3
3
);
@ -36,15 +37,15 @@ describe('createGame', () => {
it('calls sendRoomCommand with Command_CreateGame', () => {
createGame(5, create(Data.Command_CreateGameSchema, { maxPlayers: 4 }));
expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(
expect(WebClient.instance.protobuf.sendRoomCommand).toHaveBeenCalledWith(
5, Data.Command_CreateGame_ext, expect.objectContaining({ maxPlayers: 4 }), expect.any(Object)
);
});
it('onSuccess calls RoomPersistence.gameCreated with roomId', () => {
it('onSuccess calls response.room.gameCreated with roomId', () => {
createGame(5, create(Data.Command_CreateGameSchema, {}));
invokeOnSuccess();
expect(RoomPersistence.gameCreated).toHaveBeenCalledWith(5);
expect(WebClient.instance.response.room.gameCreated).toHaveBeenCalledWith(5);
});
});
@ -55,15 +56,15 @@ describe('joinGame', () => {
it('calls sendRoomCommand with Command_JoinGame', () => {
joinGame(7, create(Data.Command_JoinGameSchema, { gameId: 42, password: '' }));
expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(
expect(WebClient.instance.protobuf.sendRoomCommand).toHaveBeenCalledWith(
7, Data.Command_JoinGame_ext, expect.objectContaining({ gameId: 42, password: '' }), expect.any(Object)
);
});
it('onSuccess calls RoomPersistence.joinedGame with roomId and gameId', () => {
it('onSuccess calls response.room.joinedGame with roomId and gameId', () => {
joinGame(7, create(Data.Command_JoinGameSchema, { gameId: 42 }));
invokeOnSuccess();
expect(RoomPersistence.joinedGame).toHaveBeenCalledWith(7, 42);
expect(WebClient.instance.response.room.joinedGame).toHaveBeenCalledWith(7, 42);
});
});
@ -74,13 +75,15 @@ describe('leaveRoom', () => {
it('calls sendRoomCommand with Command_LeaveRoom', () => {
leaveRoom(3);
expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(3, Data.Command_LeaveRoom_ext, expect.any(Object), expect.any(Object));
expect(WebClient.instance.protobuf.sendRoomCommand).toHaveBeenCalledWith(
3, Data.Command_LeaveRoom_ext, expect.any(Object), expect.any(Object)
);
});
it('onSuccess calls RoomPersistence.leaveRoom with roomId', () => {
it('onSuccess calls response.room.leaveRoom with roomId', () => {
leaveRoom(3);
invokeOnSuccess();
expect(RoomPersistence.leaveRoom).toHaveBeenCalledWith(3);
expect(WebClient.instance.response.room.leaveRoom).toHaveBeenCalledWith(3);
});
});
@ -91,7 +94,7 @@ describe('roomSay', () => {
it('calls sendRoomCommand with trimmed message', () => {
roomSay(2, ' hello ');
expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(
expect(WebClient.instance.protobuf.sendRoomCommand).toHaveBeenCalledWith(
2,
Data.Command_RoomSay_ext,
expect.objectContaining({ message: 'hello' })
@ -100,11 +103,11 @@ describe('roomSay', () => {
it('does not call sendRoomCommand when message is blank', () => {
roomSay(2, ' ');
expect(webClient.protobuf.sendRoomCommand).not.toHaveBeenCalled();
expect(WebClient.instance.protobuf.sendRoomCommand).not.toHaveBeenCalled();
});
it('does not call sendRoomCommand when message is empty string', () => {
roomSay(2, '');
expect(webClient.protobuf.sendRoomCommand).not.toHaveBeenCalled();
expect(WebClient.instance.protobuf.sendRoomCommand).not.toHaveBeenCalled();
});
});