composerDraftStore.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import {
  3. clearV4ComposerDraft,
  4. getV4ComposerDraftStorageKey,
  5. persistV4ComposerDraft,
  6. readV4ComposerDraft,
  7. V4_DRAFT_SCOPE_ROOT,
  8. } from "@/v4/composer/composerDraftStore.js";
  9. const selection = {
  10. providerId: "personal-provider",
  11. modelId: "model-a",
  12. options: { reasoningLevel: "high" },
  13. };
  14. const values = new Map<string, string>();
  15. const storage = {
  16. getItem: (key: string) => values.get(key) ?? null,
  17. setItem: (key: string, value: string) => values.set(key, value),
  18. removeItem: (key: string) => values.delete(key),
  19. };
  20. const workspace = "/workspace";
  21. const read = (scope = "session-a") => readV4ComposerDraft(workspace, undefined, scope);
  22. describe("Composer Draft 持久选择", () => {
  23. beforeEach(() => {
  24. values.clear();
  25. vi.stubGlobal("window", { localStorage: storage });
  26. });
  27. afterEach(() => {
  28. vi.unstubAllGlobals();
  29. vi.restoreAllMocks();
  30. });
  31. it("空文本仍保留 mode、选择;显式清理才删除 scope", () => {
  32. persistV4ComposerDraft(workspace, undefined, "session-a", {
  33. text: "",
  34. mode: "edit",
  35. modelSelection: selection,
  36. });
  37. expect(read()).toMatchObject({ text: "", mode: "edit", modelSelection: selection });
  38. clearV4ComposerDraft(workspace, undefined, "session-a");
  39. expect(read()).toBeNull();
  40. });
  41. it("初始化过的空选择持久保留,mode 区分它与旧文本草稿", () => {
  42. persistV4ComposerDraft(workspace, undefined, "session-a", { text: "", mode: "build" });
  43. expect(read()).toMatchObject({ text: "", mode: "build" });
  44. expect(read()?.modelSelection).toBeUndefined();
  45. });
  46. it("清空已发送文本不会删除执行选择", () => {
  47. const draft = { text: "prompt", mode: "plan" as const, modelSelection: selection };
  48. persistV4ComposerDraft(workspace, undefined, "session-a", draft);
  49. persistV4ComposerDraft(workspace, undefined, "session-a", { ...draft, text: "" });
  50. expect(read()).toMatchObject({
  51. text: "",
  52. mode: "build",
  53. planEnabled: true,
  54. modelSelection: selection,
  55. });
  56. });
  57. it("同路径不同远端与 Root/Session scope 互相隔离", () => {
  58. persistV4ComposerDraft(workspace, "remote-a", V4_DRAFT_SCOPE_ROOT, { text: "", mode: "edit" });
  59. persistV4ComposerDraft(workspace, "remote-b", V4_DRAFT_SCOPE_ROOT, { text: "", mode: "plan" });
  60. persistV4ComposerDraft(workspace, "remote-a", "session-a", { text: "", mode: "yolo" });
  61. expect(readV4ComposerDraft("/other-path", "remote-a", V4_DRAFT_SCOPE_ROOT)?.mode).toBe("edit");
  62. expect(readV4ComposerDraft(workspace, "remote-b", V4_DRAFT_SCOPE_ROOT)).toMatchObject({
  63. mode: "build",
  64. planEnabled: true,
  65. });
  66. expect(readV4ComposerDraft(workspace, "remote-a", "session-a")?.mode).toBe("yolo");
  67. expect(read()).toBeNull();
  68. });
  69. it("冷启动初始化 Root scope 时保留已有 Session scope", () => {
  70. persistV4ComposerDraft(workspace, undefined, "session-a", {
  71. text: "",
  72. mode: "edit",
  73. modelSelection: {
  74. providerId: "e2e-deepseek",
  75. modelId: "deepseek-v4-flash",
  76. options: { reasoningLevel: "max" },
  77. },
  78. });
  79. persistV4ComposerDraft(workspace, undefined, V4_DRAFT_SCOPE_ROOT, {
  80. text: "",
  81. mode: "build",
  82. modelSelection: {
  83. providerId: "e2e-deepseek",
  84. modelId: "deepseek-v4-flash",
  85. options: { reasoningLevel: "max" },
  86. },
  87. });
  88. expect(read("session-a")).toMatchObject({ mode: "edit" });
  89. expect(read(V4_DRAFT_SCOPE_ROOT)).toMatchObject({ mode: "build" });
  90. });
  91. it("兼容旧文本草稿,按叶子隔离损坏的当前选择", () => {
  92. storage.setItem(
  93. getV4ComposerDraftStorageKey(workspace),
  94. JSON.stringify({
  95. version: 1,
  96. scopes: {
  97. old: { text: "old text", editorStateJson: "{}", updatedAt: 1 },
  98. "session-a": {
  99. text: "keep",
  100. mode: "edit",
  101. modelSelection: { ...selection, options: 7 },
  102. updatedAt: 2,
  103. },
  104. invalid: { text: 42, mode: "edit", updatedAt: 3 },
  105. },
  106. }),
  107. );
  108. expect(read("old")).toEqual({ text: "old text", editorStateJson: "{}", updatedAt: 1 });
  109. expect(read()?.modelSelection).toEqual({
  110. providerId: selection.providerId,
  111. modelId: selection.modelId,
  112. });
  113. expect(read()?.mode).toBe("edit");
  114. expect(read("invalid")).toBeNull();
  115. });
  116. it("长期使用的模型选择不会因 scope 数量被静默淘汰", () => {
  117. let now = 0;
  118. vi.spyOn(Date, "now").mockImplementation(() => ++now);
  119. for (let index = 0; index < 501; index++) {
  120. persistV4ComposerDraft(workspace, undefined, `scope-${index}`, { text: "", mode: "build" });
  121. if (index === 50 || index === 499) expect(read("scope-0")).not.toBeNull();
  122. }
  123. expect(read("scope-0")).not.toBeNull();
  124. expect(read("scope-1")?.mode).toBe("build");
  125. expect(read("scope-500")?.mode).toBe("build");
  126. });
  127. it("存储不可读不能让 Composer 崩溃", () => {
  128. vi.spyOn(storage, "getItem").mockImplementation(() => {
  129. throw new Error("storage unavailable");
  130. });
  131. expect(() => read()).not.toThrow();
  132. expect(read()).toBeNull();
  133. });
  134. });