workflowRunSettings.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // 「配置」的纯规则(docs/dynamic-workflow/presentation.md「The settings popover」「The settings turn」;
  2. // 命令见 docs/dynamic-workflow/launch.md「Changing a run's settings from the GUI」):哪些 run 能配、
  3. // 表单起点、Apply 发什么、被拒说哪句话、转写行与来龙去脉的措辞、借图规则与侧栏 tab 的原地替换。
  4. import { describe, expect, it } from "vitest";
  5. import type { WorkflowRunState, WorkflowSettingsAmendMeta } from "@zcode/shared/zcode-protocol-v4";
  6. import {
  7. clampWorkflowRunSettingsBound,
  8. describeWorkflowRunSettingsRejection,
  9. initialWorkflowRunSettingsDraft,
  10. isWorkflowRunConfigurable,
  11. workflowRunDefaultConcurrency,
  12. workflowRunSettingsChange,
  13. workflowRunSettingsConsequenceId,
  14. workflowRunSettingsRejectionDetail,
  15. workflowRunSettingsRejectionMessageId,
  16. workflowSessionModelOf,
  17. type WorkflowRunSettingsDraft,
  18. } from "@/components/workflow-timeline/workflowRunSettings.js";
  19. import {
  20. workflowSettingsChangeSegments,
  21. workflowSettingsProvenanceRows,
  22. } from "@/components/workflow-timeline/workflowSettingsChange.js";
  23. import {
  24. openWorkflowRunSidePane,
  25. replaceWorkflowRunSidePane,
  26. type WorkflowRunSidePaneTab,
  27. } from "@/lib/workspaceSidePane.js";
  28. import { resolveWorkflowRunGraph } from "@/v4/workflowRunCardJoin.js";
  29. import enUS from "../src/i18n/locales/en-US.js";
  30. import zhCN from "../src/i18n/locales/zh-CN.js";
  31. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  32. return {
  33. runId: "run-a",
  34. toolCallId: "tool-a",
  35. status: "running",
  36. usage: { spentTokens: 0, nodesUsed: 0 },
  37. actors: [],
  38. nodes: [],
  39. lastEventSequence: 0,
  40. ...overrides,
  41. };
  42. }
  43. /** 与 ZCodeIntlProvider 同一个替换规则(`{name}` 直接替换)。 */
  44. function formatterFor(messages: Record<string, string>) {
  45. return (descriptor: { id: string }, values?: Record<string, string | number>) => {
  46. let text = messages[descriptor.id] ?? descriptor.id;
  47. for (const [key, value] of Object.entries(values ?? {})) {
  48. text = text.replaceAll(`{${key}}`, String(value));
  49. }
  50. return text;
  51. };
  52. }
  53. describe("isWorkflowRunConfigurable", () => {
  54. it.each([
  55. ["pending", run({ status: "pending" }), true],
  56. ["running", run(), true],
  57. ["errored(换个模型重试)", run({ status: "errored" }), true],
  58. ["stopped(可恢复)", run({ status: "stopped", stopReason: "user", resumable: true }), true],
  59. [
  60. "stopped · superseded(活的是后继)",
  61. run({ status: "stopped", stopReason: "superseded", supersededBy: "run-b" }),
  62. false,
  63. ],
  64. ["completed(每个 ask 都会从缓存重放)", run({ status: "completed" }), false],
  65. ["不在投影里", undefined, false],
  66. ] as const)("%s", (_case, state, expected) => {
  67. expect(isWorkflowRunConfigurable(state)).toBe(expected);
  68. });
  69. });
  70. describe("表单起点", () => {
  71. it("两项都取 run 自己的设置:模型拆出思考档,上限取本 run 的界", () => {
  72. const draft = initialWorkflowRunSettingsDraft(
  73. run({
  74. subagentModel: "zhipu/glm-5.3$high",
  75. concurrencyCeiling: 13,
  76. concurrency: { cap: 13, ceiling: 13, limit: 4 },
  77. }),
  78. );
  79. expect(draft).toEqual({
  80. model: { kind: "model", providerId: "zhipu", modelId: "glm-5.3", level: "high" },
  81. bound: 4,
  82. });
  83. });
  84. it("没指定过模型 = 会话模型;没有自己的界 = 停在默认上;默认也未知 = null", () => {
  85. expect(initialWorkflowRunSettingsDraft(run({ concurrencyCeiling: 13 }))).toEqual({
  86. model: { kind: "session" },
  87. bound: 13,
  88. });
  89. expect(initialWorkflowRunSettingsDraft(run()).bound).toBeNull();
  90. });
  91. it("默认并发只读 run.concurrencyCeiling:老 CLI 芯片的水位是学到的 cap,不是默认,不拿来顶替", () => {
  92. expect(workflowRunDefaultConcurrency(run({ concurrencyCeiling: 12 }))).toBe(12);
  93. expect(
  94. workflowRunDefaultConcurrency(run({ concurrency: { cap: 2, ceiling: 2, limit: 1 } })),
  95. ).toBeUndefined();
  96. expect(workflowRunDefaultConcurrency(run())).toBeUndefined();
  97. });
  98. it("有自己的界、且高于默认:起点就是那个界", () => {
  99. expect(
  100. initialWorkflowRunSettingsDraft(
  101. run({ concurrencyCeiling: 8, concurrency: { cap: 8, ceiling: 8, limit: 20 } }),
  102. ).bound,
  103. ).toBe(20);
  104. });
  105. });
  106. describe("workflowRunSettingsChange:只发改过的", () => {
  107. const initial: WorkflowRunSettingsDraft = {
  108. model: { kind: "model", providerId: "zhipu", modelId: "glm-5.3", level: "high" },
  109. bound: 4,
  110. };
  111. it("两项都没变 → 没有可发的(Apply 禁用)", () => {
  112. expect(workflowRunSettingsChange(initial, { ...initial }, 13)).toBeUndefined();
  113. });
  114. it("只改上限:省略模型", () => {
  115. expect(workflowRunSettingsChange(initial, { ...initial, bound: 2 }, 13)).toEqual({
  116. maxConcurrency: 2,
  117. });
  118. });
  119. it("上限到默认 = 解除本 run 自己的界:发 null", () => {
  120. expect(workflowRunSettingsChange(initial, { ...initial, bound: 13 }, 13)).toEqual({
  121. maxConcurrency: null,
  122. });
  123. });
  124. it("默认之上是一个真的数:原样发出去,不是解除", () => {
  125. expect(workflowRunSettingsChange(initial, { ...initial, bound: 40 }, 13)).toEqual({
  126. maxConcurrency: 40,
  127. });
  128. const raised: WorkflowRunSettingsDraft = { model: { kind: "session" }, bound: 40 };
  129. expect(workflowRunSettingsChange(raised, { ...raised, bound: 13 }, 13)).toEqual({
  130. maxConcurrency: null,
  131. });
  132. });
  133. it("起点就在默认上,停在那里不算改", () => {
  134. const atCeiling: WorkflowRunSettingsDraft = { model: { kind: "session" }, bound: 13 };
  135. expect(workflowRunSettingsChange(atCeiling, { ...atCeiling }, 13)).toBeUndefined();
  136. });
  137. it("换模型发规范串;只换思考档也算换了模型", () => {
  138. expect(
  139. workflowRunSettingsChange(
  140. initial,
  141. { ...initial, model: { kind: "model", providerId: "zhipu", modelId: "glm-5.3-flash" } },
  142. 13,
  143. ),
  144. ).toEqual({ subagentModel: "zhipu/glm-5.3-flash" });
  145. expect(
  146. workflowRunSettingsChange(
  147. initial,
  148. {
  149. ...initial,
  150. model: { kind: "model", providerId: "zhipu", modelId: "glm-5.3", level: "low" },
  151. },
  152. 13,
  153. ),
  154. ).toEqual({ subagentModel: "zhipu/glm-5.3$low" });
  155. });
  156. it("回到会话模型:发 null;两项一起改就一起发", () => {
  157. expect(
  158. workflowRunSettingsChange(initial, { model: { kind: "session" }, bound: 6 }, 13),
  159. ).toEqual({ subagentModel: null, maxConcurrency: 6 });
  160. });
  161. it("默认未知时数就是数,不会被读成解除", () => {
  162. expect(workflowRunSettingsChange(initial, { ...initial, bound: 40 }, undefined)).toEqual({
  163. maxConcurrency: 40,
  164. });
  165. });
  166. it("步进器只有下限 1、没有上限:默认是起点不是天花板", () => {
  167. expect(clampWorkflowRunSettingsBound(0)).toBe(1);
  168. expect(clampWorkflowRunSettingsBound(20)).toBe(20);
  169. expect(clampWorkflowRunSettingsBound(2.7)).toBe(2);
  170. expect(clampWorkflowRunSettingsBound(500)).toBe(500);
  171. });
  172. });
  173. describe("后果句与拒绝", () => {
  174. const en = formatterFor(enUS);
  175. const zh = formatterFor(zhCN);
  176. const modelChange = { subagentModel: "zhipu/glm-5.3" };
  177. it("后果句随 run 状态换;running 的中文是用户定下的原句", () => {
  178. expect(zh({ id: workflowRunSettingsConsequenceId("running", modelChange)! })).toBe(
  179. "将停止当前运行,以新设置另起一次运行,已完成的步骤会保留。",
  180. );
  181. expect(en({ id: workflowRunSettingsConsequenceId("pending", modelChange)! })).toBe(
  182. "Starts a new run with these settings; this one has not begun any step yet.",
  183. );
  184. expect(zh({ id: workflowRunSettingsConsequenceId("stopped", modelChange)! })).toBe(
  185. "将以新设置另起一次运行接着跑,已完成的步骤会保留。",
  186. );
  187. expect(en({ id: workflowRunSettingsConsequenceId("errored", modelChange)! })).toBe(
  188. "Retries as a new run with these settings; finished steps are kept.",
  189. );
  190. });
  191. // 什么都还没改:Apply 禁用,没有后果可说;这时念任何一句都是替还没做的改动下结论
  192. // (原先 running 念「将停止当前运行」,而只改并发恰恰不会停)。
  193. it.each(["running", "pending", "stopped", "errored"] as const)(
  194. "%s 的 run 什么都还没改:没有后果句",
  195. (status) => {
  196. expect(workflowRunSettingsConsequenceId(status, undefined)).toBeUndefined();
  197. },
  198. );
  199. // 只改并发上限、而且 run 正在跑:就地生效(docs/dynamic-workflow/concurrency.md)——这一句不能再
  200. // 念「将停止当前运行」,那是它唯一说错话的场景。
  201. it("在跑的 run 只改并发上限:后果句换成「就地生效」", () => {
  202. expect(zh({ id: workflowRunSettingsConsequenceId("running", { maxConcurrency: 3 })! })).toBe(
  203. "立即应用到当前运行,不会新起一次运行。",
  204. );
  205. expect(en({ id: workflowRunSettingsConsequenceId("running", { maxConcurrency: null })! })).toBe(
  206. "Applies to this run right away; no new run is started.",
  207. );
  208. });
  209. it.each([
  210. ["还改了模型", "running" as const, { maxConcurrency: 3, subagentModel: "zhipu/glm-5.3" }],
  211. ["只改了模型", "running" as const, { subagentModel: null }],
  212. ])("%s:仍是「另起一次运行」的原句", (_case, status, change) => {
  213. expect(workflowRunSettingsConsequenceId(status, change)).toBe(
  214. "chat.toolCall.workflow.run.settings.consequence.running",
  215. );
  216. });
  217. it.each([
  218. ["pending", "pending" as const],
  219. ["stopped", "stopped" as const],
  220. ["errored", "errored" as const],
  221. ])("%s 的 run 不在飞:就地生效无从谈起,仍念各自的原句", (_case, status) => {
  222. expect(workflowRunSettingsConsequenceId(status, { maxConcurrency: 3 })).toBe(
  223. `chat.toolCall.workflow.run.settings.consequence.${status}`,
  224. );
  225. });
  226. it("词表内的 reason 反查成一句话;compile_failed 的诊断进细节块", () => {
  227. const rejection = describeWorkflowRunSettingsRejection({
  228. status: "rejected",
  229. reasonCode: "fault.command.workflowRunSettingsRejected.compile_failed",
  230. message: "L3:C1 boom",
  231. });
  232. expect(rejection).toEqual({
  233. reason: "compile_failed",
  234. code: "fault.command.workflowRunSettingsRejected.compile_failed",
  235. message: "L3:C1 boom",
  236. });
  237. expect(workflowRunSettingsRejectionDetail(rejection!)).toBe("L3:C1 boom");
  238. expect(en({ id: workflowRunSettingsRejectionMessageId(rejection!) })).toContain(
  239. "no longer compiles",
  240. );
  241. });
  242. it("start_failed 的原因嵌进那句话,不再重复一个细节块", () => {
  243. const rejection = describeWorkflowRunSettingsRejection({
  244. status: "rejected",
  245. reasonCode: "fault.command.workflowRunSettingsRejected.start_failed",
  246. message: "port gone",
  247. })!;
  248. expect(workflowRunSettingsRejectionDetail(rejection)).toBeUndefined();
  249. expect(
  250. en({ id: workflowRunSettingsRejectionMessageId(rejection) }, { message: "port gone" }),
  251. ).toBe("The new run could not be started (port gone).");
  252. });
  253. it("能力缺席 → unsupported;词表外 → generic 带 code;accepted / noop 不是拒绝", () => {
  254. expect(
  255. describeWorkflowRunSettingsRejection({
  256. status: "rejected",
  257. reasonCode: "fault.command.capabilityUnsupported",
  258. })?.reason,
  259. ).toBe("unsupported");
  260. const generic = describeWorkflowRunSettingsRejection({
  261. status: "failed",
  262. reasonCode: "fault.command.workflowRunSettingsRejected.bogus",
  263. })!;
  264. expect(generic.reason).toBe("generic");
  265. expect(zh({ id: workflowRunSettingsRejectionMessageId(generic) }, { code: generic.code })).toBe(
  266. "设置没能调整(fault.command.workflowRunSettingsRejected.bogus)。",
  267. );
  268. expect(describeWorkflowRunSettingsRejection({ status: "accepted" })).toBeUndefined();
  269. expect(describeWorkflowRunSettingsRejection({ status: "noop" })).toBeUndefined();
  270. });
  271. it("每个 reason 在两种语言里都有词条", () => {
  272. for (const reason of [
  273. "not_found",
  274. "not_configurable",
  275. "unchanged",
  276. "script_missing",
  277. "model_unavailable",
  278. "compile_failed",
  279. "missing_boundaries",
  280. "start_failed",
  281. "unsupported",
  282. "generic",
  283. ]) {
  284. const id = `chat.toolCall.workflow.run.settings.rejection.${reason}`;
  285. expect(enUS[id], id).toBeTruthy();
  286. expect(zhCN[id], id).toBeTruthy();
  287. }
  288. });
  289. });
  290. describe("会话模型", () => {
  291. it("优先持久的稀疏选择,退回 provider / model 投影,都读不出即缺席", () => {
  292. expect(
  293. workflowSessionModelOf({
  294. modelSelection: { providerId: "zhipu", modelId: "glm-5.3" },
  295. provider: "x",
  296. model: "y",
  297. }),
  298. ).toEqual({ providerId: "zhipu", modelId: "glm-5.3" });
  299. expect(workflowSessionModelOf({ provider: "zhipu", model: "glm-5.3" })).toEqual({
  300. providerId: "zhipu",
  301. modelId: "glm-5.3",
  302. });
  303. expect(workflowSessionModelOf({ provider: "", model: "" })).toBeUndefined();
  304. expect(workflowSessionModelOf(undefined)).toBeUndefined();
  305. });
  306. });
  307. describe("设置轮的措辞", () => {
  308. const both: WorkflowSettingsAmendMeta = {
  309. predecessorRunId: "run-a",
  310. subagentModel: { from: "zhipu/glm-5.3", to: "zhipu/glm-5.3-flash$high" },
  311. maxConcurrency: { to: 4 },
  312. ceiling: 13,
  313. };
  314. it("转写行:模型在前、上限在后,只写改过的;英文与中文", () => {
  315. expect(workflowSettingsChangeSegments(both, { formatMessage: formatterFor(enUS) })).toEqual([
  316. "subagents on glm-5.3-flash",
  317. "at most 4 at once",
  318. ]);
  319. expect(workflowSettingsChangeSegments(both, { formatMessage: formatterFor(zhCN) })).toEqual([
  320. "子代理改用 glm-5.3-flash",
  321. "最多 4 个同时运行",
  322. ]);
  323. });
  324. it("回到会话模型 / 解除上限(to 缺席或等于默认)各有自己的话", () => {
  325. const reset: WorkflowSettingsAmendMeta = {
  326. predecessorRunId: "run-a",
  327. subagentModel: { from: "zhipu/glm-5.3" },
  328. maxConcurrency: { from: 4, to: 13 },
  329. ceiling: 13,
  330. };
  331. expect(workflowSettingsChangeSegments(reset, { formatMessage: formatterFor(zhCN) })).toEqual([
  332. "子代理改回会话模型",
  333. "上限恢复为默认",
  334. ]);
  335. expect(
  336. workflowSettingsChangeSegments(
  337. { predecessorRunId: "run-a", maxConcurrency: { from: 4 } },
  338. { formatMessage: formatterFor(enUS) },
  339. ),
  340. ).toEqual(["limit back to the default"]);
  341. });
  342. it("高于默认的上限念它自己的数", () => {
  343. expect(
  344. workflowSettingsChangeSegments(
  345. { maxConcurrency: { to: 40 }, ceiling: 13 },
  346. { formatMessage: formatterFor(zhCN) },
  347. ),
  348. ).toEqual(["最多 40 个同时运行"]);
  349. });
  350. it("来龙去脉:每项一行「from → to」,缺席的一端写默认,默认已知时带数字", () => {
  351. expect(workflowSettingsProvenanceRows(both, { formatMessage: formatterFor(enUS) })).toEqual([
  352. { key: "model", label: "Subagent model", value: "glm-5.3 → glm-5.3-flash" },
  353. { key: "limit", label: "Max concurrency", value: "default 13 → 4" },
  354. ]);
  355. expect(
  356. workflowSettingsProvenanceRows(
  357. {
  358. predecessorRunId: "run-a",
  359. subagentModel: { to: "zhipu/glm-5.3" },
  360. maxConcurrency: { from: 2 },
  361. },
  362. { formatMessage: formatterFor(zhCN) },
  363. ),
  364. ).toEqual([
  365. { key: "model", label: "子代理模型", value: "会话模型 → glm-5.3" },
  366. { key: "limit", label: "最大并发数", value: "2 → 默认" },
  367. ]);
  368. // 高于默认的一端念数,不再被当成「本机上限」。
  369. expect(
  370. workflowSettingsProvenanceRows(
  371. { maxConcurrency: { from: 40 }, ceiling: 13 },
  372. { formatMessage: formatterFor(zhCN) },
  373. ),
  374. ).toEqual([{ key: "limit", label: "最大并发数", value: "40 → 默认 13" }]);
  375. });
  376. it("两处的词条键集在两种语言下一致", () => {
  377. for (const prefix of [
  378. "chat.toolCall.workflow.settingsChange.",
  379. "chat.workflowLaunch.settings",
  380. ]) {
  381. const en = Object.keys(enUS).filter((key) => key.startsWith(prefix));
  382. const zh = Object.keys(zhCN).filter((key) => key.startsWith(prefix));
  383. expect(en.length).toBeGreaterThan(0);
  384. expect(en).toEqual(zh);
  385. }
  386. });
  387. });
  388. describe("resolveWorkflowRunGraph:只有「配置」出来的 run 借前驱的图", () => {
  389. const GRAPH = { steps: [{ id: "ask#1" }] } as never;
  390. const graphs = new Map([["tool-a", GRAPH]]);
  391. const runs = [
  392. run({ runId: "run-a", toolCallId: "tool-a", status: "stopped", supersededBy: "run-b" }),
  393. run({ runId: "run-b", toolCallId: "settings-1", resumedFrom: "run-a" }),
  394. run({ runId: "run-c", toolCallId: "settings-2", resumedFrom: "run-b" }),
  395. run({ runId: "run-d", toolCallId: "tool-amend", resumedFrom: "run-a" }),
  396. ];
  397. it("自己的图在行窗口里就用自己的", () => {
  398. expect(resolveWorkflowRunGraph(graphs, "tool-a", runs)).toBe(GRAPH);
  399. });
  400. it("设置轮还没落地:经 resumedFrom 借前驱的图,链上多次「配置」也一路借到", () => {
  401. expect(resolveWorkflowRunGraph(graphs, "settings-1", runs)).toBe(GRAPH);
  402. expect(resolveWorkflowRunGraph(graphs, "settings-2", runs)).toBe(GRAPH);
  403. });
  404. it("改过脚本的修订(AmendWorkflow 行)不借:它画的是另一张图", () => {
  405. expect(resolveWorkflowRunGraph(graphs, "tool-amend", runs)).toBeUndefined();
  406. });
  407. it("前驱不在投影里 / 链成环时不借也不死循环", () => {
  408. expect(resolveWorkflowRunGraph(new Map(), "settings-1", runs)).toBeUndefined();
  409. const loop = [
  410. run({ runId: "x", toolCallId: "settings-x", resumedFrom: "y" }),
  411. run({ runId: "y", toolCallId: "settings-y", resumedFrom: "x" }),
  412. ];
  413. expect(resolveWorkflowRunGraph(new Map(), "settings-x", loop)).toBeUndefined();
  414. });
  415. });
  416. describe("replaceWorkflowRunSidePane:面板跟着工作流走", () => {
  417. const base = {
  418. workspaceKey: "/w",
  419. workspacePath: "/w",
  420. parentSessionId: "parent-a",
  421. };
  422. function withTabs() {
  423. const first = openWorkflowRunSidePane(null, {
  424. ...base,
  425. toolCallId: "tool-a",
  426. runId: "run-a",
  427. workflowName: "Fan-out review",
  428. });
  429. const stamped = {
  430. ...first,
  431. tabs: first.tabs.map((tab) => ({ ...tab, ownerTaskId: "task-1" })),
  432. };
  433. return openWorkflowRunSidePane(
  434. openWorkflowRunSidePane(stamped, { ...base, toolCallId: "tool-z", runId: "run-z" }),
  435. { ...base, toolCallId: "tool-a", runId: "run-a" },
  436. );
  437. }
  438. it("原地换成新 run 的 tab:同一个位置、沿用名字与归属,原来是活动 tab 就还是活动 tab", () => {
  439. const state = withTabs();
  440. const next = replaceWorkflowRunSidePane(state, {
  441. ...base,
  442. toolCallId: "settings-1",
  443. runId: "run-b",
  444. replaceRunId: "run-a",
  445. })!;
  446. const replaced = next.tabs[0] as WorkflowRunSidePaneTab;
  447. expect(next.tabs).toHaveLength(2);
  448. expect(replaced).toMatchObject({
  449. type: "workflow-run",
  450. runId: "run-b",
  451. toolCallId: "settings-1",
  452. workflowName: "Fan-out review",
  453. ownerTaskId: "task-1",
  454. });
  455. expect(next.activeTabId).toBe(replaced.id);
  456. expect((next.tabs[1] as WorkflowRunSidePaneTab).runId).toBe("run-z");
  457. });
  458. it("旧 tab 不是活动 tab:活动 tab 不动", () => {
  459. const state = withTabs();
  460. const focusedOther = { ...state, activeTabId: state.tabs[1]!.id };
  461. const next = replaceWorkflowRunSidePane(focusedOther, {
  462. ...base,
  463. toolCallId: "settings-1",
  464. runId: "run-b",
  465. replaceRunId: "run-a",
  466. })!;
  467. expect(next.activeTabId).toBe(state.tabs[1]!.id);
  468. });
  469. it("旧 tab 已被关掉(或侧栏为空):原样返回,替换不是打开", () => {
  470. const state = withTabs();
  471. const request = { ...base, toolCallId: "settings-1", runId: "run-b", replaceRunId: "run-gone" };
  472. expect(replaceWorkflowRunSidePane(state, request)).toBe(state);
  473. expect(replaceWorkflowRunSidePane(null, request)).toBeNull();
  474. });
  475. // 就地生效的修订(只改并发上限,docs/dynamic-workflow/concurrency.md)没有后继:结果里的 runId
  476. // 就是这个 run 自己。tab 的 id 只由 runId 铸,所以「新 tab 已经开着」那一支会认出它自己、把这个
  477. // tab 关掉,留下一个指向已不存在 tab 的 activeTabId——详情页就这么整个消失。
  478. it("结果里的 run 就是被替换的那个(就地生效):原样返回,什么都不动", () => {
  479. const state = withTabs();
  480. const next = replaceWorkflowRunSidePane(state, {
  481. ...base,
  482. toolCallId: "settings-1",
  483. runId: "run-a",
  484. replaceRunId: "run-a",
  485. });
  486. expect(next).toBe(state);
  487. expect(next?.tabs.map((tab) => (tab as WorkflowRunSidePaneTab).runId)).toEqual([
  488. "run-a",
  489. "run-z",
  490. ]);
  491. });
  492. it("新 run 的 tab 已经开着:关掉旧的、聚焦那一个,不出两个", () => {
  493. const state = openWorkflowRunSidePane(withTabs(), {
  494. ...base,
  495. toolCallId: "settings-1",
  496. runId: "run-b",
  497. });
  498. const onOld = { ...state, activeTabId: state.tabs[0]!.id };
  499. const next = replaceWorkflowRunSidePane(onOld, {
  500. ...base,
  501. toolCallId: "settings-1",
  502. runId: "run-b",
  503. replaceRunId: "run-a",
  504. })!;
  505. const runIds = next.tabs.map((tab) => (tab as WorkflowRunSidePaneTab).runId);
  506. expect(runIds).toEqual(["run-z", "run-b"]);
  507. expect(next.activeTabId).toBe(next.tabs[1]!.id);
  508. });
  509. });