botsStorageMigration.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { botsStateFileSchema } from "@zcode/shared";
  2. import { describe, expect, it } from "vitest";
  3. import { importLegacyBotState } from "../src/bots/storageMigration.js";
  4. async function migrate(options: unknown) {
  5. const raw = {
  6. version: 2,
  7. bots: {
  8. bot: {
  9. botId: "bot",
  10. workspacePath: "/workspace",
  11. mode: "draft",
  12. activeTaskId: null,
  13. draftOptions: { provider: "glm", ...(options as object) },
  14. updatedAt: 1,
  15. },
  16. },
  17. };
  18. return botsStateFileSchema.parse(importLegacyBotState(raw)).bots.bot?.draftOptions;
  19. }
  20. describe("Bot 一次性旧身份转换", () => {
  21. it("官方 GLM 旧选择规范大小写,自定义同名模型不改", async () => {
  22. for (const providerId of ["builtin:zai-start-plan", "personal-proxy"]) {
  23. expect(
  24. (await migrate({ model: `${providerId}/glm-5.3-flash`, thoughtLevel: "high" }))
  25. ?.modelSelection,
  26. ).toEqual({
  27. providerId: providerId === "personal-proxy" ? providerId : "account:zai-start-plan",
  28. modelId: providerId === "personal-proxy" ? "glm-5.3-flash" : "GLM-5.3-Flash",
  29. options: { reasoningLevel: "high" },
  30. });
  31. }
  32. });
  33. it.each(["deepseek/model", "custom:deepseek:model"])("保留明确的旧身份 %s", async (model) => {
  34. expect((await migrate({ model, thoughtLevel: "high" }))?.modelSelection).toEqual({
  35. providerId: "deepseek",
  36. modelId: "model",
  37. options: { reasoningLevel: "high" },
  38. });
  39. });
  40. it.each(["model", "builtin:zapi/model"])("不按模型名猜供应商 %s", async (model) => {
  41. expect((await migrate({ model }))?.modelSelection).toBeUndefined();
  42. });
  43. it.each(["missing/model", "custom:missing:model", "glm/model"])(
  44. "候选没有 %s 也保留明确身份",
  45. async (model) => {
  46. const providerId = model.startsWith("glm/") ? "glm" : "missing";
  47. expect((await migrate({ model, thoughtLevel: "future-level" }))?.modelSelection).toEqual({
  48. providerId,
  49. modelId: "model",
  50. options: { reasoningLevel: "future-level" },
  51. });
  52. },
  53. );
  54. it("已有新选择不让旧字段覆盖;损坏新选择也不回读旧字段", async () => {
  55. const modelSelection = {
  56. providerId: "deepseek",
  57. modelId: "model",
  58. options: { reasoningLevel: "low" },
  59. };
  60. expect((await migrate({ modelSelection, thoughtLevel: "high" }))?.modelSelection).toEqual(
  61. modelSelection,
  62. );
  63. expect(
  64. (await migrate({ modelSelection: null, model: "deepseek/model", thoughtLevel: "high" }))
  65. ?.modelSelection,
  66. ).toBeUndefined();
  67. });
  68. it("旧 Coding Plan 固定迁为 Individual,当前连接和候选均不参与", async () => {
  69. const options = { model: "builtin:bigmodel-coding-plan/GLM", thoughtLevel: "high" };
  70. expect((await migrate(options))?.modelSelection?.providerId).toBe(
  71. "account:bigmodel-individual-coding-plan",
  72. );
  73. expect(
  74. (
  75. await migrate({
  76. modelSelection: { providerId: "builtin:bigmodel-coding-plan", modelId: "GLM" },
  77. })
  78. )?.modelSelection?.providerId,
  79. ).toBe("account:bigmodel-individual-coding-plan");
  80. });
  81. });