offPeakAgentToolBridge.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. AUTOMATION_MUTATION_TOOL_NAMES,
  4. mergeAutomationMutationToolDenylist,
  5. mergeOffPeakMutationToolDenylist,
  6. OFF_PEAK_MUTATION_TOOL_NAMES,
  7. } from "../src/zcode-agent/automationToolPolicy.js";
  8. import {
  9. resolveOffPeakAllowedModels,
  10. resolveOffPeakCreateModel,
  11. resolveOffPeakToolSelection,
  12. toProtocolOffPeakTaskSnapshot,
  13. } from "../src/zcode-agent/zcodeAgentService.js";
  14. import { OFF_PEAK_PROVIDER_IDS } from "@zcode/shared";
  15. import { offPeakSelectionView } from "./fixtures/offPeakSelection.js";
  16. describe("off-peak tool policy(D49)", () => {
  17. it("OffPeakCreate 绝不混入 AUTOMATION_MUTATION_TOOL_NAMES(cron 轮放行是产品定案 D49-3)", () => {
  18. // 最大照抄陷阱回归锁:任何一份 automation 常量副本混入 OffPeakCreate 都会让
  19. // cron automation 执行轮误 deny 组合玩法。
  20. expect(AUTOMATION_MUTATION_TOOL_NAMES).not.toContain("OffPeakCreate");
  21. expect(OFF_PEAK_MUTATION_TOOL_NAMES).toEqual(["OffPeakCreate"]);
  22. expect(mergeAutomationMutationToolDenylist(undefined)).not.toContain("OffPeakCreate");
  23. });
  24. it("mergeOffPeakMutationToolDenylist 只追加 OffPeakCreate 且不覆盖既有策略", () => {
  25. expect(mergeOffPeakMutationToolDenylist(["CronCreate"])).toEqual([
  26. "CronCreate",
  27. "OffPeakCreate",
  28. ]);
  29. expect(mergeOffPeakMutationToolDenylist(["OffPeakCreate"])).toEqual(["OffPeakCreate"]);
  30. });
  31. });
  32. describe("Off-Peak 工具按当前 Selection View 创建", () => {
  33. const config = { enabled: true, modelSelectionView: offPeakSelectionView() };
  34. const providerId = OFF_PEAK_PROVIDER_IDS.zai;
  35. it("曝光关闭或没有候选时不开放;精确指定 Provider 时不混用另一账号域", () => {
  36. expect(resolveOffPeakAllowedModels(undefined)).toEqual([]);
  37. expect(resolveOffPeakAllowedModels({ ...config, enabled: false })).toEqual([]);
  38. expect(
  39. resolveOffPeakAllowedModels({
  40. enabled: true,
  41. modelSelectionView: { revision: 2, providers: [] },
  42. }),
  43. ).toEqual([]);
  44. expect(resolveOffPeakAllowedModels(config, providerId)).toEqual(["ZAI-only", "GLM-5.2"]);
  45. expect(resolveOffPeakAllowedModels(config, "missing")).toEqual([]);
  46. });
  47. it("省略 model 取末位;显式 trim/大小写匹配后保留原 ID,未命中不 fallback", () => {
  48. const allowed = ["GLM-5.1", "GLM-5.2"];
  49. expect(resolveOffPeakCreateModel(allowed, undefined)).toBe("GLM-5.2");
  50. expect(resolveOffPeakCreateModel(allowed, " ")).toBe("GLM-5.2");
  51. expect(resolveOffPeakCreateModel(allowed, " glm-5.1 ")).toBe("GLM-5.1");
  52. expect(resolveOffPeakCreateModel(allowed, "GLM-4")).toBeNull();
  53. expect(resolveOffPeakCreateModel([], undefined)).toBeNull();
  54. });
  55. it("新建使用公共最高档规则,结构化选择不读取旧 reasoning metadata", () => {
  56. expect(resolveOffPeakToolSelection(config.modelSelectionView, providerId, "GLM-5.2")).toEqual({
  57. providerId,
  58. modelId: "GLM-5.2",
  59. options: { reasoningLevel: "max" },
  60. });
  61. expect(
  62. resolveOffPeakToolSelection(offPeakSelectionView(["alpha", "beta"]), providerId, "GLM-5.2")
  63. ?.options?.reasoningLevel,
  64. ).toBe("beta");
  65. // values 是语义顺序的唯一权威,不能另写按名字 max/high 排序的第二套规则。
  66. expect(
  67. resolveOffPeakToolSelection(
  68. offPeakSelectionView(["max", "custom-strongest"]),
  69. providerId,
  70. "GLM-5.2",
  71. )?.options?.reasoningLevel,
  72. ).toBe("custom-strongest");
  73. });
  74. it("显式档位保留给创建服务校验,不覆盖为最高档;其他域和缺失配置不猜默认", () => {
  75. expect(
  76. resolveOffPeakToolSelection(config.modelSelectionView, providerId, "GLM-5.2", "high")?.options
  77. ?.reasoningLevel,
  78. ).toBe("high");
  79. expect(
  80. resolveOffPeakToolSelection(config.modelSelectionView, providerId, "BigModel-only"),
  81. ).toBeUndefined();
  82. expect(
  83. resolveOffPeakToolSelection(config.modelSelectionView, providerId, "glm-unknown"),
  84. ).toBeUndefined();
  85. expect(
  86. resolveOffPeakToolSelection(offPeakSelectionView([]), providerId, "GLM-5.2"),
  87. ).toBeUndefined();
  88. });
  89. });
  90. describe("toProtocolOffPeakTaskSnapshot(协议最小面)", () => {
  91. it("只暴露卡片/列表所需字段,位次非正数与空 sessionId 不下发", () => {
  92. expect(
  93. toProtocolOffPeakTaskSnapshot({
  94. offPeakTaskId: "offpeak-1",
  95. title: "t",
  96. status: "queued",
  97. queuePosition: 0,
  98. createdAt: 1_700_000_000_000,
  99. }),
  100. ).toEqual({
  101. offPeakTaskId: "offpeak-1",
  102. title: "t",
  103. status: "queued",
  104. createdAt: 1_700_000_000_000,
  105. });
  106. expect(
  107. toProtocolOffPeakTaskSnapshot({
  108. offPeakTaskId: "offpeak-2",
  109. title: "t2",
  110. status: "running",
  111. queuePosition: 2,
  112. sessionId: "sess-1",
  113. createdAt: 1,
  114. }),
  115. ).toEqual({
  116. offPeakTaskId: "offpeak-2",
  117. title: "t2",
  118. status: "running",
  119. queuePosition: 2,
  120. sessionId: "sess-1",
  121. createdAt: 1,
  122. });
  123. });
  124. });