official-glm-selection-migration.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { DatabaseSync } from "node:sqlite";
  2. import { expect, it } from "vitest";
  3. import { OFFICIAL_GLM_SELECTION_MIGRATION_SQL } from "../src/storage/session-store/migrations/0021-official-glm-selection.js";
  4. it("只规范化当前官方选择,旧字段、未知/自定义身份、坏 JSON 均不动,重复执行幂等", () => {
  5. const db = new DatabaseSync(":memory:");
  6. try {
  7. db.exec("CREATE TABLE session_entry(id TEXT, type TEXT, data TEXT)");
  8. const insert = db.prepare("INSERT INTO session_entry VALUES(?,?,?)");
  9. for (const [id, providerId, modelId] of [
  10. ["official", "account:zai-start-plan", "glm-5.3-flash"],
  11. ["custom", "my-provider", "glm-5.3-flash"],
  12. ["unknown", "account:zai-start-plan", "glm-future"],
  13. ["ticket", "account:zai-off-peak", "glm-5.3-flash"],
  14. ])
  15. insert.run(
  16. id,
  17. "runtime/model_selection",
  18. JSON.stringify({ modelId, modelSelection: { providerId, modelId } }),
  19. );
  20. insert.run("bad", "runtime/model_selection", "{");
  21. insert.run(
  22. "other",
  23. "other",
  24. JSON.stringify({
  25. modelSelection: { providerId: "account:zai-start-plan", modelId: "glm-5.3-flash" },
  26. }),
  27. );
  28. const before = db.prepare("SELECT * FROM session_entry").all();
  29. db.exec(OFFICIAL_GLM_SELECTION_MIGRATION_SQL);
  30. const after = db.prepare("SELECT * FROM session_entry").all();
  31. expect(JSON.parse(String(after[0]!.data))).toEqual({
  32. modelId: "glm-5.3-flash",
  33. modelSelection: { providerId: "account:zai-start-plan", modelId: "GLM-5.3-Flash" },
  34. });
  35. expect(after.slice(1)).toEqual(before.slice(1));
  36. db.exec(OFFICIAL_GLM_SELECTION_MIGRATION_SQL);
  37. expect(db.prepare("SELECT * FROM session_entry").all()).toEqual(after);
  38. } finally {
  39. db.close();
  40. }
  41. });