mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 22:05:50 +00:00
test: 新增测试代码文件
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { containsPathTraversal, normalizePathForConfigKey } from "../path";
|
||||
import {
|
||||
containsPathTraversal,
|
||||
expandPath,
|
||||
normalizePathForConfigKey,
|
||||
} from "../path";
|
||||
|
||||
// ─── containsPathTraversal ──────────────────────────────────────────────
|
||||
|
||||
@@ -43,6 +47,51 @@ describe("containsPathTraversal", () => {
|
||||
test("returns false for dotdot in filename without separator", () => {
|
||||
expect(containsPathTraversal("foo..bar")).toBe(false);
|
||||
});
|
||||
|
||||
test("detects backslash traversal foo\\..\\bar", () => {
|
||||
expect(containsPathTraversal("foo\\..\\bar")).toBe(true);
|
||||
});
|
||||
|
||||
test("detects .. at end of absolute path", () => {
|
||||
expect(containsPathTraversal("/path/to/..")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── expandPath ─────────────────────────────────────────────────────────
|
||||
|
||||
describe("expandPath", () => {
|
||||
test("expands ~/ to home directory", () => {
|
||||
const result = expandPath("~/Documents");
|
||||
expect(result).not.toContain("~");
|
||||
expect(result).toContain("Documents");
|
||||
});
|
||||
|
||||
test("expands bare ~ to home directory", () => {
|
||||
const result = expandPath("~");
|
||||
expect(result).not.toContain("~");
|
||||
// Should equal home directory
|
||||
const { homedir } = require("os");
|
||||
expect(result).toBe(homedir());
|
||||
});
|
||||
|
||||
test("passes absolute paths through normalized", () => {
|
||||
expect(expandPath("/usr/local/bin")).toBe("/usr/local/bin");
|
||||
});
|
||||
|
||||
test("resolves relative path against baseDir", () => {
|
||||
expect(expandPath("src", "/project")).toBe("/project/src");
|
||||
});
|
||||
|
||||
test("returns baseDir for empty string", () => {
|
||||
expect(expandPath("", "/project")).toBe("/project");
|
||||
});
|
||||
|
||||
test("returns cwd-based path for empty string without baseDir", () => {
|
||||
const result = expandPath("");
|
||||
// Should be a valid absolute path (cwd normalized)
|
||||
const { isAbsolute } = require("path");
|
||||
expect(isAbsolute(result)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── normalizePathForConfigKey ──────────────────────────────────────────
|
||||
@@ -69,4 +118,14 @@ describe("normalizePathForConfigKey", () => {
|
||||
const result = normalizePathForConfigKey("foo\\bar\\baz");
|
||||
expect(result).toBe("foo/bar/baz");
|
||||
});
|
||||
|
||||
test("normalizes mixed separators foo/bar\\baz", () => {
|
||||
const result = normalizePathForConfigKey("foo/bar\\baz");
|
||||
expect(result).toBe("foo/bar/baz");
|
||||
});
|
||||
|
||||
test("normalizes redundant separators foo//bar", () => {
|
||||
const result = normalizePathForConfigKey("foo//bar");
|
||||
expect(result).toBe("foo/bar");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user