|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + recordControlUiPerformanceEvent, |
| 4 | + startControlUiResponsivenessObserver, |
| 5 | +} from "./control-ui-performance.ts"; |
| 6 | + |
| 7 | +const originalPerformanceObserver = globalThis.PerformanceObserver; |
| 8 | + |
| 9 | +type ObserverCallback = ConstructorParameters<typeof PerformanceObserver>[0]; |
| 10 | + |
| 11 | +function installPerformanceObserverMock(options: { |
| 12 | + supportedEntryTypes: string[]; |
| 13 | + observe?: (options: PerformanceObserverInit) => void; |
| 14 | +}) { |
| 15 | + let callback: ObserverCallback | null = null; |
| 16 | + const disconnect = vi.fn(); |
| 17 | + class MockPerformanceObserver { |
| 18 | + static supportedEntryTypes = options.supportedEntryTypes; |
| 19 | + constructor(nextCallback: ObserverCallback) { |
| 20 | + callback = nextCallback; |
| 21 | + } |
| 22 | + observe(observeOptions: PerformanceObserverInit) { |
| 23 | + options.observe?.(observeOptions); |
| 24 | + } |
| 25 | + disconnect() { |
| 26 | + disconnect(); |
| 27 | + } |
| 28 | + } |
| 29 | + Object.defineProperty(globalThis, "PerformanceObserver", { |
| 30 | + configurable: true, |
| 31 | + value: MockPerformanceObserver, |
| 32 | + }); |
| 33 | + return { |
| 34 | + disconnect, |
| 35 | + emit(entries: PerformanceEntry[]) { |
| 36 | + callback?.( |
| 37 | + { |
| 38 | + getEntries: () => entries, |
| 39 | + } as PerformanceObserverEntryList, |
| 40 | + {} as PerformanceObserver, |
| 41 | + ); |
| 42 | + }, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function createHost() { |
| 47 | + return { |
| 48 | + tab: "chat" as const, |
| 49 | + eventLog: [] as Array<{ payload: Record<string, unknown> }>, |
| 50 | + eventLogBuffer: [] as Array<{ payload: Record<string, unknown> }>, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + Object.defineProperty(globalThis, "PerformanceObserver", { |
| 56 | + configurable: true, |
| 57 | + value: originalPerformanceObserver, |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("recordControlUiPerformanceEvent", () => { |
| 62 | + it("keeps the performance event buffer bounded", () => { |
| 63 | + const host = createHost(); |
| 64 | + |
| 65 | + for (let i = 0; i < 260; i += 1) { |
| 66 | + recordControlUiPerformanceEvent(host, "control-ui.test", { i }, { console: false }); |
| 67 | + } |
| 68 | + |
| 69 | + expect(host.eventLogBuffer).toHaveLength(250); |
| 70 | + expect(host.eventLogBuffer[0]?.payload).toEqual({ i: 259 }); |
| 71 | + expect(host.eventLogBuffer.at(-1)?.payload).toEqual({ i: 10 }); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("startControlUiResponsivenessObserver", () => { |
| 76 | + it("records long animation frames with script attribution", () => { |
| 77 | + const observe = vi.fn(); |
| 78 | + const mock = installPerformanceObserverMock({ |
| 79 | + supportedEntryTypes: ["longtask", "long-animation-frame"], |
| 80 | + observe, |
| 81 | + }); |
| 82 | + const host = createHost(); |
| 83 | + |
| 84 | + const observer = startControlUiResponsivenessObserver(host); |
| 85 | + mock.emit([ |
| 86 | + { |
| 87 | + name: "long-frame", |
| 88 | + startTime: 12.4, |
| 89 | + duration: 83.6, |
| 90 | + blockingDuration: 42.2, |
| 91 | + scripts: [ |
| 92 | + { |
| 93 | + duration: 12.1, |
| 94 | + sourceURL: "http://localhost/assets/a.js?token=redacted", |
| 95 | + }, |
| 96 | + { |
| 97 | + duration: 50.8, |
| 98 | + invoker: "event-listener", |
| 99 | + sourceURL: "http://localhost/assets/app.js?token=redacted#hash", |
| 100 | + sourceFunctionName: "renderApp", |
| 101 | + }, |
| 102 | + ], |
| 103 | + } as unknown as PerformanceEntry, |
| 104 | + ]); |
| 105 | + observer?.disconnect(); |
| 106 | + |
| 107 | + expect(observe).toHaveBeenCalledWith({ type: "long-animation-frame", buffered: true }); |
| 108 | + expect(mock.disconnect).toHaveBeenCalledOnce(); |
| 109 | + expect(host.eventLogBuffer).toEqual([ |
| 110 | + expect.objectContaining({ |
| 111 | + event: "control-ui.long-animation-frame", |
| 112 | + payload: expect.objectContaining({ |
| 113 | + tab: "chat", |
| 114 | + name: "long-frame", |
| 115 | + startTimeMs: 12, |
| 116 | + durationMs: 84, |
| 117 | + blockingDurationMs: 42, |
| 118 | + scriptCount: 2, |
| 119 | + topScript: { |
| 120 | + durationMs: 51, |
| 121 | + invoker: "event-listener", |
| 122 | + sourceUrl: "/assets/app.js", |
| 123 | + sourceFunctionName: "renderApp", |
| 124 | + }, |
| 125 | + }), |
| 126 | + }), |
| 127 | + ]); |
| 128 | + }); |
| 129 | + |
| 130 | + it("falls back to long task entries when long animation frames are unavailable", () => { |
| 131 | + const observe = vi.fn(); |
| 132 | + const mock = installPerformanceObserverMock({ |
| 133 | + supportedEntryTypes: ["longtask"], |
| 134 | + observe, |
| 135 | + }); |
| 136 | + const host = createHost(); |
| 137 | + |
| 138 | + startControlUiResponsivenessObserver(host); |
| 139 | + mock.emit([ |
| 140 | + { |
| 141 | + name: "self", |
| 142 | + startTime: 5, |
| 143 | + duration: 51, |
| 144 | + } as unknown as PerformanceEntry, |
| 145 | + { |
| 146 | + name: "small", |
| 147 | + startTime: 10, |
| 148 | + duration: 49, |
| 149 | + } as unknown as PerformanceEntry, |
| 150 | + ]); |
| 151 | + |
| 152 | + expect(observe).toHaveBeenCalledWith({ type: "longtask", buffered: true }); |
| 153 | + expect(host.eventLogBuffer).toEqual([ |
| 154 | + expect.objectContaining({ |
| 155 | + event: "control-ui.longtask", |
| 156 | + payload: expect.objectContaining({ |
| 157 | + name: "self", |
| 158 | + durationMs: 51, |
| 159 | + }), |
| 160 | + }), |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("returns null when responsiveness entries are unsupported or observe fails", () => { |
| 165 | + installPerformanceObserverMock({ supportedEntryTypes: [] }); |
| 166 | + expect(startControlUiResponsivenessObserver(createHost())).toBeNull(); |
| 167 | + |
| 168 | + installPerformanceObserverMock({ |
| 169 | + supportedEntryTypes: ["longtask"], |
| 170 | + observe: () => { |
| 171 | + throw new Error("unsupported"); |
| 172 | + }, |
| 173 | + }); |
| 174 | + expect(startControlUiResponsivenessObserver(createHost())).toBeNull(); |
| 175 | + }); |
| 176 | +}); |
0 commit comments