mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 22:35:51 +00:00
test: 添加 json/truncate/path/tokens 模块测试
- json.test.ts: 27 tests (safeParseJSON, safeParseJSONC, parseJSONL, addItemToJSONCArray) - truncate.test.ts: 24 tests (truncateToWidth, truncateStartToWidth, truncatePathMiddle, truncate, wrapText) - path.test.ts: 15 tests (containsPathTraversal, normalizePathForConfigKey) - tokens.test.ts: 22 tests (getTokenCountFromUsage, getTokenUsage, tokenCountFromLastAPIResponse, etc.) 使用 mock.module() 切断 log.ts/tokenEstimation.ts/slowOperations.ts 重依赖链 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
72
src/utils/__tests__/path.test.ts
Normal file
72
src/utils/__tests__/path.test.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { containsPathTraversal, normalizePathForConfigKey } from "../path";
|
||||
|
||||
// ─── containsPathTraversal ──────────────────────────────────────────────
|
||||
|
||||
describe("containsPathTraversal", () => {
|
||||
test("detects ../ at start", () => {
|
||||
expect(containsPathTraversal("../foo")).toBe(true);
|
||||
});
|
||||
|
||||
test("detects ../ in middle", () => {
|
||||
expect(containsPathTraversal("foo/../bar")).toBe(true);
|
||||
});
|
||||
|
||||
test("detects .. at end", () => {
|
||||
expect(containsPathTraversal("foo/..")).toBe(true);
|
||||
});
|
||||
|
||||
test("detects standalone ..", () => {
|
||||
expect(containsPathTraversal("..")).toBe(true);
|
||||
});
|
||||
|
||||
test("detects backslash traversal", () => {
|
||||
expect(containsPathTraversal("foo\\..\\bar")).toBe(true);
|
||||
});
|
||||
|
||||
test("returns false for normal path", () => {
|
||||
expect(containsPathTraversal("foo/bar/baz")).toBe(false);
|
||||
});
|
||||
|
||||
test("returns false for single dot", () => {
|
||||
expect(containsPathTraversal("./foo")).toBe(false);
|
||||
});
|
||||
|
||||
test("returns false for ... in filename", () => {
|
||||
expect(containsPathTraversal("foo/...bar")).toBe(false);
|
||||
});
|
||||
|
||||
test("returns false for empty string", () => {
|
||||
expect(containsPathTraversal("")).toBe(false);
|
||||
});
|
||||
|
||||
test("returns false for dotdot in filename without separator", () => {
|
||||
expect(containsPathTraversal("foo..bar")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── normalizePathForConfigKey ──────────────────────────────────────────
|
||||
|
||||
describe("normalizePathForConfigKey", () => {
|
||||
test("normalizes forward slashes (no change on POSIX)", () => {
|
||||
expect(normalizePathForConfigKey("foo/bar/baz")).toBe("foo/bar/baz");
|
||||
});
|
||||
|
||||
test("resolves dot segments", () => {
|
||||
expect(normalizePathForConfigKey("foo/./bar")).toBe("foo/bar");
|
||||
});
|
||||
|
||||
test("resolves double-dot segments", () => {
|
||||
expect(normalizePathForConfigKey("foo/bar/../baz")).toBe("foo/baz");
|
||||
});
|
||||
|
||||
test("handles absolute path", () => {
|
||||
const result = normalizePathForConfigKey("/Users/test/project");
|
||||
expect(result).toBe("/Users/test/project");
|
||||
});
|
||||
|
||||
test("converts backslashes to forward slashes", () => {
|
||||
const result = normalizePathForConfigKey("foo\\bar\\baz");
|
||||
expect(result).toBe("foo/bar/baz");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user