useSavedWorkflowRunOpeners.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @vitest-environment jsdom
  2. // 中枢运行历史行上两个「打开」的门与实参构造(docs/dynamic-workflow/launch.md「Cards」+
  3. // docs/dynamic-workflow/authoring.md「How the user sees them」)。项目档与全局档共用这一个 hook,唯一的差别是
  4. // 「目标项目怎么算」。
  5. //
  6. // 关键差异:产物 tab **不需要 `toolCallId`**(它不画因果图,也就不必回到那条 CreateWorkflow
  7. // 工具行),所以老行缺 toolCallId 时「查看实例」关闭而产物 chip 仍然可点。
  8. import { renderHook } from "@testing-library/react";
  9. import { beforeEach, describe, expect, it, vi } from "vitest";
  10. import type { ZCodeSavedWorkflowRun } from "@zcode/shared";
  11. import {
  12. useSavedWorkflowRunOpeners,
  13. type SavedWorkflowRunOpenTarget,
  14. } from "@/settings/saved-workflows/useSavedWorkflowRunOpeners.js";
  15. const onOpenWorkflowRun = vi.fn();
  16. const onOpenWorkflowArtifact = vi.fn();
  17. const TARGET: SavedWorkflowRunOpenTarget = {
  18. workspacePath: "/repo",
  19. workspaceIdentity: "ssh://host/repo",
  20. };
  21. function run(overrides: Partial<ZCodeSavedWorkflowRun> = {}): ZCodeSavedWorkflowRun {
  22. return {
  23. runId: "r-1",
  24. name: "release-check",
  25. status: "completed",
  26. createdAt: 1,
  27. updatedAt: 2,
  28. spentTokens: 0,
  29. parentSessionId: "s1",
  30. toolCallId: "t1",
  31. artifacts: [
  32. { id: "book", kind: "file", title: "审计报告", version: 2, contentType: "text/html" },
  33. ],
  34. ...overrides,
  35. };
  36. }
  37. function openers(target: SavedWorkflowRunOpenTarget | null = TARGET) {
  38. return renderHook(() =>
  39. useSavedWorkflowRunOpeners({
  40. resolveTarget: () => target,
  41. onOpenWorkflowRun,
  42. onOpenWorkflowArtifact,
  43. }),
  44. ).result.current;
  45. }
  46. beforeEach(() => {
  47. onOpenWorkflowRun.mockClear();
  48. onOpenWorkflowArtifact.mockClear();
  49. });
  50. describe("useSavedWorkflowRunOpeners", () => {
  51. it("「查看实例」带上 toolCallId 与目标项目", () => {
  52. openers().handleOpenRun(run(), "release-check");
  53. expect(onOpenWorkflowRun).toHaveBeenCalledWith({
  54. sessionId: "s1",
  55. runId: "r-1",
  56. toolCallId: "t1",
  57. workflowName: "release-check",
  58. workspacePath: "/repo",
  59. workspaceIdentity: "ssh://host/repo",
  60. });
  61. });
  62. it("产物 chip 的实参**不含 toolCallId**,但带上展示名与 contentType(终点据它分流 html 产物)", () => {
  63. openers().handleOpenArtifact(run(), "book");
  64. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  65. sessionId: "s1",
  66. runId: "r-1",
  67. artifactId: "book",
  68. title: "审计报告",
  69. contentType: "text/html",
  70. workspacePath: "/repo",
  71. workspaceIdentity: "ssh://host/repo",
  72. });
  73. });
  74. it("老行的 chip 载荷不带 contentType 时不编一个出来(少一个键是退化不是错误)", () => {
  75. openers().handleOpenArtifact(
  76. run({ artifacts: [{ id: "book", kind: "file", title: "审计报告", version: 2 }] }),
  77. "book",
  78. );
  79. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  80. sessionId: "s1",
  81. runId: "r-1",
  82. artifactId: "book",
  83. title: "审计报告",
  84. workspacePath: "/repo",
  85. workspaceIdentity: "ssh://host/repo",
  86. });
  87. });
  88. it("老行缺 toolCallId:「查看实例」关闭,产物仍然打得开", () => {
  89. const legacy = run({ toolCallId: undefined });
  90. const hook = openers();
  91. hook.handleOpenRun(legacy, "release-check");
  92. expect(onOpenWorkflowRun).not.toHaveBeenCalled();
  93. hook.handleOpenArtifact(legacy, "book");
  94. expect(onOpenWorkflowArtifact).toHaveBeenCalledTimes(1);
  95. });
  96. it("缺 parentSessionId:两个入口都关(产物 tab 必须开在发起它的会话里)", () => {
  97. const orphan = run({ parentSessionId: undefined });
  98. const hook = openers();
  99. hook.handleOpenRun(orphan, "release-check");
  100. hook.handleOpenArtifact(orphan, "book");
  101. expect(onOpenWorkflowRun).not.toHaveBeenCalled();
  102. expect(onOpenWorkflowArtifact).not.toHaveBeenCalled();
  103. });
  104. it("目标项目没打开(全局档按 cwd 反查不到):两个入口都关", () => {
  105. const hook = openers(null);
  106. hook.handleOpenRun(run(), "release-check");
  107. hook.handleOpenArtifact(run(), "book");
  108. expect(onOpenWorkflowRun).not.toHaveBeenCalled();
  109. expect(onOpenWorkflowArtifact).not.toHaveBeenCalled();
  110. });
  111. it("产物不在该行的清单里时不编造标题,只送 id", () => {
  112. openers().handleOpenArtifact(run({ artifacts: undefined }), "book");
  113. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  114. sessionId: "s1",
  115. runId: "r-1",
  116. artifactId: "book",
  117. workspacePath: "/repo",
  118. workspaceIdentity: "ssh://host/repo",
  119. });
  120. });
  121. it("本地项目没有 workspaceIdentity 时不硬塞一个空值(身份语义按 workspaceKey 回退)", () => {
  122. openers({ workspacePath: "/repo" }).handleOpenArtifact(run(), "book");
  123. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  124. sessionId: "s1",
  125. runId: "r-1",
  126. artifactId: "book",
  127. title: "审计报告",
  128. contentType: "text/html",
  129. workspacePath: "/repo",
  130. });
  131. });
  132. });