diff --git a/dsh-plugin-desktop/src/log-level.ts b/dsh-plugin-desktop/src/log-level.ts new file mode 100644 index 0000000000..d3a99dbd8d --- /dev/null +++ b/dsh-plugin-desktop/src/log-level.ts @@ -0,0 +1,17 @@ +/** One Cordis logger severity, mirroring `@deepseek-ai/cordis`'s `LoggerType`. */ +export type LogType = 'error' | 'info' | 'warn' | 'debug' + +/** User-selectable log verbosity threshold. */ +export type LogLevel = 'debug' | 'info' | 'warn' | 'error' + +const VERBOSITY: Record = { error: 0, warn: 1, info: 2, debug: 3 } + +/** Whether a message of `type` should be emitted at `threshold`. */ +export function shouldEmit(type: LogType, threshold: LogLevel): boolean { + return VERBOSITY[type] <= VERBOSITY[threshold] +} + +/** Whether a message of `type` belongs in the per-day error log. */ +export function isErrorType(type: LogType): boolean { + return type === 'warn' || type === 'error' +} diff --git a/dsh-plugin-desktop/tests/log-level.spec.ts b/dsh-plugin-desktop/tests/log-level.spec.ts new file mode 100644 index 0000000000..5b8d4b34d3 --- /dev/null +++ b/dsh-plugin-desktop/tests/log-level.spec.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { isErrorType, shouldEmit } from '../src/log-level.ts' + +describe('shouldEmit', () => { + it('emits nothing above the threshold', () => { + expect(shouldEmit('debug', 'info')).toBe(false) + expect(shouldEmit('info', 'info')).toBe(true) + expect(shouldEmit('warn', 'info')).toBe(true) + expect(shouldEmit('error', 'info')).toBe(true) + }) + + it('emits only errors at the error threshold', () => { + expect(shouldEmit('error', 'error')).toBe(true) + expect(shouldEmit('warn', 'error')).toBe(false) + expect(shouldEmit('info', 'error')).toBe(false) + expect(shouldEmit('debug', 'error')).toBe(false) + }) + + it('emits everything at the debug threshold', () => { + expect(shouldEmit('debug', 'debug')).toBe(true) + expect(shouldEmit('info', 'debug')).toBe(true) + expect(shouldEmit('warn', 'debug')).toBe(true) + expect(shouldEmit('error', 'debug')).toBe(true) + }) +}) + +describe('isErrorType', () => { + it('treats warn and error as the error log', () => { + expect(isErrorType('warn')).toBe(true) + expect(isErrorType('error')).toBe(true) + expect(isErrorType('info')).toBe(false) + expect(isErrorType('debug')).toBe(false) + }) +})