diff --git a/src/__tests__/channel.spec.ts b/src/__tests__/channel.spec.ts index 564c2a0..7bcab66 100644 --- a/src/__tests__/channel.spec.ts +++ b/src/__tests__/channel.spec.ts @@ -100,7 +100,6 @@ describe('haveSameStructure', () => { }, } expect(haveSameStructure(apiHandlers, channelMap)).toBeTruthy() - console.log() }) it('fail', () => { diff --git a/src/__tests__/logger.spec.ts b/src/__tests__/logger.spec.ts new file mode 100644 index 0000000..c24c959 --- /dev/null +++ b/src/__tests__/logger.spec.ts @@ -0,0 +1,75 @@ +import { + AbstractLogger, + DefaultLogger, + LOG_LEVEL, + mainLogger, + preloadLogger, + initialiseMain, + initialisePreload, +} from '../utils/logger' + +import type { LogLevel } from '../utils/logger' + +describe.each([ + ['main', initialiseMain], + ['preload', initialisePreload], +])('Logger (%s)', (procName: string, initialise) => { + const getLogger = (procName) => { + switch (procName) { + case 'main': + return mainLogger + + case 'preload': + return preloadLogger + } + } + describe('DefaultLogger', () => { + const errorSpy = vi.spyOn(console, 'error') + const debugSpy = vi.spyOn(console, 'debug') + const logSpy = vi.spyOn(console, 'log') + + beforeEach(() => { + errorSpy.mockReset() + debugSpy.mockReset() + logSpy.mockReset() + }) + + it('disable', () => { + initialise({ logger: {} }) + getLogger(procName).info('TEST') + expect(logSpy).not.toHaveBeenCalled() + expect(debugSpy).not.toHaveBeenCalled() + }) + + it.each([ + ['info', logSpy], + ['warn', logSpy], + ['error', errorSpy], + ['verbose', debugSpy], + ['debug', debugSpy], + ['silly', debugSpy], + ])('Assert output the log at each log lebel (%s)', (logLevel, spy) => { + initialise({ logger: { [procName]: new DefaultLogger() } }) + getLogger(procName)[logLevel]('TEST') + expect(spy).toHaveBeenCalled() + }) + }) + + describe('Assert output the log at each log level', () => { + const logMock = vi.fn() + class Logger extends AbstractLogger { + protected writeLog(level: LogLevel, message: string): void { + logMock(level, message) + } + } + beforeEach(() => { + logMock.mockReset() + initialise({ logger: { [procName]: new Logger() } }) + }) + + it.each([...Object.keys(LOG_LEVEL)])('logger test (%s)', (logLevel) => { + getLogger(procName)[logLevel]('TEST') + expect(logMock).toHaveBeenCalled() + }) + }) +}) diff --git a/src/main.ts b/src/main.ts index 5db3018..1b07690 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import { ipcMain } from 'electron' import { API_CHANNEL_MAP, getApiChannelMap, MODE } from './channel' -import { AbstractLogger, initialise, mainLogger } from './utils/logger' +import { AbstractLogger, initialiseMain as initialise, mainLogger } from './utils/logger' import type { BrowserWindow } from 'electron' import type { diff --git a/src/preload.ts b/src/preload.ts index 357d14d..d39a7f5 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -1,7 +1,7 @@ import { ipcRenderer } from 'electron' import { API_CHANNEL_MAP, MODE } from './channel' -import { AbstractLogger, initialise, preloadLogger } from './utils/logger' +import { AbstractLogger, initialisePreload as initialise, preloadLogger } from './utils/logger' import type { IpcMainInvokeEvent, IpcRendererEvent } from 'electron' import type { ApiHandler, IpcBridgeApiImplementation, ApiMode } from './channel' diff --git a/src/types/index.d.ts b/src/types/index.d.ts index d05f6a4..a81da79 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,7 +1,7 @@ import type { IpcContextBridgeApi } from '../preload' import type { IpcBridgeApiEmitterTypeGenerator } from '../main' import type { IpcBridgeApiImplementation } from '../channel' -import type { Option, LogLevel, Logger } from '../utils/logger' +import type { MainOption, PreloadOption, LogLevel, Logger } from '../utils/logger' /** * Type generator for api that will be exposed to renderer process @@ -34,8 +34,9 @@ export function getApiInvoker< T extends IpcContextBridgeApi, >(): Promise -export function initialise(option: Option): void -export type { Option, LogLevel } from '../utils/logger' +export type { MainOption, PreloadOption, LogLevel } from '../utils/logger' +export function initialise(option: MainOption): void +export function initialise(option: PreloadOption): void export abstract class AbstractLogger implements Logger { info(message: string): void diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 29f1964..550ecf4 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -82,20 +82,28 @@ class DefaultLogger extends AbstractLogger { let mainLogger: Logger = new DefaultLogger() let preloadLogger: Logger = new DefaultLogger() -type Option = { +type MainOption = { logger?: { main?: Logger + } +} +type PreloadOption = { + logger?: { preload?: Logger } } -const initialise = (option: Option = {}) => { +const initialiseMain = (option: MainOption = {}) => { if (option.logger) { if (option.logger.main) { mainLogger = option.logger.main } else { mainLogger = new DefaultLogger({ isEnabled: false }) } + } +} +const initialisePreload = (option: PreloadOption = {}) => { + if (option.logger) { if (option.logger.preload) { preloadLogger = option.logger.preload } else { @@ -103,14 +111,15 @@ const initialise = (option: Option = {}) => { } } } - export { Logger, AbstractLogger, DefaultLogger, - Option, + MainOption, + PreloadOption, LOG_LEVEL, - initialise, + initialiseMain, + initialisePreload, mainLogger, preloadLogger, }