record.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. language: "Chinese",
  40. systemPrompt: "Custom prompt from fixture.",
  41. },
  42. steps: [{ text: "hello", type: "submitPrompt" }],
  43. },
  44. modelSelection: { model: "fake", provider: "openai" },
  45. workingDirectory: "/tmp/zcode-trajectory",
  46. });
  47. equal(runtimeConfig.currentDate, "2026-06-04");
  48. equal(runtimeConfig.language, "Chinese");
  49. equal(runtimeConfig.systemPrompt, "Custom prompt from fixture.");
  50. });
  51. it("allows fixture runtime config to enable subagents for tool-call trajectory cases", () => {
  52. const runtimeConfig = buildRecorderRuntimeConfig({
  53. fixture: {
  54. name: "subagents",
  55. runtimeConfig: {
  56. subagents: {
  57. enabled: true,
  58. maxTurns: 1,
  59. },
  60. },
  61. steps: [{ text: "hello", type: "submitPrompt" }],
  62. },
  63. modelSelection: { model: "fake", provider: "openai" },
  64. workingDirectory: "/tmp/zcode-trajectory",
  65. });
  66. deepStrictEqual(runtimeConfig.subagents, {
  67. enabled: true,
  68. maxTurns: 1,
  69. });
  70. });
  71. it("waits for runtime events between fixture submitPrompt steps", async () => {
  72. const submitted: string[] = [];
  73. const observedEvents: Array<{ type: string }> = [];
  74. const waiters = new Set<() => void>();
  75. const eventObserver = {
  76. sink: {
  77. onSessionEvent(event: { type: string }) {
  78. observedEvents.push(event);
  79. for (const waiter of waiters) waiter();
  80. },
  81. },
  82. waitForEvent(input: { eventType: string }) {
  83. if (observedEvents.some((event) => event.type === input.eventType)) {
  84. return Promise.resolve();
  85. }
  86. return new Promise<void>((resolve) => {
  87. waiters.add(() => {
  88. if (!observedEvents.some((event) => event.type === input.eventType)) return;
  89. waiters.clear();
  90. resolve();
  91. });
  92. });
  93. },
  94. };
  95. const steps = executeFixtureSteps({
  96. app: {
  97. async submitPrompt(prompt: string) {
  98. submitted.push(prompt);
  99. if (prompt === "first") {
  100. queueMicrotask(() =>
  101. eventObserver.sink.onSessionEvent({ type: "BackgroundTaskCompleted" }),
  102. );
  103. }
  104. },
  105. },
  106. eventObserver,
  107. steps: [
  108. { text: "first", type: "submitPrompt" },
  109. { eventType: "BackgroundTaskCompleted", type: "waitForEvent" },
  110. { text: "second", type: "submitPrompt" },
  111. ],
  112. });
  113. await steps;
  114. deepStrictEqual(submitted, ["first", "second"]);
  115. });
  116. it("passes submitPrompt attachments from fixture steps", async () => {
  117. const submitted: unknown[] = [];
  118. await executeFixtureSteps({
  119. app: {
  120. async submitPrompt(prompt: unknown) {
  121. submitted.push(prompt);
  122. },
  123. },
  124. eventObserver: {
  125. sink: {
  126. onSessionEvent() {},
  127. },
  128. async waitForEvent() {},
  129. },
  130. steps: [
  131. {
  132. attachments: [{ path: "notes.md", type: "file" }],
  133. text: "summarize attachment",
  134. type: "submitPrompt",
  135. },
  136. ],
  137. });
  138. deepStrictEqual(submitted, [
  139. {
  140. attachments: [{ path: "notes.md", type: "file" }],
  141. text: "summarize attachment",
  142. },
  143. ]);
  144. });
  145. });