runtime-memory-drain.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { AgentRuntime } from "../src/runtime/agent-runtime.js";
  3. afterEach(() => vi.useRealTimers());
  4. describe("runtime Memory Extraction drain", () => {
  5. it("waits past the normal deadline when an explicit null disables the drain timeout", async () => {
  6. vi.useFakeTimers();
  7. const completion = Promise.withResolvers<void>();
  8. let finished = false;
  9. const draining = AgentRuntime.prototype.drainMemoryExtractions
  10. .call({ memoryExtractionScheduler: { drain: () => completion.promise } } as never, null)
  11. .then(() => {
  12. finished = true;
  13. });
  14. await vi.advanceTimersByTimeAsync(120_000);
  15. expect(finished).toBe(false);
  16. completion.resolve();
  17. await draining;
  18. expect(finished).toBe(true);
  19. expect(vi.getTimerCount()).toBe(0);
  20. });
  21. it("preserves the default bounded drain used by session close", async () => {
  22. vi.useFakeTimers();
  23. let finished = false;
  24. const draining = AgentRuntime.prototype.drainMemoryExtractions
  25. .call({ memoryExtractionScheduler: { drain: () => new Promise<void>(() => {}) } } as never)
  26. .then(() => {
  27. finished = true;
  28. });
  29. await vi.advanceTimersByTimeAsync(59_999);
  30. expect(finished).toBe(false);
  31. await vi.advanceTimersByTimeAsync(1);
  32. await draining;
  33. expect(finished).toBe(true);
  34. });
  35. it("returns immediately when there is no scheduled work", async () => {
  36. await expect(
  37. AgentRuntime.prototype.drainMemoryExtractions.call({} as never, null),
  38. ).resolves.toBeUndefined();
  39. });
  40. it.each([
  41. [{ enabled: true, cliStorageRoot: "/storage", use: true }, true],
  42. [{ enabled: false, cliStorageRoot: "/storage", use: true }, false],
  43. [{ enabled: true, cliStorageRoot: "/storage", use: false }, false],
  44. ] as const)("reports effective Memory enablement for preflight", (memory, expected) => {
  45. expect(
  46. AgentRuntime.prototype.isProjectMemoryEnabled.call({
  47. config: { memory },
  48. workspaceRoot: "/workspace",
  49. } as never),
  50. ).toBe(expected);
  51. });
  52. });