planModeTool.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getAvailableModesFromConfigOptions,
  4. getCurrentModeIdFromConfigOptions,
  5. getModeLabelFromConfigOptions,
  6. getPlanToolPayload,
  7. resolveProviderNativeModeIdFromSemantic,
  8. } from "../src/lib/planModeTool.js";
  9. describe("planModeTool", () => {
  10. it.each(["full-auto", "full_auto"])("keeps %s as a YOLO alias, not Guarded", (alias) => {
  11. const configOptions = [{
  12. id: "mode", name: "Mode", category: "mode", type: "select" as const,
  13. currentValue: "guarded",
  14. options: [{ value: "guarded", name: "Guarded" }, { value: "yolo", name: "YOLO" }],
  15. }];
  16. expect(resolveProviderNativeModeIdFromSemantic(configOptions, alias)).toBe("yolo");
  17. expect(resolveProviderNativeModeIdFromSemantic(configOptions, "guarded")).toBe("guarded");
  18. const legacyOptions = [{ ...configOptions[0], options: [{ value: alias, name: "YOLO" }] }];
  19. expect(resolveProviderNativeModeIdFromSemantic(legacyOptions, "yolo")).toBe(alias);
  20. expect(resolveProviderNativeModeIdFromSemantic(legacyOptions, "guarded")).toBe("guarded");
  21. });
  22. it("parses structured update_plan payloads into ordered plan steps", () => {
  23. const parsed = getPlanToolPayload({
  24. title: "update_plan",
  25. input: {
  26. explanation: "先同步计划,再开始实现。",
  27. plan: [
  28. { step: "梳理现有协议", status: "completed" },
  29. { step: "实现计划面板", status: "in_progress" },
  30. { step: "补充验证", status: "pending" },
  31. ],
  32. },
  33. });
  34. expect(parsed?.summary).toBe("先同步计划,再开始实现。");
  35. expect(parsed?.steps.map((step) => ({ title: step.title, status: step.status }))).toEqual([
  36. { title: "梳理现有协议", status: "completed" },
  37. { title: "实现计划面板", status: "in_progress" },
  38. { title: "补充验证", status: "pending" },
  39. ]);
  40. });
  41. it("parses markdown plans from plan mode tools", () => {
  42. const parsed = getPlanToolPayload({
  43. title: "EnterPlanMode",
  44. input: {
  45. plan: "# 计划\n\n- 对照现有工具链路\n- 渲染差异化计划面板\n- 增加交互切换",
  46. },
  47. });
  48. expect(parsed?.summary).toBe("计划");
  49. expect(parsed?.steps.map((step) => step.title)).toEqual([
  50. "对照现有工具链路",
  51. "渲染差异化计划面板",
  52. "增加交互切换",
  53. ]);
  54. });
  55. it("derives mode metadata from config options", () => {
  56. const configOptions = [
  57. {
  58. id: "mode",
  59. name: "Mode",
  60. category: "mode",
  61. type: "select" as const,
  62. currentValue: "plan",
  63. options: [
  64. { value: "default", name: "Default" },
  65. { value: "plan", name: "Plan Mode" },
  66. ],
  67. },
  68. ];
  69. expect(getCurrentModeIdFromConfigOptions(configOptions)).toBe("plan");
  70. expect(getModeLabelFromConfigOptions(configOptions, "plan")).toBe("Plan Mode");
  71. expect(getAvailableModesFromConfigOptions(configOptions)).toEqual([
  72. { id: "default", name: "Default", description: undefined },
  73. { id: "plan", name: "Plan Mode", description: undefined },
  74. ]);
  75. });
  76. it("keeps edit as a first-class semantic mode", () => {
  77. const configOptions = [
  78. {
  79. id: "mode",
  80. name: "Mode",
  81. category: "mode",
  82. type: "select" as const,
  83. currentValue: "edit",
  84. options: [
  85. { value: "build", name: "Ask before changes" },
  86. { value: "edit", name: "Edit automatically" },
  87. ],
  88. },
  89. ];
  90. expect(getCurrentModeIdFromConfigOptions(configOptions)).toBe("edit");
  91. expect(getModeLabelFromConfigOptions(configOptions, "edit")).toBe("Edit automatically");
  92. expect(resolveProviderNativeModeIdFromSemantic(configOptions, "edit", "glm")).toBe("edit");
  93. });
  94. it("does not map retired provider-native agent mode ids", () => {
  95. const configOptions = [
  96. {
  97. id: "mode",
  98. name: "Mode",
  99. category: "mode",
  100. type: "select" as const,
  101. currentValue: "agent",
  102. options: [
  103. { value: "agent", name: "Agent" },
  104. { value: "agent-full-access", name: "Agent (full access)" },
  105. ],
  106. },
  107. ];
  108. expect(resolveProviderNativeModeIdFromSemantic(configOptions, "default")).toBe("default");
  109. expect(
  110. resolveProviderNativeModeIdFromSemantic(configOptions, "bypassPermissions", "gemini"),
  111. ).toBe("bypassPermissions");
  112. });
  113. });