replaceableConversationTransport.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // ReplaceableConversationTransport 的无状态 query 转发面。
  2. //
  3. // 这个类是 pane 侧的**稳定身份**:远程 service proxy 换代时存量 pane 继续持有同一个对象,
  4. // 所以 ConversationTransport 每新增一个成员,这里都必须长出对应的转发方法。漏掉一个不会在
  5. // pane 侧炸得明显——那个 query 只是变成 undefined,功能静静地不存在。workflowRunEvents
  6. // (dwf run 事件日志分页)就漏过一次,所以它在这里有一条用例。
  7. import { describe, expect, it, vi } from "vitest";
  8. import type {
  9. V4ConversationWorkflowRunEventsResult,
  10. V4ConversationWorkflowRunsResult,
  11. } from "@zcode/shared/zcode-protocol-v4";
  12. import { ReplaceableConversationTransport } from "../src/v4/replaceableConversationTransport.js";
  13. import type { ConversationTransport } from "../src/v4/transport.js";
  14. // 桩只带 workflowRunEvents:这条转发是无状态的(不看订阅 ownership 映射),replace 也不碰
  15. // 接口的其余成员(订阅映射与监听器集合都是空的)。补齐一整张接口只会淹掉用例在测什么。
  16. function stubTransport(result: V4ConversationWorkflowRunEventsResult) {
  17. const workflowRunEvents = vi.fn(async () => result);
  18. return {
  19. workflowRunEvents,
  20. transport: { workflowRunEvents } as unknown as ConversationTransport,
  21. };
  22. }
  23. const firstPage: V4ConversationWorkflowRunEventsResult = {
  24. events: [{ sequence: 7, type: "run.started", payload: { runId: "dwfrun-1" } }],
  25. hasMore: true,
  26. };
  27. const secondPage: V4ConversationWorkflowRunEventsResult = { events: [], hasMore: false };
  28. describe("ReplaceableConversationTransport workflowRunEvents", () => {
  29. it("原样转发给当前 transport,params 一个字段不动", async () => {
  30. const first = stubTransport(firstPage);
  31. const replaceable = new ReplaceableConversationTransport(first.transport);
  32. const params = { sessionId: "session-1", runId: "dwfrun-1", afterSequence: 6, limit: 50 };
  33. await expect(replaceable.workflowRunEvents(params)).resolves.toEqual(firstPage);
  34. expect(first.workflowRunEvents).toHaveBeenCalledWith(params);
  35. });
  36. it("replace 之后转发给新 transport,旧的不再被打", async () => {
  37. const first = stubTransport(firstPage);
  38. const second = stubTransport(secondPage);
  39. const replaceable = new ReplaceableConversationTransport(first.transport);
  40. replaceable.replace(second.transport);
  41. const params = { sessionId: "session-1", runId: "dwfrun-1" };
  42. await expect(replaceable.workflowRunEvents(params)).resolves.toEqual(secondPage);
  43. expect(second.workflowRunEvents).toHaveBeenCalledWith(params);
  44. expect(first.workflowRunEvents).not.toHaveBeenCalled();
  45. });
  46. });
  47. // workflowRuns(dwf run 枚举,重启后的发现面):与 workflowRunEvents 同一类漏接风险,
  48. // 接口新增成员时这里必须同步长出转发(文件头注释的通则)。
  49. function stubRunsTransport(result: V4ConversationWorkflowRunsResult) {
  50. const workflowRuns = vi.fn(async () => result);
  51. return {
  52. workflowRuns,
  53. transport: { workflowRuns } as unknown as ConversationTransport,
  54. };
  55. }
  56. const runsPage: V4ConversationWorkflowRunsResult = {
  57. runs: [
  58. {
  59. runId: "dwfrun-1",
  60. toolCallId: "call-1",
  61. status: "cancelled",
  62. resumable: true,
  63. },
  64. ],
  65. };
  66. const emptyRuns: V4ConversationWorkflowRunsResult = { runs: [] };
  67. describe("ReplaceableConversationTransport workflowRuns", () => {
  68. it("原样转发给当前 transport", async () => {
  69. const first = stubRunsTransport(runsPage);
  70. const replaceable = new ReplaceableConversationTransport(first.transport);
  71. const params = { sessionId: "session-1", limit: 8 };
  72. await expect(replaceable.workflowRuns(params)).resolves.toEqual(runsPage);
  73. expect(first.workflowRuns).toHaveBeenCalledWith(params);
  74. });
  75. it("replace 之后转发给新 transport,旧的不再被打", async () => {
  76. const first = stubRunsTransport(runsPage);
  77. const second = stubRunsTransport(emptyRuns);
  78. const replaceable = new ReplaceableConversationTransport(first.transport);
  79. replaceable.replace(second.transport);
  80. const params = { sessionId: "session-1" };
  81. await expect(replaceable.workflowRuns(params)).resolves.toEqual(emptyRuns);
  82. expect(second.workflowRuns).toHaveBeenCalledWith(params);
  83. expect(first.workflowRuns).not.toHaveBeenCalled();
  84. });
  85. });
  86. // dwf 用户面产物的三条读面(docs/dynamic-workflow/authoring.md):与上面两条同一类漏接风险。
  87. // ⚠ 术语:artifact = 脚本经 `artifact.*` 发布给用户看的产出,不是 run 的顶层返回值。
  88. //
  89. // 这三条比前两条更值得钉:漏掉 workflowRunArtifactRead 的症状是"侧板上有卡片、点开是空的",
  90. // 而不是一个显式的错误——卡片来自状态键投影(不经本类),字节才走 transport。
  91. describe("ReplaceableConversationTransport 用户面产物与脚本 transcript 的转发", () => {
  92. /**
  93. * 三条一起测:每条各写一遍「原样转发 + replace 之后打新的」会是六个几乎一样的用例,
  94. * 而真正要证明的只有一件事——**接口上的每个成员在这个稳定身份上都有一条转发**。
  95. */
  96. const cases = [
  97. {
  98. method: "workflowRunArtifacts" as const,
  99. params: { sessionId: "session-1", runId: "dwfrun-1" },
  100. first: { artifacts: [] },
  101. second: {
  102. artifacts: [
  103. {
  104. id: "book",
  105. kind: "file" as const,
  106. version: 1,
  107. versions: [{ version: 1, publishedAt: 1 }],
  108. itemCount: 0,
  109. },
  110. ],
  111. },
  112. },
  113. {
  114. method: "workflowRunArtifactData" as const,
  115. params: { sessionId: "session-1", runId: "dwfrun-1", artifactId: "perf", limit: 200 },
  116. first: { items: [], hasMore: false },
  117. second: {
  118. items: [{ sequence: 3, siteId: "report#1", ordinal: 1, item: { round: 1 } }],
  119. hasMore: true,
  120. },
  121. },
  122. {
  123. method: "workflowRunArtifactRead" as const,
  124. params: {
  125. sessionId: "session-1",
  126. runId: "dwfrun-1",
  127. artifactId: "book",
  128. version: 1,
  129. offset: 0,
  130. limit: 4,
  131. },
  132. first: { dataBase64: "", mediaType: "application/pdf", totalBytes: 0, nextOffset: null },
  133. second: {
  134. dataBase64: "aGVsbG8=",
  135. mediaType: "application/pdf",
  136. totalBytes: 5,
  137. nextOffset: null,
  138. },
  139. },
  140. // 脚本 transcript 的两条读面(docs/dynamic-workflow/transcript-and-notifications.md「Two read queries」):
  141. // 与产物同一类漏接风险——清单漏了是「面板一直转圈」,正文漏了是「卡片展开是空的」。
  142. {
  143. method: "workflowRunWorkspace" as const,
  144. params: { sessionId: "session-1", runId: "dwfrun-1" },
  145. first: { nodes: [] },
  146. second: {
  147. nodes: [
  148. {
  149. siteId: "world-read#1",
  150. ordinal: 1,
  151. kind: "world-read" as const,
  152. op: "read",
  153. args: ["src/a.ts"],
  154. status: "completed" as const,
  155. createdAt: 1,
  156. updatedAt: 2,
  157. },
  158. ],
  159. truncated: true,
  160. },
  161. },
  162. {
  163. method: "workflowRunNodeResult" as const,
  164. params: {
  165. sessionId: "session-1",
  166. runId: "dwfrun-1",
  167. siteId: "world-read#1",
  168. ordinal: 1,
  169. maxBytes: 4096,
  170. },
  171. first: { status: "running" as const, truncated: false, totalBytes: 0 },
  172. second: {
  173. status: "completed" as const,
  174. result: "export {};\n",
  175. truncated: false,
  176. totalBytes: 11,
  177. },
  178. },
  179. ];
  180. it.each(cases)("$method 原样转发给当前 transport,params 一个字段不动", async (testCase) => {
  181. const forward = vi.fn(async () => testCase.first);
  182. const transport = { [testCase.method]: forward } as unknown as ConversationTransport;
  183. const replaceable = new ReplaceableConversationTransport(transport);
  184. await expect(
  185. (replaceable[testCase.method] as (params: unknown) => Promise<unknown>)(testCase.params),
  186. ).resolves.toEqual(testCase.first);
  187. expect(forward).toHaveBeenCalledWith(testCase.params);
  188. });
  189. it.each(cases)("$method replace 之后转发给新 transport,旧的不再被打", async (testCase) => {
  190. const firstForward = vi.fn(async () => testCase.first);
  191. const secondForward = vi.fn(async () => testCase.second);
  192. const replaceable = new ReplaceableConversationTransport({
  193. [testCase.method]: firstForward,
  194. } as unknown as ConversationTransport);
  195. replaceable.replace({ [testCase.method]: secondForward } as unknown as ConversationTransport);
  196. await expect(
  197. (replaceable[testCase.method] as (params: unknown) => Promise<unknown>)(testCase.params),
  198. ).resolves.toEqual(testCase.second);
  199. expect(secondForward).toHaveBeenCalledWith(testCase.params);
  200. expect(firstForward).not.toHaveBeenCalled();
  201. });
  202. });