v4DraftWorkspaceDefaults.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // 草稿态工具条 workspace 缺省回落(m5-composer-parity §5.1)纯逻辑测试。
  2. // Bug 背景(2026-07-07 v4 首测):工具条门控在投影 config!==null 上,草稿态整体不渲染;
  3. // 修复后草稿以 renderer intent 为显示真值,目录只在无 intent 时提供初始回落。
  4. import { describe, expect, it } from "vitest";
  5. import type { ModelSelectionView } from "@zcode/services";
  6. import {
  7. resolveDraftDisplayedConfig,
  8. resolveDraftModelThoughtOption,
  9. resolveDraftThoughtCurrentValue,
  10. } from "@/v4/composer/draftWorkspaceDefaults.js";
  11. import {
  12. applyDraftModelSelection,
  13. buildDraftCreateConfigPayload,
  14. shouldHydrateWorkspaceCatalog,
  15. } from "@/v4/composer/useDraftConfigControl.js";
  16. describe("resolveDraftDisplayedConfig", () => {
  17. it("工具条只投影 Composer 的结构化选择", () => {
  18. expect(
  19. resolveDraftDisplayedConfig({
  20. modelSelection: { providerId: "glm", modelId: "GLM-5.2" },
  21. }),
  22. ).toEqual({
  23. modelSelection: { providerId: "glm", modelId: "GLM-5.2" },
  24. provider: "glm",
  25. model: "GLM-5.2",
  26. thought: "",
  27. thoughtLevels: [],
  28. followupMode: "queue",
  29. mode: "build",
  30. });
  31. });
  32. it("同模型的旧 Session 档位也不能补齐 Composer 留空的档位", () => {
  33. expect(
  34. resolveDraftDisplayedConfig({
  35. modelSelection: { providerId: "glm", modelId: "GLM-5.2" },
  36. thought: "medium",
  37. })?.thought,
  38. ).toBe("");
  39. });
  40. it("Composer 模型留空时不能恢复 Session 模型", () => {
  41. expect(
  42. resolveDraftDisplayedConfig({
  43. provider: "glm",
  44. model: "GLM-5-Turbo",
  45. mode: "yolo",
  46. modelSelection: undefined,
  47. }),
  48. ).toBeNull();
  49. });
  50. it("显示结构化选择的档位,不看平铺旧值", () => {
  51. expect(
  52. resolveDraftDisplayedConfig({
  53. thought: "medium",
  54. modelSelection: {
  55. providerId: "glm",
  56. modelId: "GLM-5.2",
  57. options: { reasoningLevel: "low" },
  58. },
  59. })?.thought,
  60. ).toBe("low");
  61. });
  62. });
  63. describe("resolveDraftModelThoughtOption", () => {
  64. const modelSelectionView: ModelSelectionView = {
  65. revision: 1,
  66. providers: [
  67. {
  68. providerId: "glm",
  69. providerName: "GLM",
  70. config: {},
  71. models: [
  72. {
  73. modelId: "GLM-5.2",
  74. config: {
  75. optionSpecs: {
  76. reasoningLevel: {
  77. values: ["low", "medium", "high"],
  78. map: "{}",
  79. },
  80. maxOutputTokens: { max: 32_000, map: "{}" },
  81. },
  82. },
  83. },
  84. {
  85. modelId: "GLM-5-Turbo",
  86. config: {
  87. optionSpecs: {
  88. reasoningLevel: { values: ["disabled"], map: "{}" },
  89. maxOutputTokens: { max: 32_000, map: "{}" },
  90. },
  91. },
  92. },
  93. {
  94. modelId: "glm-5.2-pro",
  95. config: {
  96. optionSpecs: {
  97. reasoningLevel: { values: ["disabled"], map: "{}" },
  98. maxOutputTokens: { max: 32_000, map: "{}" },
  99. },
  100. },
  101. },
  102. ],
  103. },
  104. ],
  105. };
  106. it("returns the selected model's own levels without inventing a reasoning value", () => {
  107. expect(resolveDraftModelThoughtOption("glm", "GLM-5.2", modelSelectionView)).toMatchObject({
  108. currentValue: "",
  109. options: [{ value: "low" }, { value: "medium" }, { value: "high" }],
  110. });
  111. });
  112. it("does not fall back to another model's thought capabilities", () => {
  113. expect(resolveDraftModelThoughtOption("glm", "unknown", modelSelectionView)).toBeNull();
  114. });
  115. it("does not infer reasoning from the GLM model name", () => {
  116. expect(resolveDraftModelThoughtOption("glm", "glm-5.2-pro", modelSelectionView)).toMatchObject({
  117. currentValue: "",
  118. options: [{ value: "disabled" }],
  119. });
  120. });
  121. it("uses the Registry model option spec", () => {
  122. const updatedSelectionView: ModelSelectionView = {
  123. revision: 2,
  124. providers: [
  125. {
  126. providerId: "glm",
  127. providerName: "GLM",
  128. config: {},
  129. models: [
  130. {
  131. modelId: "GLM-5.2",
  132. config: {
  133. optionSpecs: {
  134. reasoningLevel: {
  135. values: ["high", "max"],
  136. map: "{}",
  137. },
  138. maxOutputTokens: { max: 32_000, map: "{}" },
  139. },
  140. },
  141. },
  142. ],
  143. },
  144. ],
  145. };
  146. expect(resolveDraftModelThoughtOption("glm", "GLM-5.2", updatedSelectionView)).toMatchObject({
  147. currentValue: "",
  148. options: [{ value: "high" }, { value: "max" }],
  149. });
  150. });
  151. });
  152. describe("resolveDraftThoughtCurrentValue", () => {
  153. it.each([
  154. ["high", "high"],
  155. ["removed", ""],
  156. [undefined, ""],
  157. ])("只展示有效的当前档位 %s", (thought, expected) => {
  158. expect(resolveDraftThoughtCurrentValue({ thought, thoughtLevels: ["low", "high"] })).toBe(
  159. expected,
  160. );
  161. });
  162. });
  163. describe("buildDraftCreateConfigPayload", () => {
  164. it("草稿跨模型选择会清除源模型 thought", () => {
  165. expect(
  166. applyDraftModelSelection(
  167. {
  168. provider: "glm",
  169. model: "GLM-5-Turbo",
  170. thought: "enabled",
  171. mode: "build",
  172. },
  173. { providerId: "glm", modelId: "GLM-5.2" },
  174. ),
  175. ).toEqual({
  176. provider: "glm",
  177. model: "GLM-5.2",
  178. modelSelection: { providerId: "glm", modelId: "GLM-5.2" },
  179. mode: "build",
  180. });
  181. });
  182. it("有草稿选择 → 携带 config 键;无选择 → 空对象(createSession 不带 config)", () => {
  183. expect(buildDraftCreateConfigPayload({ provider: "glm", model: "glm-4.7" })).toEqual({
  184. config: { provider: "glm", model: "glm-4.7" },
  185. });
  186. expect(buildDraftCreateConfigPayload({})).toEqual({});
  187. });
  188. it("app 交互行为偏好 → createSession.config.followupMode", () => {
  189. expect(buildDraftCreateConfigPayload({}, "guide")).toEqual({
  190. config: { followupMode: "guide" },
  191. });
  192. expect(buildDraftCreateConfigPayload({ provider: "glm", model: "glm-4.7" }, "queue")).toEqual({
  193. config: {
  194. provider: "glm",
  195. model: "glm-4.7",
  196. followupMode: "queue",
  197. },
  198. });
  199. });
  200. it("createSession 只使用当前 Composer 状态", () => {
  201. expect(
  202. buildDraftCreateConfigPayload({ model: "deepseek-v4-pro", thought: "low" }, "queue"),
  203. ).toEqual({
  204. config: {
  205. model: "deepseek-v4-pro",
  206. thought: "low",
  207. followupMode: "queue",
  208. },
  209. });
  210. });
  211. });
  212. describe("shouldHydrateWorkspaceCatalog", () => {
  213. const modePresentation = [
  214. {
  215. id: "mode",
  216. name: "Mode",
  217. category: "mode" as const,
  218. type: "select" as const,
  219. currentValue: "build",
  220. options: [{ value: "build", name: "build" }],
  221. },
  222. ];
  223. const slashCatalog = [{ name: "review", description: "Review changes" }];
  224. it("模型目录已继承但 slash 目录被 task → draft 清空时仍需水合", () => {
  225. expect(
  226. shouldHydrateWorkspaceCatalog({
  227. configOptions: modePresentation,
  228. sessionId: null,
  229. slashCommands: [],
  230. }),
  231. ).toBe(true);
  232. });
  233. it("任一目录缺失时水合;两份目录均就绪时跳过", () => {
  234. expect(
  235. shouldHydrateWorkspaceCatalog({
  236. configOptions: [],
  237. sessionId: null,
  238. slashCommands: slashCatalog,
  239. }),
  240. ).toBe(true);
  241. expect(
  242. shouldHydrateWorkspaceCatalog({
  243. configOptions: modePresentation,
  244. sessionId: null,
  245. slashCommands: slashCatalog,
  246. }),
  247. ).toBe(false);
  248. });
  249. });