record.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { deepStrictEqual, equal } from "node:assert/strict";
  2. import { describe, it } from "node:test";
  3. import { buildRecorderRuntimeConfig, executeFixtureSteps } from "../src/record.js";
  4. describe("buildRecorderRuntimeConfig", () => {
  5. it("passes fixture outputStyle through while keeping deterministic recorder defaults", () => {
  6. const outputStyle = {
  7. keepCodingInstructions: true,
  8. name: "Trajectory Review Style",
  9. prompt: "Keep the answer concise and findings-first.",
  10. };
  11. const runtimeConfig = buildRecorderRuntimeConfig({
  12. fixture: {
  13. name: "output-style",
  14. runtimeConfig: {
  15. mcp: { enabled: true },
  16. mode: "plan",
  17. modelStreaming: "off",
  18. outputStyle,
  19. },
  20. steps: [{ text: "hello", type: "submitPrompt" }],
  21. },
  22. modelSelection: { model: "fake", provider: "openai" },
  23. workingDirectory: "/tmp/zcode-trajectory",
  24. });
  25. deepStrictEqual(runtimeConfig.outputStyle, outputStyle);
  26. equal(runtimeConfig.mode, "plan");
  27. equal(runtimeConfig.modelStreaming, "off");
  28. deepStrictEqual(runtimeConfig.mcp, { enabled: false });
  29. deepStrictEqual(runtimeConfig.memory, { enabled: false });
  30. deepStrictEqual(runtimeConfig.subagents, { enabled: false });
  31. deepStrictEqual(runtimeConfig.titleGeneration, { enabled: false });
  32. });
  33. it("passes prompt-shape runtime config through for custom system fixtures", () => {
  34. const runtimeConfig = buildRecorderRuntimeConfig({
  35. fixture: {
  36. name: "custom-system-prompt",
  37. runtimeConfig: {
  38. currentDate: "2026-06-04",
  39. systemPrompt: "Custom prompt from fixture.",
  40. },
  41. steps: [{ text: "hello", type: "submitPrompt" }],
  42. },
  43. modelSelection: { model: "fake", provider: "openai" },
  44. workingDirectory: "/tmp/zcode-trajectory",
  45. });
  46. equal(runtimeConfig.currentDate, "2026-06-04");
  47. equal(runtimeConfig.systemPrompt, "Custom prompt from fixture.");
  48. });
  49. it("allows fixture runtime config to enable subagents for tool-call trajectory cases", () => {
  50. const runtimeConfig = buildRecorderRuntimeConfig({
  51. fixture: {
  52. name: "subagents",
  53. runtimeConfig: {
  54. subagents: {
  55. enabled: true,
  56. maxTurns: 1,
  57. },
  58. },
  59. steps: [{ text: "hello", type: "submitPrompt" }],
  60. },
  61. modelSelection: { model: "fake", provider: "openai" },
  62. workingDirectory: "/tmp/zcode-trajectory",
  63. });
  64. deepStrictEqual(runtimeConfig.subagents, {
  65. enabled: true,
  66. maxTurns: 1,
  67. });
  68. });
  69. it("waits for runtime events between fixture submitPrompt steps", async () => {
  70. const submitted: string[] = [];
  71. const observedEvents: Array<{ type: string }> = [];
  72. const waiters = new Set<() => void>();
  73. const eventObserver = {
  74. sink: {
  75. onSessionEvent(event: { type: string }) {
  76. observedEvents.push(event);
  77. for (const waiter of waiters) waiter();
  78. },
  79. },
  80. waitForEvent(input: { eventType: string }) {
  81. if (observedEvents.some((event) => event.type === input.eventType)) {
  82. return Promise.resolve();
  83. }
  84. return new Promise<void>((resolve) => {
  85. waiters.add(() => {
  86. if (!observedEvents.some((event) => event.type === input.eventType)) return;
  87. waiters.clear();
  88. resolve();
  89. });
  90. });
  91. },
  92. };
  93. const steps = executeFixtureSteps({
  94. app: {
  95. async submitPrompt(prompt: string) {
  96. submitted.push(prompt);
  97. if (prompt === "first") {
  98. queueMicrotask(() =>
  99. eventObserver.sink.onSessionEvent({ type: "BackgroundTaskCompleted" }),
  100. );
  101. }
  102. },
  103. },
  104. eventObserver,
  105. steps: [
  106. { text: "first", type: "submitPrompt" },
  107. { eventType: "BackgroundTaskCompleted", type: "waitForEvent" },
  108. { text: "second", type: "submitPrompt" },
  109. ],
  110. });
  111. await steps;
  112. deepStrictEqual(submitted, ["first", "second"]);
  113. });
  114. it("passes submitPrompt attachments from fixture steps", async () => {
  115. const submitted: unknown[] = [];
  116. await executeFixtureSteps({
  117. app: {
  118. async submitPrompt(prompt: unknown) {
  119. submitted.push(prompt);
  120. },
  121. },
  122. eventObserver: {
  123. sink: {
  124. onSessionEvent() {},
  125. },
  126. async waitForEvent() {},
  127. },
  128. steps: [
  129. {
  130. attachments: [{ path: "notes.md", type: "file" }],
  131. text: "summarize attachment",
  132. type: "submitPrompt",
  133. },
  134. ],
  135. });
  136. deepStrictEqual(submitted, [
  137. {
  138. attachments: [{ path: "notes.md", type: "file" }],
  139. text: "summarize attachment",
  140. },
  141. ]);
  142. });
  143. });