v4DraftWorkspaceDefaults.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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("模型目录的默认档位不能填补恢复选择的空档位", () => {
  154. const modelThoughtOption = resolveDraftModelThoughtOption(
  155. "account:bigmodel-individual-coding-plan",
  156. "GLM-5.3",
  157. {
  158. revision: 1,
  159. providers: [
  160. {
  161. providerId: "account:bigmodel-individual-coding-plan",
  162. providerName: "BigModel Coding Plan",
  163. config: {},
  164. models: [
  165. {
  166. modelId: "GLM-5.3",
  167. config: {
  168. optionSpecs: {
  169. reasoningLevel: {
  170. values: ["high", "low", "max"],
  171. map: "{}",
  172. },
  173. maxOutputTokens: { max: 32_000, map: "{}" },
  174. },
  175. },
  176. },
  177. ],
  178. },
  179. ],
  180. },
  181. );
  182. expect(
  183. resolveDraftThoughtCurrentValue({
  184. thought: "",
  185. thoughtLevels: ["high", "low", "max"],
  186. // 目录自身仍可提供新选择的默认档位,但展示当前选择时不读取它。
  187. }),
  188. ).toBe("");
  189. expect(modelThoughtOption?.currentValue).toBe("");
  190. });
  191. it.each([
  192. ["high", "high"],
  193. ["removed", ""],
  194. [undefined, ""],
  195. ])("只展示有效的当前档位 %s", (thought, expected) => {
  196. expect(resolveDraftThoughtCurrentValue({ thought, thoughtLevels: ["low", "high"] })).toBe(
  197. expected,
  198. );
  199. });
  200. });
  201. describe("buildDraftCreateConfigPayload", () => {
  202. it("草稿跨模型选择会清除源模型 thought", () => {
  203. expect(
  204. applyDraftModelSelection(
  205. {
  206. provider: "glm",
  207. model: "GLM-5-Turbo",
  208. thought: "enabled",
  209. mode: "build",
  210. },
  211. { providerId: "glm", modelId: "GLM-5.2" },
  212. ),
  213. ).toEqual({
  214. provider: "glm",
  215. model: "GLM-5.2",
  216. modelSelection: { providerId: "glm", modelId: "GLM-5.2" },
  217. mode: "build",
  218. });
  219. });
  220. it("有草稿选择 → 携带 config 键;无选择 → 空对象(createSession 不带 config)", () => {
  221. expect(buildDraftCreateConfigPayload({ provider: "glm", model: "glm-4.7" })).toEqual({
  222. config: { provider: "glm", model: "glm-4.7" },
  223. });
  224. expect(buildDraftCreateConfigPayload({})).toEqual({});
  225. });
  226. it("app 交互行为偏好 → createSession.config.followupMode", () => {
  227. expect(buildDraftCreateConfigPayload({}, "guide")).toEqual({
  228. config: { followupMode: "guide" },
  229. });
  230. expect(buildDraftCreateConfigPayload({ provider: "glm", model: "glm-4.7" }, "queue")).toEqual({
  231. config: {
  232. provider: "glm",
  233. model: "glm-4.7",
  234. followupMode: "queue",
  235. },
  236. });
  237. });
  238. it("createSession 只使用当前 Composer 状态", () => {
  239. expect(
  240. buildDraftCreateConfigPayload({ model: "deepseek-v4-pro", thought: "low" }, "queue"),
  241. ).toEqual({
  242. config: {
  243. model: "deepseek-v4-pro",
  244. thought: "low",
  245. followupMode: "queue",
  246. },
  247. });
  248. });
  249. it("空 Composer 不制造 config", () => {
  250. expect(buildDraftCreateConfigPayload({}, null)).toEqual({});
  251. });
  252. });
  253. describe("shouldHydrateWorkspaceCatalog", () => {
  254. const modePresentation = [
  255. {
  256. id: "mode",
  257. name: "Mode",
  258. category: "mode" as const,
  259. type: "select" as const,
  260. currentValue: "build",
  261. options: [{ value: "build", name: "build" }],
  262. },
  263. ];
  264. const slashCatalog = [{ name: "review", description: "Review changes" }];
  265. it("模型目录已继承但 slash 目录被 task → draft 清空时仍需水合", () => {
  266. expect(
  267. shouldHydrateWorkspaceCatalog({
  268. configOptions: modePresentation,
  269. sessionId: null,
  270. slashCommands: [],
  271. }),
  272. ).toBe(true);
  273. });
  274. it("任一目录缺失时水合;两份目录均就绪时跳过", () => {
  275. expect(
  276. shouldHydrateWorkspaceCatalog({
  277. configOptions: [],
  278. sessionId: null,
  279. slashCommands: slashCatalog,
  280. }),
  281. ).toBe(true);
  282. expect(
  283. shouldHydrateWorkspaceCatalog({
  284. configOptions: modePresentation,
  285. sessionId: null,
  286. slashCommands: slashCatalog,
  287. }),
  288. ).toBe(false);
  289. });
  290. it("已有 session 仍独立水合 workspace slash 与非模型 mode 展示", () => {
  291. expect(
  292. shouldHydrateWorkspaceCatalog({
  293. configOptions: [],
  294. sessionId: "session-known",
  295. slashCommands: slashCatalog,
  296. }),
  297. ).toBe(true);
  298. expect(
  299. shouldHydrateWorkspaceCatalog({
  300. configOptions: modePresentation,
  301. sessionId: "session-known",
  302. slashCommands: [],
  303. }),
  304. ).toBe(true);
  305. });
  306. });