conversationSessionChatRootSnapshot.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { readV4ChatRootSnapshotInBrowser } from "./e2e/helpers/conversation-session-chat-root-snapshot-script.js";
  3. import { readTaskStoreSnapshotInBrowser } from "./e2e/helpers/conversation-session-store-snapshot-script.js";
  4. afterEach(() => {
  5. vi.unstubAllGlobals();
  6. });
  7. function installV4SnapshotBrowser(options: {
  8. activeWorkspaceIdentity?: string | null;
  9. activeWorkspacePath: string;
  10. paneSessionId?: string;
  11. stopVisible?: boolean;
  12. workspaces: Record<
  13. string,
  14. {
  15. draftRuntime?: { status?: string | null };
  16. modelSwitchPending?: boolean;
  17. taskRuntimeByTaskId?: Record<
  18. string,
  19. { activeInputId?: string | null; status?: string | null }
  20. >;
  21. }
  22. >;
  23. }) {
  24. const paneSessionId = options.paneSessionId ?? "draft";
  25. // Bug 根因:snapshot helper 增加 pane 内 Stop 查询后,旧测试桩仍只实现
  26. // getAttribute,导致用例在读取 workspace 前崩溃。这里补齐被测代码依赖的
  27. // HTMLElement 最小契约,并用 stopVisible 显式模拟 control projection。
  28. const pane = {
  29. getAttribute: (name: string) => (name === "data-session-id" ? paneSessionId : null),
  30. querySelector: (selector: string) =>
  31. options.stopVisible && selector === '[data-testid="v4-stop"]' ? {} : null,
  32. };
  33. vi.stubGlobal("document", {
  34. querySelector: () => pane,
  35. querySelectorAll: () => ({ length: 0 }),
  36. });
  37. vi.stubGlobal("window", {
  38. __zcodeSessionStoreE2E: {
  39. getState: () => ({ workspaces: options.workspaces }),
  40. },
  41. __zcodeTabStoreE2E: {
  42. getState: () => ({
  43. activeWorkspaceIdentity: options.activeWorkspaceIdentity ?? null,
  44. activeWorkspacePath: options.activeWorkspacePath,
  45. }),
  46. },
  47. });
  48. }
  49. function installTaskStoreBrowser(workspace: Record<string, unknown>) {
  50. vi.stubGlobal("window", {
  51. localStorage: {},
  52. __zcodeSessionStoreE2E: {
  53. getState: () => ({ workspaces: { workspace } }),
  54. },
  55. });
  56. }
  57. describe("V4 ChatRootSnapshot workspace isolation", () => {
  58. it("reads draft runtime and model switch from the active local workspace", () => {
  59. installV4SnapshotBrowser({
  60. activeWorkspacePath: "C:/repo-b",
  61. workspaces: {
  62. "C:/repo-a": {
  63. draftRuntime: { status: "streaming" },
  64. modelSwitchPending: true,
  65. },
  66. "C:/repo-b": {
  67. draftRuntime: { status: "idle" },
  68. modelSwitchPending: false,
  69. },
  70. },
  71. });
  72. expect(readV4ChatRootSnapshotInBrowser("pane", "queue", "v4-stop")).toMatchObject({
  73. bridgeError: null,
  74. canStop: false,
  75. modelSwitchPending: false,
  76. runtimeStatus: "idle",
  77. workspaceKey: "C:/repo-b",
  78. });
  79. });
  80. it("prefers workspaceIdentity when two draft workspaces share one path", () => {
  81. const workspacePath = "/home/dev/project";
  82. const workspaceIdentityA = "ssh://host-a/home/dev/project";
  83. const workspaceIdentityB = "ssh://host-b/home/dev/project";
  84. installV4SnapshotBrowser({
  85. activeWorkspaceIdentity: ` ${workspaceIdentityB} `,
  86. activeWorkspacePath: workspacePath,
  87. workspaces: {
  88. [workspaceIdentityA]: {
  89. draftRuntime: { status: "idle" },
  90. modelSwitchPending: false,
  91. },
  92. [workspaceIdentityB]: {
  93. draftRuntime: { status: "streaming" },
  94. modelSwitchPending: true,
  95. },
  96. },
  97. });
  98. expect(readV4ChatRootSnapshotInBrowser("pane", "queue", "v4-stop")).toMatchObject({
  99. bridgeError: null,
  100. canStop: false,
  101. modelSwitchPending: true,
  102. runtimeStatus: "streaming",
  103. workspaceKey: workspaceIdentityB,
  104. });
  105. });
  106. it("uses the pane Stop control while the task runtime store is stale", () => {
  107. installV4SnapshotBrowser({
  108. activeWorkspacePath: "/repo",
  109. paneSessionId: "task-running",
  110. stopVisible: true,
  111. workspaces: {
  112. "/repo": {
  113. taskRuntimeByTaskId: {
  114. "task-running": {
  115. activeInputId: "input-running",
  116. status: "completed",
  117. },
  118. },
  119. },
  120. },
  121. });
  122. expect(readV4ChatRootSnapshotInBrowser("pane", "queue", "v4-stop")).toMatchObject({
  123. activeInputId: "input-running",
  124. canStop: true,
  125. runtimeStatus: "streaming",
  126. taskId: "task-running",
  127. });
  128. });
  129. });
  130. describe("V4 ChatRootSnapshot task Goal and stop semantics", () => {
  131. it("reads an active Goal from optimistic task metadata", () => {
  132. installTaskStoreBrowser({
  133. activeTaskId: "task-active",
  134. optimisticTaskListByTaskId: {
  135. "task-active": {
  136. taskId: "task-active",
  137. target: {
  138. objective: "修复 active goal",
  139. status: "active",
  140. targetID: "goal-active",
  141. },
  142. },
  143. },
  144. });
  145. expect(readTaskStoreSnapshotInBrowser("task-active", "workspace").snapshot.taskMeta).toMatchObject(
  146. {
  147. targetId: "goal-active",
  148. targetObjective: "修复 active goal",
  149. targetStatus: "active",
  150. },
  151. );
  152. });
  153. it("reads a completed Goal from task-list cache fallback", () => {
  154. installTaskStoreBrowser({
  155. activeTaskId: "task-completed",
  156. taskListCache: [
  157. {
  158. taskId: "task-completed",
  159. target: {
  160. objective: "修复 completed goal",
  161. status: "complete",
  162. targetID: "goal-completed",
  163. },
  164. },
  165. ],
  166. });
  167. expect(
  168. readTaskStoreSnapshotInBrowser("task-completed", "workspace").snapshot.taskMeta,
  169. ).toMatchObject({
  170. targetId: "goal-completed",
  171. targetObjective: "修复 completed goal",
  172. targetStatus: "complete",
  173. });
  174. });
  175. it("reads stopRequested from the selected workspace task bucket", () => {
  176. installTaskStoreBrowser({
  177. activeTaskId: "task-stopping",
  178. taskStopRequestedByTaskId: { "task-stopping": true },
  179. });
  180. expect(
  181. readTaskStoreSnapshotInBrowser("task-stopping", "workspace").snapshot.stopRequested,
  182. ).toBe(true);
  183. });
  184. });