mapSessionSummaryToTaskMeta.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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("fork:parentSessionId → forkedFromTaskId", () => {
  79. const meta = mapSessionSummaryToTaskMeta(summary({ parentSessionId: "parent" }), {
  80. workspacePath: "/ws",
  81. });
  82. expect(meta.forkedFromTaskId).toBe("parent");
  83. });
  84. it("previous 的 mode/provider 沿用;旧 titleOverridden 在 summary 非 custom 时保留", () => {
  85. const previous = {
  86. taskId: "s1",
  87. title: "重命名了",
  88. titleOverridden: true,
  89. provider: "claude",
  90. mode: "plan",
  91. } as unknown as ZCodeTaskMeta;
  92. const meta = mapSessionSummaryToTaskMeta(
  93. summary({ title: "Fork of 项目代码Bug排查", titleSource: "generated" }),
  94. { workspacePath: "/ws", previous },
  95. );
  96. expect(meta.title).toBe("重命名了");
  97. expect(meta.titleOverridden).toBe(true);
  98. expect(meta.mode).toBe("plan");
  99. expect(meta.provider).toBe("claude");
  100. });
  101. it("summary.titleSource=custom 时使用 session store 的权威自定义标题", () => {
  102. const previous = {
  103. taskId: "s1",
  104. title: "旧 task-index 标题",
  105. titleOverridden: true,
  106. mode: "plan",
  107. } as unknown as ZCodeTaskMeta;
  108. const meta = mapSessionSummaryToTaskMeta(
  109. summary({ title: "新的自定义标题", titleSource: "custom" }),
  110. { workspacePath: "/ws", previous },
  111. );
  112. expect(meta.title).toBe("新的自定义标题");
  113. expect(meta.titleOverridden).toBe(true);
  114. });
  115. it("workspaceIdentity 透传", () => {
  116. const meta = mapSessionSummaryToTaskMeta(summary(), {
  117. workspacePath: "/ws",
  118. workspaceIdentity: "remote::/ws",
  119. });
  120. expect(meta.workspaceIdentity).toBe("remote::/ws");
  121. });
  122. it("preserves the sessions-index pending interaction for background sidebar rows", () => {
  123. const pendingInteraction = {
  124. interactionId: "ask-1",
  125. kind: "userInput" as const,
  126. toolName: "AskUserQuestion",
  127. autoResolution: {
  128. state: "hiddenGrace" as const,
  129. startedAt: 1_000,
  130. visibleAt: 61_000,
  131. deadlineAt: 301_000,
  132. },
  133. };
  134. const meta = mapSessionSummaryToTaskMeta(summary({ pendingInteraction }), {
  135. workspacePath: "/ws",
  136. });
  137. expect(meta.pendingInteraction).toEqual(pendingInteraction);
  138. });
  139. it("透传侧栏需要的 pending interaction kind/count", () => {
  140. const meta = mapSessionSummaryToTaskMeta(
  141. summary({
  142. phase: "running",
  143. pendingInteractionSummary: {
  144. permissionCount: 1,
  145. userInputCount: 2,
  146. },
  147. }),
  148. { workspacePath: "/ws" },
  149. );
  150. expect(getTaskListRowActivity(meta)?.pendingInteractions).toEqual({
  151. permissionCount: 1,
  152. userInputCount: 2,
  153. });
  154. });
  155. });