HighspeedShareNoticeCard.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @vitest-environment jsdom
  2. import { readFileSync } from "node:fs";
  3. import { createElement } from "react";
  4. import { cleanup, fireEvent, render } from "@testing-library/react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import type { GlobalNoticeEnvelope } from "@/global-notice/globalNoticeStore.js";
  7. import { HighspeedShareNoticeCard } from "@/highspeed/HighspeedShareNoticeCard.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. const notice: GlobalNoticeEnvelope = {
  10. noticeId: "notice-1",
  11. dedupeKey: "highspeed-share:latest",
  12. type: "highspeed-share",
  13. schemaVersion: 1,
  14. scope: "user",
  15. priority: "normal",
  16. interruptPolicy: "passive",
  17. createdAt: 1,
  18. expiresAt: 10_000,
  19. payload: {
  20. cardId: "hsc-1",
  21. tokenUsage: 120_000,
  22. durationMs: 600_000,
  23. savedDurationMs: 480_000,
  24. },
  25. actions: [],
  26. dismissPolicy: { kind: "cooldown", cooldownMs: 86_400_000 },
  27. persistencePolicy: "latest-only",
  28. source: "highspeed",
  29. revision: 1,
  30. };
  31. afterEach(() => cleanup());
  32. function renderCard(target: GlobalNoticeEnvelope) {
  33. return render(
  34. createElement(
  35. ZCodeIntlProvider,
  36. { initialLocale: "en-US" },
  37. createElement(HighspeedShareNoticeCard, { notice: target, onDismiss: vi.fn() }),
  38. ),
  39. );
  40. }
  41. describe("HighspeedShareNoticeCard", () => {
  42. it("renders the compact card and opens the local share dialog", () => {
  43. const { container, getByRole } = renderCard(notice);
  44. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  45. fireEvent.click(getByRole("button", { name: "View" }));
  46. expect(getByRole("button", { name: "Copy image" })).toBeTruthy();
  47. expect(document.querySelector(".highspeed-share-dialog__benefit")).not.toBeNull();
  48. expect(document.querySelector(".highspeed-share-dialog__chart-bars--baseline")).not.toBeNull();
  49. expect(document.querySelector(".highspeed-share-dialog__chart-bars--fast")).not.toBeNull();
  50. expect(document.body.textContent).toContain("I used the ZCode Highspeed card and saved 480s.");
  51. expect(document.body.textContent).toContain("https://zcode.z.ai");
  52. expect(document.querySelector('[data-highspeed-share-dialog-card="true"]')).not.toBeNull();
  53. // Bug 回归锁:Electron 窗口不可见时 animationend 永不触发,Radix 会因等退出动画
  54. // 卡住不卸载 overlay(bg-black/60 遮罩滞留吞点击)。关闭态必须无动画以同步卸载:
  55. // overlay/content 必须挂上 highspeed 钩子类,且 CSS 对关闭态强制 animation none。
  56. expect(document.querySelector('[data-slot="dialog-overlay"]')?.className).toContain(
  57. "highspeed-share-dialog__overlay",
  58. );
  59. expect(document.querySelector('[data-slot="dialog-content"]')?.className).toContain(
  60. "highspeed-share-dialog__content",
  61. );
  62. const shareCss = readFileSync("packages/ui/src/highspeed/highspeed-share.css", "utf8");
  63. expect(shareCss).not.toMatch(
  64. /@media \(max-width: 480px\)[\s\S]*?\.highspeed-share-dialog__card\s*\{[^}]*border-radius:\s*16px/,
  65. );
  66. expect(shareCss.replace(/\s+/g, " ")).toContain(
  67. '.highspeed-share-dialog__overlay[data-state="closed"], .highspeed-share-dialog__content[data-state="closed"] { animation: none !important; }',
  68. );
  69. expect(shareCss).toContain(".highspeed-share-dialog__url:hover svg");
  70. });
  71. it("keeps the card visible but hides View when speedup is not greater than 1.2x", () => {
  72. // 纵深防御:发布门禁不会放出 ≤1.2x 的卡,这里锁的是不可信/遗留数据不得进入分享弹窗。
  73. const lowSpeedupNotice: GlobalNoticeEnvelope = {
  74. ...notice,
  75. payload: { ...notice.payload, savedDurationMs: 120_000 },
  76. };
  77. const { container, queryByRole } = renderCard(lowSpeedupNotice);
  78. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  79. expect(queryByRole("button", { name: "View" })).toBeNull();
  80. });
  81. it("renders View for a boundary card that only clears 1.2x at millisecond precision", () => {
  82. // Bug 回归(spec §9):发布门禁按毫秒判定 7325/6100≈1.2008 > 1.2 而发布;旧 payload 只带
  83. // 秒级取整值,渲染侧由 ceil(7s)/round(1s) 重建成 8/7≈1.14 ≤ 1.2 → View 被隐藏,
  84. // 出现“有卡片、无入口”的半残卡。渲染侧必须复用与门禁同精度的毫秒字段复检。
  85. const boundaryNotice: GlobalNoticeEnvelope = {
  86. ...notice,
  87. payload: { cardId: "hsc-edge", tokenUsage: 1_000, durationMs: 6_100, savedDurationMs: 1_225 },
  88. };
  89. const { container, getByRole } = renderCard(boundaryNotice);
  90. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  91. expect(getByRole("button", { name: "View" })).toBeTruthy();
  92. });
  93. it("still renders legacy second-precision payloads persisted before the millisecond fields", () => {
  94. const legacyNotice: GlobalNoticeEnvelope = {
  95. ...notice,
  96. payload: {
  97. cardId: "hsc-legacy",
  98. tokenUsage: 120_000,
  99. durationSeconds: 600,
  100. savedDurationSeconds: 480,
  101. },
  102. };
  103. const { container, getByRole } = renderCard(legacyNotice);
  104. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  105. expect(getByRole("button", { name: "View" })).toBeTruthy();
  106. });
  107. });