repo.test.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { existsSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import { NodeExecutionEnv } from "../../src/harness/env/nodejs.ts";
  4. import { JsonlSessionRepo } from "../../src/harness/session/jsonl-repo.ts";
  5. import { InMemorySessionRepo } from "../../src/harness/session/memory-repo.ts";
  6. import { createAssistantMessage, createTempDir, createUserMessage } from "./session-test-utils.ts";
  7. describe("InMemorySessionRepo", () => {
  8. it("opens, deletes, and forks by metadata", async () => {
  9. const repo = new InMemorySessionRepo();
  10. const session = await repo.create({ id: "session-1" });
  11. const metadata = await session.getMetadata();
  12. const user1 = await session.appendMessage(createUserMessage("one"));
  13. const assistant1 = await session.appendMessage(createAssistantMessage("two"));
  14. const user2 = await session.appendMessage(createUserMessage("three"));
  15. expect(await repo.open(metadata)).toBe(session);
  16. expect((await repo.list()).map((info) => info.id)).toEqual(["session-1"]);
  17. const fork = await repo.fork(metadata, { entryId: user2, id: "session-2" });
  18. expect((await fork.getEntries()).map((entry) => entry.id)).toEqual([user1, assistant1]);
  19. const fullFork = await repo.fork(metadata, { id: "session-3" });
  20. expect((await fullFork.getEntries()).map((entry) => entry.id)).toEqual([user1, assistant1, user2]);
  21. await repo.delete(metadata);
  22. await expect(repo.open(metadata)).rejects.toThrow("Session not found: session-1");
  23. });
  24. });
  25. describe("JsonlSessionRepo", () => {
  26. it("stores sessions below encoded cwd directories and lists by cwd", async () => {
  27. const root = createTempDir();
  28. const env = new NodeExecutionEnv({ cwd: root });
  29. const cwd = "/tmp/my-project";
  30. const otherCwd = "/tmp/other-project";
  31. const repo = new JsonlSessionRepo({ fs: env, sessionsRoot: root });
  32. const session = await repo.create({ cwd, id: "019de8c2-de29-73e9-ae0c-e134db34c447" });
  33. const otherSession = await repo.create({ cwd: otherCwd, id: "other-session" });
  34. const metadata = await session.getMetadata();
  35. const otherMetadata = await otherSession.getMetadata();
  36. expect(metadata.path).toContain("--tmp-my-project--");
  37. expect(otherMetadata.path).toContain("--tmp-other-project--");
  38. expect(existsSync(metadata.path)).toBe(true);
  39. expect((await repo.list({ cwd })).map((sessionMetadata) => sessionMetadata.id)).toEqual([metadata.id]);
  40. expect((await repo.list()).map((sessionMetadata) => sessionMetadata.id).sort()).toEqual(
  41. [metadata.id, otherMetadata.id].sort(),
  42. );
  43. });
  44. it("opens, deletes, and forks by metadata", async () => {
  45. const root = createTempDir();
  46. const env = new NodeExecutionEnv({ cwd: root });
  47. const repo = new JsonlSessionRepo({ fs: env, sessionsRoot: root });
  48. const source = await repo.create({ cwd: "/tmp/source", id: "source-session" });
  49. const sourceMetadata = await source.getMetadata();
  50. const user1 = await source.appendMessage(createUserMessage("one"));
  51. const assistant1 = await source.appendMessage(createAssistantMessage("two"));
  52. const user2 = await source.appendMessage(createUserMessage("three"));
  53. await expect((await repo.open(sourceMetadata)).getMetadata()).resolves.toEqual(sourceMetadata);
  54. const fork = await repo.fork(sourceMetadata, { cwd: "/tmp/target", id: "fork-session", entryId: user2 });
  55. const forkMetadata = await fork.getMetadata();
  56. expect(forkMetadata.cwd).toBe("/tmp/target");
  57. expect(forkMetadata.parentSessionPath).toBe(sourceMetadata.path);
  58. expect((await fork.getEntries()).map((entry) => entry.id)).toEqual([user1, assistant1]);
  59. const fullFork = await repo.fork(sourceMetadata, { cwd: "/tmp/target", id: "full-fork-session" });
  60. expect((await fullFork.getEntries()).map((entry) => entry.id)).toEqual([user1, assistant1, user2]);
  61. await repo.delete(sourceMetadata);
  62. expect(existsSync(sourceMetadata.path)).toBe(false);
  63. await expect(repo.open(sourceMetadata)).rejects.toThrow("Session not found");
  64. });
  65. });