migration.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { describe, expect, it } from "vitest";
  2. import { type FileEntry, migrateSessionEntries } from "../../src/core/session-manager.ts";
  3. describe("migrateSessionEntries", () => {
  4. it("should add id/parentId to v1 entries", () => {
  5. const entries: FileEntry[] = [
  6. { type: "session", id: "sess-1", timestamp: "2025-01-01T00:00:00Z", cwd: "/tmp" },
  7. { type: "message", timestamp: "2025-01-01T00:00:01Z", message: { role: "user", content: "hi", timestamp: 1 } },
  8. {
  9. type: "message",
  10. timestamp: "2025-01-01T00:00:02Z",
  11. message: {
  12. role: "assistant",
  13. content: [{ type: "text", text: "hello" }],
  14. api: "test",
  15. provider: "test",
  16. model: "test",
  17. usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0 },
  18. stopReason: "stop",
  19. timestamp: 2,
  20. },
  21. },
  22. ] as FileEntry[];
  23. migrateSessionEntries(entries);
  24. // Header should have version set (v3 is current after hookMessage->custom migration)
  25. expect((entries[0] as any).version).toBe(3);
  26. // Entries should have id/parentId
  27. const msg1 = entries[1] as any;
  28. const msg2 = entries[2] as any;
  29. expect(msg1.id).toBeDefined();
  30. expect(msg1.id.length).toBe(8);
  31. expect(msg1.parentId).toBeNull();
  32. expect(msg2.id).toBeDefined();
  33. expect(msg2.id.length).toBe(8);
  34. expect(msg2.parentId).toBe(msg1.id);
  35. });
  36. it("should be idempotent (skip already migrated)", () => {
  37. const entries: FileEntry[] = [
  38. { type: "session", id: "sess-1", version: 2, timestamp: "2025-01-01T00:00:00Z", cwd: "/tmp" },
  39. {
  40. type: "message",
  41. id: "abc12345",
  42. parentId: null,
  43. timestamp: "2025-01-01T00:00:01Z",
  44. message: { role: "user", content: "hi", timestamp: 1 },
  45. },
  46. {
  47. type: "message",
  48. id: "def67890",
  49. parentId: "abc12345",
  50. timestamp: "2025-01-01T00:00:02Z",
  51. message: {
  52. role: "assistant",
  53. content: [{ type: "text", text: "hello" }],
  54. api: "test",
  55. provider: "test",
  56. model: "test",
  57. usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0 },
  58. stopReason: "stop",
  59. timestamp: 2,
  60. },
  61. },
  62. ] as FileEntry[];
  63. migrateSessionEntries(entries);
  64. // IDs should be unchanged
  65. expect((entries[1] as any).id).toBe("abc12345");
  66. expect((entries[2] as any).id).toBe("def67890");
  67. expect((entries[2] as any).parentId).toBe("abc12345");
  68. });
  69. });