cloudContentDialogHero.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. const mockInput = {
  12. locale: "zh-CN" as const,
  13. planName: "Weekend Plan",
  14. amountLabel: "300,000,000 Tokens",
  15. benefits: ["GLM-5.3-Flash 每日额度"],
  16. endsAtLabel: "2026年9月4日 23:59",
  17. title: "Weekend Plan 领取成功",
  18. description: "GLM-5.3-Flash 已可使用。",
  19. replayLabel: "重新播放票券动画",
  20. confirmLabel: "模型设置",
  21. shareLabel: "复制分享",
  22. shareText: "Weekend Plan 分享内容",
  23. };
  24. afterEach(() => cleanup());
  25. describe("CloudDialogHero", () => {
  26. it("drops undeclared actions and falls back when ready times out", () => {
  27. vi.useFakeTimers();
  28. try {
  29. const payload = createWeekendPlanResultDialogMock(mockInput);
  30. const onAction = vi.fn();
  31. render(
  32. createElement(CloudDialogHero, {
  33. hero: payload.dialog.hero,
  34. locale: "en-US",
  35. title: "Timeout hero",
  36. onAction,
  37. }),
  38. );
  39. const frame = screen.getByTitle("Timeout hero") as HTMLIFrameElement;
  40. act(() =>
  41. window.dispatchEvent(
  42. new MessageEvent("message", {
  43. source: frame.contentWindow,
  44. data: {
  45. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  46. instanceId: frame.dataset.instanceId,
  47. type: "action",
  48. id: "claim-plan",
  49. },
  50. }),
  51. ),
  52. );
  53. expect(onAction).not.toHaveBeenCalled();
  54. act(() => vi.advanceTimersByTime(5_001));
  55. expect(screen.queryByTestId("cloud-dialog-interactive-hero")).toBeNull();
  56. expect(screen.getByTestId("cloud-dialog-hero-error")).toBeTruthy();
  57. } finally {
  58. cleanup();
  59. vi.useRealTimers();
  60. }
  61. });
  62. it("用无同源权限的 iframe 加载交互资源并发送初始化数据", () => {
  63. const payload = createWeekendPlanResultDialogMock(mockInput);
  64. render(
  65. createElement(CloudDialogHero, {
  66. hero: payload.dialog.hero,
  67. locale: "zh-CN",
  68. title: "Weekend Hero",
  69. }),
  70. );
  71. const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
  72. expect(frame.getAttribute("sandbox")).toBe("allow-scripts");
  73. expect(frame.getAttribute("sandbox")).not.toContain("allow-same-origin");
  74. expect(frame.dataset.heroType).toBe("interactive_bundle");
  75. expect(frame.dataset.status).toBe("loading");
  76. const postMessage = vi.spyOn(frame.contentWindow!, "postMessage");
  77. fireEvent.load(frame);
  78. expect(postMessage).toHaveBeenCalledWith(
  79. expect.objectContaining({
  80. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  81. type: "init",
  82. locale: "zh-CN",
  83. data: expect.objectContaining({ planName: "Weekend Plan" }),
  84. }),
  85. "*",
  86. );
  87. });
  88. it("只接受当前 iframe 与 instanceId 对应的 ready 消息", async () => {
  89. const payload = createWeekendPlanResultDialogMock(mockInput);
  90. render(
  91. createElement(CloudDialogHero, {
  92. hero: payload.dialog.hero,
  93. locale: "zh-CN",
  94. title: "Weekend Hero",
  95. }),
  96. );
  97. const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
  98. const instanceId = frame.dataset.instanceId!;
  99. expect(
  100. isTrustedCloudHeroMessage(
  101. {
  102. source: frame.contentWindow,
  103. data: {
  104. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  105. type: "ready",
  106. instanceId: "stale-instance",
  107. },
  108. },
  109. frame.contentWindow,
  110. instanceId,
  111. ),
  112. ).toBe(false);
  113. expect(
  114. isTrustedCloudHeroMessage(
  115. {
  116. source: frame.contentWindow,
  117. data: {
  118. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  119. type: "ready",
  120. instanceId,
  121. },
  122. },
  123. frame.contentWindow,
  124. instanceId,
  125. ),
  126. ).toBe(true);
  127. window.dispatchEvent(
  128. new MessageEvent("message", {
  129. source: frame.contentWindow,
  130. data: {
  131. channel: CLOUD_HERO_MESSAGE_CHANNEL,
  132. type: "ready",
  133. instanceId,
  134. },
  135. }),
  136. );
  137. await waitFor(() => expect(frame.dataset.status).toBe("ready"));
  138. });
  139. });