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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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({
  18. runId: RUN_ID,
  19. name: "nightly triage",
  20. scriptText: "return 1;",
  21. }),
  22. ).toEqual({ label: "nightly triage", labelSource: "name" });
  23. });
  24. it("name 缺席时取脚本的首个非空行,labelSource 为 script", () => {
  25. expect(
  26. resolveDynamicWorkflowRunLabel({
  27. runId: RUN_ID,
  28. scriptText: '\n\n // triage the failing tests \nconst a = agent("worker");',
  29. }),
  30. ).toEqual({ label: "// triage the failing tests", labelSource: "script" });
  31. });
  32. it("首行按 80 字符截断", () => {
  33. const line = "x".repeat(200);
  34. const { label } = resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: line });
  35. expect(label).toHaveLength(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS);
  36. expect(label).toBe("x".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS));
  37. });
  38. // 脚本文本是模型写的外部输入,字符串字面量里可以有 emoji。截断落在代理对中间时宁可少一个
  39. // 码元——孤立代理项既不是合法文本,也会让下游 JSON 编解码在某些运行时上报错
  40. // (与 create-workflow 的 boundGraphText、端口载荷有界化同一处理)。
  41. it("截断不留下孤立代理项", () => {
  42. const line = `${"a".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS - 1)}😀tail`;
  43. const { label } = resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: line });
  44. expect(label).toBe("a".repeat(DYNAMIC_WORKFLOW_RUN_LABEL_MAX_CHARS - 1));
  45. });
  46. it("空白 name 视为没起名,退回脚本首行", () => {
  47. expect(
  48. resolveDynamicWorkflowRunLabel({ runId: RUN_ID, name: " ", scriptText: "return 7;" }),
  49. ).toEqual({ label: "return 7;", labelSource: "script" });
  50. });
  51. // 理论上不发生(submit 必带 scriptText),但读面绝不能因此给出空标签:裸 runId 至少是
  52. // 可定位的,而空字符串在列表里就是一行不可点的空白。
  53. it("name 与脚本都缺席时退回 runId,labelSource 仍是 script", () => {
  54. expect(resolveDynamicWorkflowRunLabel({ runId: RUN_ID })).toEqual({
  55. label: RUN_ID,
  56. labelSource: "script",
  57. });
  58. expect(resolveDynamicWorkflowRunLabel({ runId: RUN_ID, scriptText: " \n\t\n " })).toEqual({
  59. label: RUN_ID,
  60. labelSource: "script",
  61. });
  62. });
  63. });