Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for logger and some adjustments for logic #34

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/__tests__/channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe('haveSameStructure', () => {
},
}
expect(haveSameStructure(apiHandlers, channelMap)).toBeTruthy()
console.log()
})

it('fail', () => {
Expand Down
75 changes: 75 additions & 0 deletions src/__tests__/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
7 changes: 4 additions & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,8 +34,9 @@ export function getApiInvoker<
T extends IpcContextBridgeApi<IpcBridgeApiImplementation>,
>(): Promise<T>

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
Expand Down
19 changes: 14 additions & 5 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,44 @@ 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 {
preloadLogger = new DefaultLogger({ isEnabled: false })
}
}
}

export {
Logger,
AbstractLogger,
DefaultLogger,
Option,
MainOption,
PreloadOption,
LOG_LEVEL,
initialise,
initialiseMain,
initialisePreload,
mainLogger,
preloadLogger,
}