composerRecent.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { describe, expect, it, vi } from "vitest";
  2. import { logger } from "@/logger.js";
  3. import {
  4. captureComposerRecentSubmission,
  5. readComposerRecent,
  6. resolveDraftInitialModelSelection,
  7. prepareComposerRecentReceipt,
  8. acceptComposerRecentReceipt,
  9. } from "@/lib/composerRecent.js";
  10. vi.mock("@/logger.js", () => ({ logger: { warn: vi.fn() } }));
  11. function storage() {
  12. const values = new Map<string, string>();
  13. return {
  14. getItem: (key: string) => values.get(key) ?? null,
  15. setItem: vi.fn((key: string, value: string) => {
  16. values.set(key, value);
  17. }),
  18. values,
  19. };
  20. }
  21. const selection = { providerId: "provider", modelId: "model", options: { reasoningLevel: "high" } };
  22. const key = "zcode-model-selection-recent-v1:/repo";
  23. describe("composerRecent", () => {
  24. it("PD07 持久化的接纳顺序阻止恢复查询中的旧配置回写", () => {
  25. const target = storage();
  26. const scope = { clientNamespace: "desktop", hostId: "local", projectId: "p" };
  27. const old = prepareComposerRecentReceipt(scope, { modelSelection: selection, mode: "build" });
  28. const latest = prepareComposerRecentReceipt(scope, {
  29. modelSelection: { ...selection, modelId: "next" },
  30. mode: "yolo",
  31. });
  32. expect(acceptComposerRecentReceipt(latest, target)).toBe(true);
  33. expect(acceptComposerRecentReceipt(JSON.parse(JSON.stringify(old)), target)).toBe(true);
  34. expect(readComposerRecent("/any", undefined, target, scope)).toMatchObject({
  35. mode: "yolo",
  36. modelSelection: { modelId: "next" },
  37. });
  38. });
  39. it("PD04 Recent 跟随项目而非目录,任务配置跨新目录复用", () => {
  40. const target = storage();
  41. const p = { clientNamespace: "desktop", hostId: "local", projectId: "p" };
  42. const task = { ...p, projectId: null };
  43. const accept = captureComposerRecentSubmission(
  44. "/repo",
  45. { modelSelection: selection, mode: "yolo" },
  46. undefined,
  47. target,
  48. p,
  49. );
  50. expect(readComposerRecent("/repo", undefined, target, p)).toBeNull();
  51. accept();
  52. expect(readComposerRecent("/new-root", undefined, target, p)).toEqual({
  53. modelSelection: selection,
  54. mode: "yolo",
  55. });
  56. expect(readComposerRecent("/repo", undefined, target, { ...p, projectId: "q" })).toBeNull();
  57. expect(readComposerRecent("/repo", undefined, target, task)).toBeNull();
  58. captureComposerRecentSubmission(
  59. "/task-a",
  60. { modelSelection: selection, mode: "build" },
  61. undefined,
  62. target,
  63. task,
  64. )();
  65. expect(readComposerRecent("/task-b", undefined, target, task)?.mode).toBe("build");
  66. expect(
  67. readComposerRecent("/task-b", undefined, target, { ...task, clientNamespace: "mobile" }),
  68. ).toBeNull();
  69. });
  70. it("沿用旧 key 读取纯 ModelSelection,不臆造旧记录的模式", () => {
  71. const target = storage();
  72. target.values.set(key, JSON.stringify(selection));
  73. expect(readComposerRecent("/repo", undefined, target)).toEqual({ modelSelection: selection });
  74. expect(target.setItem).not.toHaveBeenCalled();
  75. });
  76. it.each([
  77. [JSON.stringify({ modelSelection: selection, mode: "invalid" }), { modelSelection: selection }],
  78. [JSON.stringify({ modelSelection: { bad: true }, mode: "yolo" }), { mode: "yolo" }],
  79. [JSON.stringify({ mode: "plan" }), { mode: "plan" }],
  80. [
  81. JSON.stringify({
  82. providerId: "provider",
  83. modelId: "model",
  84. options: { maxOutputTokens: 4096 },
  85. }),
  86. null,
  87. ],
  88. ["{}", null],
  89. ["null", null],
  90. ["broken", null],
  91. ])("独立校验模型与模式:%s", (raw, expected) => {
  92. const target = storage();
  93. target.values.set(key, raw);
  94. expect(readComposerRecent("/repo", undefined, target)).toEqual(expected);
  95. });
  96. it("接受后一次写入冻结的模型与模式,未接纳候选不改变 Recent", () => {
  97. const target = storage();
  98. const submission = { modelSelection: { ...selection, options: {} }, mode: "yolo" as const };
  99. const accept = captureComposerRecentSubmission("/repo", submission, undefined, target);
  100. expect(readComposerRecent("/repo", undefined, target)).toBeNull();
  101. submission.modelSelection.modelId = "edited-after-submit";
  102. accept();
  103. accept();
  104. expect(target.setItem).toHaveBeenCalledTimes(1);
  105. expect(JSON.parse(target.values.get(key)!)).toEqual({
  106. modelSelection: { providerId: "provider", modelId: "model" },
  107. mode: "yolo",
  108. });
  109. captureComposerRecentSubmission(
  110. "/repo",
  111. { modelSelection: selection, mode: "build" },
  112. undefined,
  113. target,
  114. );
  115. expect(readComposerRecent("/repo", undefined, target)?.mode).toBe("yolo");
  116. });
  117. it("迟到的旧 accepted 不覆盖较新已接纳提交;较新失败不阻断较旧成功", () => {
  118. const target = storage();
  119. const older = captureComposerRecentSubmission(
  120. "/repo",
  121. { modelSelection: selection, mode: "yolo" },
  122. undefined,
  123. target,
  124. );
  125. const newer = captureComposerRecentSubmission(
  126. "/repo",
  127. { modelSelection: selection, mode: "plan" },
  128. undefined,
  129. target,
  130. );
  131. newer();
  132. older();
  133. expect(readComposerRecent("/repo", undefined, target)?.mode).toBe("plan");
  134. const accepted = captureComposerRecentSubmission(
  135. "/repo",
  136. { modelSelection: selection, mode: "edit" },
  137. undefined,
  138. target,
  139. );
  140. captureComposerRecentSubmission(
  141. "/repo",
  142. { modelSelection: selection, mode: "build" },
  143. undefined,
  144. target,
  145. );
  146. accepted();
  147. expect(readComposerRecent("/repo", undefined, target)?.mode).toBe("edit");
  148. });
  149. it("指向加速卡 Provider 的提交不写入 Recent,保留上一次合法记录(spec §5.1)", () => {
  150. const target = storage();
  151. captureComposerRecentSubmission(
  152. "/repo",
  153. { modelSelection: selection, mode: "build" },
  154. undefined,
  155. target,
  156. )();
  157. const accelerated = captureComposerRecentSubmission(
  158. "/repo",
  159. {
  160. modelSelection: { providerId: "account:zai-highspeed-card", modelId: "GLM-5.3" },
  161. mode: "build",
  162. },
  163. undefined,
  164. target,
  165. );
  166. accelerated();
  167. expect(JSON.parse(target.values.get(key)!)).toEqual({
  168. modelSelection: selection,
  169. mode: "build",
  170. });
  171. });
  172. it("读取自愈:指向加速卡 Provider 的存量模型叶子被丢弃,mode 保留(spec §5.1)", () => {
  173. const target = storage();
  174. target.values.set(
  175. key,
  176. JSON.stringify({
  177. modelSelection: { providerId: "account:bigmodel-highspeed-card", modelId: "GLM-5.3" },
  178. mode: "yolo",
  179. }),
  180. );
  181. expect(readComposerRecent("/repo", undefined, target)).toEqual({ mode: "yolo" });
  182. // 旧版纯 ModelSelection 格式同样丢弃,避免新草稿种子指向隐藏加速身份。
  183. target.values.set(
  184. key,
  185. JSON.stringify({ providerId: "account:zai-highspeed-card", modelId: "GLM-5.3" }),
  186. );
  187. expect(readComposerRecent("/repo", undefined, target)).toBeNull();
  188. });
  189. it("本地路径与同路径不同远端分别保存,空 identity 使用本地路径", () => {
  190. const target = storage();
  191. const local = captureComposerRecentSubmission(
  192. "/repo",
  193. { modelSelection: selection, mode: "build" },
  194. " ",
  195. target,
  196. );
  197. const remoteA = captureComposerRecentSubmission(
  198. "/repo",
  199. { modelSelection: selection, mode: "yolo" },
  200. " ssh://a/repo ",
  201. target,
  202. );
  203. const remoteB = captureComposerRecentSubmission(
  204. "/repo",
  205. { modelSelection: selection, mode: "plan" },
  206. "ssh://b/repo",
  207. target,
  208. );
  209. remoteB();
  210. remoteA();
  211. local();
  212. expect(readComposerRecent("/repo", undefined, target)?.mode).toBe("build");
  213. expect(readComposerRecent("/repo", "ssh://a/repo", target)?.mode).toBe("yolo");
  214. expect(readComposerRecent("/repo", "ssh://b/repo", target)?.mode).toBe("plan");
  215. expect(readComposerRecent("/elsewhere", undefined, target)).toBeNull();
  216. });
  217. it("存储不可用或写入失败不抛给已接纳发送", () => {
  218. const failure = new Error("quota exceeded");
  219. const target = {
  220. getItem: () => {
  221. throw failure;
  222. },
  223. setItem: () => {
  224. throw failure;
  225. },
  226. };
  227. expect(readComposerRecent("/repo", undefined, target)).toBeNull();
  228. const accept = captureComposerRecentSubmission(
  229. "/repo",
  230. { modelSelection: selection, mode: "yolo" },
  231. undefined,
  232. target,
  233. );
  234. expect(accept).not.toThrow();
  235. expect(logger.warn).toHaveBeenCalledWith(
  236. expect.stringContaining("ComposerRecent"),
  237. expect.objectContaining({ error: failure }),
  238. );
  239. expect(
  240. captureComposerRecentSubmission(
  241. "/repo",
  242. { modelSelection: selection, mode: "yolo" },
  243. undefined,
  244. null,
  245. ),
  246. ).not.toThrow();
  247. });
  248. it("非法提交配置不阻断发送侧的 Recent 捕获", () => {
  249. vi.clearAllMocks();
  250. const target = storage();
  251. const invalidMode = {
  252. modelSelection: selection,
  253. mode: "invalid",
  254. } as never;
  255. const invalidModel = {
  256. modelSelection: null,
  257. mode: "yolo",
  258. } as never;
  259. expect(() =>
  260. captureComposerRecentSubmission("/repo", invalidMode, undefined, target),
  261. ).not.toThrow();
  262. expect(() =>
  263. captureComposerRecentSubmission("/repo", invalidModel, undefined, target),
  264. ).not.toThrow();
  265. expect(target.setItem).not.toHaveBeenCalled();
  266. expect(logger.warn).toHaveBeenCalledTimes(2);
  267. });
  268. it("失效 Model 清空;失效 Reasoning 保留模型并清空档位", () => {
  269. const view = {
  270. revision: 1,
  271. preferredSelection: {
  272. providerId: "provider",
  273. modelId: "model-b",
  274. options: { reasoningLevel: "high" },
  275. },
  276. providers: [
  277. {
  278. providerId: "provider",
  279. config: {},
  280. models: [
  281. {
  282. modelId: "model-a",
  283. config: { optionSpecs: { reasoningLevel: { values: ["low", "high"] } } },
  284. },
  285. {
  286. modelId: "model-b",
  287. config: { optionSpecs: { reasoningLevel: { values: ["low", "high"] } } },
  288. },
  289. ],
  290. },
  291. ],
  292. } as const;
  293. expect(
  294. resolveDraftInitialModelSelection(view, {
  295. providerId: "removed",
  296. modelId: "removed",
  297. }),
  298. ).toEqual({ selection: null, invalidated: true });
  299. expect(
  300. resolveDraftInitialModelSelection(view, {
  301. providerId: "provider",
  302. modelId: "model-a",
  303. options: { reasoningLevel: "removed" },
  304. }),
  305. ).toEqual({ selection: { providerId: "provider", modelId: "model-a" }, invalidated: true });
  306. expect(
  307. resolveDraftInitialModelSelection({ ...view, preferredSelection: undefined }, null),
  308. ).toEqual({ selection: null, invalidated: false });
  309. });
  310. });