list-workflow-runs.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. LIST_WORKFLOW_RUNS_TOOL_NAME,
  4. ListWorkflowRunsInputJsonSchema,
  5. ListWorkflowRunsInputSchema,
  6. ListWorkflowRunsOutputJsonSchema,
  7. ListWorkflowRunsOutputSchema,
  8. WORKFLOW_RUN_LIFECYCLE_STATUSES,
  9. } from "../src/tools/list-workflow-runs.js";
  10. const RUN = {
  11. runId: "dwfrun-1",
  12. label: "nightly triage",
  13. labelSource: "name" as const,
  14. status: "running" as const,
  15. ownedByThisSession: true,
  16. createdAt: 1_755_000_000_000,
  17. updatedAt: 1_755_000_001_000,
  18. spentTokens: 1_204,
  19. };
  20. describe("ListWorkflowRuns input schema", () => {
  21. it("names the tool exactly as it registers", () => {
  22. expect(LIST_WORKFLOW_RUNS_TOOL_NAME).toBe("ListWorkflowRuns");
  23. });
  24. it("defaults limit to 20 when the model omits it", () => {
  25. expect(ListWorkflowRunsInputSchema.parse({})).toEqual({ limit: 20 });
  26. });
  27. // 用户裁决(spec 的「`ListWorkflowRuns` 输出」行):limit 是**钳制**而不是拒绝。一次只读
  28. // 枚举没有理由因为一个越界数字变成模型要从中恢复的工具错误。
  29. it("clamps an out-of-range limit into [1, 50] instead of rejecting it", () => {
  30. expect(ListWorkflowRunsInputSchema.parse({ limit: 0 }).limit).toBe(1);
  31. expect(ListWorkflowRunsInputSchema.parse({ limit: -7 }).limit).toBe(1);
  32. expect(ListWorkflowRunsInputSchema.parse({ limit: 500 }).limit).toBe(50);
  33. expect(ListWorkflowRunsInputSchema.parse({ limit: 7 }).limit).toBe(7);
  34. // 小数被截断而不是被拒:模型偶尔会给 12.5,而 SQL 的 limit 只接受整数。
  35. expect(ListWorkflowRunsInputSchema.parse({ limit: 12.5 }).limit).toBe(12);
  36. });
  37. it("still rejects a non-numeric limit", () => {
  38. for (const limit of ["many", null, {}]) {
  39. expect(ListWorkflowRunsInputSchema.safeParse({ limit }).success).toBe(false);
  40. }
  41. });
  42. it("accepts a status subset and rejects unknown status literals", () => {
  43. expect(ListWorkflowRunsInputSchema.parse({ statuses: ["errored", "running"] })).toEqual({
  44. limit: 20,
  45. statuses: ["errored", "running"],
  46. });
  47. expect(ListWorkflowRunsInputSchema.safeParse({ statuses: ["lost"] }).success).toBe(false);
  48. // 旧词汇(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md 之前的 failed / cancelled)不再是合法输入。
  49. expect(ListWorkflowRunsInputSchema.safeParse({ statuses: ["failed"] }).success).toBe(false);
  50. expect(ListWorkflowRunsInputSchema.safeParse({ statuses: ["cancelled"] }).success).toBe(false);
  51. // 空数组是合法输入(端口把它读作「不匹配任何状态」)。
  52. expect(ListWorkflowRunsInputSchema.parse({ statuses: [] }).statuses).toEqual([]);
  53. });
  54. it("stays strict about unknown keys", () => {
  55. // 刻意没有 cwd 输入:工具恒查 context.workingDirectory,模型无权跨项目扫库。
  56. expect(ListWorkflowRunsInputSchema.safeParse({ cwd: "/elsewhere" }).success).toBe(false);
  57. });
  58. it("publishes the bounds and the default to the model without requiring limit", () => {
  59. const schema = ListWorkflowRunsInputJsonSchema as {
  60. properties?: Record<string, Record<string, unknown>>;
  61. required?: string[];
  62. };
  63. expect(schema.properties?.limit).toMatchObject({
  64. type: "integer",
  65. minimum: 1,
  66. maximum: 50,
  67. default: 20,
  68. });
  69. expect(schema.properties?.statuses?.type).toBe("array");
  70. // 两个字段都可省:TaskOutput 那个手写 required 覆盖是为了固定它自己的 provider schema,
  71. // 本工具没有那个约束,默认值就该读作「可以不传」。
  72. expect(schema.required ?? []).toEqual([]);
  73. });
  74. });
  75. describe("ListWorkflowRuns output schema", () => {
  76. it("roundtrips a minimal run list", () => {
  77. const output = { runs: [RUN] };
  78. expect(ListWorkflowRunsOutputSchema.parse(output)).toEqual(output);
  79. });
  80. it("carries the optional annotations and the page-level truncation flag", () => {
  81. const output = {
  82. runs: [{ ...RUN, ownedByThisSession: false, possiblyInterrupted: true }],
  83. truncated: true,
  84. };
  85. expect(ListWorkflowRunsOutputSchema.parse(output)).toEqual(output);
  86. });
  87. it("omits possiblyInterrupted and truncated rather than defaulting them to false", () => {
  88. const parsed = ListWorkflowRunsOutputSchema.parse({ runs: [RUN] });
  89. expect("truncated" in parsed).toBe(false);
  90. expect("possiblyInterrupted" in parsed.runs[0]!).toBe(false);
  91. });
  92. it("accepts an empty project list", () => {
  93. expect(ListWorkflowRunsOutputSchema.parse({ runs: [] })).toEqual({ runs: [] });
  94. });
  95. it("keeps the lifecycle vocabulary aligned with the port", () => {
  96. expect([...WORKFLOW_RUN_LIFECYCLE_STATUSES]).toEqual([
  97. "completed",
  98. "errored",
  99. "pending",
  100. "running",
  101. "stopped",
  102. ]);
  103. for (const status of WORKFLOW_RUN_LIFECYCLE_STATUSES) {
  104. expect(ListWorkflowRunsOutputSchema.safeParse({ runs: [{ ...RUN, status }] }).success).toBe(
  105. true,
  106. );
  107. }
  108. });
  109. it("stays strict about unknown keys on the page and on a row", () => {
  110. expect(ListWorkflowRunsOutputSchema.safeParse({ runs: [RUN], cursor: "x" }).success).toBe(false);
  111. expect(
  112. ListWorkflowRunsOutputSchema.safeParse({ runs: [{ ...RUN, scriptText: "return 1;" }] })
  113. .success,
  114. ).toBe(false);
  115. });
  116. it("requires the label source to be one of the two derivations", () => {
  117. expect(
  118. ListWorkflowRunsOutputSchema.safeParse({ runs: [{ ...RUN, labelSource: "runId" }] }).success,
  119. ).toBe(false);
  120. });
  121. it("projects every row field into the model-facing JSON schema", () => {
  122. const rowProperties = (
  123. ListWorkflowRunsOutputJsonSchema as {
  124. properties?: { runs?: { items?: { properties?: Record<string, unknown> } } };
  125. }
  126. ).properties?.runs?.items?.properties;
  127. expect(Object.keys(rowProperties ?? {})).toEqual([
  128. "runId",
  129. "label",
  130. "labelSource",
  131. "status",
  132. "stopReason",
  133. "resumedFrom",
  134. "supersededBy",
  135. "ownedByThisSession",
  136. "possiblyInterrupted",
  137. "createdAt",
  138. "updatedAt",
  139. "spentTokens",
  140. ]);
  141. });
  142. // stopped 的原因(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md):五值枚举,只在 stopped 上有意义,
  143. // 但 schema 层不绑定状态(端口才是裁定者),只校验词汇。
  144. it("accepts the five stop reasons and rejects anything else", () => {
  145. for (const stopReason of ["user", "model", "provider", "interrupted", "superseded"]) {
  146. expect(
  147. ListWorkflowRunsOutputSchema.safeParse({
  148. runs: [{ ...RUN, status: "stopped", stopReason }],
  149. }).success,
  150. ).toBe(true);
  151. }
  152. expect(
  153. ListWorkflowRunsOutputSchema.safeParse({
  154. runs: [{ ...RUN, status: "stopped", stopReason: "crash" }],
  155. }).success,
  156. ).toBe(false);
  157. });
  158. });