session-store-model-selection-codec.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. decodeMessageRow,
  4. decodePartRow,
  5. decodeSessionEntryRow,
  6. isCollaborationMode,
  7. } from "../src/storage/session-store/codecs.js";
  8. describe("session message model selection codec", () => {
  9. it("持久 mode codec 保留 guarded,不将其丢弃为默认值", () => {
  10. expect(isCollaborationMode("guarded")).toBe(true);
  11. expect(isCollaborationMode("unsupported")).toBe(false);
  12. });
  13. it("Timeline 当前 Selection 不从旧大写身份补值,也不暴露旧别名", () => {
  14. const part = decodePartRow({
  15. id: "part-current",
  16. message_id: "message-1",
  17. session_id: "session-1",
  18. sequence: 1,
  19. time_created: 1,
  20. time_updated: 1,
  21. data: JSON.stringify({
  22. type: "timeline",
  23. timelineType: "model_change",
  24. display: "separator",
  25. toModel: {
  26. providerID: "old-provider",
  27. modelID: "old-model",
  28. variant: "high",
  29. },
  30. toModelSelection: { providerId: "current" },
  31. }),
  32. });
  33. expect(part).not.toHaveProperty("toModel");
  34. expect(JSON.stringify(part)).not.toContain("old-provider");
  35. expect(JSON.stringify(part)).not.toContain("old-model");
  36. });
  37. it.each([null, {}, { providerId: "new-provider" }])(
  38. "当前 user Selection 即使不完整也不回读旧 model:%j",
  39. (modelSelection) => {
  40. const message = decodeMessageRow({
  41. id: "message-new",
  42. session_id: "session-1",
  43. sequence: 1,
  44. time_created: 1,
  45. time_updated: 1,
  46. data: JSON.stringify({
  47. role: "user",
  48. modelSelection,
  49. model: { providerID: "old-provider", modelID: "old-model", variant: "high" },
  50. }),
  51. });
  52. expect(message).not.toHaveProperty("model");
  53. expect(message).not.toHaveProperty("modelSelection");
  54. expect(JSON.stringify(message)).not.toContain("old-provider");
  55. },
  56. );
  57. it.each([
  58. undefined,
  59. null,
  60. {},
  61. { providerId: "p" },
  62. { providerId: "p", modelId: "m", options: { reasoningLevel: 3 } },
  63. ])("无效 Subtask 选择不阻断正文读取:%j", (modelSelection) => {
  64. const part = decodePartRow({
  65. id: "part-subtask",
  66. message_id: "message-1",
  67. session_id: "session-1",
  68. sequence: 1,
  69. time_created: 1,
  70. time_updated: 1,
  71. data: JSON.stringify({
  72. type: "subtask",
  73. prompt: "保留子任务正文",
  74. description: "说明",
  75. agent: "worker",
  76. modelSelection,
  77. }),
  78. });
  79. expect(part).toMatchObject({ prompt: "保留子任务正文" });
  80. expect(part).not.toHaveProperty("model");
  81. });
  82. it("有效但当前不存在的 Timeline 模型与 label 保留,不检查 Registry", () => {
  83. const toModelSelection = {
  84. providerId: "removed-provider",
  85. modelId: "removed-model",
  86. label: "历史模型",
  87. };
  88. const part = decodePartRow({
  89. id: "part-timeline",
  90. message_id: "message-1",
  91. session_id: "session-1",
  92. sequence: 1,
  93. time_created: 1,
  94. time_updated: 1,
  95. data: JSON.stringify({ type: "timeline", timelineType: "model_change", toModelSelection }),
  96. });
  97. expect(part).toMatchObject({ toModel: toModelSelection });
  98. });
  99. it("当前 assistant 身份字段存在但缺失时,不从旧大写字段补值", () => {
  100. const message = decodeMessageRow({
  101. id: "message-new",
  102. session_id: "session-1",
  103. sequence: 1,
  104. time_created: 1,
  105. time_updated: 1,
  106. data: JSON.stringify({
  107. role: "assistant",
  108. providerId: "",
  109. modelId: "current",
  110. providerID: "old-provider",
  111. modelID: "old-model",
  112. variant: "high",
  113. }),
  114. });
  115. expect(message).not.toHaveProperty("providerID");
  116. expect(message).not.toHaveProperty("variant");
  117. expect(JSON.stringify(message)).not.toContain("old-provider");
  118. });
  119. it.each([{}, { reasoningLevel: "low" }, { reasoningLevel: 3 }])(
  120. "当前 entry options 优先,不能用 thoughtLevel 修补:%j",
  121. (options) => {
  122. const entry = decodeSessionEntryRow({
  123. id: "entry-current",
  124. session_id: "session-1",
  125. type: "runtime/model_selection",
  126. time_created: 1,
  127. time_updated: 1,
  128. data: JSON.stringify({
  129. providerId: "legacy-provider",
  130. modelId: "legacy-model",
  131. modelSelection: { providerId: "provider-a", modelId: "model-a", options },
  132. thoughtLevel: "high",
  133. }),
  134. });
  135. expect(entry.data).not.toHaveProperty("thoughtLevel");
  136. expect(entry.data).toMatchObject({ providerId: "provider-a", modelId: "model-a", options });
  137. },
  138. );
  139. it("已记账再升级缺新 User 字段时,不在 Reader 重新导入旧选择", () => {
  140. const message = decodeMessageRow({
  141. id: "message-1",
  142. session_id: "session-1",
  143. sequence: 1,
  144. time_created: 1,
  145. time_updated: 1,
  146. data: JSON.stringify({
  147. role: "user",
  148. time: { created: 1 },
  149. agent: "zcode-agent",
  150. model: { providerID: "provider-a", modelID: "model-a", variant: "high" },
  151. }),
  152. });
  153. expect(message).toMatchObject({ role: "user", agent: "zcode-agent" });
  154. expect(message).not.toHaveProperty("modelSelection");
  155. expect(message).not.toHaveProperty("model");
  156. });
  157. it("已记账再升级缺新 Assistant 字段时,不在 Reader 重新导入来源", () => {
  158. const message = decodeMessageRow({
  159. id: "message-2",
  160. session_id: "session-1",
  161. sequence: 2,
  162. time_created: 2,
  163. time_updated: 2,
  164. data: JSON.stringify({
  165. role: "assistant",
  166. time: { created: 2 },
  167. parentID: "message-1",
  168. providerID: "provider-a",
  169. modelID: "model-a",
  170. variant: "high",
  171. mode: "build",
  172. agent: "zcode-agent",
  173. path: { cwd: "/workspace", root: "/workspace" },
  174. cost: 0,
  175. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  176. }),
  177. });
  178. expect(message).toMatchObject({ role: "assistant", parentID: "message-1" });
  179. expect(message).not.toHaveProperty("providerId");
  180. expect(message).not.toHaveProperty("modelId");
  181. expect(message).not.toHaveProperty("providerID");
  182. expect(message).not.toHaveProperty("modelID");
  183. expect(message).not.toHaveProperty("variant");
  184. });
  185. it("已记账再升级缺新 Timeline 字段时,保留内容类型但不重新导入模型", () => {
  186. const part = decodePartRow({
  187. id: "part-1",
  188. message_id: "message-2",
  189. session_id: "session-1",
  190. sequence: 1,
  191. time_created: 2,
  192. time_updated: 2,
  193. data: JSON.stringify({
  194. type: "timeline",
  195. timelineType: "model_change",
  196. display: "separator",
  197. fromModel: { providerID: "provider-a", modelID: "model-a", variant: "low" },
  198. toModel: {
  199. providerID: "provider-b",
  200. modelID: "model-b",
  201. variant: "high",
  202. label: "Model B",
  203. },
  204. }),
  205. });
  206. expect(part).toMatchObject({ type: "timeline", timelineType: "model_change" });
  207. expect(part).not.toHaveProperty("fromModel");
  208. expect(part).not.toHaveProperty("toModel");
  209. });
  210. it("普通 entry reader 不再导入旧 thoughtLevel,必须先经过一次性迁移", () => {
  211. const entry = decodeSessionEntryRow({
  212. id: "entry-1",
  213. session_id: "session-1",
  214. type: "runtime/model_selection",
  215. time_created: 1,
  216. time_updated: 1,
  217. data: JSON.stringify({
  218. providerId: "provider-a",
  219. modelId: "model-a",
  220. thoughtLevel: "high",
  221. }),
  222. });
  223. expect(entry.data).toBeUndefined();
  224. });
  225. });