HighspeedShareNoticeCard.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { GlobalNoticeEnvelope } from "@/global-notice/globalNoticeStore.js";
  6. import { HighspeedShareNoticeCard } from "@/highspeed/HighspeedShareNoticeCard.js";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. const notice: GlobalNoticeEnvelope = {
  9. noticeId: "notice-1",
  10. dedupeKey: "highspeed-share:latest",
  11. type: "highspeed-share",
  12. schemaVersion: 1,
  13. scope: "user",
  14. priority: "normal",
  15. interruptPolicy: "passive",
  16. createdAt: 1,
  17. expiresAt: 10_000,
  18. payload: {
  19. cardId: "hsc-1",
  20. tokenUsage: 120_000,
  21. durationMs: 600_000,
  22. savedDurationMs: 480_000,
  23. },
  24. actions: [],
  25. dismissPolicy: { kind: "cooldown", cooldownMs: 86_400_000 },
  26. persistencePolicy: "latest-only",
  27. source: "highspeed",
  28. revision: 1,
  29. };
  30. afterEach(() => cleanup());
  31. function renderCard(target: GlobalNoticeEnvelope) {
  32. return render(
  33. createElement(
  34. ZCodeIntlProvider,
  35. { initialLocale: "en-US" },
  36. createElement(HighspeedShareNoticeCard, { notice: target, onDismiss: vi.fn() }),
  37. ),
  38. );
  39. }
  40. describe("HighspeedShareNoticeCard", () => {
  41. it("renders the compact card and opens the local share dialog", () => {
  42. const { container, getByRole } = renderCard(notice);
  43. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  44. fireEvent.click(getByRole("button", { name: "View" }));
  45. expect(getByRole("button", { name: "Copy image" })).toBeTruthy();
  46. expect(document.body.textContent).toContain("I used the ZCode Highspeed card and saved 480s.");
  47. expect(document.body.textContent).toContain("https://zcode.z.ai");
  48. expect(document.querySelector('[data-highspeed-share-dialog-card="true"]')).not.toBeNull();
  49. });
  50. it("keeps the card visible but hides View when speedup is not greater than 1.2x", () => {
  51. // 纵深防御:发布门禁不会放出 ≤1.2x 的卡,这里锁的是不可信/遗留数据不得进入分享弹窗。
  52. const lowSpeedupNotice: GlobalNoticeEnvelope = {
  53. ...notice,
  54. payload: { ...notice.payload, savedDurationMs: 120_000 },
  55. };
  56. const { container, queryByRole } = renderCard(lowSpeedupNotice);
  57. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  58. expect(queryByRole("button", { name: "View" })).toBeNull();
  59. });
  60. it("renders View for a boundary card that only clears 1.2x at millisecond precision", () => {
  61. // Bug 回归(spec §9):发布门禁按毫秒判定 7325/6100≈1.2008 > 1.2 而发布;旧 payload 只带
  62. // 秒级取整值,渲染侧由 ceil(7s)/round(1s) 重建成 8/7≈1.14 ≤ 1.2 → View 被隐藏,
  63. // 出现“有卡片、无入口”的半残卡。渲染侧必须复用与门禁同精度的毫秒字段复检。
  64. const boundaryNotice: GlobalNoticeEnvelope = {
  65. ...notice,
  66. payload: { cardId: "hsc-edge", tokenUsage: 1_000, durationMs: 6_100, savedDurationMs: 1_225 },
  67. };
  68. const { container, getByRole } = renderCard(boundaryNotice);
  69. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  70. expect(getByRole("button", { name: "View" })).toBeTruthy();
  71. });
  72. it("still renders legacy second-precision payloads persisted before the millisecond fields", () => {
  73. const legacyNotice: GlobalNoticeEnvelope = {
  74. ...notice,
  75. payload: {
  76. cardId: "hsc-legacy",
  77. tokenUsage: 120_000,
  78. durationSeconds: 600,
  79. savedDurationSeconds: 480,
  80. },
  81. };
  82. const { container, getByRole } = renderCard(legacyNotice);
  83. expect(container.querySelector('[data-global-notice-type="highspeed-share"]')).not.toBeNull();
  84. expect(getByRole("button", { name: "View" })).toBeTruthy();
  85. });
  86. });