backgroundBashJobs.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { describe, expect, it } from "vitest";
  2. import { parseZCodeBackgroundBashJobs } from "../src/background-bash-jobs.js";
  3. import { mergeZCodeBackgroundTaskControlItems } from "../src/background-task-control-merge.js";
  4. import { parseZCodeBackgroundTaskControlItems } from "../src/background-task-controls.js";
  5. describe("background bash jobs", () => {
  6. it("后台 Bash 取消 ID 优先使用 protocol taskId", () => {
  7. const jobs = parseZCodeBackgroundBashJobs([
  8. {
  9. taskId: "exec_background_123",
  10. id: "call_like_generic_id",
  11. toolCallId: "call_00_abc",
  12. toolName: "Bash",
  13. command: "node background-ticker.js",
  14. status: "running",
  15. pid: 5542,
  16. },
  17. ]);
  18. expect(jobs).toHaveLength(1);
  19. expect(jobs[0]).toMatchObject({
  20. jobId: "exec_background_123",
  21. toolCallId: "call_00_abc",
  22. command: "node background-ticker.js",
  23. taskKind: "bash",
  24. status: "running",
  25. pid: 5542,
  26. });
  27. });
  28. it("Bash-only parser does not include background Agent tasks", () => {
  29. const jobs = parseZCodeBackgroundBashJobs([
  30. {
  31. taskId: "agent_background_123",
  32. toolCallId: "call_agent_00",
  33. toolName: "Agent",
  34. description: "Review the branch",
  35. status: "running",
  36. },
  37. ]);
  38. expect(jobs).toEqual([]);
  39. });
  40. it("parses running background Agent tasks for the shared background control surface", () => {
  41. const jobs = parseZCodeBackgroundTaskControlItems([
  42. {
  43. taskId: "agent_background_123",
  44. toolCallId: "call_agent_00",
  45. toolName: "Agent",
  46. description: "Review the branch",
  47. status: "running",
  48. outputPath: "/tmp/agent/output.txt",
  49. },
  50. ]);
  51. expect(jobs).toHaveLength(1);
  52. expect(jobs[0]).toMatchObject({
  53. jobId: "agent_background_123",
  54. toolCallId: "call_agent_00",
  55. command: "Review the branch",
  56. title: "Review the branch",
  57. status: "running",
  58. taskKind: "agent",
  59. });
  60. });
  61. it("accepts already-normalized background task control items", () => {
  62. const jobs = parseZCodeBackgroundTaskControlItems([
  63. {
  64. jobId: "agent_normalized_123",
  65. taskKind: "agent",
  66. command: "Review the branch",
  67. status: "running",
  68. cancellable: true,
  69. },
  70. ]);
  71. expect(jobs).toHaveLength(1);
  72. expect(jobs[0]).toMatchObject({
  73. jobId: "agent_normalized_123",
  74. command: "Review the branch",
  75. status: "running",
  76. taskKind: "agent",
  77. cancellable: true,
  78. });
  79. });
  80. it("does not classify generic task records as background Agent tasks", () => {
  81. const jobs = parseZCodeBackgroundTaskControlItems([
  82. {
  83. taskId: "generic_task_123",
  84. type: "task",
  85. description: "Generic task record, not a background Agent control item",
  86. status: "running",
  87. },
  88. ]);
  89. expect(jobs).toEqual([]);
  90. });
  91. it("does not classify generic background_task records without explicit task kind", () => {
  92. const jobs = parseZCodeBackgroundTaskControlItems([
  93. {
  94. taskId: "background_task_123",
  95. type: "background_task",
  96. command: "sleep 30",
  97. status: "running",
  98. },
  99. ]);
  100. expect(jobs).toEqual([]);
  101. });
  102. it("preserves cancellable capability for explicit Bash background controls", () => {
  103. const jobs = parseZCodeBackgroundTaskControlItems([
  104. {
  105. taskId: "background_task_unsupported",
  106. type: "background_task",
  107. toolName: "Bash",
  108. command: "workflow run long-task",
  109. status: "running",
  110. cancellable: false,
  111. },
  112. ]);
  113. expect(jobs).toHaveLength(1);
  114. expect(jobs[0]).toMatchObject({
  115. cancellable: false,
  116. jobId: "background_task_unsupported",
  117. status: "running",
  118. });
  119. });
  120. it("normalizes terminal protocol statuses for shared controls", () => {
  121. const jobs = parseZCodeBackgroundTaskControlItems([
  122. {
  123. taskId: "background_task_timeout",
  124. toolName: "Bash",
  125. command: "sleep 999",
  126. status: "timed_out",
  127. },
  128. {
  129. taskId: "background_task_spawn_error",
  130. toolName: "Bash",
  131. command: "missing-command",
  132. status: "spawn_error",
  133. },
  134. ]);
  135. expect(jobs).toEqual([
  136. expect.objectContaining({
  137. jobId: "background_task_timeout",
  138. status: "killed",
  139. }),
  140. expect.objectContaining({
  141. jobId: "background_task_spawn_error",
  142. status: "failed",
  143. }),
  144. ]);
  145. });
  146. it("merges partial background task control updates by job id", () => {
  147. const merged = mergeZCodeBackgroundTaskControlItems(
  148. [
  149. {
  150. jobId: "background_task_1",
  151. command: "sleep 1",
  152. taskKind: "bash",
  153. status: "running",
  154. },
  155. {
  156. jobId: "background_task_2",
  157. command: "Review branch",
  158. taskKind: "agent",
  159. status: "running",
  160. },
  161. ],
  162. [
  163. {
  164. jobId: "background_task_1",
  165. command: "sleep 1",
  166. taskKind: "bash",
  167. status: "completed",
  168. outputTail: "done",
  169. },
  170. ],
  171. );
  172. expect(merged).toEqual([
  173. expect.objectContaining({
  174. jobId: "background_task_1",
  175. status: "completed",
  176. outputTail: "done",
  177. }),
  178. expect.objectContaining({
  179. jobId: "background_task_2",
  180. status: "running",
  181. }),
  182. ]);
  183. });
  184. });