|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { PageMotionBlurSummary } from "@hyperframes/engine"; |
| 3 | + |
| 4 | +const trackEvent = vi.fn(); |
| 5 | +vi.mock("./client.js", () => ({ |
| 6 | + trackEvent: (...args: unknown[]) => trackEvent(...args), |
| 7 | + flush: () => Promise.resolve(), |
| 8 | + shouldTrack: () => true, |
| 9 | +})); |
| 10 | +vi.mock("./system.js", async () => ({ |
| 11 | + ...(await vi.importActual<typeof import("./system.js")>("./system.js")), |
| 12 | + getPowerState: () => ({ on_battery: false, low_power_mode: false }), |
| 13 | +})); |
| 14 | + |
| 15 | +const { trackRenderComplete } = await import("./events.js"); |
| 16 | +const { renderMotionBlurEventProperties, renderMotionBlurTelemetryPayload } = |
| 17 | + await import("./renderMotionBlur.js"); |
| 18 | + |
| 19 | +const fingerprint = (over: Partial<NonNullable<PageMotionBlurSummary["fingerprint"]>>) => ({ |
| 20 | + canvas2d: 0, |
| 21 | + webgl: 0, |
| 22 | + video: 0, |
| 23 | + iframe: 0, |
| 24 | + customElement: 0, |
| 25 | + svgClip: 0, |
| 26 | + mask: 0, |
| 27 | + scrollContainer: 0, |
| 28 | + threeD: 0, |
| 29 | + overflowingMover: 0, |
| 30 | + frontOfMover: 0, |
| 31 | + blendMode: 0, |
| 32 | + fixedInMover: 0, |
| 33 | + pseudoOutside: 0, |
| 34 | + ...over, |
| 35 | +}); |
| 36 | + |
| 37 | +const blur = (over: Partial<PageMotionBlurSummary> = {}): PageMotionBlurSummary => ({ |
| 38 | + tier: "filter", |
| 39 | + reasonCode: "accelerated-canvas", |
| 40 | + movers: 4, |
| 41 | + failedFrames: 0, |
| 42 | + readEvents: false, |
| 43 | + fingerprint: fingerprint({}), |
| 44 | + ...over, |
| 45 | +}); |
| 46 | + |
| 47 | +const complete = (motionBlur: PageMotionBlurSummary | undefined) => { |
| 48 | + trackRenderComplete({ |
| 49 | + durationMs: 1, |
| 50 | + fps: 30, |
| 51 | + quality: "draft", |
| 52 | + docker: false, |
| 53 | + gpu: false, |
| 54 | + ...renderMotionBlurTelemetryPayload(motionBlur), |
| 55 | + }); |
| 56 | + return trackEvent.mock.calls[0]?.[1] as Record<string, unknown>; |
| 57 | +}; |
| 58 | + |
| 59 | +describe("render_complete motion blur properties", () => { |
| 60 | + beforeEach(() => trackEvent.mockClear()); |
| 61 | + |
| 62 | + it("sends the tier, movers, failed frames and one property per non-zero count", () => { |
| 63 | + const props = complete( |
| 64 | + blur({ |
| 65 | + failedFrames: 7, |
| 66 | + fingerprint: fingerprint({ webgl: 1, overflowingMover: 3, canvas2d: 2 }), |
| 67 | + }), |
| 68 | + ); |
| 69 | + expect(props).toMatchObject({ |
| 70 | + motion_blur_tier: "filter", |
| 71 | + motion_blur_movers: 4, |
| 72 | + motion_blur_failed_frames: 7, |
| 73 | + motion_blur_fp_canvas2d: 2, |
| 74 | + motion_blur_fp_webgl: 1, |
| 75 | + motion_blur_fp_overflowing_mover: 3, |
| 76 | + }); |
| 77 | + const keys = Object.keys(props).filter((k) => k.startsWith("motion_blur_")); |
| 78 | + expect(keys.sort()).toEqual([ |
| 79 | + "motion_blur_failed_frames", |
| 80 | + "motion_blur_fp_canvas2d", |
| 81 | + "motion_blur_fp_overflowing_mover", |
| 82 | + "motion_blur_fp_webgl", |
| 83 | + "motion_blur_movers", |
| 84 | + "motion_blur_tier", |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("sends nothing when blur was never on", () => { |
| 89 | + const sent = Object.entries(complete(undefined)).filter( |
| 90 | + ([key, value]) => key.startsWith("motion_blur_") && value !== undefined, |
| 91 | + ); |
| 92 | + expect(sent).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + it("sends failed frames for a blur that ended before it named a tier, and no zero counts", () => { |
| 96 | + const props = renderMotionBlurEventProperties( |
| 97 | + renderMotionBlurTelemetryPayload(blur({ tier: null, failedFrames: 5, fingerprint: null })), |
| 98 | + ); |
| 99 | + expect(props).toEqual({ |
| 100 | + motion_blur_tier: undefined, |
| 101 | + motion_blur_movers: 4, |
| 102 | + motion_blur_failed_frames: 5, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("names every count the runtime can report, in snake case", () => { |
| 107 | + const all = Object.fromEntries( |
| 108 | + Object.keys(fingerprint({})).map((field) => [field, 1]), |
| 109 | + ) as ReturnType<typeof fingerprint>; |
| 110 | + const props = renderMotionBlurEventProperties({ motionBlurFingerprint: all }); |
| 111 | + expect(Object.keys(props).filter((k) => k.startsWith("motion_blur_fp_"))).toEqual([ |
| 112 | + "motion_blur_fp_canvas2d", |
| 113 | + "motion_blur_fp_webgl", |
| 114 | + "motion_blur_fp_video", |
| 115 | + "motion_blur_fp_iframe", |
| 116 | + "motion_blur_fp_custom_element", |
| 117 | + "motion_blur_fp_svg_clip", |
| 118 | + "motion_blur_fp_mask", |
| 119 | + "motion_blur_fp_scroll_container", |
| 120 | + "motion_blur_fp_three_d", |
| 121 | + "motion_blur_fp_overflowing_mover", |
| 122 | + "motion_blur_fp_front_of_mover", |
| 123 | + "motion_blur_fp_blend_mode", |
| 124 | + "motion_blur_fp_fixed_in_mover", |
| 125 | + "motion_blur_fp_pseudo_outside", |
| 126 | + ]); |
| 127 | + }); |
| 128 | +}); |
0 commit comments