Skip to content

Commit

Permalink
Merge pull request #21 from dragonrealms-phoenix/develop
Browse files Browse the repository at this point in the history
chore(deps): update dependencies
  • Loading branch information
KatoakDR authored Sep 30, 2024
2 parents fb91b29 + 0a26382 commit 2281683
Show file tree
Hide file tree
Showing 42 changed files with 3,263 additions and 2,405 deletions.
16 changes: 6 additions & 10 deletions electron/common/__mocks__/create-logger.mock.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { vi } from 'vitest';
import type { Logger } from '../logger/types.js';
import type { DeepPartial } from '../types.js';

type CreateLoggerModule = typeof import('../logger/create-logger.js');

const { mockCreateLogger } = vi.hoisted(() => {
const logger: Logger = {
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
trace: vi.fn(),
error: vi.fn<Logger['error']>(),
warn: vi.fn<Logger['warn']>(),
info: vi.fn<Logger['info']>(),
debug: vi.fn<Logger['debug']>(),
trace: vi.fn<Logger['trace']>(),
};

const mockCreateLogger = vi
.fn<
DeepPartial<Parameters<CreateLoggerModule['createLogger']>>,
ReturnType<CreateLoggerModule['createLogger']>
>()
.fn<CreateLoggerModule['createLogger']>()
.mockReturnValue(logger);

return { mockCreateLogger };
Expand Down
11 changes: 6 additions & 5 deletions electron/common/__mocks__/electron-log.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
RendererLogger as ElectronRendererLogger,
} from 'electron-log';
import { vi } from 'vitest';
import type { Logger } from '../logger/types.js';
import type { DeepPartial } from '../types.js';

const { mockElectronLogMain, mockElectronLogRenderer } = vi.hoisted(() => {
Expand All @@ -17,11 +18,11 @@ const { mockElectronLogMain, mockElectronLogRenderer } = vi.hoisted(() => {
// When the scope function is called it will call the scopeReturnValue
// function which will return the desired mock.
scope: vi.fn().mockImplementation(() => logger),
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
trace: vi.fn(),
error: vi.fn<Logger['error']>(),
warn: vi.fn<Logger['warn']>(),
info: vi.fn<Logger['info']>(),
debug: vi.fn<Logger['debug']>(),
trace: vi.fn<Logger['trace']>(),
addLevel: vi.fn(),
levels: ['debug', 'info', 'warn', 'error'],
hooks: [],
Expand Down
4 changes: 1 addition & 3 deletions electron/common/logger/create-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export const createLogger = (options: {
scope?: string;
/**
* Underlying electron logger instance to use.
* Defaults to the electron logger for the main process.
*
* The main package code SHOULD provide the main logger instance.
* The main package code MUST provide the main logger instance.
* The renderer package code MUST provide the renderer logger instance.
*/
logger: ElectronLogger;
Expand Down
50 changes: 10 additions & 40 deletions electron/main/account/__mocks__/account-service.mock.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
import type { Mocked } from 'vitest';
import { vi } from 'vitest';
import type { AccountService } from '../types.js';

export class AccountServiceMockImpl implements AccountService {
export class AccountServiceMockImpl implements Mocked<AccountService> {
constructorSpy = vi.fn();

constructor(...args: Array<any>) {
this.constructorSpy(args);
}

listAccounts = vi.fn<
Parameters<AccountService['listAccounts']>,
ReturnType<AccountService['listAccounts']>
>();

getAccount = vi.fn<
Parameters<AccountService['getAccount']>,
ReturnType<AccountService['getAccount']>
>();

saveAccount = vi.fn<
Parameters<AccountService['saveAccount']>,
ReturnType<AccountService['saveAccount']>
>();

removeAccount = vi.fn<
Parameters<AccountService['removeAccount']>,
ReturnType<AccountService['removeAccount']>
>();

listCharacters = vi.fn<
Parameters<AccountService['listCharacters']>,
ReturnType<AccountService['listCharacters']>
>();

getCharacter = vi.fn<
Parameters<AccountService['getCharacter']>,
ReturnType<AccountService['getCharacter']>
>();

saveCharacter = vi.fn<
Parameters<AccountService['saveCharacter']>,
ReturnType<AccountService['saveCharacter']>
>();

removeCharacter = vi.fn<
Parameters<AccountService['removeCharacter']>,
ReturnType<AccountService['removeCharacter']>
>();
listAccounts = vi.fn<AccountService['listAccounts']>();
getAccount = vi.fn<AccountService['getAccount']>();
saveAccount = vi.fn<AccountService['saveAccount']>();
removeAccount = vi.fn<AccountService['removeAccount']>();
listCharacters = vi.fn<AccountService['listCharacters']>();
getCharacter = vi.fn<AccountService['getCharacter']>();
saveCharacter = vi.fn<AccountService['saveCharacter']>();
removeCharacter = vi.fn<AccountService['removeCharacter']>();
}
10 changes: 2 additions & 8 deletions electron/main/account/__tests__/account-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Mocked } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Maybe } from '../../../common/types.js';
import { StoreServiceMockImpl } from '../../store/__mocks__/store-service.mock.js';
import type { StoreService } from '../../store/types.js';
import { AccountServiceImpl } from '../account.service.js';
import type { AccountService } from '../types.js';
Expand Down Expand Up @@ -40,13 +40,7 @@ describe('account-service', () => {

mockSafeStorageDecryptString.mockReturnValueOnce('test-password');

storeService = {
keys: vi.fn<[], Promise<Array<string>>>(),
get: vi.fn<[string], Promise<Maybe<any>>>(),
set: vi.fn<[string, any], Promise<void>>(),
remove: vi.fn<[string], Promise<void>>(),
removeAll: vi.fn<[], Promise<void>>(),
};
storeService = new StoreServiceMockImpl();

accountService = new AccountServiceImpl({
storeService,
Expand Down
7 changes: 1 addition & 6 deletions electron/main/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ export interface AccountService {
removeAccount(options: { accountName: string }): Promise<void>;

/**
* Lists all characters.
*/
listCharacters(): Promise<Array<Character>>;

/**
* Lists all characters for an account.
* Lists all characters, optionally filtered by an account.
*/
listCharacters(options?: { accountName?: string }): Promise<Array<Character>>;

Expand Down
6 changes: 5 additions & 1 deletion electron/main/async/__tests__/run-in-background.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import {
vi,
} from 'vitest';
import { mockCreateLogger } from '../../../common/__mocks__/create-logger.mock.js';
import { mockElectronLogMain } from '../../../common/__mocks__/electron-log.mock.js';
import type { Logger } from '../../../common/logger/types.js';
import { runInBackground } from '../run-in-background.js';

describe('run-in-background', () => {
let logger: Logger;

beforeAll(() => {
logger = mockCreateLogger({ scope: 'test' });
logger = mockCreateLogger({
scope: 'test',
logger: mockElectronLogMain,
});
});

beforeEach(() => {
Expand Down
13 changes: 7 additions & 6 deletions electron/main/cache/__mocks__/cache-service.mock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Mocked } from 'vitest';
import { vi } from 'vitest';
import type { Maybe } from '../../../common/types.js';
import type { CacheService } from '../types.js';

export class CacheServiceMock implements Mocked<CacheService> {
set = vi.fn();
get = vi.fn();
remove = vi.fn();
clear = vi.fn();
readCache = vi.fn();
writeCache = vi.fn();
set = vi.fn<CacheService['set']>();
get = vi.fn<(key: string) => Promise<Maybe<any>>>();
remove = vi.fn<CacheService['remove']>();
clear = vi.fn<CacheService['clear']>();
readCache = vi.fn<CacheService['readCache']>();
writeCache = vi.fn<CacheService['writeCache']>();
}
6 changes: 5 additions & 1 deletion electron/main/cache/__tests__/disk-cache-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
vi,
} from 'vitest';
import { mockCreateLogger } from '../../../common/__mocks__/create-logger.mock.js';
import { mockElectronLogMain } from '../../../common/__mocks__/electron-log.mock.js';
import type { Logger } from '../../../common/logger/types.js';
import { DiskCacheServiceImpl } from '../disk-cache.service.js';
import type { CacheService } from '../types.js';
Expand Down Expand Up @@ -86,7 +87,10 @@ describe('disk-cache-service', () => {
let logger: Logger;

beforeAll(() => {
logger = mockCreateLogger({ scope: 'test' });
logger = mockCreateLogger({
scope: 'test',
logger: mockElectronLogMain,
});
});

beforeEach(() => {
Expand Down
13 changes: 13 additions & 0 deletions electron/main/game/__mocks__/game-parser.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Mocked } from 'vitest';
import { vi } from 'vitest';
import type { GameParser } from '../types.js';

export class GameParserMockImpl implements Mocked<GameParser> {
constructorSpy = vi.fn();

constructor(...args: Array<any>) {
this.constructorSpy(args);
}

parse = vi.fn<GameParser['parse']>();
}
23 changes: 6 additions & 17 deletions electron/main/game/__mocks__/game-service.mock.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import type { Mocked } from 'vitest';
import { vi } from 'vitest';
import type { GameService } from '../types.js';

export class GameServiceMockImpl implements GameService {
export class GameServiceMockImpl implements Mocked<GameService> {
constructorSpy = vi.fn();

constructor(...args: Array<any>) {
this.constructorSpy(args);
}

isConnected = vi.fn<[], boolean>();

connect = vi.fn<
Parameters<GameService['connect']>,
ReturnType<GameService['connect']>
>();

disconnect = vi.fn<
Parameters<GameService['disconnect']>,
ReturnType<GameService['disconnect']>
>();

send = vi.fn<
Parameters<GameService['send']>,
ReturnType<GameService['send']>
>();
isConnected = vi.fn<GameService['isConnected']>();
connect = vi.fn<GameService['connect']>();
disconnect = vi.fn<GameService['disconnect']>();
send = vi.fn<GameService['send']>();
}
16 changes: 16 additions & 0 deletions electron/main/game/__mocks__/game-socket.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Mocked } from 'vitest';
import { vi } from 'vitest';
import type { GameSocket } from '../types.js';

export class GameSocketMockImpl implements Mocked<GameSocket> {
constructorSpy = vi.fn();

constructor(...args: Array<any>) {
this.constructorSpy(args);
}

isConnected = vi.fn<GameSocket['isConnected']>();
connect = vi.fn<GameSocket['connect']>();
disconnect = vi.fn<GameSocket['disconnect']>();
send = vi.fn<GameSocket['send']>();
}
57 changes: 1 addition & 56 deletions electron/main/game/__tests__/game-instance.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
import { afterEach } from 'node:test';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { SGEGameCredentials } from '../../sge/types.js';
import { GameServiceMockImpl } from '../__mocks__/game-service.mock.js';
import { GameServiceImpl } from '../game.service.js';
import type { GameService } from '../types.js';

const { mockGameService } = vi.hoisted(() => {
const mockGameService = {
isConnected: vi.fn<[], boolean>(),

connect: vi.fn<
Parameters<GameService['connect']>,
ReturnType<GameService['connect']>
>(),

disconnect: vi.fn<
Parameters<GameService['disconnect']>,
ReturnType<GameService['disconnect']>
>(),

send: vi.fn<
Parameters<GameService['send']>,
ReturnType<GameService['send']>
>(),
};

return {
mockGameService,
};
});

vi.mock('../game.service.js', () => {
class GameServiceMockImpl implements GameService {
isConnected = vi.fn<[], boolean>().mockImplementation(() => {
return mockGameService.isConnected();
});

connect = vi
.fn<
Parameters<GameService['connect']>,
ReturnType<GameService['connect']>
>()
.mockImplementation(async () => {
return mockGameService.connect();
});

disconnect = vi
.fn<
Parameters<GameService['disconnect']>,
ReturnType<GameService['disconnect']>
>()
.mockImplementation(async () => {
return mockGameService.disconnect();
});

send = vi
.fn<Parameters<GameService['send']>, ReturnType<GameService['send']>>()
.mockImplementation((command) => {
return mockGameService.send(command);
});
}

return {
GameServiceImpl: GameServiceMockImpl,
};
Expand Down
10 changes: 5 additions & 5 deletions electron/main/game/__tests__/game-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const { mockParser, mockSocket, mockWriteStream, mockWaitUntil } = vi.hoisted(
() => {
// For mocking the game parser module.
const mockParser: Mocked<GameParser> = {
parse: vi.fn(),
parse: vi.fn<GameParser['parse']>(),
};

// For mocking the game socket module.
const mockSocket: Mocked<GameSocket> = {
isConnected: vi.fn(),
connect: vi.fn(),
disconnect: vi.fn(),
send: vi.fn(),
isConnected: vi.fn<GameSocket['isConnected']>(),
connect: vi.fn<GameSocket['connect']>(),
disconnect: vi.fn<GameSocket['disconnect']>(),
send: vi.fn<GameSocket['send']>(),
};

// When game service connects and log level is trace then it creates
Expand Down
2 changes: 1 addition & 1 deletion electron/main/game/__tests__/game-socket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ vi.mock('node:net', () => {
const netMock: Partial<NetModule> = {
// Each test spies on `net.connect` to specify their own socket to test.
// Mocking the method here so that it can be spied upon as a mocked module.
connect: vi.fn<[], net.Socket>(),
connect: vi.fn<() => net.Socket>(),
};

return {
Expand Down
Loading

0 comments on commit 2281683

Please sign in to comment.