highspeedShare.test.ts 7.5 KB

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