cloudContentDialogHero.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import {
  6. CLOUD_HERO_MESSAGE_CHANNEL,
  7. CloudDialogHero,
  8. isTrustedCloudHeroMessage,
  9. } from "@/components/cloud-content-dialog/CloudDialogHero.js";
  10. import { createWeekendPlanResultDialogMock } from "./helpers/weekendPlanDialogFixture.js";
  11. import weekendPlanResultDialogFixture from "@/components/cloud-content-dialog/mocks/weekendPlanResultDialog.zh-CN.json" with { type: "json" };
  12. const mockInput = {
  13. locale: "zh-CN" as const,
  14. planName: "Weekend Plan",
  15. amountLabel: "300,000,000 Tokens",
  16. benefits: ["GLM-5.3-Flash 每日额度"],
  17. endsAtLabel: "2026年9月4日 23:59",
  18. title: "Weekend Plan 领取成功",
  19. description: "GLM-5.3-Flash 已可使用。",
  20. replayLabel: "重新播放票券动画",
  21. confirmLabel: "模型设置",
  22. shareLabel: "复制分享",
  23. shareText: "Weekend Plan 分享内容",
  24. };
  25. afterEach(() => cleanup());
  26. describe("cloud content dialog mock", () => {
  27. it("提供 HTTP mock 填充 ZIP 元数据之前的受控内容模板", () => {
  28. expect(weekendPlanResultDialogFixture).toMatchObject({
  29. schemaVersion: 1,
  30. id: "weekend-plan-result-dialog",
  31. revision: 1,
  32. kind: "campaign",
  33. locale: "zh-CN",
  34. dialog: {
  35. hero: {
  36. type: "interactive_bundle",
  37. },
  38. title: "Weekend Plan 领取成功",
  39. description: { format: "plain_text" },
  40. buttons: expect.arrayContaining([
  41. expect.objectContaining({ actionId: "open-model-settings" }),
  42. expect.objectContaining({ actionId: "copy-share" }),
  43. ]),
  44. },
  45. actions: {
  46. "open-model-settings": { type: "navigate", destination: "model_settings" },
  47. "copy-share": { type: "copy_text" },
  48. },
  49. });
  50. });
  51. it("使用未来云端协议形状描述当前 Weekend Hero 资源", () => {
  52. const payload = createWeekendPlanResultDialogMock(mockInput);
  53. expect(payload).toMatchObject({
  54. schemaVersion: 1,
  55. id: "weekend-plan-result-dialog",
  56. revision: 1,
  57. kind: "campaign",
  58. locale: "zh-CN",
  59. dialog: {
  60. title: mockInput.title,
  61. description: { format: "plain_text", text: mockInput.description },
  62. hero: {
  63. type: "interactive_bundle",
  64. runtime: "zcode-hero-sandbox-v1",
  65. resolvedUrl: expect.stringContaining("weekend-plan-hero"),
  66. viewport: { aspectRatio: "4:3" },
  67. data: {
  68. planName: mockInput.planName,
  69. amountLabel: mockInput.amountLabel,
  70. benefits: mockInput.benefits,
  71. endsAtLabel: mockInput.endsAtLabel,
  72. replayLabel: mockInput.replayLabel,
  73. },
  74. },
  75. buttons: [
  76. {
  77. id: "open-model-settings",
  78. label: mockInput.confirmLabel,
  79. actionId: "open-model-settings",
  80. },
  81. {
  82. id: "copy-share",
  83. label: mockInput.shareLabel,
  84. actionId: "copy-share",
  85. },
  86. ],
  87. },
  88. actions: {
  89. "open-model-settings": { type: "navigate", destination: "model_settings" },
  90. "copy-share": { type: "copy_text", text: mockInput.shareText },
  91. },
  92. });
  93. expect(payload.dialog.hero.type).toBe("interactive_bundle");
  94. if (payload.dialog.hero.type !== "interactive_bundle") return;
  95. expect(payload.dialog.hero.resolvedUrl).toContain("weekend-plan-hero");
  96. expect(payload.dialog.hero.bundle).toBeUndefined();
  97. });
  98. });
  99. describe("CloudDialogHero", () => {
  100. it("drops undeclared actions and falls back when ready times out", () => {
  101. vi.useFakeTimers();
  102. try {
  103. const payload = createWeekendPlanResultDialogMock(mockInput);
  104. const onAction = vi.fn();
  105. render(
  106. createElement(CloudDialogHero, {
  107. hero: payload.dialog.hero,
  108. locale: "en-US",
  109. title: "Timeout hero",
  110. onAction,
  111. }),
  112. );
  113. const frame = screen.getByTitle("Timeout hero") as HTMLIFrameElement;
  114. act(() =>
  115. window.dispatchEvent(
  116. new MessageEvent("message", {
  117. source: frame.contentWindow,
  118. data: {
  119. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  120. instanceId: frame.dataset.instanceId,
  121. type: "action",
  122. id: "claim-plan",
  123. },
  124. }),
  125. ),
  126. );
  127. expect(onAction).not.toHaveBeenCalled();
  128. act(() => vi.advanceTimersByTime(5_001));
  129. expect(screen.queryByTestId("cloud-dialog-interactive-hero")).toBeNull();
  130. expect(screen.getByTestId("cloud-dialog-hero-error")).toBeTruthy();
  131. } finally {
  132. cleanup();
  133. vi.useRealTimers();
  134. }
  135. });
  136. it("用无同源权限的 iframe 加载交互资源并发送初始化数据", () => {
  137. const payload = createWeekendPlanResultDialogMock(mockInput);
  138. render(
  139. createElement(CloudDialogHero, {
  140. hero: payload.dialog.hero,
  141. locale: "zh-CN",
  142. title: "Weekend Hero",
  143. }),
  144. );
  145. const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
  146. expect(frame.getAttribute("sandbox")).toBe("allow-scripts");
  147. expect(frame.getAttribute("sandbox")).not.toContain("allow-same-origin");
  148. expect(frame.dataset.heroType).toBe("interactive_bundle");
  149. expect(frame.dataset.status).toBe("loading");
  150. const postMessage = vi.spyOn(frame.contentWindow!, "postMessage");
  151. fireEvent.load(frame);
  152. expect(postMessage).toHaveBeenCalledWith(
  153. expect.objectContaining({
  154. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  155. type: "init",
  156. locale: "zh-CN",
  157. data: expect.objectContaining({ planName: "Weekend Plan" }),
  158. }),
  159. "*",
  160. );
  161. });
  162. it("只接受当前 iframe 与 instanceId 对应的 ready 消息", async () => {
  163. const payload = createWeekendPlanResultDialogMock(mockInput);
  164. render(
  165. createElement(CloudDialogHero, {
  166. hero: payload.dialog.hero,
  167. locale: "zh-CN",
  168. title: "Weekend Hero",
  169. }),
  170. );
  171. const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
  172. const instanceId = frame.dataset.instanceId!;
  173. expect(
  174. isTrustedCloudHeroMessage(
  175. {
  176. source: frame.contentWindow,
  177. data: {
  178. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  179. type: "ready",
  180. instanceId: "stale-instance",
  181. },
  182. },
  183. frame.contentWindow,
  184. instanceId,
  185. ),
  186. ).toBe(false);
  187. expect(
  188. isTrustedCloudHeroMessage(
  189. {
  190. source: frame.contentWindow,
  191. data: {
  192. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  193. type: "ready",
  194. instanceId,
  195. },
  196. },
  197. frame.contentWindow,
  198. instanceId,
  199. ),
  200. ).toBe(true);
  201. window.dispatchEvent(
  202. new MessageEvent("message", {
  203. source: frame.contentWindow,
  204. data: {
  205. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  206. type: "ready",
  207. instanceId,
  208. },
  209. }),
  210. );
  211. await waitFor(() => expect(frame.dataset.status).toBe("ready"));
  212. });
  213. });