mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 22:05:50 +00:00
refactor: 统一 log.ts/debug.ts 的测试 mock 为共享定义
- 新增 tests/mocks/log.ts 和 tests/mocks/debug.ts,覆盖源文件全部实际导出 - 移除旧 mock 中不存在的导出(logToFile、logEvent、getLogFilePath) - 13 个测试文件改为使用共享 mock,避免定义分散和不一致 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { mock, describe, test, expect, beforeEach } from 'bun:test'
|
||||
import { debugMock } from '../../../../tests/mocks/debug'
|
||||
|
||||
// Mock @langfuse/otel before any imports
|
||||
const mockForceFlush = mock(() => Promise.resolve())
|
||||
@@ -71,9 +72,7 @@ mock.module('@langfuse/tracing', () => ({
|
||||
}))
|
||||
|
||||
// Mock debug logger
|
||||
mock.module('src/utils/debug.ts', () => ({
|
||||
logForDebugging: mock(() => {}),
|
||||
}))
|
||||
mock.module('src/utils/debug.ts', debugMock)
|
||||
|
||||
// Mock user data — resolveLangfuseUserId uses getCoreUserData().email and .deviceId
|
||||
mock.module('src/utils/user.js', () => ({
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { mock, describe, expect, test, afterEach } from "bun:test";
|
||||
import { debugMock } from "../../../../tests/mocks/debug";
|
||||
|
||||
mock.module("axios", () => ({
|
||||
default: { get: async () => ({ data: { servers: [] } }) },
|
||||
}));
|
||||
mock.module("src/utils/debug.ts", () => ({
|
||||
logForDebugging: () => {},
|
||||
}));
|
||||
mock.module("src/utils/debug.ts", debugMock);
|
||||
|
||||
const { isOfficialMcpUrl, resetOfficialMcpUrlsForTesting } = await import(
|
||||
"../officialRegistry"
|
||||
|
||||
@@ -1,20 +1,8 @@
|
||||
import { mock, describe, expect, test } from "bun:test";
|
||||
import { debugMock } from "../../../tests/mocks/debug";
|
||||
|
||||
// Mock debug.ts to cut bootstrap/state dependency chain
|
||||
mock.module("src/utils/debug.ts", () => ({
|
||||
logForDebugging: () => {},
|
||||
isDebugMode: () => false,
|
||||
isDebugToStdErr: () => false,
|
||||
getDebugFilePath: () => null,
|
||||
getDebugFilter: () => null,
|
||||
getMinDebugLogLevel: () => "debug",
|
||||
getDebugLogPath: () => "/tmp/mock-debug.log",
|
||||
flushDebugLogs: async () => {},
|
||||
enableDebugLogging: () => false,
|
||||
setHasFormattedOutput: () => {},
|
||||
getHasFormattedOutput: () => false,
|
||||
logAntError: () => {},
|
||||
}));
|
||||
mock.module("src/utils/debug.ts", debugMock);
|
||||
|
||||
const { validateBoundedIntEnvVar } = await import("../envValidation");
|
||||
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import { mock, describe, expect, test } from "bun:test";
|
||||
import { logMock } from "../../../tests/mocks/log";
|
||||
|
||||
// Mock log.ts to cut the heavy dependency chain (log.ts → bootstrap/state.ts → analytics)
|
||||
mock.module("src/utils/log.ts", () => ({
|
||||
logError: () => {},
|
||||
logToFile: () => {},
|
||||
getLogDisplayTitle: () => "",
|
||||
logEvent: () => {},
|
||||
}));
|
||||
mock.module("src/utils/log.ts", logMock);
|
||||
|
||||
const { safeParseJSON, safeParseJSONC, parseJSONL, addItemToJSONCArray } =
|
||||
await import("../json");
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import { mock, describe, expect, test, beforeEach } from "bun:test";
|
||||
import { logMock } from "../../../tests/mocks/log";
|
||||
|
||||
// Mock log.ts to cut the bootstrap/state dependency chain
|
||||
mock.module("src/utils/log.ts", () => ({
|
||||
logError: () => {},
|
||||
logToFile: () => {},
|
||||
getLogDisplayTitle: () => "",
|
||||
logEvent: () => {},
|
||||
}));
|
||||
mock.module("src/utils/log.ts", logMock);
|
||||
|
||||
const { memoizeWithTTL, memoizeWithTTLAsync, memoizeWithLRU } = await import(
|
||||
"../memoize"
|
||||
|
||||
@@ -1,22 +1,8 @@
|
||||
import { mock, describe, expect, test } from "bun:test";
|
||||
import { logMock } from "../../../tests/mocks/log";
|
||||
|
||||
// Mock heavy dependency chain: tokenEstimation.ts → log.ts → bootstrap/state.ts
|
||||
mock.module("src/utils/log.ts", () => ({
|
||||
logError: () => {},
|
||||
logToFile: () => {},
|
||||
getLogDisplayTitle: () => "",
|
||||
logEvent: () => {},
|
||||
logMCPError: () => {},
|
||||
logMCPDebug: () => {},
|
||||
dateToFilename: (d: Date) => d.toISOString().replace(/[:.]/g, "-"),
|
||||
getLogFilePath: () => "/tmp/mock-log",
|
||||
attachErrorLogSink: () => {},
|
||||
getInMemoryErrors: () => [],
|
||||
loadErrorLogs: async () => [],
|
||||
getErrorLogByIndex: async () => null,
|
||||
captureAPIRequest: () => {},
|
||||
_resetErrorLogForTesting: () => {},
|
||||
}));
|
||||
mock.module("src/utils/log.ts", logMock);
|
||||
|
||||
// Mock tokenEstimation to avoid pulling in API provider deps
|
||||
mock.module("src/services/tokenEstimation.ts", () => ({
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { mock, describe, expect, test, beforeEach, afterEach } from "bun:test";
|
||||
import { logMock } from "../../../../tests/mocks/log";
|
||||
|
||||
mock.module("src/utils/log.ts", () => ({
|
||||
logError: () => {},
|
||||
logToFile: () => {},
|
||||
getLogDisplayTitle: () => "",
|
||||
logEvent: () => {},
|
||||
}));
|
||||
mock.module("src/utils/log.ts", logMock);
|
||||
|
||||
const {
|
||||
isExternalPermissionMode,
|
||||
|
||||
@@ -1,24 +1,10 @@
|
||||
import { mock, describe, expect, test } from 'bun:test'
|
||||
import { logMock } from '../../../../tests/mocks/log'
|
||||
import { createFileStateCacheWithSizeLimit } from '../../../utils/fileStateCache.js'
|
||||
import { createSubagentContext } from '../../../utils/forkedAgent.js'
|
||||
import { getEmptyToolPermissionContext } from '../../../Tool.js'
|
||||
|
||||
mock.module('src/utils/log.ts', () => ({
|
||||
logError: () => {},
|
||||
logToFile: () => {},
|
||||
getLogDisplayTitle: () => '',
|
||||
logEvent: () => {},
|
||||
logMCPError: () => {},
|
||||
logMCPDebug: () => {},
|
||||
dateToFilename: (d: Date) => d.toISOString().replace(/[:.]/g, '-'),
|
||||
getLogFilePath: () => '/tmp/mock-log',
|
||||
attachErrorLogSink: () => {},
|
||||
getInMemoryErrors: () => [],
|
||||
loadErrorLogs: async () => [],
|
||||
getErrorLogByIndex: async () => null,
|
||||
captureAPIRequest: () => {},
|
||||
_resetErrorLogForTesting: () => {},
|
||||
}))
|
||||
mock.module('src/utils/log.ts', logMock)
|
||||
|
||||
const {
|
||||
getDenyRuleForTool,
|
||||
|
||||
@@ -1,20 +1,8 @@
|
||||
import { mock, describe, expect, test, afterEach } from "bun:test";
|
||||
import { debugMock } from "../../../../tests/mocks/debug";
|
||||
|
||||
// Mock debug.ts to cut the bootstrap/state dependency chain
|
||||
mock.module("src/utils/debug.ts", () => ({
|
||||
logForDebugging: () => {},
|
||||
isDebugMode: () => false,
|
||||
isDebugToStdErr: () => false,
|
||||
getDebugFilePath: () => null,
|
||||
getDebugFilter: () => null,
|
||||
getMinDebugLogLevel: () => "debug",
|
||||
getDebugLogPath: () => "/tmp/mock-debug.log",
|
||||
flushDebugLogs: async () => {},
|
||||
enableDebugLogging: () => false,
|
||||
setHasFormattedOutput: () => {},
|
||||
getHasFormattedOutput: () => false,
|
||||
logAntError: () => {},
|
||||
}));
|
||||
mock.module("src/utils/debug.ts", debugMock);
|
||||
|
||||
const {
|
||||
getMaxOutputLength,
|
||||
|
||||
Reference in New Issue
Block a user