Skip to content

Commit 9613ba1

Browse files
committed
feat: ipc to list accounts
1 parent 6ff3a6c commit 9613ba1

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { AccountServiceMockImpl } from '../../../account/__mocks__/account-service.mock.js';
3+
import type { ListAccountsItemType } from '../../../account/types.js';
4+
import { listAccountsHandler } from '../list-accounts.js';
5+
6+
describe('list-accounts', () => {
7+
beforeEach(() => {
8+
vi.useFakeTimers({ shouldAdvanceTime: true });
9+
});
10+
11+
afterEach(() => {
12+
vi.clearAllMocks();
13+
vi.clearAllTimers();
14+
vi.useRealTimers();
15+
});
16+
17+
describe('#listAccountsHandler', async () => {
18+
it('lists accounts', async () => {
19+
const mockAccountService = new AccountServiceMockImpl();
20+
21+
const mockAccount: ListAccountsItemType = {
22+
accountName: 'test-account-name',
23+
};
24+
25+
mockAccountService.listAccounts.mockResolvedValueOnce([mockAccount]);
26+
27+
const handler = listAccountsHandler({
28+
accountService: mockAccountService,
29+
});
30+
31+
const accounts = await handler([]);
32+
33+
expect(accounts).toEqual([mockAccount]);
34+
});
35+
});
36+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { AccountService } from '../../account/types.js';
2+
import { logger } from '../logger.js';
3+
import type { IpcInvokeHandler, IpcSgeAccount } from '../types.js';
4+
5+
export const listAccountsHandler = (options: {
6+
accountService: AccountService;
7+
}): IpcInvokeHandler<'listAccounts'> => {
8+
const { accountService } = options;
9+
10+
return async (_args): Promise<Array<IpcSgeAccount>> => {
11+
logger.debug('listAccountsHandler');
12+
13+
return accountService.listAccounts();
14+
};
15+
};

electron/main/ipc/ipc.controller.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AccountServiceImpl } from '../account/account.service.js';
44
import type { AccountService } from '../account/types.js';
55
import { Game } from '../game/game.instance.js';
66
import { Store } from '../store/store.instance.js';
7+
import { listAccountsHandler } from './handlers/list-accounts.js';
78
import { listCharactersHandler } from './handlers/list-characters.js';
89
import { pingHandler } from './handlers/ping.js';
910
import { playCharacterHandler } from './handlers/play-character.js';
@@ -83,6 +84,10 @@ export class IpcController {
8384
accountService: this.accountService,
8485
}),
8586

87+
listAccounts: listAccountsHandler({
88+
accountService: this.accountService,
89+
}),
90+
8691
saveCharacter: saveCharacterHandler({
8792
accountService: this.accountService,
8893
}),

electron/main/ipc/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export type IpcHandlerRegistry = {
2424
[channel in IpcInvokableEvent]: IpcInvokeHandler<channel>;
2525
};
2626

27+
export type IpcSgeAccount = {
28+
accountName: string;
29+
};
30+
2731
export type IpcSgeCharacter = {
2832
gameCode: string;
2933
accountName: string;

electron/preload/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ declare const appAPI: {
1414
* Remove credentials for a play.net account.
1515
*/
1616
removeAccount: (options: { accountName: string }) => Promise<void>;
17+
/**
18+
* List added accounts.
19+
*/
20+
listAccounts: () => Promise<
21+
{
22+
accountName: string;
23+
}[]
24+
>;
1725
/**
1826
* Add or update a character for a given play.net account and game instance.
1927
*/

electron/preload/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ const appAPI = {
2626
removeAccount: async (options: { accountName: string }): Promise<void> => {
2727
return ipcRenderer.invoke('removeAccount', options);
2828
},
29+
/**
30+
* List added accounts.
31+
*/
32+
listAccounts: async (): Promise<
33+
Array<{
34+
accountName: string;
35+
}>
36+
> => {
37+
return ipcRenderer.invoke('listAccounts');
38+
},
2939
/**
3040
* Add or update a character for a given play.net account and game instance.
3141
*/

0 commit comments

Comments
 (0)