From 6d3c413f8e21b4fb06376bb043e89701fd39fb62 Mon Sep 17 00:00:00 2001 From: 91he66 <3350903664@qq.com> Date: Sun, 16 Aug 2026 20:53:48 +0800 Subject: [PATCH] feat(desktop): add log-level verbosity helpers --- dsh-plugin-desktop/src/log-level.ts | 17 +++++++++++ dsh-plugin-desktop/tests/log-level.spec.ts | 34 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 dsh-plugin-desktop/src/log-level.ts create mode 100644 dsh-plugin-desktop/tests/log-level.spec.ts 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) + }) +})