project-memory-retrieval-branch.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. resolveProjectMemoryRetrievalBranch,
  4. type ProjectMemoryRetrievalBranch,
  5. } from "../src/memory/project-memory-retrieval-branch.js";
  6. import { loadProjectMemoryIndexContent } from "../src/runtime/methods/context.js";
  7. import { MemoryFileSystem } from "./memory-test-utils.js";
  8. describe("project Memory retrieval branch", () => {
  9. it.each<{ enabled: boolean; expected: ProjectMemoryRetrievalBranch }>([
  10. { enabled: false, expected: "default-index" },
  11. { enabled: true, expected: "semantic-recall" },
  12. ])("maps $enabled to $expected", ({ enabled, expected }) => {
  13. expect(resolveProjectMemoryRetrievalBranch(enabled)).toBe(expected);
  14. });
  15. it("loads and seeds MEMORY.md only for the default-index branch", async () => {
  16. const memoryRoot = "/storage/memories/projects/project-0123456789abcdef/memory";
  17. const indexPath = `${memoryRoot}/MEMORY.md`;
  18. const content = "- [Database policy](database-policy.md) — use the real database";
  19. const defaultFileSystem = new MemoryFileSystem({ [indexPath]: content });
  20. const defaultReadFileState = new Map();
  21. const defaultRuntime = {
  22. fileSystemPort: defaultFileSystem,
  23. now: () => new Date("2026-07-22T00:00:00.000Z"),
  24. readFileState: defaultReadFileState,
  25. } as Parameters<typeof loadProjectMemoryIndexContent>[0];
  26. await expect(
  27. loadProjectMemoryIndexContent(defaultRuntime, memoryRoot, "default-index"),
  28. ).resolves.toBe(content);
  29. expect(defaultFileSystem.readRequests).toEqual([{ maxBytes: undefined, path: indexPath }]);
  30. expect(defaultReadFileState.size).toBe(1);
  31. const semanticFileSystem = new MemoryFileSystem({ [indexPath]: content });
  32. const semanticReadFileState = new Map();
  33. const semanticRuntime = {
  34. fileSystemPort: semanticFileSystem,
  35. now: () => new Date("2026-07-22T00:00:00.000Z"),
  36. readFileState: semanticReadFileState,
  37. } as Parameters<typeof loadProjectMemoryIndexContent>[0];
  38. await expect(
  39. loadProjectMemoryIndexContent(semanticRuntime, memoryRoot, "semantic-recall"),
  40. ).resolves.toBeUndefined();
  41. expect(semanticFileSystem.readRequests).toEqual([]);
  42. expect(semanticReadFileState.size).toBe(0);
  43. });
  44. it.each([
  45. { content: "", name: "empty" },
  46. { content: "---\ninternal: hidden\n---\n", name: "frontmatter-only" },
  47. { content: "<!-- hidden top-level comment -->\n", name: "top-level-comment-only" },
  48. ])("does not seed read state for $name MEMORY.md", async ({ content }) => {
  49. const memoryRoot = "/storage/memories/projects/project-0123456789abcdef/memory";
  50. const indexPath = `${memoryRoot}/MEMORY.md`;
  51. const fileSystem = new MemoryFileSystem({ [indexPath]: content });
  52. const readFileState = new Map();
  53. const runtime = {
  54. fileSystemPort: fileSystem,
  55. now: () => new Date("2026-07-22T00:00:00.000Z"),
  56. readFileState,
  57. } as Parameters<typeof loadProjectMemoryIndexContent>[0];
  58. await expect(
  59. loadProjectMemoryIndexContent(runtime, memoryRoot, "default-index"),
  60. ).resolves.toBeUndefined();
  61. expect(fileSystem.readRequests).toEqual([{ maxBytes: undefined, path: indexPath }]);
  62. expect(readFileState.size).toBe(0);
  63. });
  64. });