mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-23 08:45:50 +00:00
feat: 工具层及 mcp 大重构 (#252)
* feat: 第一版大重构 * fix: 修复类型问题 * chore: 更新版本到 1.3.2 * Add brave as alternative WebSearchTool * fix: 修正顺序 * fix: 修复对穷鬼模式的 auto dream 和 session memory 越过 * feat: 穷鬼模式去除 session-summary * feat: 创建 builtin-tools 包,搬运所有工具实现 将 src/tools/ 下的全部 60 个工具目录迁移至 packages/builtin-tools/src/tools/, 内部导入路径已更新为 src/ alias 模式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 更新 src/ 中所有工具引用至 builtin-tools 包,删除 src/tools/ - src/tools.ts 及 178 个 src/ 文件的 import 路径从 ./tools/ 改为 builtin-tools/tools/ - 删除 src/tools/ 整个目录(已迁移至 packages/builtin-tools/) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: 添加 builtin-tools 路径别名至 tsconfig,更新 bun.lock - tsconfig.json 新增 builtin-tools/* 和 builtin-tools 路径映射 - 新增 packages/builtin-tools/src 至 include Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 为 builtin-tools、mcp-client、agent-tools 添加 @claude-code-best 作用域前缀 所有包名及 import 路径统一添加 @claude-code-best/ 前缀: - builtin-tools → @claude-code-best/builtin-tools - mcp-client → @claude-code-best/mcp-client - agent-tools → @claude-code-best/agent-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复 node 环境没有 bun 的问题 --------- Co-authored-by: Eric-Guo <eric.guocz@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
import { mock, describe, expect, test } from "bun:test";
|
||||
|
||||
// Mock log.ts to cut the heavy dependency chain
|
||||
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: () => {},
|
||||
}));
|
||||
|
||||
const {
|
||||
normalizeQuotes,
|
||||
stripTrailingWhitespace,
|
||||
findActualString,
|
||||
preserveQuoteStyle,
|
||||
applyEditToFile,
|
||||
LEFT_SINGLE_CURLY_QUOTE,
|
||||
RIGHT_SINGLE_CURLY_QUOTE,
|
||||
LEFT_DOUBLE_CURLY_QUOTE,
|
||||
RIGHT_DOUBLE_CURLY_QUOTE,
|
||||
} = await import("../utils");
|
||||
|
||||
// ─── normalizeQuotes ────────────────────────────────────────────────────
|
||||
|
||||
describe("normalizeQuotes", () => {
|
||||
test("converts left single curly to straight", () => {
|
||||
expect(normalizeQuotes(`${LEFT_SINGLE_CURLY_QUOTE}hello`)).toBe("'hello");
|
||||
});
|
||||
|
||||
test("converts right single curly to straight", () => {
|
||||
expect(normalizeQuotes(`hello${RIGHT_SINGLE_CURLY_QUOTE}`)).toBe("hello'");
|
||||
});
|
||||
|
||||
test("converts left double curly to straight", () => {
|
||||
expect(normalizeQuotes(`${LEFT_DOUBLE_CURLY_QUOTE}hello`)).toBe('"hello');
|
||||
});
|
||||
|
||||
test("converts right double curly to straight", () => {
|
||||
expect(normalizeQuotes(`hello${RIGHT_DOUBLE_CURLY_QUOTE}`)).toBe('hello"');
|
||||
});
|
||||
|
||||
test("leaves straight quotes unchanged", () => {
|
||||
expect(normalizeQuotes("'hello' \"world\"")).toBe("'hello' \"world\"");
|
||||
});
|
||||
|
||||
test("handles empty string", () => {
|
||||
expect(normalizeQuotes("")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
// ─── stripTrailingWhitespace ────────────────────────────────────────────
|
||||
|
||||
describe("stripTrailingWhitespace", () => {
|
||||
test("strips trailing spaces from lines", () => {
|
||||
expect(stripTrailingWhitespace("hello \nworld ")).toBe("hello\nworld");
|
||||
});
|
||||
|
||||
test("strips trailing tabs", () => {
|
||||
expect(stripTrailingWhitespace("hello\t\nworld\t")).toBe("hello\nworld");
|
||||
});
|
||||
|
||||
test("preserves leading whitespace", () => {
|
||||
expect(stripTrailingWhitespace(" hello \n world ")).toBe(
|
||||
" hello\n world"
|
||||
);
|
||||
});
|
||||
|
||||
test("handles empty string", () => {
|
||||
expect(stripTrailingWhitespace("")).toBe("");
|
||||
});
|
||||
|
||||
test("handles CRLF line endings", () => {
|
||||
expect(stripTrailingWhitespace("hello \r\nworld ")).toBe(
|
||||
"hello\r\nworld"
|
||||
);
|
||||
});
|
||||
|
||||
test("handles no trailing whitespace", () => {
|
||||
expect(stripTrailingWhitespace("hello\nworld")).toBe("hello\nworld");
|
||||
});
|
||||
|
||||
test("handles CR-only line endings", () => {
|
||||
expect(stripTrailingWhitespace("hello \rworld ")).toBe("hello\rworld");
|
||||
});
|
||||
|
||||
test("handles content with no trailing newline", () => {
|
||||
expect(stripTrailingWhitespace("hello ")).toBe("hello");
|
||||
});
|
||||
});
|
||||
|
||||
// ─── findActualString ───────────────────────────────────────────────────
|
||||
|
||||
describe("findActualString", () => {
|
||||
test("finds exact match", () => {
|
||||
expect(findActualString("hello world", "hello")).toBe("hello");
|
||||
});
|
||||
|
||||
test("finds match with curly quotes normalized", () => {
|
||||
const fileContent = `${LEFT_DOUBLE_CURLY_QUOTE}hello${RIGHT_DOUBLE_CURLY_QUOTE}`;
|
||||
const result = findActualString(fileContent, '"hello"');
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
test("returns null when not found", () => {
|
||||
expect(findActualString("hello world", "xyz")).toBeNull();
|
||||
});
|
||||
|
||||
test("returns null for empty search in non-empty content", () => {
|
||||
// Empty string is always found at index 0 via includes()
|
||||
const result = findActualString("hello", "");
|
||||
expect(result).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
// ─── preserveQuoteStyle ─────────────────────────────────────────────────
|
||||
|
||||
describe("preserveQuoteStyle", () => {
|
||||
test("returns newString unchanged when no normalization happened", () => {
|
||||
expect(preserveQuoteStyle("hello", "hello", "world")).toBe("world");
|
||||
});
|
||||
|
||||
test("converts straight double quotes to curly in replacement", () => {
|
||||
const oldString = '"hello"';
|
||||
const actualOldString = `${LEFT_DOUBLE_CURLY_QUOTE}hello${RIGHT_DOUBLE_CURLY_QUOTE}`;
|
||||
const newString = '"world"';
|
||||
const result = preserveQuoteStyle(oldString, actualOldString, newString);
|
||||
expect(result).toContain(LEFT_DOUBLE_CURLY_QUOTE);
|
||||
expect(result).toContain(RIGHT_DOUBLE_CURLY_QUOTE);
|
||||
});
|
||||
|
||||
test("converts straight single quotes to curly in replacement", () => {
|
||||
const oldString = "'hello'";
|
||||
const actualOldString = `${LEFT_SINGLE_CURLY_QUOTE}hello${RIGHT_SINGLE_CURLY_QUOTE}`;
|
||||
const newString = "'world'";
|
||||
const result = preserveQuoteStyle(oldString, actualOldString, newString);
|
||||
expect(result).toContain(LEFT_SINGLE_CURLY_QUOTE);
|
||||
expect(result).toContain(RIGHT_SINGLE_CURLY_QUOTE);
|
||||
});
|
||||
|
||||
test("treats apostrophe in contraction as right curly quote", () => {
|
||||
const oldString = "'it's a test'";
|
||||
const actualOldString = `${LEFT_SINGLE_CURLY_QUOTE}it${RIGHT_SINGLE_CURLY_QUOTE}s a test${RIGHT_SINGLE_CURLY_QUOTE}`;
|
||||
const newString = "'don't worry'";
|
||||
const result = preserveQuoteStyle(oldString, actualOldString, newString);
|
||||
// The leading ' at position 0 should be LEFT_SINGLE_CURLY_QUOTE
|
||||
expect(result[0]).toBe(LEFT_SINGLE_CURLY_QUOTE);
|
||||
// The apostrophe in "don't" (between n and t) should be RIGHT_SINGLE_CURLY_QUOTE
|
||||
expect(result).toContain(RIGHT_SINGLE_CURLY_QUOTE);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── applyEditToFile ────────────────────────────────────────────────────
|
||||
|
||||
describe("applyEditToFile", () => {
|
||||
test("replaces first occurrence by default", () => {
|
||||
expect(applyEditToFile("foo bar foo", "foo", "baz")).toBe("baz bar foo");
|
||||
});
|
||||
|
||||
test("replaces all occurrences with replaceAll=true", () => {
|
||||
expect(applyEditToFile("foo bar foo", "foo", "baz", true)).toBe(
|
||||
"baz bar baz"
|
||||
);
|
||||
});
|
||||
|
||||
test("handles deletion (empty newString) with trailing newline", () => {
|
||||
const result = applyEditToFile("line1\nline2\nline3\n", "line2", "");
|
||||
expect(result).toBe("line1\nline3\n");
|
||||
});
|
||||
|
||||
test("handles deletion without trailing newline", () => {
|
||||
const result = applyEditToFile("foobar", "foo", "");
|
||||
expect(result).toBe("bar");
|
||||
});
|
||||
|
||||
test("handles no match (returns original)", () => {
|
||||
expect(applyEditToFile("hello world", "xyz", "abc")).toBe("hello world");
|
||||
});
|
||||
|
||||
test("handles empty original content with insertion", () => {
|
||||
expect(applyEditToFile("", "", "new content")).toBe("new content");
|
||||
});
|
||||
|
||||
test("handles multiline oldString and newString", () => {
|
||||
const content = "line1\nline2\nline3\n";
|
||||
const result = applyEditToFile(content, "line2\nline3", "replaced");
|
||||
expect(result).toBe("line1\nreplaced\n");
|
||||
});
|
||||
|
||||
test("handles multiline replacement across multiple lines", () => {
|
||||
const content = "header\nold line A\nold line B\nfooter\n";
|
||||
const result = applyEditToFile(
|
||||
content,
|
||||
"old line A\nold line B",
|
||||
"new line X\nnew line Y"
|
||||
);
|
||||
expect(result).toBe("header\nnew line X\nnew line Y\nfooter\n");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user