session-info-modified-timestamp.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { writeFileSync } from "node:fs";
  2. import { stat } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { dirname, join } from "node:path";
  5. import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
  6. import type { SessionHeader } from "../src/core/session-manager.ts";
  7. import { SessionManager } from "../src/core/session-manager.ts";
  8. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  9. function createSessionFile(path: string): void {
  10. const header: SessionHeader = {
  11. type: "session",
  12. id: "test-session",
  13. version: 3,
  14. timestamp: new Date(0).toISOString(),
  15. cwd: "/tmp",
  16. };
  17. writeFileSync(path, `${JSON.stringify(header)}\n`, "utf8");
  18. // SessionManager only persists once it has seen at least one assistant message.
  19. // Add a minimal assistant entry so subsequent appends are persisted.
  20. const mgr = SessionManager.open(path);
  21. mgr.appendMessage({
  22. role: "assistant",
  23. content: [{ type: "text", text: "hi" }],
  24. api: "openai-completions",
  25. provider: "openai",
  26. model: "test",
  27. usage: {
  28. input: 1,
  29. output: 1,
  30. cacheRead: 0,
  31. cacheWrite: 0,
  32. totalTokens: 2,
  33. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  34. },
  35. stopReason: "stop",
  36. timestamp: Date.now(),
  37. });
  38. }
  39. describe("SessionInfo.modified", () => {
  40. beforeAll(() => initTheme("dark"));
  41. afterEach(() => {
  42. vi.restoreAllMocks();
  43. });
  44. it("uses last user/assistant message timestamp instead of file mtime", async () => {
  45. const filePath = join(tmpdir(), `pi-session-${Date.now()}-modified.jsonl`);
  46. createSessionFile(filePath);
  47. const before = await stat(filePath);
  48. // Ensure the file mtime can differ from our message timestamp even on coarse filesystems.
  49. await new Promise((r) => setTimeout(r, 10));
  50. const mgr = SessionManager.open(filePath);
  51. const msgTime = Date.now();
  52. mgr.appendMessage({
  53. role: "assistant",
  54. content: [{ type: "text", text: "later" }],
  55. api: "openai-completions",
  56. provider: "openai",
  57. model: "test",
  58. usage: {
  59. input: 1,
  60. output: 1,
  61. cacheRead: 0,
  62. cacheWrite: 0,
  63. totalTokens: 2,
  64. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  65. },
  66. stopReason: "stop",
  67. timestamp: msgTime,
  68. });
  69. const sessions = await SessionManager.list("/tmp", dirname(filePath));
  70. const s = sessions.find((x) => x.path === filePath);
  71. expect(s).toBeDefined();
  72. expect(s!.modified.getTime()).toBe(msgTime);
  73. expect(s!.modified.getTime()).not.toBe(before.mtime.getTime());
  74. });
  75. });