plan-file-continuity.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createRootTraceContext,
  4. createSessionId,
  5. createTurnId,
  6. } from "@zcode/contracts";
  7. import { MemoryFileSystem } from "./memory-test-utils.js";
  8. import {
  9. formatPlanFileReference,
  10. readApprovedPlanFileReferenceEntry,
  11. resolveApprovedPlanFilePath,
  12. writeApprovedPlanFile,
  13. } from "../src/runtime/helpers/plan-file-continuity.js";
  14. describe("plan file continuity", () => {
  15. it("resolves approved plan file under workspace root with sanitized session id", () => {
  16. const path = resolveApprovedPlanFilePath({
  17. sessionId: "session:with/slashes",
  18. workspaceRoot: "/workspace/project",
  19. });
  20. expect(path.replace(/\\/g, "/")).toBe(
  21. "/workspace/project/.zcode/plans/plan-session-with-slashes.md",
  22. );
  23. });
  24. it("writes the approved plan byte-for-byte through FileSystemPort using the deterministic path", async () => {
  25. const fileSystemPort = new MemoryFileSystem({});
  26. const sessionId = createSessionId("plan-file-write");
  27. const traceContext = createRootTraceContext({
  28. sessionId,
  29. turnId: createTurnId("turn-plan-file-write"),
  30. });
  31. const approvedPlan = "\n 1. Read current code\n2. Add compact reminder\n\n";
  32. const result = await writeApprovedPlanFile({
  33. fileSystemPort,
  34. plan: approvedPlan,
  35. sessionId,
  36. traceContext,
  37. workspaceRoot: "/workspace/project",
  38. });
  39. expect(result.path.replace(/\\/g, "/")).toBe(
  40. "/workspace/project/.zcode/plans/plan-sess_plan-file-write.md",
  41. );
  42. expect(fileSystemPort.files[result.path]).toBe(approvedPlan);
  43. });
  44. it("formats the plan_file_reference reminder body", () => {
  45. expect(
  46. formatPlanFileReference({
  47. planContent: "1. Keep the approved plan\n2. Verify after compact",
  48. planFilePath: "/workspace/project/.zcode/plans/plan-session.md",
  49. }),
  50. ).toBe(
  51. [
  52. "A plan file exists from plan mode at: /workspace/project/.zcode/plans/plan-session.md",
  53. "",
  54. "Plan contents:",
  55. "",
  56. "1. Keep the approved plan\n2. Verify after compact",
  57. "",
  58. "If this plan is relevant to the current work and not already complete, continue working on it.",
  59. ].join("\n"),
  60. );
  61. });
  62. it("returns a model-only plan_file_reference entry when the plan file exists", async () => {
  63. const sessionId = createSessionId("plan-file-reference");
  64. const fileSystemPort = new MemoryFileSystem({
  65. "/workspace/project/.zcode/plans/plan-sess_plan-file-reference.md":
  66. "1. Preserve plan marker E2E_PLAN_FILE_CONTINUITY",
  67. });
  68. const entry = await readApprovedPlanFileReferenceEntry({
  69. fileSystemPort,
  70. sessionId,
  71. workspaceRoot: "/workspace/project",
  72. });
  73. expect(entry).toMatchObject({
  74. content: expect.stringContaining("A plan file exists from plan mode at:"),
  75. metadata: { source: "plan_file_reference" },
  76. });
  77. expect(entry?.content).toContain("Plan contents:");
  78. expect(entry?.content).toContain("E2E_PLAN_FILE_CONTINUITY");
  79. expect(entry?.content).toContain(
  80. "If this plan is relevant to the current work and not already complete, continue working on it.",
  81. );
  82. });
  83. it("returns undefined when the deterministic plan file does not exist", async () => {
  84. const entry = await readApprovedPlanFileReferenceEntry({
  85. fileSystemPort: new MemoryFileSystem({}),
  86. sessionId: createSessionId("no-plan-file"),
  87. workspaceRoot: "/workspace/project",
  88. });
  89. expect(entry).toBeUndefined();
  90. });
  91. });