dynamic-workflow-run-label.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * run 展示标签的兜底链(docs/dynamic-workflow/launch.md「`ListWorkflowRuns`」)。
  3. *
  4. * 被测对象是一个纯函数,且它**刻意住在自己的模块里**:列表面与详情面必须用同一条链
  5. * (同一个 run 在两处显示不同名字是最难被抓住、最直接损害信任的那类不一致),而纯函数
  6. * 单独成模块才能被这条测试直接 import——run service 本身拖着一整条 AgentRuntime 依赖链。
  7. */
  8. import { describe, expect, it } from "vitest";
  9. import {
  10. DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS,
  11. resolveDynamicWorkflowRunLabel,
  12. } from "../src/app/dynamic-workflow-run-label.js";
  13. const RUN_ID = "dwfrun-0f2a";
  14. describe("resolveDynamicWorkflowRunLabel", () => {
  15. it("name 命中即用它,labelSource 为 name", () => {
  16. expect(
  17. resolveDynamicWorkflowRunLabel({ runId: RUN_ID, name: "nightly triage", scriptText: "return 1;" }),
  18. ).toEqual({ label: "nightly triage", labelSource: "name" });
  19. });
  20. it("name 缺席时取脚本的首个非空行,labelSource 为 script", () => {
  21. expect(
  22. resolveDynamicWorkflowRunLabel({
  23. runId: RUN_ID,
  24. scriptText: "\n\n // triage the failing tests \nconst a = agent(\"worker\");",
  25. }),
  26. ).toEqual({ label: '// triage the failing tests', labelSource: "script" });
  27. });
  28. it("首行按 80 字符截断", () => {
  29. const line = "x".repeat(200);
  30. const { label } = resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: line });
  31. expect(label).toHaveLength(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS);
  32. expect(label).toBe("x".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS));
  33. });
  34. // 脚本文本是模型写的外部输入,字符串字面量里可以有 emoji。截断落在代理对中间时宁可少一个
  35. // 码元——孤立代理项既不是合法文本,也会让下游 JSON 编解码在某些运行时上报错
  36. // (与 create-workflow 的 boundGraphText、端口载荷有界化同一处理)。
  37. it("截断不留下孤立代理项", () => {
  38. const line = `${"a".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS - 1)}😀tail`;
  39. const { label } = resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: line });
  40. expect(label).toBe("a".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS - 1));
  41. expect(JSON.parse(JSON.stringify(label))).toBe(label);
  42. });
  43. it("空白 name 视为没起名,退回脚本首行", () => {
  44. expect(
  45. resolveDynamicWorkflowRunLabel({ runId: RUN_ID, name: " ", scriptText: "return 7;" }),
  46. ).toEqual({ label: "return 7;", labelSource: "script" });
  47. });
  48. // 理论上不发生(submit 必带 scriptText),但读面绝不能因此给出空标签:裸 runId 至少是
  49. // 可定位的,而空字符串在列表里就是一行不可点的空白。
  50. it("name 与脚本都缺席时退回 runId,labelSource 仍是 script", () => {
  51. expect(resolveDynamicWorkflowRunLabel({ runId: RUN_ID })).toEqual({
  52. label: RUN_ID,
  53. labelSource: "script",
  54. });
  55. expect(
  56. resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: " \n\t\n " }),
  57. ).toEqual({ label: RUN_ID, labelSource: "script" });
  58. });
  59. });