| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- // @vitest-environment jsdom
- import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
- import { createElement } from "react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- CLOUD_HERO_MESSAGE_CHANNEL,
- CloudDialogHero,
- isTrustedCloudHeroMessage,
- } from "@/components/cloud-content-dialog/CloudDialogHero.js";
- import { createWeekendPlanResultDialogMock } from "./helpers/weekendPlanDialogFixture.js";
- const mockInput = {
- locale: "zh-CN" as const,
- planName: "Weekend Plan",
- amountLabel: "300,000,000 Tokens",
- benefits: ["GLM-5.3-Flash 每日额度"],
- endsAtLabel: "2026年9月4日 23:59",
- title: "Weekend Plan 领取成功",
- description: "GLM-5.3-Flash 已可使用。",
- replayLabel: "重新播放票券动画",
- confirmLabel: "模型设置",
- shareLabel: "复制分享",
- shareText: "Weekend Plan 分享内容",
- };
- afterEach(() => cleanup());
- describe("CloudDialogHero", () => {
- it("drops undeclared actions and falls back when ready times out", () => {
- vi.useFakeTimers();
- try {
- const payload = createWeekendPlanResultDialogMock(mockInput);
- const onAction = vi.fn();
- render(
- createElement(CloudDialogHero, {
- hero: payload.dialog.hero,
- locale: "en-US",
- title: "Timeout hero",
- onAction,
- }),
- );
- const frame = screen.getByTitle("Timeout hero") as HTMLIFrameElement;
- act(() =>
- window.dispatchEvent(
- new MessageEvent("message", {
- source: frame.contentWindow,
- data: {
- channel: CLOUD_HERO_MESSAGE_CHANNEL,
- instanceId: frame.dataset.instanceId,
- type: "action",
- id: "claim-plan",
- },
- }),
- ),
- );
- expect(onAction).not.toHaveBeenCalled();
- act(() => vi.advanceTimersByTime(5_001));
- expect(screen.queryByTestId("cloud-dialog-interactive-hero")).toBeNull();
- expect(screen.getByTestId("cloud-dialog-hero-error")).toBeTruthy();
- } finally {
- cleanup();
- vi.useRealTimers();
- }
- });
- it("用无同源权限的 iframe 加载交互资源并发送初始化数据", () => {
- const payload = createWeekendPlanResultDialogMock(mockInput);
- render(
- createElement(CloudDialogHero, {
- hero: payload.dialog.hero,
- locale: "zh-CN",
- title: "Weekend Hero",
- }),
- );
- const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
- expect(frame.getAttribute("sandbox")).toBe("allow-scripts");
- expect(frame.getAttribute("sandbox")).not.toContain("allow-same-origin");
- expect(frame.dataset.heroType).toBe("interactive_bundle");
- expect(frame.dataset.status).toBe("loading");
- const postMessage = vi.spyOn(frame.contentWindow!, "postMessage");
- fireEvent.load(frame);
- expect(postMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- channel: CLOUD_HERO_MESSAGE_CHANNEL,
- type: "init",
- locale: "zh-CN",
- data: expect.objectContaining({ planName: "Weekend Plan" }),
- }),
- "*",
- );
- });
- it("只接受当前 iframe 与 instanceId 对应的 ready 消息", async () => {
- const payload = createWeekendPlanResultDialogMock(mockInput);
- render(
- createElement(CloudDialogHero, {
- hero: payload.dialog.hero,
- locale: "zh-CN",
- title: "Weekend Hero",
- }),
- );
- const frame = screen.getByTitle("Weekend Hero") as HTMLIFrameElement;
- const instanceId = frame.dataset.instanceId!;
- expect(
- isTrustedCloudHeroMessage(
- {
- source: frame.contentWindow,
- data: {
- channel: CLOUD_HERO_MESSAGE_CHANNEL,
- type: "ready",
- instanceId: "stale-instance",
- },
- },
- frame.contentWindow,
- instanceId,
- ),
- ).toBe(false);
- expect(
- isTrustedCloudHeroMessage(
- {
- source: frame.contentWindow,
- data: {
- channel: CLOUD_HERO_MESSAGE_CHANNEL,
- type: "ready",
- instanceId,
- },
- },
- frame.contentWindow,
- instanceId,
- ),
- ).toBe(true);
- window.dispatchEvent(
- new MessageEvent("message", {
- source: frame.contentWindow,
- data: {
- channel: CLOUD_HERO_MESSAGE_CHANNEL,
- type: "ready",
- instanceId,
- },
- }),
- );
- await waitFor(() => expect(frame.dataset.status).toBe("ready"));
- });
- });
|