useAppPanelsScope.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // @vitest-environment jsdom
  2. import { act, cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import { useAppPanels } from "@/hooks/useAppPanels.js";
  5. import { clearTaskSidePaneMemoryStateForTest } from "@/lib/taskSidePaneMemory.js";
  6. import { getVisibleSidePaneTabs } from "@/lib/workspaceSidePane.js";
  7. vi.mock("@/hooks/useInterfaceMode.js", () => ({ useIsOfficeMode: () => false }));
  8. vi.mock("@/hooks/useServices.js", () => ({
  9. useServices: () => ({ zcodeSessionService: { closeSession: vi.fn(async () => {}) } }),
  10. }));
  11. vi.mock("@/logger.js", () => ({ logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn() } }));
  12. const a = { workspaceIdentity: "ssh://one/repo", ownerTaskId: "a" as string | null };
  13. const b = { ...a, ownerTaskId: "b" };
  14. function setup() {
  15. const platform = { browserViewCloseTab: vi.fn(async () => {}) };
  16. const rendered = renderHook(
  17. (scope: typeof a) =>
  18. useAppPanels({
  19. workspaceAbsPath: "/repo",
  20. workspaceIdentity: scope.workspaceIdentity,
  21. activeTaskId: null,
  22. sidePaneOwnerId: scope.ownerTaskId,
  23. isDesktop: true,
  24. supportsEmbeddedBrowser: true,
  25. defaultWhiteboardNamePrefix: "Whiteboard",
  26. platform,
  27. }),
  28. { initialProps: a },
  29. );
  30. const open = (origin: string) => {
  31. act(() => rendered.result.current.handleOpenBrowserPermissionSettings(origin));
  32. return rendered.result.current.sidePaneState!.activeTabId;
  33. };
  34. const ids = () => rendered.result.current.sidePaneState?.tabs.map((tab) => tab.id) ?? [];
  35. return { ...rendered, platform, open, ids };
  36. }
  37. describe("侧栏显示、关闭与恢复共用工作区/对话归属", () => {
  38. beforeEach(clearTaskSidePaneMemoryStateForTest);
  39. afterEach(() => {
  40. cleanup();
  41. clearTaskSidePaneMemoryStateForTest();
  42. });
  43. it.each(["", "ssh://two/repo"])(
  44. "跨工作区文件请求只在目标 %s 的 owner 中创建,返回后仍可见",
  45. (identity) => {
  46. const h = setup();
  47. const previous = h.open("https://a.test");
  48. act(() =>
  49. h.result.current.handleOpenFileManager({
  50. workspacePath: "/repo",
  51. ...(identity
  52. ? { workspaceIdentity: identity, workspaceRemoteSessionId: "remote-two" }
  53. : {}),
  54. }),
  55. );
  56. expect(h.result.current.sidePaneState?.tabs.some((tab) => tab.type === "file-manager")).toBe(
  57. false,
  58. );
  59. const targetScope = { workspaceIdentity: identity, ownerTaskId: "target-task" };
  60. h.rerender(targetScope);
  61. const fileTab = h.result.current.sidePaneState?.tabs.find(
  62. (tab) => tab.type === "file-manager",
  63. );
  64. expect(fileTab).toMatchObject({
  65. workspaceKey: identity || "/repo",
  66. ownerTaskId: "target-task",
  67. target: { workspacePath: "/repo" },
  68. });
  69. expect(h.result.current.isSidePaneCollapsed).toBe(false);
  70. h.rerender(a);
  71. expect(h.ids()).toContain(previous);
  72. expect(h.ids()).not.toContain(fileTab!.id);
  73. h.rerender(targetScope);
  74. expect(h.ids()).toContain(fileTab!.id);
  75. expect(h.result.current.isSidePaneCollapsed).toBe(false);
  76. },
  77. );
  78. it.each(["all", "others"] as const)(
  79. "B 关闭 %s 不删除 A,最近关闭和重新打开保持原归属",
  80. async (mode) => {
  81. const h = setup();
  82. const aId = h.open("https://a.test");
  83. h.rerender(b);
  84. const bId = h.open("https://b.test");
  85. const keep = h.open("https://keep.test");
  86. await act(async () =>
  87. mode === "all"
  88. ? h.result.current.handleCloseAllSidePaneTabs()
  89. : h.result.current.handleCloseOtherSidePaneTabs(keep),
  90. );
  91. expect(h.ids()).toContain(aId);
  92. expect(h.ids()).not.toContain(bId);
  93. expect(h.result.current.recentClosedSidePaneTabs.map((item) => item.tab.id)).not.toContain(
  94. aId,
  95. );
  96. h.rerender(a);
  97. expect(h.result.current.sidePaneState?.activeTabId).toBe(aId);
  98. expect(h.result.current.isSidePaneCollapsed).toBe(false);
  99. expect(h.result.current.recentClosedSidePaneTabs).toEqual([]);
  100. act(() => h.result.current.handleReopenClosedSidePaneTab(bId));
  101. expect(h.ids()).not.toContain(bId);
  102. h.rerender(b);
  103. act(() => h.result.current.handleReopenClosedSidePaneTab(bId));
  104. expect(h.result.current.sidePaneState?.activeTabId).toBe(bId);
  105. },
  106. );
  107. it("最近关闭不跨同路径的 workspaceIdentity,返回原工作区可恢复", async () => {
  108. const h = setup();
  109. const id = h.open("https://a.test");
  110. await act(async () => h.result.current.handleCloseSidePaneTab(id));
  111. h.rerender({ ...a, workspaceIdentity: "ssh://two/repo" });
  112. expect(h.result.current.recentClosedSidePaneTabs).toEqual([]);
  113. act(() => h.result.current.handleReopenClosedSidePaneTab(id));
  114. expect(h.ids()).toEqual([]);
  115. h.rerender(a);
  116. act(() => h.result.current.handleReopenClosedSidePaneTab(id));
  117. expect(h.result.current.sidePaneState?.tabs.map((tab) => tab.type)).toEqual([
  118. "browser-permissions",
  119. ]);
  120. expect(h.result.current.sidePaneState?.activeTabId).toBe(id);
  121. });
  122. it("快捷入口打开的 Browser 也冻结工作区,最近关闭不泄漏到同 owner 的其他工作区", async () => {
  123. const h = setup();
  124. act(() => h.result.current.handleToggleBrowser());
  125. const id = h.result.current.sidePaneState!.activeTabId;
  126. expect(h.result.current.sidePaneState!.tabs[0]).toMatchObject({
  127. ownerTaskId: "a",
  128. workspaceKey: a.workspaceIdentity,
  129. });
  130. await act(async () => h.result.current.handleCloseSidePaneTab(id));
  131. h.rerender({ ...a, workspaceIdentity: "ssh://two/repo" });
  132. expect(h.result.current.recentClosedSidePaneTabs).toEqual([]);
  133. act(() => h.result.current.handleReopenClosedSidePaneTab(id));
  134. expect(h.ids()).toEqual([]);
  135. });
  136. it("关闭最后一个可见 tab 收起面板,隐藏 tab 保留;公共 Git 在同工作区仍可恢复", async () => {
  137. const h = setup();
  138. const aId = h.open("https://a.test");
  139. h.rerender(b);
  140. const bId = h.open("https://b.test");
  141. await act(async () => h.result.current.handleCloseSidePaneTab(bId));
  142. expect(h.ids()).toContain(aId);
  143. const visible = getVisibleSidePaneTabs(h.result.current.sidePaneState!.tabs, {
  144. workspaceKey: b.workspaceIdentity,
  145. ownerTaskId: b.ownerTaskId,
  146. });
  147. expect(visible).toEqual([]);
  148. expect(h.result.current.isSidePaneCollapsed).toBe(true);
  149. act(() => h.result.current.handleOpenGit());
  150. await act(async () => h.result.current.handleCloseSidePaneTab("git"));
  151. h.rerender(a);
  152. expect(h.result.current.recentClosedSidePaneTabs.map((item) => item.tab.id)).toEqual(["git"]);
  153. act(() => h.result.current.handleReopenClosedSidePaneTab("git"));
  154. expect(h.result.current.sidePaneState?.activeTabId).toBe("git");
  155. });
  156. it("重新进入草稿不继承历史展开偏好,显式展开恢复原标签", () => {
  157. const h = setup();
  158. h.rerender({ ...a, ownerTaskId: null });
  159. act(() => h.result.current.handleToggleSidePaneCollapse());
  160. const draftId = h.result.current.sidePaneState!.activeTabId;
  161. h.rerender(a);
  162. expect(h.result.current.isSidePaneCollapsed).toBe(true);
  163. h.rerender({ ...a, ownerTaskId: null });
  164. expect(h.result.current.isSidePaneCollapsed).toBe(true);
  165. act(() => h.result.current.handleToggleSidePaneCollapse());
  166. expect(h.result.current.sidePaneState!.activeTabId).toBe(draftId);
  167. });
  168. it("同一 batch 的折叠写入与 toggle 读取一致", () => {
  169. const h = setup();
  170. act(() => h.result.current.handleToggleSidePaneCollapse());
  171. const id = h.result.current.sidePaneState!.activeTabId;
  172. act(() => {
  173. h.result.current.setIsSidePaneCollapsed(true);
  174. h.result.current.handleToggleSidePaneCollapse();
  175. });
  176. expect(h.result.current.isSidePaneCollapsed).toBe(false);
  177. expect(h.result.current.sidePaneState!.activeTabId).toBe(id);
  178. });
  179. it("null 草稿与具名草稿分开;关闭不可见 id 不产生副作用", async () => {
  180. const h = setup();
  181. const aId = h.open("https://a.test");
  182. h.rerender({ ...a, ownerTaskId: null });
  183. const draftId = h.open("https://draft.test");
  184. await act(async () => h.result.current.handleCloseSidePaneTab(aId));
  185. expect(h.ids()).toEqual([aId, draftId]);
  186. await act(async () => h.result.current.handleCloseAllSidePaneTabs());
  187. expect(h.ids()).toContain(aId);
  188. expect(
  189. getVisibleSidePaneTabs(h.result.current.sidePaneState!.tabs, {
  190. workspaceKey: a.workspaceIdentity,
  191. ownerTaskId: null,
  192. }),
  193. ).toEqual([]);
  194. expect(h.ids()).not.toContain(draftId);
  195. });
  196. it.each(["owner", "workspace"] as const)(
  197. "异步 Browser 关闭后切换 %s,只提交选中的旧 id",
  198. async (switchKind) => {
  199. const h = setup();
  200. let finish!: () => void;
  201. h.platform.browserViewCloseTab.mockImplementation(
  202. () =>
  203. new Promise<void>((resolve) => {
  204. finish = resolve;
  205. }),
  206. );
  207. act(() => h.result.current.handleOpenBrowserTab());
  208. const browserId = h.result.current.sidePaneState!.activeTabId;
  209. const oldSetting = h.open("https://old.test");
  210. act(() => h.result.current.handleCloseAllSidePaneTabs());
  211. const addedDuringClose = h.open("https://new.test");
  212. const nextScope = switchKind === "owner" ? b : { ...a, workspaceIdentity: "ssh://two/repo" };
  213. h.rerender(nextScope);
  214. const nextId = h.open("https://next.test");
  215. await act(async () => finish());
  216. expect(h.result.current.sidePaneState?.activeTabId).toBe(nextId);
  217. expect(h.result.current.isSidePaneCollapsed).toBe(false);
  218. expect(h.platform.browserViewCloseTab).toHaveBeenCalledTimes(1);
  219. expect(h.platform.browserViewCloseTab).toHaveBeenCalledWith(
  220. expect.objectContaining({ tabId: browserId, workspaceKey: a.workspaceIdentity }),
  221. );
  222. h.rerender(a);
  223. expect(h.ids()).toContain(addedDuringClose);
  224. expect(h.ids()).not.toContain(oldSetting);
  225. expect(h.ids()).not.toContain(browserId);
  226. expect(
  227. getVisibleSidePaneTabs(h.result.current.sidePaneState!.tabs, {
  228. workspaceKey: a.workspaceIdentity,
  229. ownerTaskId: a.ownerTaskId,
  230. }).map((tab) => tab.id),
  231. ).toEqual([addedDuringClose]);
  232. },
  233. );
  234. it("main 拒绝关闭时保留标签与最近关闭列表", async () => {
  235. const h = setup();
  236. act(() => h.result.current.handleOpenBrowserTab());
  237. const before = h.result.current.sidePaneState;
  238. h.platform.browserViewCloseTab.mockRejectedValue(new Error("close rejected"));
  239. await act(async () => h.result.current.handleCloseAllSidePaneTabs());
  240. expect(h.result.current.sidePaneState).toBe(before);
  241. expect(h.result.current.recentClosedSidePaneTabs).toEqual([]);
  242. });
  243. });