|
| 1 | +import { ensureReliablePerformanceTimeOrigin } from '../../src/js/utils/performanceclock'; |
| 2 | + |
| 3 | +describe('ensureReliablePerformanceTimeOrigin', () => { |
| 4 | + const originalPerformance = (globalThis as { performance?: unknown }).performance; |
| 5 | + const NOW = 1_700_000_000_000; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + jest.spyOn(Date, 'now').mockReturnValue(NOW); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + if (originalPerformance !== undefined) { |
| 13 | + (globalThis as { performance?: unknown }).performance = originalPerformance; |
| 14 | + } else { |
| 15 | + delete (globalThis as { performance?: unknown }).performance; |
| 16 | + } |
| 17 | + jest.restoreAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + const setPerformance = (value: unknown): void => { |
| 21 | + (globalThis as { performance?: unknown }).performance = value; |
| 22 | + }; |
| 23 | + |
| 24 | + it('neutralizes timeOrigin and returns the drift when timeOrigin + now() drifts far from Date.now()', () => { |
| 25 | + // timeOrigin + now() = 100 + 1000 = 1100, ~1.7e12 ms behind Date.now(): far past the threshold. |
| 26 | + setPerformance({ now: () => 1000, timeOrigin: 100 }); |
| 27 | + |
| 28 | + const drift = ensureReliablePerformanceTimeOrigin(); |
| 29 | + |
| 30 | + expect((globalThis as { performance: { timeOrigin: number } }).performance.timeOrigin).toBe(0); |
| 31 | + expect(drift).toBe(NOW - 1100); |
| 32 | + }); |
| 33 | + |
| 34 | + it('leaves a healthy timeOrigin untouched and returns undefined', () => { |
| 35 | + // timeOrigin + now() === Date.now(): no drift. |
| 36 | + const timeOrigin = NOW - 5000; |
| 37 | + setPerformance({ now: () => 5000, timeOrigin }); |
| 38 | + |
| 39 | + const drift = ensureReliablePerformanceTimeOrigin(); |
| 40 | + |
| 41 | + expect((globalThis as { performance: { timeOrigin: number } }).performance.timeOrigin).toBe(timeOrigin); |
| 42 | + expect(drift).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('leaves timeOrigin untouched when drift is within the threshold', () => { |
| 46 | + // 60s of drift, under the 5-minute threshold. |
| 47 | + const timeOrigin = NOW - 5000 - 60_000; |
| 48 | + setPerformance({ now: () => 5000, timeOrigin }); |
| 49 | + |
| 50 | + const drift = ensureReliablePerformanceTimeOrigin(); |
| 51 | + |
| 52 | + expect((globalThis as { performance: { timeOrigin: number } }).performance.timeOrigin).toBe(timeOrigin); |
| 53 | + expect(drift).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does nothing when performance is missing', () => { |
| 57 | + delete (globalThis as { performance?: unknown }).performance; |
| 58 | + |
| 59 | + expect(ensureReliablePerformanceTimeOrigin()).toBeUndefined(); |
| 60 | + expect((globalThis as { performance?: unknown }).performance).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does nothing when timeOrigin is not a number', () => { |
| 64 | + setPerformance({ now: () => 1000 }); |
| 65 | + |
| 66 | + expect(ensureReliablePerformanceTimeOrigin()).toBeUndefined(); |
| 67 | + expect((globalThis as { performance: { timeOrigin?: number } }).performance.timeOrigin).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('does nothing when now is not a function', () => { |
| 71 | + setPerformance({ timeOrigin: 100 }); |
| 72 | + |
| 73 | + expect(ensureReliablePerformanceTimeOrigin()).toBeUndefined(); |
| 74 | + expect((globalThis as { performance: { timeOrigin: number } }).performance.timeOrigin).toBe(100); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns undefined without throwing when timeOrigin cannot be redefined', () => { |
| 78 | + // A non-configurable `timeOrigin` makes `Object.defineProperty` throw; the guard must swallow it. |
| 79 | + const performance = { now: () => 1000 }; |
| 80 | + Object.defineProperty(performance, 'timeOrigin', { configurable: false, value: 100 }); |
| 81 | + setPerformance(performance); |
| 82 | + |
| 83 | + expect(() => ensureReliablePerformanceTimeOrigin()).not.toThrow(); |
| 84 | + expect(ensureReliablePerformanceTimeOrigin()).toBeUndefined(); |
| 85 | + expect((globalThis as { performance: { timeOrigin: number } }).performance.timeOrigin).toBe(100); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +// Integration coverage against the real `@sentry/core` timestamp function, not a stub. |
| 90 | +// This exercises the property the fix actually relies on: `@sentry/core` builds its |
| 91 | +// timestamp closure lazily on the FIRST `timestampInSeconds()` call and reads the same |
| 92 | +// global `performance` object the guard mutates. `jest.isolateModules` gives each case a |
| 93 | +// fresh module registry so `@sentry/core` re-derives its lazily-cached timestamp function. |
| 94 | +describe('ensureReliablePerformanceTimeOrigin against the real @sentry/core timestampInSeconds', () => { |
| 95 | + const originalPerformance = (globalThis as { performance?: unknown }).performance; |
| 96 | + const NOW = 1_700_000_000_000; |
| 97 | + |
| 98 | + const requireFreshTimestampInSeconds = (): (() => number) => { |
| 99 | + let timestampInSeconds!: () => number; |
| 100 | + jest.isolateModules(() => { |
| 101 | + timestampInSeconds = require('@sentry/core').timestampInSeconds; |
| 102 | + }); |
| 103 | + return timestampInSeconds; |
| 104 | + }; |
| 105 | + |
| 106 | + beforeEach(() => { |
| 107 | + jest.spyOn(Date, 'now').mockReturnValue(NOW); |
| 108 | + // Stale clock: timeOrigin + now() = 1100ms, ~1.7e12ms behind Date.now() — the #6630 shape. |
| 109 | + (globalThis as { performance?: unknown }).performance = { now: () => 1000, timeOrigin: 100 }; |
| 110 | + }); |
| 111 | + |
| 112 | + afterEach(() => { |
| 113 | + if (originalPerformance !== undefined) { |
| 114 | + (globalThis as { performance?: unknown }).performance = originalPerformance; |
| 115 | + } else { |
| 116 | + delete (globalThis as { performance?: unknown }).performance; |
| 117 | + } |
| 118 | + jest.restoreAllMocks(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('reproduces the bug: a stale timeOrigin drifts @sentry/core span/log timestamps', () => { |
| 122 | + const timestampInSeconds = requireFreshTimestampInSeconds(); |
| 123 | + |
| 124 | + // (100 + 1000) / 1000 = 1.1s — the high-resolution path, wildly behind Date.now()/1000. |
| 125 | + expect(timestampInSeconds()).toBeCloseTo(1.1, 5); |
| 126 | + expect(timestampInSeconds()).not.toBeCloseTo(NOW / 1000, 0); |
| 127 | + }); |
| 128 | + |
| 129 | + it('running the guard before the first timestamp makes @sentry/core fall back to Date.now()', () => { |
| 130 | + ensureReliablePerformanceTimeOrigin(); |
| 131 | + |
| 132 | + // Guard ran first, so @sentry/core captures a zeroed (falsy) timeOrigin on first use and |
| 133 | + // gates onto the `dateTimestampInSeconds` (Date.now) path — the same path errors already use. |
| 134 | + const timestampInSeconds = requireFreshTimestampInSeconds(); |
| 135 | + |
| 136 | + expect(timestampInSeconds()).toBeCloseTo(NOW / 1000, 5); |
| 137 | + }); |
| 138 | + |
| 139 | + it('importing @sentry/core before the guard does not pre-cache the origin, so the guard still forces the Date.now() fallback', () => { |
| 140 | + jest.isolateModules(() => { |
| 141 | + // Realistic startup order: @sentry/core is imported at app boot, before init() runs the |
| 142 | + // guard. Importing it must NOT capture the origin — only the first timestampInSeconds() does. |
| 143 | + const { timestampInSeconds } = require('@sentry/core'); |
| 144 | + |
| 145 | + ensureReliablePerformanceTimeOrigin(); |
| 146 | + |
| 147 | + // First call happens after the guard, so the closure captures the zeroed origin. |
| 148 | + expect(timestampInSeconds()).toBeCloseTo(NOW / 1000, 5); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('is order-dependent: running after the first timestamp cannot repair the cached closure', () => { |
| 153 | + // @sentry/core caches the drifted closure on first use, before the guard runs. |
| 154 | + const timestampInSeconds = requireFreshTimestampInSeconds(); |
| 155 | + expect(timestampInSeconds()).toBeCloseTo(1.1, 5); |
| 156 | + |
| 157 | + ensureReliablePerformanceTimeOrigin(); |
| 158 | + |
| 159 | + // The closure captured the original timeOrigin as a local const, so zeroing the property |
| 160 | + // afterwards is a no-op. This is why the guard must run before `initAndBind`. |
| 161 | + expect(timestampInSeconds()).toBeCloseTo(1.1, 5); |
| 162 | + }); |
| 163 | +}); |
0 commit comments