highspeedShare.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildHighspeedShareNotice,
  4. copyHighspeedCardImage,
  5. copyHighspeedShareText,
  6. isHighspeedShareEligible,
  7. isHighspeedShareNoticePayload,
  8. resolveHighspeedShareNoticeMetrics,
  9. } from "@/highspeed/highspeedShare.js";
  10. const metrics = { outputTokens: 120_000, durationMs: 600_000, regularTps: 20, highspeedTps: 100 };
  11. describe("highspeed client share", () => {
  12. it("requires a complete speedup greater than 1.2x before enabling sharing", () => {
  13. expect(
  14. isHighspeedShareEligible({
  15. outputTokens: 100,
  16. durationMs: 10_000,
  17. regularTps: 10,
  18. highspeedTps: 18,
  19. savedDurationMs: 8_000,
  20. }),
  21. ).toBe(true);
  22. expect(
  23. isHighspeedShareEligible({
  24. outputTokens: 100,
  25. durationMs: 10_000,
  26. regularTps: 10,
  27. highspeedTps: 18,
  28. savedDurationMs: 2_000,
  29. }),
  30. ).toBe(false);
  31. expect(
  32. isHighspeedShareEligible({
  33. outputTokens: 100,
  34. durationMs: 10_000,
  35. regularTps: 10,
  36. highspeedTps: 18,
  37. savedDurationMs: 2_001,
  38. }),
  39. ).toBe(true);
  40. });
  41. it("rejects missing or invalid share metrics", () => {
  42. expect(
  43. isHighspeedShareEligible({
  44. outputTokens: 0,
  45. durationMs: 10_000,
  46. regularTps: 10,
  47. highspeedTps: 18,
  48. savedDurationMs: 8_000,
  49. }),
  50. ).toBe(false);
  51. expect(
  52. isHighspeedShareEligible({
  53. outputTokens: 100,
  54. durationMs: 0,
  55. regularTps: 10,
  56. highspeedTps: 18,
  57. savedDurationMs: 8_000,
  58. }),
  59. ).toBe(false);
  60. expect(
  61. isHighspeedShareEligible({
  62. outputTokens: 100,
  63. durationMs: 10_000,
  64. regularTps: 0,
  65. highspeedTps: 18,
  66. }),
  67. ).toBe(false);
  68. });
  69. it("publishes millisecond metrics without a server share id or URL", () => {
  70. const notice = buildHighspeedShareNotice({ cardId: "hsc_xxx", metrics, now: 100_000 });
  71. expect(notice.payload).toMatchObject({
  72. cardId: "hsc_xxx",
  73. tokenUsage: 120_000,
  74. durationMs: 600_000,
  75. savedDurationMs: 1_066_667,
  76. });
  77. // 回归锁:payload 不再写秒级取整字段,渲染侧只能从毫秒字段派生展示值(spec §9)。
  78. expect(notice.payload).not.toHaveProperty("durationSeconds");
  79. expect(notice.payload).not.toHaveProperty("savedDurationSeconds");
  80. expect(notice.payload).not.toHaveProperty("shareId");
  81. expect(notice.payload).not.toHaveProperty("shareUrl");
  82. });
  83. it("keeps boundary speedups eligible after a publish/render round trip", () => {
  84. // Bug 回归:6100ms/1225ms 实际倍率 7325/6100≈1.2008 > 1.2 而发布;旧 payload 只带
  85. // ceil(duration)=7s / round(saved)=1s,渲染侧重建为 8/7≈1.14 ≤ 1.2 便隐藏了 View。
  86. const boundary = {
  87. outputTokens: 1_000,
  88. durationMs: 6_100,
  89. regularTps: 10,
  90. highspeedTps: 18,
  91. savedDurationMs: 1_225,
  92. };
  93. expect(isHighspeedShareEligible(boundary)).toBe(true);
  94. const notice = buildHighspeedShareNotice({ cardId: "hsc_edge", metrics: boundary, now: 1 });
  95. const payload = notice.payload;
  96. if (!isHighspeedShareNoticePayload(payload)) throw new Error("payload shape changed");
  97. const rebuilt = resolveHighspeedShareNoticeMetrics(payload);
  98. expect(rebuilt).toMatchObject({
  99. outputTokens: 1_000,
  100. durationMs: 6_100,
  101. savedDurationMs: 1_225,
  102. });
  103. expect(isHighspeedShareEligible(rebuilt)).toBe(true);
  104. });
  105. it("reads legacy second-precision payloads by scaling back to milliseconds", () => {
  106. const rebuilt = resolveHighspeedShareNoticeMetrics({
  107. cardId: "hsc_legacy",
  108. tokenUsage: 120_000,
  109. durationSeconds: 600,
  110. savedDurationSeconds: 480,
  111. });
  112. expect(rebuilt).toMatchObject({
  113. outputTokens: 120_000,
  114. durationMs: 600_000,
  115. savedDurationMs: 480_000,
  116. });
  117. expect(isHighspeedShareEligible(rebuilt)).toBe(true);
  118. });
  119. it("copies an image blob containing only the metric card", async () => {
  120. let copied: Blob | undefined;
  121. await copyHighspeedCardImage(metrics, async (blob) => {
  122. copied = blob;
  123. });
  124. expect(copied?.type).toBe("image/svg+xml");
  125. expect(await copied?.text()).toContain("120,000 tokens");
  126. expect(await copied?.text()).not.toContain("Copy image");
  127. });
  128. it("uses localized labels when copying the image", async () => {
  129. let copied: Blob | undefined;
  130. await copyHighspeedCardImage(
  131. metrics,
  132. async (blob) => {
  133. copied = blob;
  134. },
  135. {
  136. benefit: "ZCode 与 Coding Plan 专属权益",
  137. standardSpeed: "标准生成速度",
  138. speedSummary: "HighSpeed · 20 分钟",
  139. lessWaiting: "减少等待 80%",
  140. imageSummary: "120,000 tokens · 节省 1,067 秒",
  141. },
  142. );
  143. const svg = await copied?.text();
  144. expect(svg).toContain("标准生成速度");
  145. expect(svg).toContain("减少等待 80%");
  146. expect(svg).toContain("节省 1,067 秒");
  147. expect(svg).not.toContain("standard generation speed");
  148. });
  149. it("copies the complete share text", async () => {
  150. let copied = "";
  151. await copyHighspeedShareText(
  152. "我用 ZCode 高速卡节省了 120 秒的时间,你也来试试吧。下载 ZCode https://zcode.z.ai",
  153. async (text) => {
  154. copied = text;
  155. },
  156. );
  157. expect(copied).toBe(
  158. "我用 ZCode 高速卡节省了 120 秒的时间,你也来试试吧。下载 ZCode https://zcode.z.ai",
  159. );
  160. });
  161. it("rasterizes the SVG before writing it to the image clipboard", async () => {
  162. const originalImage = globalThis.Image;
  163. const originalDocument = globalThis.document;
  164. const originalCreateObjectURL = URL.createObjectURL;
  165. const originalRevokeObjectURL = URL.revokeObjectURL;
  166. const onload = vi.fn();
  167. let rasterSource: Blob | undefined;
  168. const canvasContext = {
  169. beginPath: vi.fn(),
  170. moveTo: vi.fn(),
  171. lineTo: vi.fn(),
  172. arcTo: vi.fn(),
  173. closePath: vi.fn(),
  174. clip: vi.fn(),
  175. drawImage: vi.fn(),
  176. };
  177. Object.assign(globalThis, {
  178. Image: class {
  179. set src(_value: string) {
  180. onload();
  181. (this as { onload?: () => void }).onload?.();
  182. }
  183. onload?: () => void;
  184. },
  185. document: {
  186. createElement: () => ({
  187. width: 0,
  188. height: 0,
  189. getContext: () => canvasContext,
  190. toBlob: (callback: (blob: Blob) => void) =>
  191. callback(new Blob(["png"], { type: "image/png" })),
  192. }),
  193. },
  194. });
  195. URL.createObjectURL = vi.fn((blob: Blob) => {
  196. rasterSource = blob;
  197. return "blob:test";
  198. });
  199. URL.revokeObjectURL = vi.fn();
  200. try {
  201. let copied: Blob | undefined;
  202. await copyHighspeedCardImage(metrics, async (blob) => {
  203. copied = blob;
  204. });
  205. expect(copied?.type).toBe("image/png");
  206. expect(onload).toHaveBeenCalled();
  207. // 回归:canvas 禁止再做二次圆角裁剪——SVG 位图边缘与矢量 clip 边缘叠加会在
  208. // 四角残留浅色弧线错位;圆角透明角只能由 SVG rect rx 自带。
  209. expect(canvasContext.clip).not.toHaveBeenCalled();
  210. // 回归:光栅化 SVG 必须按超采样尺寸(jsdom 无 devicePixelRatio,兜底 2x)
  211. // 直接光栅化,viewBox 保持逻辑坐标,避免位图拉伸导致边缘模糊。
  212. const rasterSvg = await rasterSource?.text();
  213. expect(rasterSvg).toContain('width="872" height="608" viewBox="0 0 436 304"');
  214. // 回归:导出 PNG 必须是满幅矩形——自带透明圆角会在粘贴环境(聊天工具的圆角
  215. // 图片容器)内再叠一层圆角,两层半径不一致时四角露出容器背景色形成浅色弧线。
  216. // 圆角统一交给展示环境裁剪。
  217. expect(rasterSvg).not.toContain('rx="20"');
  218. } finally {
  219. Object.assign(globalThis, { Image: originalImage, document: originalDocument });
  220. URL.createObjectURL = originalCreateObjectURL;
  221. URL.revokeObjectURL = originalRevokeObjectURL;
  222. }
  223. });
  224. });