botsCommandParser.test.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, expect, it } from "vitest";
  2. import { parseBotCommand } from "../src/bots/commandParser.js";
  3. describe("bots command parser", () => {
  4. it("parses known commands", () => {
  5. expect(parseBotCommand("/leave")).toEqual({ type: "topic.leave" });
  6. expect(parseBotCommand("/help")).toEqual({ type: "help" });
  7. expect(parseBotCommand("/cancel")).toEqual({ type: "selection.cancel" });
  8. expect(parseBotCommand("/取消")).toEqual({ type: "selection.cancel" });
  9. expect(parseBotCommand("/status")).toEqual({ type: "status" });
  10. expect(parseBotCommand("/new")).toEqual({ type: "new" });
  11. expect(parseBotCommand("/clear")).toEqual({ type: "new" });
  12. expect(parseBotCommand("/reconnect")).toEqual({ type: "reconnect" });
  13. expect(parseBotCommand("/重连")).toEqual({ type: "reconnect" });
  14. expect(parseBotCommand("/workspace")).toEqual({ type: "workspace.list" });
  15. expect(parseBotCommand("/workspace 1")).toEqual({ type: "workspace.set", value: "1" });
  16. expect(parseBotCommand("/project")).toEqual({ type: "workspace.list" });
  17. expect(parseBotCommand("/项目 1")).toEqual({ type: "workspace.set", value: "1" });
  18. expect(parseBotCommand("/model gpt-5.4")).toEqual({ type: "model.set", value: "gpt-5.4" });
  19. expect(parseBotCommand("/model provider 1")).toEqual({
  20. type: "model.provider.set",
  21. value: "1",
  22. });
  23. expect(parseBotCommand("/model model 1")).toEqual({ type: "model.set", value: "1" });
  24. expect(parseBotCommand("/模型 gpt-5.4")).toEqual({ type: "model.set", value: "gpt-5.4" });
  25. expect(parseBotCommand("/thoughtLevel medium")).toEqual({
  26. type: "thoughtLevel.set",
  27. value: "medium",
  28. });
  29. expect(parseBotCommand("/think medium")).toEqual({ type: "thoughtLevel.set", value: "medium" });
  30. expect(parseBotCommand("/思考 medium")).toEqual({ type: "thoughtLevel.set", value: "medium" });
  31. expect(parseBotCommand("/mode auto")).toEqual({ type: "mode.set", value: "auto" });
  32. expect(parseBotCommand("/模式 auto")).toEqual({ type: "mode.set", value: "auto" });
  33. expect(parseBotCommand("/回复 full")).toEqual({ type: "reply.set", value: "full" });
  34. expect(parseBotCommand("/停止")).toEqual({ type: "stop" });
  35. expect(parseBotCommand("/permission 1")).toEqual({ type: "permission.respond", value: "1" });
  36. expect(parseBotCommand("/elicitation 2")).toEqual({ type: "elicitation.respond", value: "2" });
  37. expect(parseBotCommand("/answer custom value")).toEqual({
  38. type: "elicitation.respond",
  39. value: "custom value",
  40. });
  41. expect(parseBotCommand("/回答 完成")).toEqual({ type: "elicitation.submit" });
  42. expect(parseBotCommand("/bind ABC123")).toEqual({ type: "bind", code: "ABC123" });
  43. });
  44. it("parses Chinese aliases for top-level commands", () => {
  45. expect(parseBotCommand("/帮助")).toEqual({ type: "help" });
  46. expect(parseBotCommand("/状态")).toEqual({ type: "status" });
  47. expect(parseBotCommand("/新建")).toEqual({ type: "new" });
  48. });
  49. it("parses approval commands", () => {
  50. expect(parseBotCommand("/approve req allow")).toEqual({
  51. type: "approve",
  52. requestId: "req",
  53. optionId: "allow",
  54. });
  55. expect(parseBotCommand("/deny req")).toEqual({ type: "deny", requestId: "req" });
  56. });
  57. it("treats normal text as a message and incomplete commands as unknown", () => {
  58. expect(parseBotCommand("hello")).toEqual({ type: "message", text: "hello" });
  59. expect(parseBotCommand("0")).toEqual({ type: "selection.cancel" });
  60. expect(parseBotCommand("/bind")).toEqual({ type: "unknown", name: "bind", raw: "/bind" });
  61. expect(parseBotCommand("/wat")).toEqual({ type: "unknown", name: "wat", raw: "/wat" });
  62. expect(parseBotCommand("/approval on-request")).toEqual({
  63. type: "unknown",
  64. name: "approval",
  65. raw: "/approval on-request",
  66. });
  67. expect(parseBotCommand("/cli")).toEqual({
  68. type: "unknown",
  69. name: "cli",
  70. raw: "/cli",
  71. });
  72. expect(parseBotCommand("/sandbox workspace-write")).toEqual({
  73. type: "unknown",
  74. name: "sandbox",
  75. raw: "/sandbox workspace-write",
  76. });
  77. });
  78. });