dwf-command.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import type { DynamicWorkflowRunSessionSummary } from "@zcode/contracts";
  4. import {
  5. createCommandCenter,
  6. listSlashCommandSuggestions,
  7. parseSlashCommand,
  8. } from "../src/command-center.js";
  9. import type { CommandCenterApp, CommandCenterDeps } from "../src/command-center.js";
  10. /**
  11. * `/dwf` 的命令面测试(docs/dynamic-workflow/launch.md「`/dwf`」)。
  12. * 三条不变量在这里守着:能力缺席要说「不可用」而不是空表;cancel 缺 runId 只在
  13. * 恰一在飞时替用户决定;resume 的拒绝理由原样来自服务端,绝不客户端重新推导。
  14. */
  15. const ABORT = { abortSignal: new AbortController().signal };
  16. function createHarness(app: Partial<CommandCenterApp>) {
  17. const deps: CommandCenterDeps = {
  18. getApp: async () =>
  19. ({
  20. sessionId: "sess_active",
  21. traceId: "trace-active",
  22. resume: async () => {
  23. throw new Error("resume should not run");
  24. },
  25. submitPrompt: async () => {
  26. throw new Error("prompt should not run for /dwf");
  27. },
  28. ...app,
  29. }) as never,
  30. getMode: () => "build" as never,
  31. resumeApp: async () => {
  32. throw new Error("resumeApp should not run");
  33. },
  34. };
  35. return createCommandCenter(deps);
  36. }
  37. function run(overrides: Partial<DynamicWorkflowRunSessionSummary> = {}) {
  38. return {
  39. resumable: false,
  40. runId: "dwfrun_1",
  41. status: "running",
  42. ...overrides,
  43. } as DynamicWorkflowRunSessionSummary;
  44. }
  45. test("/dwf lists this session's dynamic workflow runs as text", async () => {
  46. let requestedLimit: number | undefined;
  47. const submitPrompt = createHarness({
  48. listDynamicWorkflowRuns: async (input) => {
  49. requestedLimit = input.limit;
  50. return [
  51. run({ label: "nightly audit", runId: "dwfrun_live", status: "running" }),
  52. run({
  53. failureCode: "Interrupted",
  54. failureMessage: "host exited",
  55. label: "flaky test hunt",
  56. resumable: true,
  57. runId: "dwfrun_dead",
  58. status: "stopped",
  59. stopReason: "interrupted",
  60. }),
  61. ];
  62. },
  63. });
  64. const result = await submitPrompt("/dwf", ABORT);
  65. assert.equal(requestedLimit, 20);
  66. assert.match(result.response, /Dynamic workflow runs \(2\)/);
  67. assert.match(result.response, /dwfrun_live · nightly audit · running/);
  68. // resumable 与 failure 一起呈现:用户看到「可恢复」才知道 /dwf resume 有意义。
  69. assert.match(
  70. result.response,
  71. /dwfrun_dead · flaky test hunt · stopped\/interrupted · resumable \(Interrupted: host exited\)/,
  72. );
  73. });
  74. test("/dwf list renders the updated time when the server sends one", async () => {
  75. const updatedAt = new Date(2026, 7, 22, 14, 5).getTime();
  76. const submitPrompt = createHarness({
  77. listDynamicWorkflowRuns: async () => [run({ label: "audit", updatedAt })],
  78. });
  79. const result = await submitPrompt("/dwf list", ABORT);
  80. assert.match(result.response, /updated 2026-08-22 14:05/);
  81. });
  82. test("/dwf list degrades gracefully when label and updatedAt are absent", async () => {
  83. const submitPrompt = createHarness({
  84. listDynamicWorkflowRuns: async () => [run({ runId: "dwfrun_old" })],
  85. });
  86. const result = await submitPrompt("/dwf list", ABORT);
  87. // 老服务端不发这两个可选键:标签回落 runId、时间整段省略,行仍然完整可读。
  88. assert.match(result.response, /- dwfrun_old · running/);
  89. assert.ok(!result.response.includes("updated"), "no time column when the server omits it");
  90. });
  91. test("/dwf list does not repeat the runId when label fell back to it", async () => {
  92. const submitPrompt = createHarness({
  93. listDynamicWorkflowRuns: async () => [run({ label: "dwfrun_bare", runId: "dwfrun_bare" })],
  94. });
  95. const result = await submitPrompt("/dwf list", ABORT);
  96. // 服务端的 label 兜底最后一档就是 runId;照抄会印成 "dwfrun_bare dwfrun_bare"。
  97. assert.match(result.response, /- dwfrun_bare · running/);
  98. });
  99. test("/dwf list and bare /dwf take the same path", async () => {
  100. const submitPrompt = createHarness({
  101. listDynamicWorkflowRuns: async () => [run()],
  102. });
  103. const bare = await submitPrompt("/dwf", ABORT);
  104. const explicit = await submitPrompt("/dwf list", ABORT);
  105. assert.equal(bare.response, explicit.response);
  106. });
  107. test("/dwf list reports an empty session without pretending it is broken", async () => {
  108. const submitPrompt = createHarness({
  109. listDynamicWorkflowRuns: async () => [],
  110. });
  111. const result = await submitPrompt("/dwf list", ABORT);
  112. assert.match(result.response, /No dynamic workflow runs in this session\./);
  113. });
  114. test("/dwf list says not available when the capability is absent", async () => {
  115. const submitPrompt = createHarness({});
  116. const result = await submitPrompt("/dwf list", ABORT);
  117. // 能力缺席 ≠ 没有 run:必须区分,否则用户以为工作流丢了。
  118. assert.match(result.response, /not available in this client/);
  119. });
  120. test("/dwf cancel without a run id cancels the only in-flight run", async () => {
  121. let cancelledTaskId: string | undefined;
  122. const submitPrompt = createHarness({
  123. listDynamicWorkflowRuns: async () => [
  124. run({ runId: "dwfrun_live", status: "running" }),
  125. run({ runId: "dwfrun_done", status: "completed" }),
  126. ],
  127. cancelBackgroundTask: async (taskId) => {
  128. cancelledTaskId = taskId;
  129. return { cancelled: true, status: "cancelled", taskId };
  130. },
  131. });
  132. const result = await submitPrompt("/dwf cancel", ABORT);
  133. // runId ≡ taskId:终态 run 不是候选,所以唯一在飞的那个被取消。
  134. assert.equal(cancelledTaskId, "dwfrun_live");
  135. assert.match(result.response, /Cancelled dynamic workflow run dwfrun_live\./);
  136. });
  137. test("/dwf cancel without a run id lists candidates when several are in flight", async () => {
  138. let cancelCalled = false;
  139. const submitPrompt = createHarness({
  140. listDynamicWorkflowRuns: async () => [
  141. run({ runId: "dwfrun_a", status: "running" }),
  142. run({ runId: "dwfrun_b", status: "pending" }),
  143. ],
  144. cancelBackgroundTask: async (taskId) => {
  145. cancelCalled = true;
  146. return { cancelled: true, status: "cancelled", taskId };
  147. },
  148. });
  149. const result = await submitPrompt("/dwf cancel", ABORT);
  150. // 多个候选时绝不猜:取消是丢掉的进度,猜错的代价不对称。
  151. assert.equal(cancelCalled, false);
  152. assert.match(result.response, /Multiple in-flight dynamic workflow runs/);
  153. assert.match(result.response, /dwfrun_a · running/);
  154. assert.match(result.response, /dwfrun_b · pending/);
  155. });
  156. test("/dwf cancel without a run id reports when nothing is in flight", async () => {
  157. let cancelCalled = false;
  158. const submitPrompt = createHarness({
  159. listDynamicWorkflowRuns: async () => [run({ status: "completed" })],
  160. cancelBackgroundTask: async (taskId) => {
  161. cancelCalled = true;
  162. return { cancelled: true, status: "cancelled", taskId };
  163. },
  164. });
  165. const result = await submitPrompt("/dwf cancel", ABORT);
  166. assert.equal(cancelCalled, false);
  167. assert.match(result.response, /No in-flight dynamic workflow runs to cancel\./);
  168. });
  169. test("/dwf cancel passes an explicit run id straight through", async () => {
  170. let cancelledTaskId: string | undefined;
  171. const submitPrompt = createHarness({
  172. listDynamicWorkflowRuns: async () => {
  173. throw new Error("an explicit run id must not need the run list");
  174. },
  175. cancelBackgroundTask: async (taskId) => {
  176. cancelledTaskId = taskId;
  177. return { cancelled: true, status: "cancelled", taskId };
  178. },
  179. });
  180. const result = await submitPrompt("/dwf cancel dwfrun_named", ABORT);
  181. assert.equal(cancelledTaskId, "dwfrun_named");
  182. assert.match(result.response, /Cancelled dynamic workflow run dwfrun_named\./);
  183. });
  184. test("/dwf cancel renders a refused cancellation", async () => {
  185. const submitPrompt = createHarness({
  186. cancelBackgroundTask: async (taskId) => ({
  187. cancelled: false,
  188. reason: "task already settled",
  189. status: "completed",
  190. taskId,
  191. }),
  192. });
  193. const result = await submitPrompt("/dwf cancel dwfrun_gone", ABORT);
  194. assert.match(result.response, /Could not cancel dwfrun_gone \(completed\): task already settled/);
  195. });
  196. test("/dwf cancel says not available when the capability is absent", async () => {
  197. const submitPrompt = createHarness({});
  198. const result = await submitPrompt("/dwf cancel dwfrun_1", ABORT);
  199. assert.match(result.response, /not available in this client/);
  200. });
  201. test("/dwf resume forwards the run id as workId", async () => {
  202. let resumedWorkId: string | undefined;
  203. const submitPrompt = createHarness({
  204. resumeWorkflowRun: async (input) => {
  205. resumedWorkId = input.workId;
  206. return { ok: true, runId: input.workId };
  207. },
  208. });
  209. const result = await submitPrompt("/dwf resume dwfrun_dead", ABORT);
  210. assert.equal(resumedWorkId, "dwfrun_dead");
  211. assert.match(result.response, /Resumed dynamic workflow run dwfrun_dead\./);
  212. });
  213. test("/dwf resume renders the server's structured rejection", async () => {
  214. const submitPrompt = createHarness({
  215. resumeWorkflowRun: async () => ({ ok: false, reason: "not_resumable" }),
  216. });
  217. const result = await submitPrompt("/dwf resume dwfrun_done", ABORT);
  218. // 服务端裁定原样呈现:友好一行 + 原始 reason code(不按 status 客户端重推导)。
  219. assert.match(result.response, /Cannot resume dwfrun_done/);
  220. assert.match(result.response, /only a stopped run can be resumed/);
  221. assert.match(result.response, /\(not_resumable\)/);
  222. });
  223. test("/dwf resume surfaces every structured reason with its code", async () => {
  224. const reasons = [
  225. "not_found",
  226. "not_resumable",
  227. "already_running",
  228. "script_missing",
  229. "script_mismatch",
  230. "compile_failed",
  231. ] as const;
  232. for (const reason of reasons) {
  233. const submitPrompt = createHarness({
  234. resumeWorkflowRun: async () => ({ ok: false, reason }),
  235. });
  236. const result = await submitPrompt("/dwf resume dwfrun_1", ABORT);
  237. assert.match(result.response, new RegExp(`\\(${reason}\\)`));
  238. }
  239. });
  240. test("/dwf resume compile_failed prints the diagnostics the server attached", async () => {
  241. const submitPrompt = createHarness({
  242. resumeWorkflowRun: async () => ({
  243. ok: false,
  244. reason: "compile_failed",
  245. message: "L3:C1 Property 'askWithOldFacade' does not exist",
  246. }),
  247. });
  248. const result = await submitPrompt("/dwf resume dwfrun_1", ABORT);
  249. assert.match(result.response, /\(compile_failed\)/);
  250. assert.match(result.response, /AmendWorkflow/);
  251. assert.match(result.response, /askWithOldFacade/);
  252. });
  253. test("/dwf resume without a run id prints usage", async () => {
  254. let resumeCalled = false;
  255. const submitPrompt = createHarness({
  256. resumeWorkflowRun: async () => {
  257. resumeCalled = true;
  258. return { ok: true, runId: "dwfrun_1" };
  259. },
  260. });
  261. const result = await submitPrompt("/dwf resume", ABORT);
  262. assert.equal(resumeCalled, false);
  263. assert.match(result.response, /Usage: \/dwf \[list\|cancel \[runId\]\|resume <runId>\]/);
  264. });
  265. test("/dwf resume says not available when the capability is absent", async () => {
  266. const submitPrompt = createHarness({});
  267. const result = await submitPrompt("/dwf resume dwfrun_1", ABORT);
  268. assert.match(result.response, /not available in this client/);
  269. });
  270. test("/dwf rejects an unknown subcommand with usage", async () => {
  271. const submitPrompt = createHarness({
  272. listDynamicWorkflowRuns: async () => [run()],
  273. });
  274. const result = await submitPrompt("/dwf status", ABORT);
  275. // 刻意不做 /dwf status:GetWorkflowRun 是模型工具,用户问模型即可。
  276. assert.match(result.response, /Usage: \/dwf/);
  277. });
  278. test("the slash command registry knows /dwf and /workflow and has dropped /workflows", () => {
  279. const dwf = parseSlashCommand("/dwf list");
  280. assert.equal(dwf?.type, "known");
  281. assert.equal(dwf?.type === "known" ? dwf.name : undefined, "dwf");
  282. // legacy `/workflows` 命名空间整体移除(面板机器随之删除)。`/workflow` 不是它的残留:
  283. // 它是与 `/init` 同路的内置 prompt 命令(docs/design/v2/commands.md),名字由 TUI 识别、
  284. // 正文由 bootstrap 展开。
  285. assert.equal(parseSlashCommand("/workflow")?.type, "known");
  286. assert.equal(parseSlashCommand("/workflows")?.type, "unknown");
  287. const names = listSlashCommandSuggestions().map((entry) => entry.name);
  288. assert.ok(names.includes("dwf"), "/dwf must be suggestible in the composer");
  289. assert.ok(names.includes("workflow"), "/workflow is a builtin and must be suggestible");
  290. assert.ok(!names.includes("workflows"));
  291. // /expert 是另一套 legacy 面,本轮不动。
  292. assert.ok(names.includes("expert"));
  293. });