usePaneSessionPersistence.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { act, cleanup, renderHook } from "@testing-library/react";
  4. import { useState } from "react";
  5. import {
  6. shouldRestorePersistedPaneSession,
  7. usePaneSessionPersistence,
  8. persistPaneSession,
  9. readPersistedPaneSession,
  10. } from "@/v4/usePaneSessionPersistence.js";
  11. describe("pane last-session restore precedence", () => {
  12. it("restores only a cold workspace without an explicit draft intent", () => {
  13. expect(
  14. shouldRestorePersistedPaneSession({
  15. activeSessionId: null,
  16. draftFocusVersion: 0,
  17. enabled: true,
  18. rendererReload: true,
  19. }),
  20. ).toBe(true);
  21. });
  22. it("keeps an explicit draft instead of restoring the previous session", () => {
  23. expect(
  24. shouldRestorePersistedPaneSession({
  25. activeSessionId: null,
  26. draftFocusVersion: 1,
  27. enabled: true,
  28. rendererReload: true,
  29. }),
  30. ).toBe(false);
  31. });
  32. it("does not restore over an already selected session", () => {
  33. expect(
  34. shouldRestorePersistedPaneSession({
  35. activeSessionId: "session-current",
  36. draftFocusVersion: 0,
  37. enabled: true,
  38. rendererReload: true,
  39. }),
  40. ).toBe(false);
  41. });
  42. it("does not consume desktop persistence in web remote replayable mode", () => {
  43. expect(
  44. shouldRestorePersistedPaneSession({
  45. activeSessionId: null,
  46. draftFocusVersion: 0,
  47. enabled: false,
  48. rendererReload: true,
  49. }),
  50. ).toBe(false);
  51. });
  52. it("does not restore a persisted session on app cold launch or workspace entry", () => {
  53. expect(
  54. shouldRestorePersistedPaneSession({
  55. activeSessionId: null,
  56. draftFocusVersion: 0,
  57. enabled: true,
  58. rendererReload: false,
  59. }),
  60. ).toBe(false);
  61. });
  62. });
  63. afterEach(() => {
  64. cleanup();
  65. localStorage.clear();
  66. });
  67. describe("页签指定的启动 pane 恢复", () => {
  68. it("冷启动只恢复持久化激活页签,显式新任务随后清除旧恢复目标", () => {
  69. const workspaceKey = "remote:server:a:/srv/app";
  70. persistPaneSession(workspaceKey, "task-before-restart");
  71. const { result } = renderHook(() => {
  72. const [sessionId, setSessionId] = useState<string | null>(null);
  73. const [draftFocusVersion, setDraftFocusVersion] = useState(0);
  74. usePaneSessionPersistence({
  75. workspaceKey,
  76. activeSessionId: sessionId,
  77. draftFocusVersion,
  78. restoreOnStartup: true,
  79. selectSession: setSessionId,
  80. });
  81. return {
  82. sessionId,
  83. newTask: () => {
  84. setSessionId(null);
  85. setDraftFocusVersion(1);
  86. },
  87. };
  88. });
  89. expect(result.current.sessionId).toBe("task-before-restart");
  90. act(() => result.current.newTask());
  91. expect(readPersistedPaneSession(workspaceKey)).toBeNull();
  92. expect(result.current.sessionId).toBeNull();
  93. });
  94. });
  95. describe("挂回后验证原会话", () => {
  96. it("挂回门禁关闭时不查询,权威确认已删除后回到当前项目的新任务页", async () => {
  97. const workspaceKey = "remote:server:a:/srv/app";
  98. persistPaneSession(workspaceKey, "deleted-task");
  99. const lookup = vi.fn(async () => false);
  100. const { result, rerender } = renderHook(
  101. ({ ready }) => {
  102. const [sessionId, setSessionId] = useState<string | null>("deleted-task");
  103. usePaneSessionPersistence({
  104. workspaceKey,
  105. activeSessionId: sessionId,
  106. draftFocusVersion: 0,
  107. selectSession: setSessionId,
  108. lookupSession: ready ? lookup : undefined,
  109. onSessionMissing: () => setSessionId(null),
  110. });
  111. return sessionId;
  112. },
  113. { initialProps: { ready: false } },
  114. );
  115. expect(result.current).toBe("deleted-task");
  116. expect(lookup).not.toHaveBeenCalled();
  117. await act(async () => rerender({ ready: true }));
  118. expect(lookup).toHaveBeenCalledWith("deleted-task");
  119. expect(result.current).toBeNull();
  120. expect(readPersistedPaneSession(workspaceKey)).toBeNull();
  121. });
  122. });
  123. describe("恢复查询的意图与模式边界", () => {
  124. it("新任务刚被 ACK 选中但索引尚未写入时,不能当作恢复目标删除", async () => {
  125. const lookup = vi.fn(async () => false);
  126. const missing = vi.fn();
  127. const { rerender } = renderHook(
  128. ({ activeSessionId }) =>
  129. usePaneSessionPersistence({
  130. workspaceKey: "remote:server:a:/srv/app",
  131. activeSessionId,
  132. draftFocusVersion: 1,
  133. selectSession: vi.fn(),
  134. lookupSession: lookup,
  135. onSessionMissing: missing,
  136. }),
  137. { initialProps: { activeSessionId: null as string | null } },
  138. );
  139. await act(async () => rerender({ activeSessionId: "just-created-task" }));
  140. expect(lookup).not.toHaveBeenCalled();
  141. expect(missing).not.toHaveBeenCalled();
  142. });
  143. it("查询失败保留恢复会话;下一 attachment 就绪后可重新验证", async () => {
  144. const workspaceKey = "remote:server:a:/srv/app";
  145. const missing = vi.fn();
  146. const lookup = vi.fn().mockRejectedValue(new Error("disconnected"));
  147. const { rerender } = renderHook(
  148. ({ lookupSession }) =>
  149. usePaneSessionPersistence({
  150. workspaceKey,
  151. activeSessionId: "task-a",
  152. draftFocusVersion: 0,
  153. selectSession: vi.fn(),
  154. lookupSession,
  155. onSessionMissing: missing,
  156. }),
  157. { initialProps: { lookupSession: lookup } },
  158. );
  159. await act(async () => {});
  160. expect(missing).not.toHaveBeenCalled();
  161. expect(readPersistedPaneSession(workspaceKey)).toBe("task-a");
  162. const nextLookup = vi.fn(async () => true);
  163. await act(async () => rerender({ lookupSession: nextLookup }));
  164. expect(nextLookup).toHaveBeenCalledWith("task-a");
  165. expect(missing).not.toHaveBeenCalled();
  166. });
  167. it.each(["workspace", "task", "draft", "attachment"])(
  168. "%s 变化后的旧删除查询不得覆盖新意图",
  169. async (change) => {
  170. let finish!: (exists: boolean) => void;
  171. const lookup = vi.fn(
  172. () =>
  173. new Promise<boolean>((resolve) => {
  174. finish = resolve;
  175. }),
  176. );
  177. const missing = vi.fn();
  178. const initialProps = {
  179. workspaceKey: "remote:server:a:/srv/app",
  180. activeSessionId: "task-a" as string | null,
  181. draftFocusVersion: 0,
  182. lookupSession: lookup as (id: string) => Promise<boolean>,
  183. };
  184. const { rerender } = renderHook(
  185. (props) =>
  186. usePaneSessionPersistence({
  187. ...props,
  188. selectSession: vi.fn(),
  189. onSessionMissing: missing,
  190. }),
  191. { initialProps },
  192. );
  193. const finishOld = finish;
  194. rerender({
  195. ...initialProps,
  196. ...(change === "workspace" ? { workspaceKey: "remote:server:b:/srv/app" } : {}),
  197. ...(change === "task" ? { activeSessionId: "task-b" } : {}),
  198. ...(change === "draft" ? { activeSessionId: null, draftFocusVersion: 1 } : {}),
  199. ...(change === "attachment" ? { lookupSession: async () => true } : {}),
  200. });
  201. await act(async () => finishOld(false));
  202. expect(missing).not.toHaveBeenCalled();
  203. },
  204. );
  205. it("手机 replayable 不消费、不覆盖桌面选择且不启动恢复查询", async () => {
  206. const workspaceKey = "remote:server:a:/srv/app";
  207. persistPaneSession(workspaceKey, "desktop-task");
  208. const lookup = vi.fn(async () => false);
  209. const selectSession = vi.fn();
  210. const missing = vi.fn();
  211. renderHook(() =>
  212. usePaneSessionPersistence({
  213. workspaceKey,
  214. activeSessionId: "mobile-task",
  215. draftFocusVersion: 0,
  216. enabled: false,
  217. restoreOnStartup: true,
  218. selectSession,
  219. lookupSession: lookup,
  220. onSessionMissing: missing,
  221. }),
  222. );
  223. await act(async () => {});
  224. expect(readPersistedPaneSession(workspaceKey)).toBe("desktop-task");
  225. expect(selectSession).not.toHaveBeenCalled();
  226. expect(lookup).not.toHaveBeenCalled();
  227. expect(missing).not.toHaveBeenCalled();
  228. });
  229. });