custom-session-id.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { existsSync, mkdtempSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { basename, join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { SessionManager } from "../../src/core/session-manager.ts";
  6. const UUID_V7_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
  7. describe("SessionManager.newSession with custom id", () => {
  8. it("uses the provided id instead of generating one", () => {
  9. const session = SessionManager.inMemory();
  10. session.newSession({ id: "my-custom-id" });
  11. expect(session.getSessionId()).toBe("my-custom-id");
  12. });
  13. it("allows alphanumeric session ids with interior punctuation", () => {
  14. const session = SessionManager.inMemory();
  15. session.newSession({ id: "abc-123_def.456" });
  16. expect(session.getSessionId()).toBe("abc-123_def.456");
  17. });
  18. it("rejects invalid custom session ids", () => {
  19. const invalidIds = ["", "-abc", "abc-", "_abc", "abc_", ".abc", "abc.", "abc/def", "abc\\def", "abc def"];
  20. for (const id of invalidIds) {
  21. const session = SessionManager.inMemory();
  22. expect(() => session.newSession({ id })).toThrow(
  23. "Session id must be non-empty, contain only alphanumeric characters",
  24. );
  25. }
  26. });
  27. it("generates a UUIDv7 id when no id is provided", () => {
  28. const session = SessionManager.inMemory();
  29. session.newSession();
  30. const id = session.getSessionId();
  31. expect(id).toBeDefined();
  32. expect(id).not.toBe("");
  33. expect(id).toMatch(UUID_V7_RE);
  34. });
  35. it("generates a UUIDv7 id when options is provided without id", () => {
  36. const session = SessionManager.inMemory();
  37. session.newSession({ parentSession: "parent.jsonl" });
  38. const id = session.getSessionId();
  39. expect(id).toBeDefined();
  40. expect(id).not.toBe("");
  41. expect(id).toMatch(UUID_V7_RE);
  42. });
  43. it("includes the custom id in the session header", () => {
  44. const session = SessionManager.inMemory();
  45. session.newSession({ id: "header-test-id" });
  46. const header = session.getHeader();
  47. expect(header).not.toBeNull();
  48. expect(header!.id).toBe("header-test-id");
  49. });
  50. it("generates a UUIDv7 id when constructed without an explicit id", () => {
  51. const session = SessionManager.inMemory();
  52. expect(session.getSessionId()).toMatch(UUID_V7_RE);
  53. expect(session.getHeader()!.id).toBe(session.getSessionId());
  54. });
  55. it("uses the provided id when creating a persisted session", () => {
  56. const tempDir = mkdtempSync(join(tmpdir(), "pi-session-manager-"));
  57. const session = SessionManager.create(tempDir, tempDir, { id: "created-session-id" });
  58. expect(session.getSessionId()).toBe("created-session-id");
  59. expect(session.getHeader()!.id).toBe("created-session-id");
  60. const sessionFile = session.getSessionFile()!;
  61. expect(sessionFile).toContain("created-session-id");
  62. expect(basename(sessionFile)).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z_created-session-id\.jsonl$/);
  63. expect(existsSync(sessionFile)).toBe(false);
  64. });
  65. it("generates a UUIDv7 id when creating a branched session", () => {
  66. const session = SessionManager.inMemory();
  67. const firstId = session.appendMessage({
  68. role: "user",
  69. content: [{ type: "text", text: "hello" }],
  70. timestamp: Date.now(),
  71. });
  72. session.createBranchedSession(firstId);
  73. expect(session.getSessionId()).toMatch(UUID_V7_RE);
  74. expect(session.getHeader()!.id).toBe(session.getSessionId());
  75. });
  76. it("generates a UUIDv7 id when forking from another session file", () => {
  77. const tempDir = mkdtempSync(join(tmpdir(), "pi-session-manager-"));
  78. const sourcePath = join(tempDir, "source.jsonl");
  79. writeFileSync(
  80. sourcePath,
  81. `${[
  82. JSON.stringify({
  83. type: "session",
  84. version: 3,
  85. id: "legacy-session-id",
  86. timestamp: new Date().toISOString(),
  87. cwd: tempDir,
  88. }),
  89. JSON.stringify({
  90. type: "message",
  91. id: "entry-1",
  92. parentId: null,
  93. timestamp: new Date().toISOString(),
  94. message: {
  95. role: "assistant",
  96. content: [{ type: "text", text: "hello" }],
  97. api: "openai-responses",
  98. provider: "openai",
  99. model: "gpt-5.4",
  100. usage: {
  101. input: 0,
  102. output: 0,
  103. cacheRead: 0,
  104. cacheWrite: 0,
  105. totalTokens: 0,
  106. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  107. },
  108. stopReason: "stop",
  109. timestamp: Date.now(),
  110. },
  111. }),
  112. ].join("\n")}
  113. `,
  114. );
  115. const forked = SessionManager.forkFrom(sourcePath, tempDir, tempDir);
  116. const header = forked.getHeader();
  117. expect(header).not.toBeNull();
  118. expect(header!.id).toMatch(UUID_V7_RE);
  119. expect(header!.parentSession).toBe(sourcePath);
  120. });
  121. it("uses the provided id when forking from another session file", () => {
  122. const tempDir = mkdtempSync(join(tmpdir(), "pi-session-manager-"));
  123. const sourcePath = join(tempDir, "source.jsonl");
  124. writeFileSync(
  125. sourcePath,
  126. `${JSON.stringify({
  127. type: "session",
  128. version: 3,
  129. id: "source-session-id",
  130. timestamp: new Date().toISOString(),
  131. cwd: tempDir,
  132. })}\n`,
  133. );
  134. const forked = SessionManager.forkFrom(sourcePath, tempDir, tempDir, { id: "forked-session-id" });
  135. const header = forked.getHeader();
  136. expect(header).not.toBeNull();
  137. expect(header!.id).toBe("forked-session-id");
  138. expect(header!.parentSession).toBe(sourcePath);
  139. const sessionFile = forked.getSessionFile()!;
  140. expect(sessionFile).toContain("forked-session-id");
  141. expect(basename(sessionFile)).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z_forked-session-id\.jsonl$/);
  142. });
  143. });