mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 22:05:50 +00:00
test: Phase 2-4 — 添加 12 个测试文件 (+321 tests, 968 total)
Phase 2 (轻 Mock): envUtils, sleep/sequential, memoize, groupToolUses, dangerousPatterns, outputLimits Phase 3 (补全): zodToJsonSchema, PermissionMode, envValidation Phase 4 (工具模块): mcpStringUtils, destructiveCommandWarning, commandSemantics Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
87
src/tools/BashTool/__tests__/commandSemantics.test.ts
Normal file
87
src/tools/BashTool/__tests__/commandSemantics.test.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { mock, describe, expect, test } from "bun:test";
|
||||
|
||||
// Mock commands.ts to cut the heavy shell/prefix.ts → analytics → api chain
|
||||
mock.module("src/utils/bash/commands.ts", () => ({
|
||||
splitCommand_DEPRECATED: (cmd: string) =>
|
||||
cmd.split(/\s*(?:[|;&]+)\s*/).filter(Boolean),
|
||||
quote: (args: string[]) => args.join(" "),
|
||||
}));
|
||||
|
||||
const { interpretCommandResult } = await import("../commandSemantics");
|
||||
|
||||
describe("interpretCommandResult", () => {
|
||||
// ─── Default semantics ────────────────────────────────────────────
|
||||
test("exit 0 is not an error for unknown commands", () => {
|
||||
const result = interpretCommandResult("echo hello", 0, "hello", "");
|
||||
expect(result.isError).toBe(false);
|
||||
});
|
||||
|
||||
test("non-zero exit is an error for unknown commands", () => {
|
||||
const result = interpretCommandResult("echo hello", 1, "", "fail");
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.message).toContain("exit code 1");
|
||||
});
|
||||
|
||||
// ─── grep semantics ──────────────────────────────────────────────
|
||||
test("grep exit 0 is not an error", () => {
|
||||
const result = interpretCommandResult("grep pattern file", 0, "match", "");
|
||||
expect(result.isError).toBe(false);
|
||||
});
|
||||
|
||||
test("grep exit 1 means no matches (not error)", () => {
|
||||
const result = interpretCommandResult("grep pattern file", 1, "", "");
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("No matches found");
|
||||
});
|
||||
|
||||
test("grep exit 2 is an error", () => {
|
||||
const result = interpretCommandResult("grep pattern file", 2, "", "err");
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
|
||||
// ─── diff semantics ──────────────────────────────────────────────
|
||||
test("diff exit 1 means files differ (not error)", () => {
|
||||
const result = interpretCommandResult("diff a.txt b.txt", 1, "diff", "");
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("Files differ");
|
||||
});
|
||||
|
||||
test("diff exit 2 is an error", () => {
|
||||
const result = interpretCommandResult("diff a.txt b.txt", 2, "", "err");
|
||||
expect(result.isError).toBe(true);
|
||||
});
|
||||
|
||||
// ─── test/[ semantics ────────────────────────────────────────────
|
||||
test("test exit 1 means condition false (not error)", () => {
|
||||
const result = interpretCommandResult("test -f nofile", 1, "", "");
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("Condition is false");
|
||||
});
|
||||
|
||||
// ─── piped commands ──────────────────────────────────────────────
|
||||
test("uses last command in pipe for semantics", () => {
|
||||
// "cat file | grep pattern" → last command is "grep pattern"
|
||||
const result = interpretCommandResult(
|
||||
"cat file | grep pattern",
|
||||
1,
|
||||
"",
|
||||
""
|
||||
);
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("No matches found");
|
||||
});
|
||||
|
||||
// ─── rg (ripgrep) semantics ──────────────────────────────────────
|
||||
test("rg exit 1 means no matches (not error)", () => {
|
||||
const result = interpretCommandResult("rg pattern", 1, "", "");
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("No matches found");
|
||||
});
|
||||
|
||||
// ─── find semantics ──────────────────────────────────────────────
|
||||
test("find exit 1 is partial success", () => {
|
||||
const result = interpretCommandResult("find . -name '*.ts'", 1, "", "");
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.message).toBe("Some directories were inaccessible");
|
||||
});
|
||||
});
|
||||
112
src/tools/BashTool/__tests__/destructiveCommandWarning.test.ts
Normal file
112
src/tools/BashTool/__tests__/destructiveCommandWarning.test.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { getDestructiveCommandWarning } from "../destructiveCommandWarning";
|
||||
|
||||
describe("getDestructiveCommandWarning", () => {
|
||||
// ─── Git data loss ─────────────────────────────────────────────────
|
||||
test("detects git reset --hard", () => {
|
||||
const w = getDestructiveCommandWarning("git reset --hard HEAD~1");
|
||||
expect(w).toContain("discard uncommitted changes");
|
||||
});
|
||||
|
||||
test("detects git push --force", () => {
|
||||
const w = getDestructiveCommandWarning("git push --force origin main");
|
||||
expect(w).toContain("overwrite remote history");
|
||||
});
|
||||
|
||||
test("detects git push -f", () => {
|
||||
expect(getDestructiveCommandWarning("git push -f")).toContain(
|
||||
"overwrite remote history"
|
||||
);
|
||||
});
|
||||
|
||||
test("detects git clean -f", () => {
|
||||
const w = getDestructiveCommandWarning("git clean -fd");
|
||||
expect(w).toContain("delete untracked files");
|
||||
});
|
||||
|
||||
test("does not flag git clean --dry-run", () => {
|
||||
expect(getDestructiveCommandWarning("git clean -fdn")).toBeNull();
|
||||
});
|
||||
|
||||
test("detects git checkout .", () => {
|
||||
const w = getDestructiveCommandWarning("git checkout -- .");
|
||||
expect(w).toContain("discard all working tree changes");
|
||||
});
|
||||
|
||||
test("detects git restore .", () => {
|
||||
const w = getDestructiveCommandWarning("git restore -- .");
|
||||
expect(w).toContain("discard all working tree changes");
|
||||
});
|
||||
|
||||
test("detects git stash drop", () => {
|
||||
const w = getDestructiveCommandWarning("git stash drop");
|
||||
expect(w).toContain("remove stashed changes");
|
||||
});
|
||||
|
||||
test("detects git branch -D", () => {
|
||||
const w = getDestructiveCommandWarning("git branch -D feature");
|
||||
expect(w).toContain("force-delete a branch");
|
||||
});
|
||||
|
||||
// ─── Git safety bypass ────────────────────────────────────────────
|
||||
test("detects --no-verify", () => {
|
||||
const w = getDestructiveCommandWarning("git commit --no-verify -m 'x'");
|
||||
expect(w).toContain("skip safety hooks");
|
||||
});
|
||||
|
||||
test("detects git commit --amend", () => {
|
||||
const w = getDestructiveCommandWarning("git commit --amend");
|
||||
expect(w).toContain("rewrite the last commit");
|
||||
});
|
||||
|
||||
// ─── File deletion ────────────────────────────────────────────────
|
||||
test("detects rm -rf", () => {
|
||||
const w = getDestructiveCommandWarning("rm -rf /tmp/dir");
|
||||
expect(w).toContain("recursively force-remove");
|
||||
});
|
||||
|
||||
test("detects rm -r", () => {
|
||||
const w = getDestructiveCommandWarning("rm -r dir");
|
||||
expect(w).toContain("recursively remove");
|
||||
});
|
||||
|
||||
test("detects rm -f", () => {
|
||||
const w = getDestructiveCommandWarning("rm -f file.txt");
|
||||
expect(w).toContain("force-remove");
|
||||
});
|
||||
|
||||
// ─── Database ─────────────────────────────────────────────────────
|
||||
test("detects DROP TABLE", () => {
|
||||
const w = getDestructiveCommandWarning("psql -c 'DROP TABLE users'");
|
||||
expect(w).toContain("drop or truncate");
|
||||
});
|
||||
|
||||
test("detects TRUNCATE TABLE", () => {
|
||||
const w = getDestructiveCommandWarning("TRUNCATE TABLE logs");
|
||||
expect(w).toContain("drop or truncate");
|
||||
});
|
||||
|
||||
test("detects DELETE FROM without WHERE", () => {
|
||||
const w = getDestructiveCommandWarning("DELETE FROM users;");
|
||||
expect(w).toContain("delete all rows");
|
||||
});
|
||||
|
||||
// ─── Infrastructure ───────────────────────────────────────────────
|
||||
test("detects kubectl delete", () => {
|
||||
const w = getDestructiveCommandWarning("kubectl delete pod my-pod");
|
||||
expect(w).toContain("delete Kubernetes");
|
||||
});
|
||||
|
||||
test("detects terraform destroy", () => {
|
||||
const w = getDestructiveCommandWarning("terraform destroy");
|
||||
expect(w).toContain("destroy Terraform");
|
||||
});
|
||||
|
||||
// ─── Safe commands ────────────────────────────────────────────────
|
||||
test("returns null for safe commands", () => {
|
||||
expect(getDestructiveCommandWarning("ls -la")).toBeNull();
|
||||
expect(getDestructiveCommandWarning("git status")).toBeNull();
|
||||
expect(getDestructiveCommandWarning("npm install")).toBeNull();
|
||||
expect(getDestructiveCommandWarning("cat file.txt")).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user