refactor web socket layer

This commit is contained in:
seavor 2026-04-14 14:39:46 -05:00
parent 19f5eefdd2
commit 141f0e59f5
124 changed files with 927 additions and 853 deletions

View file

@ -1,11 +1,5 @@
// Shared mock setup for session command tests
vi.mock('../../services/BackendService', () => ({
BackendService: {
sendSessionCommand: vi.fn(),
},
}));
vi.mock('../../persistence', async () => {
const { makeSessionPersistenceMock } = await import('../../__mocks__/sessionCommandMocks');
return {
@ -33,7 +27,6 @@ vi.mock('./', async () => {
import { Mock } from 'vitest';
import { makeCallbackHelpers } from '../../__mocks__/callbackHelpers';
import { BackendService } from '../../services/BackendService';
import { SessionPersistence } from '../../persistence';
import { RoomPersistence } from '../../persistence';
import webClient from '../../WebClient';
@ -95,7 +88,7 @@ import { replayGetCode } from './replayGetCode';
import { replaySubmitCode } from './replaySubmitCode';
const { invokeOnSuccess, invokeCallback } = makeCallbackHelpers(
BackendService.sendSessionCommand as Mock,
webClient.protobuf.sendSessionCommand as Mock,
2
);
@ -113,7 +106,7 @@ describe('accountEdit', () => {
it('sends Command_AccountEdit with correct params', () => {
accountEdit('pw', 'Alice', 'a@b.com', 'US');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_AccountEdit_ext,
expect.objectContaining({ passwordCheck: 'pw', realName: 'Alice', email: 'a@b.com', country: 'US' }),
expect.any(Object)
@ -133,7 +126,7 @@ describe('accountImage', () => {
it('sends Command_AccountImage', () => {
const img = new Uint8Array([1, 2]);
accountImage(img);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_AccountImage_ext, expect.objectContaining({ image: img }), expect.any(Object)
);
});
@ -151,7 +144,7 @@ describe('accountPassword', () => {
it('sends Command_AccountPassword', () => {
accountPassword('old', 'new', 'hashed');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_AccountPassword_ext,
expect.objectContaining({ oldPassword: 'old', newPassword: 'new', hashedNewPassword: 'hashed' }),
expect.any(Object)
@ -170,7 +163,7 @@ describe('deckDel', () => {
it('sends Command_DeckDel', () => {
deckDel(42);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_DeckDel_ext,
expect.objectContaining({ deckId: 42 }),
expect.any(Object)
@ -189,7 +182,7 @@ describe('deckDelDir', () => {
it('sends Command_DeckDelDir', () => {
deckDelDir('/path');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_DeckDelDir_ext, expect.objectContaining({ path: '/path' }), expect.any(Object)
);
});
@ -206,7 +199,7 @@ describe('deckList', () => {
it('sends Command_DeckList', () => {
deckList();
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_DeckList_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_DeckList_ext })
@ -226,7 +219,7 @@ describe('deckNewDir', () => {
it('sends Command_DeckNewDir', () => {
deckNewDir('/path', 'dir');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_DeckNewDir_ext, expect.objectContaining({ path: '/path', dirName: 'dir' }), expect.any(Object)
);
});
@ -243,7 +236,7 @@ describe('deckUpload', () => {
it('sends Command_DeckUpload', () => {
deckUpload('/path', 1, 'content');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_DeckUpload_ext,
expect.objectContaining({ path: '/path', deckId: 1, deckList: 'content' }),
expect.objectContaining({ responseExt: Response_DeckUpload_ext })
@ -272,7 +265,7 @@ describe('getGamesOfUser', () => {
it('sends Command_GetGamesOfUser', () => {
getGamesOfUser('alice');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_GetGamesOfUser_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_GetGamesOfUser_ext })
@ -292,7 +285,7 @@ describe('getUserInfo', () => {
it('sends Command_GetUserInfo', () => {
getUserInfo('alice');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_GetUserInfo_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_GetUserInfo_ext })
@ -312,7 +305,7 @@ describe('joinRoom', () => {
it('sends Command_JoinRoom', () => {
joinRoom(5);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_JoinRoom_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_JoinRoom_ext })
@ -332,7 +325,7 @@ describe('listRooms (command)', () => {
it('sends Command_ListRooms', () => {
listRooms();
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(Command_ListRooms_ext, expect.any(Object));
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(Command_ListRooms_ext, expect.any(Object));
});
});
@ -341,7 +334,7 @@ describe('listUsers', () => {
it('sends Command_ListUsers', () => {
listUsers();
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ListUsers_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_ListUsers_ext })
@ -361,7 +354,7 @@ describe('message', () => {
it('sends Command_Message', () => {
message('bob', 'hi');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_Message_ext, expect.objectContaining({ userName: 'bob', message: 'hi' })
);
});
@ -374,7 +367,7 @@ describe('ping', () => {
it('sends Command_Ping', () => {
const pingReceived = vi.fn();
ping(pingReceived);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(Command_Ping_ext, expect.any(Object), expect.any(Object));
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(Command_Ping_ext, expect.any(Object), expect.any(Object));
});
it('calls pingReceived via onResponse', () => {
@ -390,7 +383,7 @@ describe('replayDeleteMatch', () => {
it('sends Command_ReplayDeleteMatch', () => {
replayDeleteMatch(7);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ReplayDeleteMatch_ext,
expect.objectContaining({ gameId: 7 }),
expect.any(Object)
@ -409,7 +402,7 @@ describe('replayList', () => {
it('sends Command_ReplayList', () => {
replayList();
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ReplayList_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_ReplayList_ext })
@ -429,7 +422,7 @@ describe('replayModifyMatch', () => {
it('sends Command_ReplayModifyMatch', () => {
replayModifyMatch(7, true);
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ReplayModifyMatch_ext, expect.objectContaining({ gameId: 7, doNotHide: true }), expect.any(Object)
);
});
@ -446,7 +439,7 @@ describe('addToList / addToBuddyList / addToIgnoreList', () => {
it('addToBuddyList sends Command_AddToList with list=buddy', () => {
addToBuddyList('alice');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_AddToList_ext,
expect.objectContaining({ list: 'buddy' }),
expect.any(Object)
@ -455,7 +448,7 @@ describe('addToList / addToBuddyList / addToIgnoreList', () => {
it('addToIgnoreList sends Command_AddToList with list=ignore', () => {
addToIgnoreList('bob');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_AddToList_ext,
expect.objectContaining({ list: 'ignore' }),
expect.any(Object)
@ -474,7 +467,7 @@ describe('removeFromList / removeFromBuddyList / removeFromIgnoreList', () => {
it('removeFromBuddyList sends Command_RemoveFromList with list=buddy', () => {
removeFromBuddyList('alice');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_RemoveFromList_ext,
expect.objectContaining({ list: 'buddy' }),
expect.any(Object)
@ -483,7 +476,7 @@ describe('removeFromList / removeFromBuddyList / removeFromIgnoreList', () => {
it('removeFromIgnoreList sends Command_RemoveFromList with list=ignore', () => {
removeFromIgnoreList('bob');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_RemoveFromList_ext,
expect.objectContaining({ list: 'ignore' }),
expect.any(Object)
@ -502,7 +495,7 @@ describe('replayGetCode', () => {
it('sends Command_ReplayGetCode with gameId and responseExt', () => {
replayGetCode(42, vi.fn());
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ReplayGetCode_ext,
expect.any(Object),
expect.objectContaining({ responseExt: Response_ReplayGetCode_ext })
@ -522,7 +515,7 @@ describe('replaySubmitCode', () => {
it('sends Command_ReplaySubmitCode with replayCode', () => {
replaySubmitCode('42-abc123');
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith(
Command_ReplaySubmitCode_ext, expect.objectContaining({ replayCode: '42-abc123' }), expect.any(Object)
);
});