mapSessionSummaryToTaskMeta.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeTaskMeta } from "@zcode/shared";
  3. import type { SessionSummary } from "@zcode/shared/zcode-protocol-v4";
  4. import { mapSessionSummaryToTaskMeta } from "@/v4/mapSessionSummaryToTaskMeta.js";
  5. import { getTaskListRowActivity } from "@/v4/taskListRowActivity.js";
  6. type TestSessionSummary = SessionSummary & {
  7. titleSource?: "default" | "generated" | "custom";
  8. };
  9. function summary(overrides: Partial<TestSessionSummary> = {}): TestSessionSummary {
  10. return {
  11. sessionId: "s1",
  12. workspaceId: "ws-1",
  13. title: "标题",
  14. phase: "completedSuccess",
  15. sessionEnded: true,
  16. hasBackgroundWork: false,
  17. lastActivityAt: 200,
  18. createdAt: 100,
  19. ...overrides,
  20. };
  21. }
  22. describe("mapSessionSummaryToTaskMeta", () => {
  23. it("基础映射:taskId/title/时间/status/mode 默认", () => {
  24. const meta = mapSessionSummaryToTaskMeta(summary(), {
  25. workspacePath: "/ws",
  26. });
  27. expect(meta.taskId).toBe("s1");
  28. expect(meta.title).toBe("标题");
  29. expect(meta.createdAt).toBe(100);
  30. expect(meta.updatedAt).toBe(200);
  31. expect(meta.status).toBe("completed");
  32. expect(meta.mode).toBe("default");
  33. expect(meta.workspacePath).toBe("/ws");
  34. expect(meta.traceId).toBe("session-s1");
  35. expect(getTaskListRowActivity(meta)).toEqual({
  36. phase: "completedSuccess",
  37. lastActivityAt: 200,
  38. hasBackgroundWork: false,
  39. });
  40. });
  41. it("workflowActivity 原样搬进 sidecar(侧栏运行行的数据),缺席时不建键", () => {
  42. const workflowActivity: SessionSummary["workflowActivity"] = {
  43. runs: [
  44. {
  45. runId: "run-1",
  46. toolCallId: "tool-1",
  47. name: "Deep research",
  48. status: "running",
  49. phases: [
  50. { name: "Research", status: "running" },
  51. { name: "Write", status: "pending" },
  52. ],
  53. currentPhase: "Research",
  54. agentsWorking: 2,
  55. },
  56. ],
  57. };
  58. const meta = mapSessionSummaryToTaskMeta(summary({ workflowActivity }), {
  59. workspacePath: "/ws",
  60. });
  61. expect(getTaskListRowActivity(meta)?.workflowActivity).toEqual(workflowActivity);
  62. expect(
  63. getTaskListRowActivity(mapSessionSummaryToTaskMeta(summary(), { workspacePath: "/ws" })),
  64. ).not.toHaveProperty("workflowActivity");
  65. });
  66. it("phase → status 映射", () => {
  67. expect(
  68. mapSessionSummaryToTaskMeta(summary({ phase: "running" }), { workspacePath: "/ws" }).status,
  69. ).toBe("running");
  70. expect(
  71. mapSessionSummaryToTaskMeta(summary({ phase: "error" }), { workspacePath: "/ws" }).status,
  72. ).toBe("error");
  73. // draft 无 status
  74. expect(
  75. mapSessionSummaryToTaskMeta(summary({ phase: "draft" }), { workspacePath: "/ws" }).status,
  76. ).toBeUndefined();
  77. });
  78. it("sessionEnded=true 不是删除语义:成功收口的会话照常映射进列表", () => {
  79. const meta = mapSessionSummaryToTaskMeta(
  80. summary({ sessionEnded: true, phase: "completedSuccess" }),
  81. { workspacePath: "/ws" },
  82. );
  83. expect(meta.taskId).toBe("s1");
  84. expect(meta.status).toBe("completed");
  85. });
  86. it("fork:parentSessionId → forkedFromTaskId", () => {
  87. const meta = mapSessionSummaryToTaskMeta(summary({ parentSessionId: "parent" }), {
  88. workspacePath: "/ws",
  89. });
  90. expect(meta.forkedFromTaskId).toBe("parent");
  91. });
  92. it("previous 的 mode/provider 沿用;旧 titleOverridden 在 summary 非 custom 时保留", () => {
  93. const previous = {
  94. taskId: "s1",
  95. title: "重命名了",
  96. titleOverridden: true,
  97. provider: "claude",
  98. mode: "plan",
  99. } as unknown as ZCodeTaskMeta;
  100. const meta = mapSessionSummaryToTaskMeta(
  101. summary({ title: "Fork of 项目代码Bug排查", titleSource: "generated" }),
  102. { workspacePath: "/ws", previous },
  103. );
  104. expect(meta.title).toBe("重命名了");
  105. expect(meta.titleOverridden).toBe(true);
  106. expect(meta.mode).toBe("plan");
  107. expect(meta.provider).toBe("claude");
  108. });
  109. it("summary.titleSource=custom 时使用 session store 的权威自定义标题", () => {
  110. const previous = {
  111. taskId: "s1",
  112. title: "旧 task-index 标题",
  113. titleOverridden: true,
  114. mode: "plan",
  115. } as unknown as ZCodeTaskMeta;
  116. const meta = mapSessionSummaryToTaskMeta(
  117. summary({ title: "新的自定义标题", titleSource: "custom" }),
  118. { workspacePath: "/ws", previous },
  119. );
  120. expect(meta.title).toBe("新的自定义标题");
  121. expect(meta.titleOverridden).toBe(true);
  122. });
  123. it("workspaceIdentity 透传", () => {
  124. const meta = mapSessionSummaryToTaskMeta(summary(), {
  125. workspacePath: "/ws",
  126. workspaceIdentity: "remote::/ws",
  127. });
  128. expect(meta.workspaceIdentity).toBe("remote::/ws");
  129. });
  130. it("preserves the sessions-index pending interaction for background sidebar rows", () => {
  131. const pendingInteraction = {
  132. interactionId: "ask-1",
  133. kind: "userInput" as const,
  134. toolName: "AskUserQuestion",
  135. autoResolution: {
  136. state: "hiddenGrace" as const,
  137. startedAt: 1_000,
  138. visibleAt: 61_000,
  139. deadlineAt: 301_000,
  140. },
  141. };
  142. const meta = mapSessionSummaryToTaskMeta(summary({ pendingInteraction }), {
  143. workspacePath: "/ws",
  144. });
  145. expect(meta.pendingInteraction).toEqual(pendingInteraction);
  146. });
  147. it("透传侧栏需要的 pending interaction kind/count", () => {
  148. const meta = mapSessionSummaryToTaskMeta(
  149. summary({
  150. phase: "running",
  151. pendingInteractionSummary: {
  152. permissionCount: 1,
  153. userInputCount: 2,
  154. },
  155. }),
  156. { workspacePath: "/ws" },
  157. );
  158. expect(getTaskListRowActivity(meta)?.pendingInteractions).toEqual({
  159. permissionCount: 1,
  160. userInputCount: 2,
  161. });
  162. });
  163. });