conversationShareSelection.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render as rtlRender, screen } from "@testing-library/react";
  4. import { TooltipProvider } from "@/components/ui/tooltip.js";
  5. const render = (ui: Parameters<typeof rtlRender>[0]) => rtlRender(ui, { wrapper: TooltipProvider });
  6. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. intl: {
  10. formatMessage: ({ id }: { id: string }, values?: Record<string, string | number>) =>
  11. values ? `${id}:${JSON.stringify(values)}` : id,
  12. },
  13. }),
  14. }));
  15. class ResizeObserverMock {
  16. observe() {}
  17. unobserve() {}
  18. disconnect() {}
  19. }
  20. beforeEach(() => {
  21. vi.stubGlobal("ResizeObserver", ResizeObserverMock);
  22. });
  23. afterEach(() => {
  24. cleanup();
  25. vi.unstubAllGlobals();
  26. });
  27. describe("conversation partial share selection store", () => {
  28. beforeEach(async () => {
  29. const { useConversationShareSelectionStore } =
  30. await import("@/store/conversationShareSelectionStore.js");
  31. useConversationShareSelectionStore.getState().resetForTests();
  32. });
  33. it("首次进入默认全选,补齐历史时新候选仍默认选中", async () => {
  34. const {
  35. getConversationShareDraft,
  36. getConversationShareSelectedRowIds,
  37. useConversationShareSelectionStore,
  38. } = await import("@/store/conversationShareSelectionStore.js");
  39. const store = useConversationShareSelectionStore.getState();
  40. store.openShare("task-a");
  41. expect(
  42. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-a").open,
  43. ).toBe(true);
  44. expect(
  45. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-a").accessMode,
  46. ).toBe("public_importable");
  47. store.syncAvailableTurns(
  48. "task-a",
  49. [11, 22].map((rowId) => ({ rowId, productTurnId: String(rowId) })),
  50. );
  51. store.beginSelectionEdit("task-a");
  52. store.togglePendingRow("task-a", 22);
  53. store.confirmSelectionEdit("task-a");
  54. store.syncAvailableTurns(
  55. "task-a",
  56. [5, 11, 22].map((rowId) => ({ rowId, productTurnId: String(rowId) })),
  57. );
  58. expect(
  59. getConversationShareSelectedRowIds(useConversationShareSelectionStore.getState(), "task-a"),
  60. ).toEqual([5, 11]);
  61. });
  62. it("按 productTurnId 整轮取消:同一轮的多条 query 一起移除,其他轮不受影响", async () => {
  63. const {
  64. getConversationShareSelectedProductTurnIds,
  65. getConversationShareSelectedRowIds,
  66. useConversationShareSelectionStore,
  67. } = await import("@/store/conversationShareSelectionStore.js");
  68. const store = useConversationShareSelectionStore.getState();
  69. store.openShare("task-a");
  70. // product-turn-1 含两条 steer query,UI 目录里是两项,但「取消该轮」必须整轮生效。
  71. store.syncAvailableTurns("task-a", [
  72. { rowId: 11, productTurnId: "product-turn-1" },
  73. { rowId: 12, productTurnId: "product-turn-1" },
  74. { rowId: 21, productTurnId: "product-turn-2" },
  75. ]);
  76. store.deselectProductTurn("task-a", "product-turn-1");
  77. let state = useConversationShareSelectionStore.getState();
  78. expect(getConversationShareSelectedRowIds(state, "task-a")).toEqual([21]);
  79. expect(getConversationShareSelectedProductTurnIds(state, "task-a")).toEqual(["product-turn-2"]);
  80. // 幂等:重复取消不改变状态,也不会把已排除的行再翻回来(toggle 语义的老 bug)。
  81. store.deselectProductTurn("task-a", "product-turn-1");
  82. state = useConversationShareSelectionStore.getState();
  83. expect(getConversationShareSelectedRowIds(state, "task-a")).toEqual([21]);
  84. });
  85. it("进入局部选择时保留用户选择的分享权限", async () => {
  86. const { getConversationShareDraft, useConversationShareSelectionStore } =
  87. await import("@/store/conversationShareSelectionStore.js");
  88. const store = useConversationShareSelectionStore.getState();
  89. store.setAccessMode("task-a", "public_importable");
  90. store.openShare("task-a");
  91. expect(
  92. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-a").accessMode,
  93. ).toBe("public_importable");
  94. });
  95. });
  96. describe("ConversationShareSelectionPanel", () => {
  97. it("按会话容器和底部 dock 的实时位置计算面板最大高度", async () => {
  98. const {
  99. CONVERSATION_SHARE_SELECTION_PANEL_CENTER_Y_PROPERTY,
  100. CONVERSATION_SHARE_SELECTION_PANEL_MAX_HEIGHT_PROPERTY,
  101. resolveConversationShareSelectionPanelLayout,
  102. syncConversationShareSelectionPanelLayout,
  103. } = await import("@/v4/conversationShareSelectionPanelLayout.js");
  104. expect(
  105. resolveConversationShareSelectionPanelLayout({
  106. containerHeightPx: 891,
  107. dockStartPx: 647,
  108. }),
  109. ).toEqual({
  110. centerYPx: 327.5,
  111. maxHeightPx: 607,
  112. topPx: 24,
  113. bottomPx: 631,
  114. });
  115. expect(
  116. resolveConversationShareSelectionPanelLayout({
  117. containerHeightPx: 1200,
  118. dockStartPx: 1000,
  119. }),
  120. ).toEqual({
  121. centerYPx: 504,
  122. maxHeightPx: 960,
  123. topPx: 24,
  124. bottomPx: 984,
  125. });
  126. const expanded = resolveConversationShareSelectionPanelLayout({
  127. containerHeightPx: 1200,
  128. dockStartPx: 1000,
  129. });
  130. const shrunk = resolveConversationShareSelectionPanelLayout({
  131. containerHeightPx: 700,
  132. dockStartPx: 500,
  133. });
  134. expect(expanded.maxHeightPx).toBeGreaterThan(shrunk.maxHeightPx);
  135. expect(shrunk.bottomPx).toBeLessThanOrEqual(484);
  136. // 短视口/高 dock 下先回收顶部安全距,再保底最小高度,不允许压成零高度薄片。
  137. const collided = resolveConversationShareSelectionPanelLayout({
  138. containerHeightPx: 400,
  139. dockStartPx: 100,
  140. });
  141. expect(collided.topPx).toBe(0);
  142. expect(collided.maxHeightPx).toBe(120);
  143. // 容器本身比最小高度还矮时,面板仍必须留在容器内。
  144. const tiny = resolveConversationShareSelectionPanelLayout({
  145. containerHeightPx: 100,
  146. dockStartPx: 60,
  147. });
  148. expect(tiny.maxHeightPx).toBe(52);
  149. expect(tiny.bottomPx).toBeLessThanOrEqual(100);
  150. const container = document.createElement("div");
  151. const dock = document.createElement("div");
  152. vi.spyOn(container, "getBoundingClientRect").mockReturnValue({
  153. x: 0,
  154. y: 53,
  155. left: 0,
  156. top: 53,
  157. right: 1241,
  158. bottom: 944,
  159. width: 1241,
  160. height: 891,
  161. toJSON: () => ({}),
  162. });
  163. vi.spyOn(dock, "getBoundingClientRect").mockReturnValue({
  164. x: 0,
  165. y: 700,
  166. left: 0,
  167. top: 700,
  168. right: 1241,
  169. bottom: 944,
  170. width: 1241,
  171. height: 244,
  172. toJSON: () => ({}),
  173. });
  174. syncConversationShareSelectionPanelLayout(container, dock);
  175. expect(
  176. container.style.getPropertyValue(CONVERSATION_SHARE_SELECTION_PANEL_MAX_HEIGHT_PROPERTY),
  177. ).toBe("607px");
  178. expect(
  179. container.style.getPropertyValue(CONVERSATION_SHARE_SELECTION_PANEL_CENTER_Y_PROPERTY),
  180. ).toBe("327.5px");
  181. });
  182. it("把减半后的可见 thumb 映射到完整轨道", async () => {
  183. const { resolveConversationShareScrollbarIndicatorMetrics } =
  184. await import("@/v4/conversationShareScrollbarMetrics.js");
  185. const input = {
  186. trackSize: 400,
  187. viewportSize: 400,
  188. contentSize: 800,
  189. };
  190. expect(
  191. resolveConversationShareScrollbarIndicatorMetrics({ ...input, scrollOffset: 0 }),
  192. ).toEqual({ visible: true, size: 100, offset: 0 });
  193. expect(
  194. resolveConversationShareScrollbarIndicatorMetrics({ ...input, scrollOffset: 200 }),
  195. ).toEqual({ visible: true, size: 100, offset: 150 });
  196. expect(
  197. resolveConversationShareScrollbarIndicatorMetrics({ ...input, scrollOffset: 400 }),
  198. ).toEqual({ visible: true, size: 100, offset: 300 });
  199. });
  200. it("toggles through checkbox, summary and row while the locate action only inspects", async () => {
  201. const { ConversationShareSelectionPanel } =
  202. await import("@/v4/ConversationShareSelectionPanel.js");
  203. const onToggle = vi.fn();
  204. const onInspect = vi.fn();
  205. const items = [
  206. {
  207. key: "turn-1:query:11",
  208. turnId: "turn-1",
  209. unitIndex: 0,
  210. rowId: 11,
  211. userPreview: "第一条用户问题",
  212. assistantPreview: "第一条回答摘要",
  213. assistantPreviewKind: "text" as const,
  214. isRunning: false,
  215. },
  216. {
  217. key: "turn-2:query:22",
  218. turnId: "turn-2",
  219. unitIndex: 1,
  220. rowId: 22,
  221. userPreview: "第二条用户问题",
  222. assistantPreview: "第二条回答摘要",
  223. assistantPreviewKind: "text" as const,
  224. isRunning: false,
  225. },
  226. ];
  227. render(
  228. createElement(ConversationShareSelectionPanel, {
  229. visible: true,
  230. items,
  231. selectedRowIds: new Set([11]),
  232. onToggle,
  233. onInspect,
  234. }),
  235. );
  236. const panel = screen.getByTestId("conversation-share-selection-panel");
  237. expect(screen.queryByRole("button", { name: "conversationShare.selection.close" })).toBeNull();
  238. expect(panel.getAttribute("data-conversation-share-keep-open")).toBeNull();
  239. const summaryButton = screen.getByRole("button", { name: "第一条用户问题" });
  240. const firstItem = summaryButton.parentElement;
  241. expect(firstItem?.getAttribute("data-conversation-share-selection-state")).toBe("selected");
  242. const secondSummaryButton = screen.getByRole("button", {
  243. name: "第二条用户问题",
  244. });
  245. const secondItem = secondSummaryButton.parentElement;
  246. expect(secondItem?.getAttribute("data-conversation-share-selection-state")).toBe("unselected");
  247. const checkboxHitAreas = panel.querySelectorAll(
  248. '[data-conversation-share-checkbox-hit-area="true"]',
  249. );
  250. expect(checkboxHitAreas).toHaveLength(2);
  251. fireEvent.click(checkboxHitAreas[0]!);
  252. expect(onToggle).toHaveBeenCalledWith(11);
  253. expect(onInspect).not.toHaveBeenCalled();
  254. expect(onToggle).toHaveBeenCalledTimes(1);
  255. fireEvent.click(summaryButton);
  256. expect(onToggle).toHaveBeenCalledTimes(2);
  257. expect(onInspect).not.toHaveBeenCalled();
  258. fireEvent.click(firstItem!);
  259. expect(onToggle).toHaveBeenCalledTimes(3);
  260. fireEvent.click(screen.getAllByTestId("conversation-share-selection-locate")[0]!);
  261. expect(onToggle).toHaveBeenCalledTimes(3);
  262. expect(onInspect).toHaveBeenCalledWith({ unitIndex: 0, rowId: 11 });
  263. });
  264. });
  265. describe("ConversationShareConfirmationDock", () => {
  266. it("浮窗配置使用范围按钮调整内容,关闭和生成各自触发回调", async () => {
  267. const { ConversationShareConfirmationDock } =
  268. await import("@/v4/ConversationShareConfirmationDock.js");
  269. const onCancel = vi.fn(),
  270. onEditSelection = vi.fn(),
  271. onConfirm = vi.fn();
  272. render(
  273. createElement(ConversationShareConfirmationDock, {
  274. title: "分享标题",
  275. selectedCount: 3,
  276. totalCount: 5,
  277. disclosureAccepted: true,
  278. onCancel,
  279. onEditSelection,
  280. onConfirm,
  281. }),
  282. );
  283. expect(screen.getByTestId("conversation-share-confirmation-header").textContent).toContain(
  284. "conversationShare.popover.title",
  285. );
  286. fireEvent.click(screen.getByTestId("conversation-share-range-summary"));
  287. fireEvent.click(screen.getByRole("button", { name: "conversationShare.result.dismiss" }));
  288. fireEvent.click(screen.getByTestId("conversation-share-confirm"));
  289. expect(onEditSelection).toHaveBeenCalledTimes(1);
  290. expect(onCancel).toHaveBeenCalledTimes(1);
  291. expect(onConfirm).toHaveBeenCalledTimes(1);
  292. expect(screen.queryByTestId("conversation-share-back")).toBeNull();
  293. });
  294. });
  295. describe("ConversationSharePreflightStatus", () => {
  296. it("只展示预检诊断与问题操作,检查态由范围按钮承载", async () => {
  297. const { ConversationSharePreflightStatus } =
  298. await import("@/conversation-share-interaction/ConversationSharePreflightStatus.js");
  299. const common = {
  300. onDeselectTurn: vi.fn(),
  301. };
  302. const { rerender } = render(
  303. createElement(ConversationSharePreflightStatus, {
  304. ...common,
  305. preflight: { status: "checking" },
  306. }),
  307. );
  308. expect(screen.queryByTestId("conversation-share-selection-preflight-status")).toBeNull();
  309. expect(screen.queryByTestId("conversation-share-next")).toBeNull();
  310. rerender(
  311. createElement(ConversationSharePreflightStatus, {
  312. ...common,
  313. preflight: {
  314. status: "ready",
  315. revision: 7,
  316. logEpoch: "epoch-1",
  317. capabilitiesFingerprint: "fingerprint",
  318. blockingIssues: [
  319. // turnOrdinal 只用于文案;「取消该轮」按 productTurnId 定位。
  320. {
  321. code: "running_turn",
  322. scope: "turn",
  323. turnOrdinal: 2,
  324. productTurnId: "product-turn-2",
  325. },
  326. ],
  327. skippableWarnings: [],
  328. deferredIssues: [],
  329. supportedArtifactTypes: [],
  330. turnResults: [],
  331. },
  332. }),
  333. );
  334. fireEvent.click(screen.getByTestId("conversation-share-selection-preflight-status"));
  335. expect(
  336. await screen.findByTestId("conversation-share-selection-preflight-popover"),
  337. ).toBeTruthy();
  338. fireEvent.click(screen.getByRole("button", { name: "conversationShare.issue.deselectTurn" }));
  339. expect(common.onDeselectTurn).toHaveBeenCalledWith("product-turn-2");
  340. rerender(
  341. createElement(ConversationSharePreflightStatus, {
  342. ...common,
  343. preflight: {
  344. status: "ready",
  345. revision: 7,
  346. logEpoch: "epoch-1",
  347. capabilitiesFingerprint: "fingerprint",
  348. blockingIssues: [],
  349. skippableWarnings: [
  350. {
  351. code: "artifact_type_not_allowed",
  352. scope: "artifact",
  353. turnOrdinal: 1,
  354. artifactDisplayName: "video.mp4",
  355. artifactType: "mp4",
  356. },
  357. ],
  358. deferredIssues: [],
  359. supportedArtifactTypes: [],
  360. turnResults: [],
  361. },
  362. }),
  363. );
  364. expect(screen.getByTestId("conversation-share-selection-preflight-status")).toBeTruthy();
  365. });
  366. it("传输类阻断提供重新检查入口,轮次级阻断仍只给取消勾选", async () => {
  367. const { ConversationSharePreflightStatus } =
  368. await import("@/conversation-share-interaction/ConversationSharePreflightStatus.js");
  369. const common = {
  370. onDeselectTurn: vi.fn(),
  371. onRetryPreflight: vi.fn(),
  372. };
  373. const readyPreflight = {
  374. status: "ready" as const,
  375. revision: 7,
  376. logEpoch: "epoch-1",
  377. capabilitiesFingerprint: "fingerprint",
  378. skippableWarnings: [],
  379. deferredIssues: [],
  380. supportedArtifactTypes: [],
  381. turnResults: [],
  382. };
  383. const { rerender } = render(
  384. createElement(ConversationSharePreflightStatus, {
  385. ...common,
  386. preflight: {
  387. ...readyPreflight,
  388. blockingIssues: [{ code: "unknown" as const, scope: "transport" as const }],
  389. },
  390. }),
  391. );
  392. fireEvent.click(screen.getByTestId("conversation-share-selection-preflight-status"));
  393. expect(
  394. await screen.findByTestId("conversation-share-selection-preflight-popover"),
  395. ).toBeTruthy();
  396. fireEvent.click(screen.getByTestId("conversation-share-preflight-retry"));
  397. expect(common.onRetryPreflight).toHaveBeenCalledTimes(1);
  398. rerender(
  399. createElement(ConversationSharePreflightStatus, {
  400. ...common,
  401. preflight: {
  402. ...readyPreflight,
  403. blockingIssues: [
  404. {
  405. code: "running_turn" as const,
  406. scope: "turn" as const,
  407. turnOrdinal: 2,
  408. },
  409. ],
  410. },
  411. }),
  412. );
  413. expect(screen.queryByTestId("conversation-share-preflight-retry")).toBeNull();
  414. });
  415. });
  416. describe("conversation share selection mode motion", () => {
  417. it("面板保持 200ms 动效,减弱动效时立即切换", async () => {
  418. const { resolveConversationShareSelectionPanelMotion } =
  419. await import("@/v4/conversationShareModeMotion.js");
  420. expect(resolveConversationShareSelectionPanelMotion(false)).toEqual({
  421. initial: { opacity: 0, transform: "translate3d(-8px, -50%, 0)" },
  422. animate: { opacity: 1, transform: "translate3d(0, -50%, 0)" },
  423. exit: { opacity: 0, transform: "translate3d(-8px, -50%, 0)" },
  424. transition: { duration: 0.2, ease: [0.4, 0, 0.2, 1] },
  425. });
  426. expect(resolveConversationShareSelectionPanelMotion(true)).toEqual({
  427. initial: false,
  428. animate: { opacity: 1, transform: "translate3d(0, -50%, 0)" },
  429. exit: { opacity: 1, transform: "translate3d(0, -50%, 0)" },
  430. transition: { duration: 0 },
  431. });
  432. });
  433. });
  434. describe("conversation share selection interaction policy", () => {
  435. it("局部勾选期间禁用全局 message 框选,退出后恢复", async () => {
  436. const { resolveConversationSelectionTooltipEnabled } =
  437. await import("@/v4/conversationShareModePolicy.js");
  438. expect(
  439. resolveConversationSelectionTooltipEnabled({
  440. selectionActionsEnabled: true,
  441. partialShareActive: false,
  442. }),
  443. ).toBe(true);
  444. expect(
  445. resolveConversationSelectionTooltipEnabled({
  446. selectionActionsEnabled: true,
  447. partialShareActive: true,
  448. }),
  449. ).toBe(false);
  450. expect(
  451. resolveConversationSelectionTooltipEnabled({
  452. selectionActionsEnabled: false,
  453. partialShareActive: false,
  454. }),
  455. ).toBe(false);
  456. });
  457. });
  458. describe("ConversationBottomDockTransition", () => {
  459. it("returns animated and reduced-motion configurations for the bottom dock", async () => {
  460. const { resolveConversationBottomDockMotion } =
  461. await import("@/v4/ConversationBottomDockTransition.js");
  462. expect(resolveConversationBottomDockMotion(false)).toEqual({
  463. initial: { opacity: 0, transform: "translate3d(0, 32px, 0) scale(0.96)" },
  464. animate: {
  465. opacity: 1,
  466. transform: "translate3d(0, 0, 0) scale(1)",
  467. transition: { duration: 0.26, ease: [0.23, 1, 0.32, 1] },
  468. },
  469. exit: {
  470. opacity: 0,
  471. transform: "translate3d(0, 32px, 0) scale(0.97)",
  472. transition: { duration: 0.18, ease: [0.23, 1, 0.32, 1] },
  473. },
  474. });
  475. expect(resolveConversationBottomDockMotion(true)).toEqual({
  476. initial: false,
  477. animate: {
  478. opacity: 1,
  479. transform: "translate3d(0, 0, 0) scale(1)",
  480. transition: { duration: 0 },
  481. },
  482. exit: {
  483. opacity: 1,
  484. transform: "translate3d(0, 0, 0) scale(1)",
  485. transition: { duration: 0 },
  486. },
  487. });
  488. });
  489. });
  490. it.each(["idle", "stale", "checking"] as const)("有选择但预检 %s 时不能确认", async (status) => {
  491. const { ConversationShareSelectionPanel } =
  492. await import("@/v4/ConversationShareSelectionPanel.js");
  493. const confirm = vi.fn();
  494. render(
  495. createElement(ConversationShareSelectionPanel, {
  496. visible: true,
  497. items: [
  498. {
  499. key: "1",
  500. rowId: 1,
  501. unitIndex: 0,
  502. userPreview: "hello",
  503. assistantPreview: "world",
  504. isRunning: false,
  505. },
  506. ],
  507. selectedRowIds: new Set([1]),
  508. preflight: { status },
  509. onToggle: vi.fn(),
  510. onInspect: vi.fn(),
  511. onCancel: vi.fn(),
  512. onConfirm: confirm,
  513. }),
  514. );
  515. const apply = screen.getByTestId("conversation-share-selection-apply") as HTMLButtonElement;
  516. expect(apply.disabled).toBe(true);
  517. fireEvent.click(apply);
  518. expect(confirm).not.toHaveBeenCalled();
  519. });