tool-perf.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. classifyCommand,
  4. classifySafeCommandIdentity,
  5. commandHash,
  6. } from "../src/tool/handlers/tool-perf.js";
  7. describe("tool performance telemetry helpers", () => {
  8. it("classifies only the bounded command prefix so heredoc bodies do not skew category", () => {
  9. const command = `cat <<'EOF'\n${"x".repeat(20_000)}\nnpm test\nEOF`;
  10. expect(classifyCommand(command)).toBe("other");
  11. });
  12. it("uses a bounded command hash bucket for very large commands", () => {
  13. const prefix = `python <<'PY'\n${"p".repeat(6_000)}`;
  14. const suffix = `${"s".repeat(6_000)}\nPY`;
  15. const left = `${prefix}${"a".repeat(10_000)}${suffix}`;
  16. const right = `${prefix}${"b".repeat(10_000)}${suffix}`;
  17. expect(commandHash(left)).toBe(commandHash(right));
  18. });
  19. it("只暴露公开 Registry 中的单一可执行文件名", () => {
  20. expect(classifySafeCommandIdentity("/usr/bin/git status --short")).toEqual({
  21. count: 1,
  22. name: "git",
  23. });
  24. expect(classifySafeCommandIdentity("./customer-private-script --token secret")).toEqual({
  25. count: 1,
  26. name: "other",
  27. });
  28. });
  29. it("动态和复合命令只进入固定低基数桶", () => {
  30. expect(classifySafeCommandIdentity("git status && pnpm test")).toEqual({
  31. count: 2,
  32. name: "compound",
  33. });
  34. expect(classifySafeCommandIdentity("$PRIVATE_COMMAND --arg")).toMatchObject({
  35. name: "other",
  36. });
  37. });
  38. it("超长命令不进入完整 parser,也不伪造命令数量", () => {
  39. expect(classifySafeCommandIdentity(`git status # ${"x".repeat(9_000)}`)).toEqual({
  40. name: "other",
  41. });
  42. });
  43. });