v4ConversationStatusPanelModel.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. import { describe, expect, it } from "vitest";
  2. import type { GitRepositorySummary } from "@zcode/shared";
  3. import type {
  4. BackgroundWorkSummary,
  5. GoalState,
  6. PlanState,
  7. ToolCallRow,
  8. WorkflowRunNode,
  9. WorkflowRunState,
  10. } from "@zcode/shared/zcode-protocol-v4";
  11. import {
  12. buildConversationStatusPanelModel,
  13. resolveSoleRunningWorkflowRunTarget,
  14. workflowRunOpenTarget,
  15. } from "@/v4/conversationStatusPanelModel.js";
  16. import {
  17. CONVERSATION_STATUS_PANEL_WIDE_OFFSET_CLASS_NAME,
  18. getConversationStatusPanelOffsetClassName,
  19. resolveConversationStatusPanelVariant,
  20. shouldUseConversationStatusPanelInlineLayout,
  21. } from "@/v4/conversationLayout.js";
  22. function gitSummary(overrides: Partial<GitRepositorySummary> = {}): GitRepositorySummary {
  23. return {
  24. workspacePath: "/repo",
  25. repoRoot: "/repo",
  26. workspaceInRepoPath: "",
  27. autoRefreshWatchPaths: [],
  28. branchName: "main",
  29. trackingBranchName: "origin/main",
  30. headRefType: "branch",
  31. ahead: 0,
  32. behind: 0,
  33. isDirty: true,
  34. isGitAvailable: true,
  35. isRepository: true,
  36. ...overrides,
  37. };
  38. }
  39. function goalState(overrides: Partial<GoalState> = {}): GoalState {
  40. return {
  41. objective: "修好聊天渲染",
  42. status: "active",
  43. iteration: 2,
  44. verifications: [],
  45. ...overrides,
  46. };
  47. }
  48. function planState(overrides: Partial<PlanState> = {}): PlanState {
  49. return {
  50. updatedAt: 1_700_000_000_000,
  51. items: [
  52. { id: "1", content: "看旧实现", status: "completed" },
  53. { id: "2", content: "补状态卡", status: "inProgress" },
  54. { id: "3", content: "跑校验", status: "pending" },
  55. ],
  56. ...overrides,
  57. };
  58. }
  59. function backgroundWork(overrides: Partial<BackgroundWorkSummary> = {}): BackgroundWorkSummary {
  60. return {
  61. workId: "work-1",
  62. kind: "subagent",
  63. title: "探索代码",
  64. status: "running",
  65. startedAt: 1_700_000_000_000,
  66. anchorRowId: null,
  67. ...overrides,
  68. };
  69. }
  70. function workflowNode(
  71. overrides: Partial<WorkflowRunNode> & Pick<WorkflowRunNode, "phase">,
  72. ): WorkflowRunNode {
  73. return { siteId: "ask#1", ordinal: 1, ...overrides };
  74. }
  75. function workflowRun(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  76. return {
  77. runId: "dwfrun-1",
  78. toolCallId: "tool-wf-1",
  79. status: "running",
  80. usage: { spentTokens: 0, nodesUsed: 0 },
  81. actors: [],
  82. nodes: [],
  83. lastEventSequence: 0,
  84. ...overrides,
  85. };
  86. }
  87. function sessionPlan(rowId: number, overrides: Partial<ToolCallRow> = {}): ToolCallRow {
  88. return {
  89. rowId,
  90. turnId: `turn-${rowId}`,
  91. createdAt: 1_700_000_000_000 + rowId,
  92. createdAtSeq: rowId,
  93. kind: "toolCall",
  94. toolCallId: `plan-${rowId}`,
  95. toolName: "ExitPlanMode",
  96. status: "success",
  97. input: { plan: `# 计划 ${rowId}` },
  98. inputText: JSON.stringify({ plan: `# 计划 ${rowId}` }),
  99. ...overrides,
  100. };
  101. }
  102. describe("buildConversationStatusPanelModel", () => {
  103. it("aggregates git, goal, todo, and running background work", () => {
  104. const model = buildConversationStatusPanelModel({
  105. gitSummary: gitSummary(),
  106. gitDirtyFileCount: 3,
  107. gitWorktreeChangeSummary: { added: 42, removed: 0 },
  108. goal: goalState(),
  109. plan: planState(),
  110. backgroundWorks: [
  111. backgroundWork(),
  112. backgroundWork({ workId: "work-bash", kind: "bash" }),
  113. backgroundWork({ workId: "work-2", status: "resultPending" }),
  114. ],
  115. runningSubagents: [
  116. {
  117. childSessionId: "child-1",
  118. subagentType: "Explore",
  119. title: "探索代码",
  120. status: "running",
  121. startedAt: 1_700_000_000_000,
  122. },
  123. ],
  124. });
  125. expect(model.hasContent).toBe(true);
  126. expect(model.git).toMatchObject({
  127. added: 42,
  128. removed: 0,
  129. dirtyFileCount: 3,
  130. isClean: false,
  131. });
  132. expect(model.goal?.objective).toBe("修好聊天渲染");
  133. expect(model.plan).toMatchObject({
  134. completedCount: 1,
  135. waitingCount: 2,
  136. totalCount: 3,
  137. });
  138. expect(model.runningBashWorks.map((work) => work.workId)).toEqual(["work-bash"]);
  139. expect(model.runningSubagentWorks.map((work) => work.childSessionId)).toEqual(["child-1"]);
  140. });
  141. // 回归:dwf run 的停止入口在任何投影形态下都不许消失——面板的停止是 spec 里三个取消入口
  142. // 之一(取消只有一条实现)。旧版把 workflow work 塞进 Terminals(runningBashWorks)来保住
  143. // 它,那个错标现在由 Workflows 分区取代;这条用例因此反过来钉「不在 Terminals 里」。
  144. it("routes a running dwf workflow work into the workflow section instead of Terminals", () => {
  145. const model = buildConversationStatusPanelModel({
  146. backgroundWorks: [backgroundWork({ workId: "dwfrun-1", kind: "workflow" })],
  147. workflowRuns: [workflowRun({ runId: "dwfrun-1" })],
  148. });
  149. expect(model.hasContent).toBe(true);
  150. expect(model.runningWorkflowRuns.map((row) => row.runId)).toEqual(["dwfrun-1"]);
  151. expect(model.runningBashWorks).toEqual([]);
  152. });
  153. it("joins each active run with its background work by workId ≡ runId", () => {
  154. const model = buildConversationStatusPanelModel({
  155. backgroundWorks: [
  156. backgroundWork({
  157. workId: "dwfrun-1",
  158. kind: "workflow",
  159. title: "重构状态胶囊",
  160. startedAt: 1_700_000_000_500,
  161. cancellable: true,
  162. }),
  163. ],
  164. workflowRuns: [
  165. workflowRun({
  166. runId: "dwfrun-1",
  167. toolCallId: "tool-wf-9",
  168. nodes: [
  169. workflowNode({ phase: "settled" }),
  170. workflowNode({ ordinal: 2, phase: "queued" }),
  171. ],
  172. }),
  173. ],
  174. });
  175. expect(model.runningWorkflowRuns).toEqual([
  176. {
  177. runId: "dwfrun-1",
  178. toolCallId: "tool-wf-9",
  179. status: "running",
  180. nodesSettled: 1,
  181. nodesTotal: 2,
  182. phases: [],
  183. title: "重构状态胶囊",
  184. startedAt: 1_700_000_000_500,
  185. workId: "dwfrun-1",
  186. cancellable: true,
  187. },
  188. ]);
  189. });
  190. // 无 work 的 run 照常成行:状态词与步数来自投影本身,只有时长和 Stop 依赖 work。
  191. it("keeps a run without background work as a status-only row", () => {
  192. const model = buildConversationStatusPanelModel({
  193. workflowRuns: [
  194. workflowRun({ runId: "dwfrun-lonely", nodes: [workflowNode({ phase: "settled" })] }),
  195. ],
  196. });
  197. expect(model.hasContent).toBe(true);
  198. expect(model.runningWorkflowRuns).toEqual([
  199. {
  200. runId: "dwfrun-lonely",
  201. toolCallId: "tool-wf-1",
  202. status: "running",
  203. nodesSettled: 1,
  204. nodesTotal: 1,
  205. phases: [],
  206. },
  207. ]);
  208. });
  209. // 偏斜兜底:旧 CLI 不发 workflowRuns 投影键,只有 backgroundWorks 那一条 workflow work。
  210. // 降级行没有状态词与步数(无从得知),但题名/时长/Stop 齐全——cancel 入口在任何偏斜下不消失。
  211. it("degrades a workflow work with no matching run into a title-and-stop row", () => {
  212. const model = buildConversationStatusPanelModel({
  213. backgroundWorks: [
  214. backgroundWork({
  215. workId: "dwfrun-skew",
  216. kind: "workflow",
  217. title: "旧 CLI 的 run",
  218. startedAt: 1_700_000_001_000,
  219. }),
  220. ],
  221. workflowRuns: [],
  222. });
  223. expect(model.runningWorkflowRuns).toEqual([
  224. {
  225. runId: "dwfrun-skew",
  226. workId: "dwfrun-skew",
  227. title: "旧 CLI 的 run",
  228. startedAt: 1_700_000_001_000,
  229. // cancellable 缺省即可停(与 Agent 行同款 `!== false` 语义)。
  230. cancellable: true,
  231. },
  232. ]);
  233. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("status");
  234. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("nodesSettled");
  235. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("phases");
  236. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("toolCallId");
  237. expect(model.hasContent).toBe(true);
  238. });
  239. // 站点表与侧栏运行行同一条推导(shared 的 deriveSessionWorkflowPhases):声明表在场用声明表,
  240. // 当前站 running、进入过的 done、其余 pending;任务岛不另写第二套。
  241. it("carries the sidebar's phase list and the current phase on a live row", () => {
  242. const model = buildConversationStatusPanelModel({
  243. workflowRuns: [
  244. workflowRun({
  245. runId: "dwfrun-1",
  246. phaseNames: ["盘点", "计划", "构建"],
  247. phases: [{ name: "盘点", rounds: 1 }],
  248. currentPhase: "计划",
  249. }),
  250. ],
  251. });
  252. expect(model.runningWorkflowRuns[0]?.phases).toEqual([
  253. { name: "盘点", status: "done" },
  254. { name: "计划", status: "running" },
  255. { name: "构建", status: "pending" },
  256. ]);
  257. expect(model.runningWorkflowRuns[0]?.currentPhase).toBe("计划");
  258. });
  259. // 降级行排在 run 支撑的行之后:投影序是启动序,偏斜行没有位置可言,追加是唯一不撒谎的落位。
  260. it("appends degraded skew rows after the run-backed rows", () => {
  261. const model = buildConversationStatusPanelModel({
  262. backgroundWorks: [
  263. backgroundWork({ workId: "dwfrun-skew", kind: "workflow" }),
  264. backgroundWork({ workId: "dwfrun-2", kind: "workflow" }),
  265. ],
  266. workflowRuns: [workflowRun({ runId: "dwfrun-1" }), workflowRun({ runId: "dwfrun-2" })],
  267. });
  268. expect(model.runningWorkflowRuns.map((row) => row.runId)).toEqual([
  269. "dwfrun-1",
  270. "dwfrun-2",
  271. "dwfrun-skew",
  272. ]);
  273. });
  274. it("counts pending as active and drops terminal runs from the section", () => {
  275. const model = buildConversationStatusPanelModel({
  276. workflowRuns: [
  277. workflowRun({ runId: "dwfrun-pending", status: "pending" }),
  278. workflowRun({ runId: "dwfrun-running", status: "running" }),
  279. workflowRun({ runId: "dwfrun-completed", status: "completed" }),
  280. workflowRun({ runId: "dwfrun-failed", status: "failed" }),
  281. workflowRun({ runId: "dwfrun-cancelled", status: "cancelled" }),
  282. ],
  283. });
  284. expect(model.runningWorkflowRuns.map((row) => row.runId)).toEqual([
  285. "dwfrun-pending",
  286. "dwfrun-running",
  287. ]);
  288. expect(model.runningWorkflowRuns[0]?.status).toBe("pending");
  289. });
  290. // 顺序 = 投影 runs 序 = 启动序;模型不排序(重排会让面板行在每次投影更新时跳位)。
  291. it("preserves projection order for active runs", () => {
  292. const model = buildConversationStatusPanelModel({
  293. workflowRuns: [
  294. workflowRun({ runId: "dwfrun-c" }),
  295. workflowRun({ runId: "dwfrun-a", status: "completed" }),
  296. workflowRun({ runId: "dwfrun-b", status: "pending" }),
  297. ],
  298. });
  299. expect(model.runningWorkflowRuns.map((row) => row.runId)).toEqual(["dwfrun-c", "dwfrun-b"]);
  300. });
  301. // 步数与聊天紧凑卡同源(workflowRunCardJoin 的共享 helper):分母是**已排程**节点数,
  302. // 动态工作流没有静态总数,未结算的相位一律不计入 settled。
  303. it("counts settled over observed nodes through the shared card helper", () => {
  304. const model = buildConversationStatusPanelModel({
  305. workflowRuns: [
  306. workflowRun({
  307. nodes: [
  308. workflowNode({ phase: "settled", outcome: "ok" }),
  309. workflowNode({ ordinal: 2, phase: "settled", outcome: "failed" }),
  310. workflowNode({ ordinal: 3, phase: "settled", outcome: "cancelled" }),
  311. workflowNode({ ordinal: 4, phase: "dispatched" }),
  312. workflowNode({ ordinal: 5, phase: "repairing" }),
  313. workflowNode({ ordinal: 6, phase: "nudged" }),
  314. workflowNode({ ordinal: 7, phase: "queued" }),
  315. ],
  316. }),
  317. ],
  318. });
  319. expect(model.runningWorkflowRuns[0]).toMatchObject({ nodesSettled: 3, nodesTotal: 7 });
  320. });
  321. // 联接按**字段含义**分簇,而不是按 work 的 status 一刀切:title / startedAt 是静态元数据,
  322. // work 走到 resultPending 时把它们一起丢掉,行会当场闪成 fallbackName 并失去时长——run 还
  323. // 在跑,这就是纯视觉故障。只有 Stop 真的没了:已结束的 work 没有可取消的东西。
  324. it("keeps title and elapsed from a settled work while dropping only its Stop control", () => {
  325. const model = buildConversationStatusPanelModel({
  326. backgroundWorks: [
  327. backgroundWork({
  328. workId: "dwfrun-1",
  329. kind: "workflow",
  330. status: "resultPending",
  331. title: "结果待投递的工作流",
  332. startedAt: 1_700_000_002_000,
  333. cancellable: true,
  334. }),
  335. backgroundWork({ workId: "work-bash", kind: "bash" }),
  336. ],
  337. workflowRuns: [workflowRun({ runId: "dwfrun-1" })],
  338. });
  339. expect(model.runningWorkflowRuns).toEqual([
  340. {
  341. runId: "dwfrun-1",
  342. toolCallId: "tool-wf-1",
  343. status: "running",
  344. nodesSettled: 0,
  345. nodesTotal: 0,
  346. phases: [],
  347. title: "结果待投递的工作流",
  348. startedAt: 1_700_000_002_000,
  349. },
  350. ]);
  351. // Stop 的两个前提字段一起缺席:渲染层只看 workId 就能判断这行停不了。
  352. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("workId");
  353. expect(model.runningWorkflowRuns[0]).not.toHaveProperty("cancellable");
  354. expect(model.runningBashWorks.map((work) => work.workId)).toEqual(["work-bash"]);
  355. });
  356. // 反向:running 的 work 把 Stop 的两个字段一起带上(与上一条共同钉住这条分簇线)。
  357. it("gates the Stop control on the work still running", () => {
  358. const model = buildConversationStatusPanelModel({
  359. backgroundWorks: [
  360. backgroundWork({ workId: "dwfrun-1", kind: "workflow", cancellable: false }),
  361. ],
  362. workflowRuns: [workflowRun({ runId: "dwfrun-1" })],
  363. });
  364. expect(model.runningWorkflowRuns[0]).toMatchObject({
  365. workId: "dwfrun-1",
  366. cancellable: false,
  367. });
  368. });
  369. // 降级行只收 running 的 work:既没有 run、work 也已结束,这行没有任何可操作的东西,
  370. // 显示它等于在面板上留一条永不消失的死行。
  371. it("does not degrade a settled workflow work with no run into a row", () => {
  372. const model = buildConversationStatusPanelModel({
  373. backgroundWorks: [
  374. backgroundWork({ workId: "dwfrun-gone", kind: "workflow", status: "resultPending" }),
  375. backgroundWork({ workId: "dwfrun-failed", kind: "workflow", status: "failed" }),
  376. backgroundWork({ workId: "dwfrun-cancelled", kind: "workflow", status: "cancelled" }),
  377. ],
  378. workflowRuns: [],
  379. });
  380. expect(model.runningWorkflowRuns).toEqual([]);
  381. expect(model.hasContent).toBe(false);
  382. });
  383. it("stays empty without repository or session status", () => {
  384. const model = buildConversationStatusPanelModel({
  385. gitSummary: gitSummary({ isRepository: false }),
  386. gitDirtyFileCount: 0,
  387. gitWorktreeChangeSummary: { added: 0, removed: 0 },
  388. goal: null,
  389. plan: null,
  390. backgroundWorks: [],
  391. });
  392. expect(model.hasContent).toBe(false);
  393. expect(model.git).toBeNull();
  394. });
  395. it("keeps the complete Todo projection in snapshot order when statuses are mixed", () => {
  396. const model = buildConversationStatusPanelModel({
  397. plan: planState({
  398. items: [
  399. { id: "done-1", content: "已完成一", status: "completed" },
  400. { id: "active", content: "进行中", status: "inProgress" },
  401. { id: "pending", content: "待处理", status: "pending" },
  402. { id: "done-2", content: "已完成二", status: "completed" },
  403. { id: "pending-2", content: "待处理二", status: "pending" },
  404. ],
  405. }),
  406. });
  407. expect(model.plan?.displayItems.map((item) => item.id)).toEqual([
  408. "done-1",
  409. "active",
  410. "pending",
  411. "done-2",
  412. "pending-2",
  413. ]);
  414. });
  415. it("joins running Agent controls only by exact child session identity", () => {
  416. const model = buildConversationStatusPanelModel({
  417. backgroundWorks: [
  418. backgroundWork({
  419. workId: "agent-control-work",
  420. childSessionId: "child-match",
  421. cancellable: true,
  422. }),
  423. backgroundWork({
  424. workId: "agent-disabled-work",
  425. childSessionId: "child-disabled",
  426. cancellable: false,
  427. }),
  428. backgroundWork({
  429. workId: "agent-terminal-work",
  430. childSessionId: "child-terminal",
  431. status: "cancelled",
  432. }),
  433. backgroundWork({
  434. workId: "agent-ambiguous-a",
  435. childSessionId: "child-ambiguous",
  436. }),
  437. backgroundWork({
  438. workId: "agent-ambiguous-b",
  439. childSessionId: "child-ambiguous",
  440. }),
  441. ],
  442. runningSubagents: [
  443. {
  444. childSessionId: "child-match",
  445. subagentType: "Explore",
  446. title: "相同标题",
  447. status: "running",
  448. },
  449. {
  450. childSessionId: "child-disabled",
  451. subagentType: "Explore",
  452. title: "不可停止",
  453. status: "running",
  454. },
  455. {
  456. childSessionId: "child-foreground",
  457. subagentType: "Explore",
  458. title: "相同标题",
  459. status: "running",
  460. },
  461. {
  462. childSessionId: "child-ambiguous",
  463. subagentType: "Explore",
  464. title: "身份重复",
  465. status: "running",
  466. },
  467. ],
  468. });
  469. expect(model.runningSubagentWorks).toMatchObject([
  470. {
  471. childSessionId: "child-match",
  472. controlWorkId: "agent-control-work",
  473. cancellable: true,
  474. },
  475. {
  476. childSessionId: "child-disabled",
  477. controlWorkId: "agent-disabled-work",
  478. cancellable: false,
  479. },
  480. {
  481. childSessionId: "child-foreground",
  482. },
  483. {
  484. childSessionId: "child-ambiguous",
  485. },
  486. ]);
  487. expect(model.runningSubagentWorks[2]).not.toHaveProperty("controlWorkId");
  488. expect(model.runningSubagentWorks[3]).not.toHaveProperty("controlWorkId");
  489. });
  490. it("keeps a uniquely identified running Agent controllable while directory projection catches up", () => {
  491. const model = buildConversationStatusPanelModel({
  492. backgroundWorks: [
  493. backgroundWork({
  494. workId: "agent-runtime-work",
  495. childSessionId: "child-runtime",
  496. title: "运行中的 Agent",
  497. blocked: true,
  498. cancellable: true,
  499. }),
  500. backgroundWork({
  501. workId: "ambiguous-a",
  502. childSessionId: "child-ambiguous",
  503. }),
  504. backgroundWork({
  505. workId: "ambiguous-b",
  506. childSessionId: "child-ambiguous",
  507. }),
  508. ],
  509. runningSubagents: [],
  510. });
  511. expect(model.runningSubagentWorks).toEqual([
  512. expect.objectContaining({
  513. agentId: "agent-runtime-work",
  514. childSessionId: "child-runtime",
  515. controlWorkId: "agent-runtime-work",
  516. status: "blocked",
  517. title: "运行中的 Agent",
  518. }),
  519. ]);
  520. });
  521. it("does not show git tools for a clean repository without worktree line changes", () => {
  522. const model = buildConversationStatusPanelModel({
  523. gitSummary: gitSummary({ isDirty: false, ahead: 0 }),
  524. gitDirtyFileCount: 0,
  525. gitWorktreeChangeSummary: { added: 0, removed: 0 },
  526. goal: null,
  527. plan: null,
  528. backgroundWorks: [],
  529. });
  530. expect(model.hasContent).toBe(false);
  531. expect(model.git).toBeNull();
  532. });
  533. it("lists only terminal ExitPlanMode rows with markdown, newest first", () => {
  534. const model = buildConversationStatusPanelModel({
  535. workspacePath: "/repo",
  536. sessionPlans: [
  537. sessionPlan(4, { status: "cancelled" }),
  538. sessionPlan(8, { status: "error", input: { plan: "# 最新计划" } }),
  539. sessionPlan(9, { status: "running" }),
  540. sessionPlan(10, { toolName: "Write" }),
  541. sessionPlan(11, { input: {}, inputText: "{}" }),
  542. ],
  543. });
  544. expect(model.hasContent).toBe(true);
  545. expect(model.sessionPlans?.items.map((item) => item.toolCallId)).toEqual(["plan-8", "plan-4"]);
  546. expect(model.sessionPlans?.items[0]?.title).toBe("最新计划");
  547. });
  548. });
  549. describe("conversation status panel layout", () => {
  550. it("maps the status panel layout to its offset class", () => {
  551. expect(getConversationStatusPanelOffsetClassName("auto")).toBe(
  552. CONVERSATION_STATUS_PANEL_WIDE_OFFSET_CLASS_NAME,
  553. );
  554. expect(getConversationStatusPanelOffsetClassName("inline")).toBe(
  555. CONVERSATION_STATUS_PANEL_WIDE_OFFSET_CLASS_NAME,
  556. );
  557. expect(getConversationStatusPanelOffsetClassName("none")).toBeUndefined();
  558. });
  559. it("preserves manual variants while leaving auto mode to CSS", () => {
  560. expect(
  561. resolveConversationStatusPanelVariant({
  562. variantOverride: "mini",
  563. }),
  564. ).toBe("mini");
  565. expect(
  566. resolveConversationStatusPanelVariant({
  567. variantOverride: null,
  568. }),
  569. ).toBe("auto");
  570. });
  571. it("only applies inline offset when content can render in expanded inline mode", () => {
  572. expect(
  573. shouldUseConversationStatusPanelInlineLayout({
  574. hasContent: true,
  575. variant: "auto",
  576. }),
  577. ).toBe(true);
  578. expect(
  579. shouldUseConversationStatusPanelInlineLayout({
  580. hasContent: false,
  581. variant: "panel",
  582. }),
  583. ).toBe(false);
  584. expect(
  585. shouldUseConversationStatusPanelInlineLayout({
  586. hasContent: true,
  587. variant: "mini",
  588. }),
  589. ).toBe(false);
  590. });
  591. });
  592. // composer 徽标直达(docs/dynamic-workflow/presentation.md「Other places a run appears」):
  593. // 判定吃的是 buildConversationStatusPanelModel 的产出,徽标与胶囊共用同一份运行态真值。
  594. describe("resolveSoleRunningWorkflowRunTarget", () => {
  595. const workflowWork = backgroundWork({
  596. workId: "dwfrun-1",
  597. kind: "workflow",
  598. title: "发布前检查",
  599. startedAt: 1_700_000_000_000,
  600. });
  601. it("returns the open target when the only running activity is one workflow run", () => {
  602. const model = buildConversationStatusPanelModel({
  603. backgroundWorks: [workflowWork],
  604. workflowRuns: [workflowRun()],
  605. });
  606. expect(resolveSoleRunningWorkflowRunTarget(model)).toEqual({
  607. runId: "dwfrun-1",
  608. toolCallId: "tool-wf-1",
  609. workflowName: "发布前检查",
  610. });
  611. });
  612. it("treats a pending run as running and omits the name when the title is the id", () => {
  613. const model = buildConversationStatusPanelModel({
  614. backgroundWorks: [
  615. backgroundWork({ workId: "dwfrun-1", kind: "workflow", title: "dwfrun-1" }),
  616. ],
  617. workflowRuns: [workflowRun({ status: "pending" })],
  618. });
  619. expect(resolveSoleRunningWorkflowRunTarget(model)).toEqual({
  620. runId: "dwfrun-1",
  621. toolCallId: "tool-wf-1",
  622. });
  623. });
  624. it("returns null once a terminal or a subagent runs alongside the workflow", () => {
  625. const withBash = buildConversationStatusPanelModel({
  626. backgroundWorks: [workflowWork, backgroundWork({ workId: "bash-1", kind: "bash" })],
  627. workflowRuns: [workflowRun()],
  628. });
  629. expect(resolveSoleRunningWorkflowRunTarget(withBash)).toBeNull();
  630. const withSubagent = buildConversationStatusPanelModel({
  631. backgroundWorks: [workflowWork],
  632. workflowRuns: [workflowRun()],
  633. runningSubagents: [
  634. {
  635. childSessionId: "child-1",
  636. agentId: "agent-1",
  637. subagentType: "Explore",
  638. title: "分析",
  639. status: "running",
  640. },
  641. ],
  642. });
  643. expect(resolveSoleRunningWorkflowRunTarget(withSubagent)).toBeNull();
  644. });
  645. it("returns null for two workflow runs and for a sole run without a tool call id", () => {
  646. const two = buildConversationStatusPanelModel({
  647. backgroundWorks: [workflowWork, backgroundWork({ workId: "dwfrun-2", kind: "workflow" })],
  648. workflowRuns: [workflowRun(), workflowRun({ runId: "dwfrun-2", toolCallId: "tool-wf-2" })],
  649. });
  650. expect(resolveSoleRunningWorkflowRunTarget(two)).toBeNull();
  651. const withoutToolCallId = buildConversationStatusPanelModel({
  652. backgroundWorks: [workflowWork],
  653. workflowRuns: [workflowRun({ toolCallId: undefined })],
  654. });
  655. expect(resolveSoleRunningWorkflowRunTarget(withoutToolCallId)).toBeNull();
  656. // 偏斜降级行(work 有、run 无)同样没有可开的详情页。
  657. const degraded = buildConversationStatusPanelModel({
  658. backgroundWorks: [workflowWork],
  659. workflowRuns: [],
  660. });
  661. expect(degraded.runningWorkflowRuns).toHaveLength(1);
  662. expect(resolveSoleRunningWorkflowRunTarget(degraded)).toBeNull();
  663. expect(resolveSoleRunningWorkflowRunTarget(buildConversationStatusPanelModel({}))).toBeNull();
  664. });
  665. it("shares the row-to-target conversion with the panel row", () => {
  666. expect(workflowRunOpenTarget({ runId: "r", toolCallId: "t", title: "名字" })).toEqual({
  667. runId: "r",
  668. toolCallId: "t",
  669. workflowName: "名字",
  670. });
  671. expect(workflowRunOpenTarget({ runId: "r", toolCallId: "t", title: "r" })).toEqual({
  672. runId: "r",
  673. toolCallId: "t",
  674. });
  675. expect(workflowRunOpenTarget({ runId: "r", title: "名字" })).toBeNull();
  676. });
  677. });
  678. describe("通用模式 Git 投影", () => {
  679. it("隐藏 Git 但保留目标和计划,切回后恢复原始 Git 数据", () => {
  680. const input = {
  681. gitSummary: gitSummary(),
  682. gitWorktreeChangeSummary: { added: 7, removed: 2 },
  683. goal: goalState(),
  684. plan: planState(),
  685. };
  686. const original = JSON.stringify(input);
  687. const coding = buildConversationStatusPanelModel(input);
  688. const general = buildConversationStatusPanelModel({ ...input, isOfficeMode: true });
  689. expect(general.git).toBeNull();
  690. expect(general.goal).toEqual(coding.goal);
  691. expect(general.plan).toEqual(coding.plan);
  692. expect(general.hasContent).toBe(true);
  693. expect(buildConversationStatusPanelModel(input).git).toMatchObject({ added: 7, removed: 2 });
  694. expect(JSON.stringify(input)).toBe(original);
  695. });
  696. });