conversationSharePopoverState.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { beforeEach, describe, expect, it } from "vitest";
  2. import {
  3. getConversationShareSelectedRowIds,
  4. getConversationShareSelectedProductTurnIds,
  5. useConversationShareSelectionStore,
  6. } from "@/store/conversationShareSelectionStore.js";
  7. const state = () => useConversationShareSelectionStore.getState();
  8. describe("分享浮窗的范围事务", () => {
  9. beforeEach(() => state().resetForTests());
  10. const prepare = () => {
  11. state().openShare("a");
  12. state().syncAvailableTurns("a", [
  13. { rowId: 1, productTurnId: "t1" },
  14. { rowId: 2, productTurnId: "t1" },
  15. { rowId: 3, productTurnId: "t2" },
  16. ]);
  17. };
  18. it("pending budget selection keeps committed turns intact and bulk updates each turn once", () => {
  19. prepare();
  20. state().beginSelectionEdit("a");
  21. state().setAllPendingRowsSelected("a", false);
  22. expect(getConversationShareSelectedProductTurnIds(state(), "a", "pending")).toEqual([]);
  23. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(["t1", "t2"]);
  24. state().setAllPendingRowsSelected("a", true);
  25. expect(getConversationShareSelectedProductTurnIds(state(), "a", "pending")).toEqual([
  26. "t1",
  27. "t2",
  28. ]);
  29. state().togglePendingRow("a", 1);
  30. expect(getConversationShareSelectedProductTurnIds(state(), "a", "pending")).toEqual(["t2"]);
  31. state().cancelSelectionEdit("a");
  32. expect(getConversationShareSelectedProductTurnIds(state(), "a", "pending")).toEqual([
  33. "t1",
  34. "t2",
  35. ]);
  36. });
  37. it("取消回滚,确认才生效且同一轮一起选择", () => {
  38. prepare();
  39. state().beginSelectionEdit("a");
  40. state().togglePendingRow("a", 1);
  41. expect(getConversationShareSelectedRowIds(state(), "a")).toEqual([1, 2, 3]);
  42. expect(state().drafts.a.pendingExcludedRowIds).toEqual([1, 2]);
  43. state().cancelSelectionEdit("a");
  44. expect(getConversationShareSelectedRowIds(state(), "a")).toEqual([1, 2, 3]);
  45. state().beginSelectionEdit("a");
  46. state().togglePendingRow("a", 1);
  47. state().confirmSelectionEdit("a");
  48. expect(getConversationShareSelectedRowIds(state(), "a")).toEqual([3]);
  49. });
  50. it("空选择不能确认,取消保留披露确认,关闭重开保留草稿", () => {
  51. prepare();
  52. state().updateDockState("a", {
  53. title: "保留标题",
  54. disclosureAccepted: true,
  55. });
  56. state().beginSelectionEdit("a");
  57. state().togglePendingRow("a", 1);
  58. state().togglePendingRow("a", 3);
  59. state().confirmSelectionEdit("a");
  60. expect(state().drafts.a.pendingExcludedRowIds).not.toBeNull();
  61. state().cancelSelectionEdit("a");
  62. expect(state().dockStates.a.disclosureAccepted).toBe(true);
  63. state().closeShare("a");
  64. state().openShare("a");
  65. expect(state().dockStates.a.title).toBe("保留标题");
  66. expect(state().drafts.a.open).toBe(true);
  67. });
  68. it("发布锁定和会话隔离", () => {
  69. prepare();
  70. state().openShare("b");
  71. state().updateDockState("a", { publishing: true });
  72. state().closeShare("a");
  73. state().beginSelectionEdit("a");
  74. expect(state().drafts.a.open).toBe(true);
  75. expect(state().drafts.a.pendingExcludedRowIds).toBeNull();
  76. state().closeShare("b");
  77. expect(state().drafts.b.open).toBe(false);
  78. });
  79. it("失败后关闭重开保留草稿和原请求,供原地重试", () => {
  80. prepare();
  81. state().beginSelectionEdit("a");
  82. state().togglePendingRow("a", 3);
  83. state().confirmSelectionEdit("a");
  84. state().updateDockState("a", {
  85. disclosureAccepted: true,
  86. attempt: { key: "retry", clientRequestId: "retry-request", disclosureAcceptedAt: 1 },
  87. error: { issues: [], issueCount: 0, messageId: "conversationShare.issue.unknown" },
  88. });
  89. const dock = state().dockStates.a;
  90. state().closeShare("a");
  91. state().openShare("a");
  92. expect(state().dockStates.a).toBe(dock);
  93. expect(getConversationShareSelectedRowIds(state(), "a")).toEqual([1, 2]);
  94. });
  95. it("成功后重开开始新分享,保留标题权限,重置范围和发布确认", () => {
  96. prepare();
  97. state().setAccessMode("a", "public_readonly");
  98. state().beginSelectionEdit("a");
  99. state().togglePendingRow("a", 1);
  100. state().confirmSelectionEdit("a");
  101. state().updateDockState("a", {
  102. title: "再次分享",
  103. disclosureAccepted: true,
  104. publishedShareUrl: "https://example.com/share/old",
  105. attempt: { key: "old", clientRequestId: "old-request", disclosureAcceptedAt: 1 },
  106. progress: "checking",
  107. completedArtifacts: 2,
  108. totalArtifacts: 2,
  109. warnings: { issues: [], issueCount: 1 },
  110. });
  111. state().closeShare("a");
  112. state().openShare("a");
  113. expect(state().dockStates.a).toMatchObject({
  114. title: "再次分享",
  115. disclosureAccepted: false,
  116. publishedShareUrl: null,
  117. attempt: null,
  118. progress: "collecting",
  119. completedArtifacts: 0,
  120. totalArtifacts: 0,
  121. warnings: null,
  122. error: null,
  123. });
  124. expect(state().drafts.a.accessMode).toBe("public_readonly");
  125. expect(getConversationShareSelectedRowIds(state(), "a")).toEqual([1, 2, 3]);
  126. state().beginSelectionEdit("a");
  127. expect(state().drafts.a.pendingExcludedRowIds).toEqual([]);
  128. });
  129. });
  130. it("A/B 标题权限、范围、披露、发布进度和结果分别归属各自任务", () => {
  131. state().resetForTests();
  132. for (const id of ["a", "b"]) {
  133. state().openShare(id);
  134. state().syncAvailableTurns(id, [
  135. { rowId: 1, productTurnId: "t1" },
  136. { rowId: 2, productTurnId: "t2" },
  137. ]);
  138. }
  139. state().setAccessMode("a", "public_readonly");
  140. state().updateDockState("a", { title: "A", disclosureAccepted: true });
  141. state().beginSelectionEdit("a");
  142. state().togglePendingRow("a", 1);
  143. state().confirmSelectionEdit("a");
  144. state().updateDockState("a", { publishing: true, progress: "uploading", completedArtifacts: 2 });
  145. const aDraft = state().drafts.a;
  146. const aDock = state().dockStates.a;
  147. state().updateDockState("b", {
  148. title: "B",
  149. disclosureAccepted: true,
  150. publishedShareUrl: "https://example.com/b",
  151. });
  152. state().closeShare("b");
  153. state().openShare("b");
  154. expect(state().drafts.a).toBe(aDraft);
  155. expect(state().dockStates.a).toBe(aDock);
  156. expect(state().drafts.b.accessMode).toBe("public_importable");
  157. expect(getConversationShareSelectedProductTurnIds(state(), "b")).toEqual(["t1", "t2"]);
  158. expect(state().dockStates.b).toMatchObject({
  159. title: "B",
  160. publishing: false,
  161. progress: "collecting",
  162. completedArtifacts: 0,
  163. publishedShareUrl: null,
  164. disclosureAccepted: false,
  165. });
  166. state().updateDockState("a", { publishing: false, publishedShareUrl: "https://example.com/a" });
  167. expect(state().dockStates.b.publishedShareUrl).toBeNull();
  168. });
  169. it("发布中所有范围事务命令均无效", () => {
  170. state().resetForTests();
  171. state().openShare("a");
  172. state().syncAvailableTurns("a", [{ rowId: 1, productTurnId: "t1" }]);
  173. state().beginSelectionEdit("a");
  174. state().updateDockState("a", { publishing: true });
  175. const draft = state().drafts.a;
  176. state().togglePendingRow("a", 1);
  177. state().setAllPendingRowsSelected("a", false);
  178. state().confirmSelectionEdit("a");
  179. state().cancelSelectionEdit("a");
  180. state().closeShare("a");
  181. expect(state().drafts.a).toBe(draft);
  182. });
  183. it.each(["confirm", "cancel"])("问题详情整轮移除遵守 pending 事务:%s", (action) => {
  184. state().resetForTests();
  185. state().openShare("a");
  186. state().syncAvailableTurns("a", [
  187. { rowId: 1, productTurnId: "t1" },
  188. { rowId: 2, productTurnId: "t1" },
  189. { rowId: 3, productTurnId: "t2" },
  190. ]);
  191. state().updateDockState("a", { disclosureAccepted: true });
  192. state().beginSelectionEdit("a");
  193. state().deselectProductTurn("a", "t1");
  194. state().deselectProductTurn("a", "t1");
  195. expect(state().drafts.a.pendingExcludedRowIds).toEqual([1, 2]);
  196. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(["t1", "t2"]);
  197. expect(getConversationShareSelectedProductTurnIds(state(), "a", "pending")).toEqual(["t2"]);
  198. expect(state().dockStates.a.disclosureAccepted).toBe(true);
  199. if (action === "confirm") state().confirmSelectionEdit("a");
  200. else state().cancelSelectionEdit("a");
  201. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(
  202. action === "confirm" ? ["t2"] : ["t1", "t2"],
  203. );
  204. expect(state().dockStates.a.disclosureAccepted).toBe(action === "cancel");
  205. });
  206. it("配置阶段整轮移除立即生效并重置披露,发布期间忽略移除", () => {
  207. state().resetForTests();
  208. state().openShare("a");
  209. state().syncAvailableTurns("a", [
  210. { rowId: 1, productTurnId: "t1" },
  211. { rowId: 2, productTurnId: "t2" },
  212. ]);
  213. state().updateDockState("a", { disclosureAccepted: true });
  214. state().deselectProductTurn("a", "t1");
  215. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(["t2"]);
  216. expect(state().dockStates.a.disclosureAccepted).toBe(false);
  217. state().updateDockState("a", { publishing: true });
  218. state().deselectProductTurn("a", "t2");
  219. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(["t2"]);
  220. });
  221. it.each(["reopen", "running-completed", "pending-cancel"])(
  222. "候选同步扩大已确认范围时披露失效:%s",
  223. (mode) => {
  224. state().resetForTests();
  225. const turns = [
  226. { rowId: 1, productTurnId: "t1" },
  227. { rowId: 2, productTurnId: "t2" },
  228. ];
  229. state().openShare("a");
  230. state().syncAvailableTurns("a", turns);
  231. state().deselectProductTurn("a", "t2");
  232. state().updateDockState("a", { disclosureAccepted: true });
  233. if (mode === "reopen") {
  234. state().closeShare("a");
  235. state().openShare("a");
  236. }
  237. if (mode === "pending-cancel") {
  238. state().beginSelectionEdit("a");
  239. state().deselectProductTurn("a", "t1");
  240. }
  241. state().syncAvailableTurns("a", [...turns, { rowId: 3, productTurnId: "private-new-turn" }]);
  242. expect(state().dockStates.a.disclosureAccepted).toBe(false);
  243. if (mode === "pending-cancel") state().cancelSelectionEdit("a");
  244. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual([
  245. "t1",
  246. "private-new-turn",
  247. ]);
  248. expect(state().dockStates.a.disclosureAccepted).toBe(false);
  249. },
  250. );
  251. it("候选顺序、同轮 query 或未选轮次变化而有效范围不变时保留披露", () => {
  252. state().resetForTests();
  253. state().openShare("a");
  254. state().syncAvailableTurns("a", [
  255. { rowId: 1, productTurnId: "t1" },
  256. { rowId: 2, productTurnId: "t2" },
  257. ]);
  258. state().deselectProductTurn("a", "t2");
  259. state().updateDockState("a", { disclosureAccepted: true });
  260. state().closeShare("a");
  261. state().openShare("a");
  262. state().syncAvailableTurns("a", [
  263. { rowId: 3, productTurnId: "t1" },
  264. { rowId: 1, productTurnId: "t1" },
  265. ]);
  266. expect(getConversationShareSelectedProductTurnIds(state(), "a")).toEqual(["t1"]);
  267. expect(state().dockStates.a.disclosureAccepted).toBe(true);
  268. });