session-cwd.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { mkdirSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { type CreateAgentSessionRuntimeFactory, createAgentSessionRuntime } from "../src/core/agent-session-runtime.ts";
  6. import { getMissingSessionCwdIssue, MissingSessionCwdError } from "../src/core/session-cwd.ts";
  7. import { SessionManager } from "../src/core/session-manager.ts";
  8. function createTempDir(name: string): string {
  9. const dir = join(tmpdir(), `${name}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
  10. mkdirSync(dir, { recursive: true });
  11. return dir;
  12. }
  13. function writeSessionFile(path: string, cwd: string): void {
  14. writeFileSync(
  15. path,
  16. `${JSON.stringify({
  17. type: "session",
  18. version: 3,
  19. id: "session-id",
  20. timestamp: new Date().toISOString(),
  21. cwd,
  22. })}\n`,
  23. );
  24. }
  25. describe("session cwd handling", () => {
  26. const cleanupPaths: string[] = [];
  27. afterEach(() => {
  28. for (const path of cleanupPaths.splice(0)) {
  29. rmSync(path, { recursive: true, force: true });
  30. }
  31. });
  32. it("detects missing session cwd from persisted sessions", () => {
  33. const fallbackCwd = createTempDir("pi-session-cwd-fallback");
  34. const missingCwd = join(fallbackCwd, "does-not-exist");
  35. const sessionDir = createTempDir("pi-session-cwd-session-dir");
  36. const sessionFile = join(sessionDir, "session.jsonl");
  37. cleanupPaths.push(fallbackCwd, sessionDir);
  38. writeSessionFile(sessionFile, missingCwd);
  39. const sessionManager = SessionManager.open(sessionFile);
  40. const issue = getMissingSessionCwdIssue(sessionManager, fallbackCwd);
  41. expect(issue).toEqual({
  42. sessionFile: sessionManager.getSessionFile(),
  43. sessionCwd: missingCwd,
  44. fallbackCwd,
  45. });
  46. });
  47. it("supports overriding the effective cwd when opening a session", () => {
  48. const fallbackCwd = createTempDir("pi-session-cwd-override");
  49. const missingCwd = join(fallbackCwd, "does-not-exist");
  50. const sessionDir = createTempDir("pi-session-cwd-override-session-dir");
  51. const sessionFile = join(sessionDir, "session.jsonl");
  52. cleanupPaths.push(fallbackCwd, sessionDir);
  53. writeSessionFile(sessionFile, missingCwd);
  54. const sessionManager = SessionManager.open(sessionFile, undefined, fallbackCwd);
  55. expect(sessionManager.getCwd()).toBe(fallbackCwd);
  56. expect(getMissingSessionCwdIssue(sessionManager, fallbackCwd)).toBeUndefined();
  57. });
  58. it("throws a controlled error before runtime creation when the stored cwd is missing", async () => {
  59. const fallbackCwd = createTempDir("pi-session-cwd-runtime");
  60. const missingCwd = join(fallbackCwd, "does-not-exist");
  61. const sessionDir = createTempDir("pi-session-cwd-runtime-session-dir");
  62. const sessionFile = join(sessionDir, "session.jsonl");
  63. cleanupPaths.push(fallbackCwd, sessionDir);
  64. writeSessionFile(sessionFile, missingCwd);
  65. const sessionManager = SessionManager.open(sessionFile);
  66. let createRuntimeCalled = false;
  67. const createRuntime: CreateAgentSessionRuntimeFactory = async () => {
  68. createRuntimeCalled = true;
  69. throw new Error("should not be called");
  70. };
  71. await expect(
  72. createAgentSessionRuntime(createRuntime, {
  73. cwd: fallbackCwd,
  74. agentDir: fallbackCwd,
  75. sessionManager,
  76. }),
  77. ).rejects.toBeInstanceOf(MissingSessionCwdError);
  78. expect(createRuntimeCalled).toBe(false);
  79. });
  80. });