mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
migrate to Protobuf ES
This commit is contained in:
parent
68e22d22bf
commit
fd55f4fb7f
133 changed files with 1745 additions and 1621 deletions
|
|
@ -19,11 +19,6 @@ vi.mock('../../WebClient', async () => {
|
|||
return { __esModule: true, default: makeWebClientMock() };
|
||||
});
|
||||
|
||||
vi.mock('../../services/ProtoController', async () => {
|
||||
const { makeProtoControllerRootMock } = await import('../../__mocks__/sessionCommandMocks');
|
||||
return { ProtoController: { root: makeProtoControllerRootMock() } };
|
||||
});
|
||||
|
||||
vi.mock('../../utils', async () => {
|
||||
const { makeUtilsMock } = await import('../../__mocks__/sessionCommandMocks');
|
||||
return makeUtilsMock();
|
||||
|
|
@ -43,6 +38,38 @@ import { RoomPersistence } from '../../persistence';
|
|||
import webClient from '../../WebClient';
|
||||
import * as SessionCommands from './';
|
||||
import { hashPassword, generateSalt, passwordSaltSupported } from '../../utils';
|
||||
import {
|
||||
Command_AccountEdit_ext,
|
||||
Command_AccountImage_ext,
|
||||
Command_AccountPassword_ext,
|
||||
Command_AddToList_ext,
|
||||
Command_GetGamesOfUser_ext,
|
||||
Command_GetUserInfo_ext,
|
||||
Command_JoinRoom_ext,
|
||||
Command_ListRooms_ext,
|
||||
Command_ListUsers_ext,
|
||||
Command_Message_ext,
|
||||
Command_Ping_ext,
|
||||
Command_RemoveFromList_ext,
|
||||
} from 'generated/proto/session_commands_pb';
|
||||
import { Command_DeckDel_ext } from 'generated/proto/command_deck_del_pb';
|
||||
import { Command_DeckDelDir_ext } from 'generated/proto/command_deck_del_dir_pb';
|
||||
import { Command_DeckList_ext } from 'generated/proto/command_deck_list_pb';
|
||||
import { Command_DeckNewDir_ext } from 'generated/proto/command_deck_new_dir_pb';
|
||||
import { Command_DeckUpload_ext } from 'generated/proto/command_deck_upload_pb';
|
||||
import { Command_ReplayDeleteMatch_ext } from 'generated/proto/command_replay_delete_match_pb';
|
||||
import { Command_ReplayGetCode_ext } from 'generated/proto/command_replay_get_code_pb';
|
||||
import { Command_ReplayList_ext } from 'generated/proto/command_replay_list_pb';
|
||||
import { Command_ReplayModifyMatch_ext } from 'generated/proto/command_replay_modify_match_pb';
|
||||
import { Command_ReplaySubmitCode_ext } from 'generated/proto/command_replay_submit_code_pb';
|
||||
import { Response_DeckList_ext } from 'generated/proto/response_deck_list_pb';
|
||||
import { Response_DeckUpload_ext } from 'generated/proto/response_deck_upload_pb';
|
||||
import { Response_GetGamesOfUser_ext } from 'generated/proto/response_get_games_of_user_pb';
|
||||
import { Response_GetUserInfo_ext } from 'generated/proto/response_get_user_info_pb';
|
||||
import { Response_JoinRoom_ext } from 'generated/proto/response_join_room_pb';
|
||||
import { Response_ListUsers_ext } from 'generated/proto/response_list_users_pb';
|
||||
import { Response_ReplayGetCode_ext } from 'generated/proto/response_replay_get_code_pb';
|
||||
import { Response_ReplayList_ext } from 'generated/proto/response_replay_list_pb';
|
||||
import { accountEdit } from './accountEdit';
|
||||
import { accountImage } from './accountImage';
|
||||
import { accountPassword } from './accountPassword';
|
||||
|
|
@ -68,7 +95,8 @@ import { replayGetCode } from './replayGetCode';
|
|||
import { replaySubmitCode } from './replaySubmitCode';
|
||||
|
||||
const { invokeOnSuccess, invokeCallback } = makeCallbackHelpers(
|
||||
BackendService.sendSessionCommand as vi.Mock
|
||||
BackendService.sendSessionCommand as vi.Mock,
|
||||
2
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
|
|
@ -86,8 +114,8 @@ describe('accountEdit', () => {
|
|||
it('sends Command_AccountEdit with correct params', () => {
|
||||
accountEdit('pw', 'Alice', 'a@b.com', 'US');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_AccountEdit',
|
||||
{ passwordCheck: 'pw', realName: 'Alice', email: 'a@b.com', country: 'US' },
|
||||
Command_AccountEdit_ext,
|
||||
expect.objectContaining({ passwordCheck: 'pw', realName: 'Alice', email: 'a@b.com', country: 'US' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
|
@ -105,7 +133,9 @@ describe('accountImage', () => {
|
|||
it('sends Command_AccountImage', () => {
|
||||
const img = new Uint8Array([1, 2]);
|
||||
accountImage(img);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_AccountImage', { image: img }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_AccountImage_ext, expect.objectContaining({ image: img }), expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('calls SessionPersistence.accountImageChanged on success', () => {
|
||||
|
|
@ -122,8 +152,8 @@ describe('accountPassword', () => {
|
|||
it('sends Command_AccountPassword', () => {
|
||||
accountPassword('old', 'new', 'hashed');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_AccountPassword',
|
||||
{ oldPassword: 'old', newPassword: 'new', hashedNewPassword: 'hashed' },
|
||||
Command_AccountPassword_ext,
|
||||
expect.objectContaining({ oldPassword: 'old', newPassword: 'new', hashedNewPassword: 'hashed' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
|
@ -140,7 +170,11 @@ describe('deckDel', () => {
|
|||
|
||||
it('sends Command_DeckDel', () => {
|
||||
deckDel(42);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_DeckDel', { deckId: 42 }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_DeckDel_ext,
|
||||
expect.objectContaining({ deckId: 42 }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('calls deleteServerDeck on success', () => {
|
||||
|
|
@ -155,7 +189,9 @@ describe('deckDelDir', () => {
|
|||
|
||||
it('sends Command_DeckDelDir', () => {
|
||||
deckDelDir('/path');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_DeckDelDir', { path: '/path' }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_DeckDelDir_ext, expect.objectContaining({ path: '/path' }), expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('calls deleteServerDeckDir on success', () => {
|
||||
|
|
@ -170,13 +206,17 @@ describe('deckList', () => {
|
|||
|
||||
it('sends Command_DeckList', () => {
|
||||
deckList();
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_DeckList', {}, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_DeckList_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_DeckList_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls updateServerDecks on success', () => {
|
||||
deckList();
|
||||
const resp = { folders: [] };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_DeckList.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.updateServerDecks).toHaveBeenCalledWith(resp);
|
||||
});
|
||||
});
|
||||
|
|
@ -187,7 +227,7 @@ describe('deckNewDir', () => {
|
|||
it('sends Command_DeckNewDir', () => {
|
||||
deckNewDir('/path', 'dir');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_DeckNewDir', { path: '/path', dirName: 'dir' }, expect.any(Object)
|
||||
Command_DeckNewDir_ext, expect.objectContaining({ path: '/path', dirName: 'dir' }), expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -204,16 +244,16 @@ describe('deckUpload', () => {
|
|||
it('sends Command_DeckUpload', () => {
|
||||
deckUpload('/path', 1, 'content');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_DeckUpload',
|
||||
{ path: '/path', deckId: 1, deckList: 'content' },
|
||||
expect.any(Object)
|
||||
Command_DeckUpload_ext,
|
||||
expect.objectContaining({ path: '/path', deckId: 1, deckList: 'content' }),
|
||||
expect.objectContaining({ responseExt: Response_DeckUpload_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls uploadServerDeck on success', () => {
|
||||
deckUpload('/path', 1, 'content');
|
||||
const resp = { newFile: { id: 1 } };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_DeckUpload.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.uploadServerDeck).toHaveBeenCalledWith('/path', resp.newFile);
|
||||
});
|
||||
});
|
||||
|
|
@ -232,13 +272,17 @@ describe('getGamesOfUser', () => {
|
|||
|
||||
it('sends Command_GetGamesOfUser', () => {
|
||||
getGamesOfUser('alice');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_GetGamesOfUser', { userName: 'alice' }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_GetGamesOfUser_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_GetGamesOfUser_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls getGamesOfUser on success', () => {
|
||||
getGamesOfUser('alice');
|
||||
const resp = { gameList: [] };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_GetGamesOfUser.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.getGamesOfUser).toHaveBeenCalledWith('alice', resp);
|
||||
});
|
||||
});
|
||||
|
|
@ -248,13 +292,17 @@ describe('getUserInfo', () => {
|
|||
|
||||
it('sends Command_GetUserInfo', () => {
|
||||
getUserInfo('alice');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_GetUserInfo', { userName: 'alice' }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_GetUserInfo_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_GetUserInfo_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls getUserInfo on success', () => {
|
||||
getUserInfo('alice');
|
||||
const resp = { userInfo: { name: 'alice' } };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_GetUserInfo.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.getUserInfo).toHaveBeenCalledWith(resp.userInfo);
|
||||
});
|
||||
});
|
||||
|
|
@ -264,13 +312,17 @@ describe('joinRoom', () => {
|
|||
|
||||
it('sends Command_JoinRoom', () => {
|
||||
joinRoom(5);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_JoinRoom', { roomId: 5 }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_JoinRoom_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_JoinRoom_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls RoomPersistence.joinRoom on success', () => {
|
||||
joinRoom(5);
|
||||
const resp = { roomInfo: { roomId: 5 } };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_JoinRoom.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(RoomPersistence.joinRoom).toHaveBeenCalledWith(resp.roomInfo);
|
||||
});
|
||||
});
|
||||
|
|
@ -280,7 +332,7 @@ describe('listRooms (command)', () => {
|
|||
|
||||
it('sends Command_ListRooms', () => {
|
||||
listRooms();
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_ListRooms', {}, {});
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(Command_ListRooms_ext, expect.any(Object));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -289,13 +341,17 @@ describe('listUsers', () => {
|
|||
|
||||
it('sends Command_ListUsers', () => {
|
||||
listUsers();
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_ListUsers', {}, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_ListUsers_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_ListUsers_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls SessionPersistence.updateUsers with the user list on success', () => {
|
||||
listUsers();
|
||||
const resp = { userList: [{ name: 'Alice' }] };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_ListUsers.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.updateUsers).toHaveBeenCalledWith([{ name: 'Alice' }]);
|
||||
});
|
||||
});
|
||||
|
|
@ -306,7 +362,7 @@ describe('message', () => {
|
|||
it('sends Command_Message', () => {
|
||||
message('bob', 'hi');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_Message', { userName: 'bob', message: 'hi' }, expect.any(Object)
|
||||
Command_Message_ext, expect.objectContaining({ userName: 'bob', message: 'hi' })
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -318,7 +374,7 @@ describe('ping', () => {
|
|||
it('sends Command_Ping', () => {
|
||||
const pingReceived = vi.fn();
|
||||
ping(pingReceived);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_Ping', {}, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(Command_Ping_ext, expect.any(Object), expect.any(Object));
|
||||
});
|
||||
|
||||
it('calls pingReceived via onResponse', () => {
|
||||
|
|
@ -335,7 +391,11 @@ describe('replayDeleteMatch', () => {
|
|||
|
||||
it('sends Command_ReplayDeleteMatch', () => {
|
||||
replayDeleteMatch(7);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_ReplayDeleteMatch', { gameId: 7 }, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_ReplayDeleteMatch_ext,
|
||||
expect.objectContaining({ gameId: 7 }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('calls replayDeleteMatch on success', () => {
|
||||
|
|
@ -350,13 +410,17 @@ describe('replayList', () => {
|
|||
|
||||
it('sends Command_ReplayList', () => {
|
||||
replayList();
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith('Command_ReplayList', {}, expect.any(Object));
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
Command_ReplayList_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_ReplayList_ext })
|
||||
);
|
||||
});
|
||||
|
||||
it('calls replayList on success', () => {
|
||||
replayList();
|
||||
const resp = { matchList: [] };
|
||||
invokeOnSuccess(resp, { responseCode: 0, '.Response_ReplayList.ext': resp });
|
||||
invokeOnSuccess(resp, { responseCode: 0 });
|
||||
expect(SessionPersistence.replayList).toHaveBeenCalledWith([]);
|
||||
});
|
||||
});
|
||||
|
|
@ -367,7 +431,7 @@ describe('replayModifyMatch', () => {
|
|||
it('sends Command_ReplayModifyMatch', () => {
|
||||
replayModifyMatch(7, true);
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_ReplayModifyMatch', { gameId: 7, doNotHide: true }, expect.any(Object)
|
||||
Command_ReplayModifyMatch_ext, expect.objectContaining({ gameId: 7, doNotHide: true }), expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -384,14 +448,18 @@ describe('addToList / addToBuddyList / addToIgnoreList', () => {
|
|||
it('addToBuddyList sends Command_AddToList with list=buddy', () => {
|
||||
addToBuddyList('alice');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_AddToList', { list: 'buddy', userName: 'alice' }, expect.any(Object)
|
||||
Command_AddToList_ext,
|
||||
expect.objectContaining({ list: 'buddy' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('addToIgnoreList sends Command_AddToList with list=ignore', () => {
|
||||
addToIgnoreList('bob');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_AddToList', { list: 'ignore', userName: 'bob' }, expect.any(Object)
|
||||
Command_AddToList_ext,
|
||||
expect.objectContaining({ list: 'ignore' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -408,14 +476,18 @@ describe('removeFromList / removeFromBuddyList / removeFromIgnoreList', () => {
|
|||
it('removeFromBuddyList sends Command_RemoveFromList with list=buddy', () => {
|
||||
removeFromBuddyList('alice');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_RemoveFromList', { list: 'buddy', userName: 'alice' }, expect.any(Object)
|
||||
Command_RemoveFromList_ext,
|
||||
expect.objectContaining({ list: 'buddy' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('removeFromIgnoreList sends Command_RemoveFromList with list=ignore', () => {
|
||||
removeFromIgnoreList('bob');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_RemoveFromList', { list: 'ignore', userName: 'bob' }, expect.any(Object)
|
||||
Command_RemoveFromList_ext,
|
||||
expect.objectContaining({ list: 'ignore' }),
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -429,12 +501,12 @@ describe('removeFromList / removeFromBuddyList / removeFromIgnoreList', () => {
|
|||
describe('replayGetCode', () => {
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
it('sends Command_ReplayGetCode with gameId and responseName', () => {
|
||||
it('sends Command_ReplayGetCode with gameId and responseExt', () => {
|
||||
replayGetCode(42, vi.fn());
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_ReplayGetCode',
|
||||
{ gameId: 42 },
|
||||
expect.objectContaining({ responseName: 'Response_ReplayGetCode' })
|
||||
Command_ReplayGetCode_ext,
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ responseExt: Response_ReplayGetCode_ext })
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -452,9 +524,7 @@ describe('replaySubmitCode', () => {
|
|||
it('sends Command_ReplaySubmitCode with replayCode', () => {
|
||||
replaySubmitCode('42-abc123');
|
||||
expect(BackendService.sendSessionCommand).toHaveBeenCalledWith(
|
||||
'Command_ReplaySubmitCode',
|
||||
{ replayCode: '42-abc123' },
|
||||
expect.any(Object)
|
||||
Command_ReplaySubmitCode_ext, expect.objectContaining({ replayCode: '42-abc123' }), expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue