|
| 1 | +import { ExperimentalFeature, addExperimentalFeatures, isExperimentalFeatureEnabled } from '@datadog/browser-core' |
| 2 | +import { resetExperimentalFeatures } from '@datadog/browser-core/src/tools/experimentalFeatures' |
| 3 | +import { registerCleanupTask } from '@datadog/browser-core/test' |
| 4 | +import type { AssembledRumEvent } from '../rawRumEvent.types' |
| 5 | +import { RumEventType } from '../rawRumEvent.types' |
| 6 | +import { computeAssembledViewDiff, PARTIAL_VIEW_UPDATE_CHECKPOINT_INTERVAL } from './startRumBatch' |
| 7 | + |
| 8 | +function makeAssembledView(overrides: Record<string, unknown> = {}): AssembledRumEvent { |
| 9 | + return { |
| 10 | + type: RumEventType.VIEW, |
| 11 | + date: 1000, |
| 12 | + application: { id: 'app-1' }, |
| 13 | + session: { id: 'sess-1', type: 'user' }, |
| 14 | + view: { |
| 15 | + id: 'view-1', |
| 16 | + name: 'Home', |
| 17 | + url: '/home', |
| 18 | + referrer: '', |
| 19 | + is_active: true, |
| 20 | + action: { count: 0 }, |
| 21 | + error: { count: 0 }, |
| 22 | + long_task: { count: 0 }, |
| 23 | + resource: { count: 0 }, |
| 24 | + time_spent: 0, |
| 25 | + }, |
| 26 | + _dd: { |
| 27 | + document_version: 1, |
| 28 | + format_version: 2, |
| 29 | + sdk_name: 'rum', |
| 30 | + configuration: { start_session_replay_recording_manually: false }, |
| 31 | + }, |
| 32 | + service: 'my-service', |
| 33 | + version: '1.0.0', |
| 34 | + ddtags: 'env:prod', |
| 35 | + source: 'browser', |
| 36 | + context: {}, |
| 37 | + ...overrides, |
| 38 | + } as unknown as AssembledRumEvent |
| 39 | +} |
| 40 | + |
| 41 | +describe('computeAssembledViewDiff', () => { |
| 42 | + it('should return undefined when nothing has changed', () => { |
| 43 | + const last = makeAssembledView() |
| 44 | + const current = makeAssembledView({ |
| 45 | + _dd: { |
| 46 | + document_version: 2, |
| 47 | + format_version: 2, |
| 48 | + sdk_name: 'rum', |
| 49 | + configuration: { start_session_replay_recording_manually: false }, |
| 50 | + }, |
| 51 | + }) |
| 52 | + const result = computeAssembledViewDiff(current, last) |
| 53 | + |
| 54 | + // Only document_version changed (always required, not a "meaningful change") |
| 55 | + // view.* unchanged → should return undefined |
| 56 | + expect(result).toBeUndefined() |
| 57 | + }) |
| 58 | + |
| 59 | + it('should always include required routing fields', () => { |
| 60 | + const last = makeAssembledView() |
| 61 | + const current = makeAssembledView({ |
| 62 | + _dd: { |
| 63 | + document_version: 2, |
| 64 | + format_version: 2, |
| 65 | + sdk_name: 'rum', |
| 66 | + configuration: { start_session_replay_recording_manually: false }, |
| 67 | + }, |
| 68 | + view: { |
| 69 | + id: 'view-1', |
| 70 | + name: 'Home', |
| 71 | + url: '/home', |
| 72 | + referrer: '', |
| 73 | + is_active: true, |
| 74 | + action: { count: 1 }, |
| 75 | + error: { count: 0 }, |
| 76 | + long_task: { count: 0 }, |
| 77 | + resource: { count: 0 }, |
| 78 | + time_spent: 100, |
| 79 | + }, |
| 80 | + }) |
| 81 | + const result = computeAssembledViewDiff(current, last)! |
| 82 | + |
| 83 | + expect(result.type).toBe(RumEventType.VIEW_UPDATE) |
| 84 | + expect((result as any).application).toEqual({ id: 'app-1' }) |
| 85 | + expect((result as any).session).toEqual({ id: 'sess-1', type: 'user' }) |
| 86 | + expect((result.view as any).id).toBe('view-1') |
| 87 | + expect((result.view as any).url).toBe('/home') |
| 88 | + expect((result._dd as any).document_version).toBe(2) |
| 89 | + expect((result._dd as any).format_version).toBe(2) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should include only changed view.* fields', () => { |
| 93 | + const last = makeAssembledView() |
| 94 | + const current = makeAssembledView({ |
| 95 | + _dd: { |
| 96 | + document_version: 2, |
| 97 | + format_version: 2, |
| 98 | + sdk_name: 'rum', |
| 99 | + configuration: { start_session_replay_recording_manually: false }, |
| 100 | + }, |
| 101 | + view: { |
| 102 | + id: 'view-1', |
| 103 | + name: 'Home', |
| 104 | + url: '/home', |
| 105 | + referrer: '', |
| 106 | + is_active: true, |
| 107 | + action: { count: 3 }, |
| 108 | + error: { count: 0 }, |
| 109 | + long_task: { count: 0 }, |
| 110 | + resource: { count: 0 }, |
| 111 | + time_spent: 5000, |
| 112 | + }, |
| 113 | + }) |
| 114 | + const result = computeAssembledViewDiff(current, last)! |
| 115 | + |
| 116 | + expect((result.view as any).action).toEqual({ count: 3 }) // changed |
| 117 | + expect((result.view as any).time_spent).toBe(5000) // changed |
| 118 | + expect((result.view as any).error).toBeUndefined() // unchanged, stripped |
| 119 | + expect((result.view as any).name).toBeUndefined() // unchanged, stripped |
| 120 | + expect((result.view as any).url).toBe('/home') // required routing field, always present |
| 121 | + }) |
| 122 | + |
| 123 | + it('should strip unchanged top-level assembled fields', () => { |
| 124 | + const last = makeAssembledView({ service: 'svc', version: '1.0.0' }) |
| 125 | + const current = makeAssembledView({ |
| 126 | + _dd: { |
| 127 | + document_version: 2, |
| 128 | + format_version: 2, |
| 129 | + sdk_name: 'rum', |
| 130 | + configuration: { start_session_replay_recording_manually: false }, |
| 131 | + }, |
| 132 | + view: { |
| 133 | + id: 'view-1', |
| 134 | + name: 'Home', |
| 135 | + url: '/home', |
| 136 | + referrer: '', |
| 137 | + is_active: true, |
| 138 | + action: { count: 1 }, |
| 139 | + error: { count: 0 }, |
| 140 | + long_task: { count: 0 }, |
| 141 | + resource: { count: 0 }, |
| 142 | + time_spent: 100, |
| 143 | + }, |
| 144 | + service: 'svc', |
| 145 | + version: '1.0.0', |
| 146 | + }) |
| 147 | + const result = computeAssembledViewDiff(current, last)! |
| 148 | + |
| 149 | + expect(result.service).toBeUndefined() // unchanged, stripped |
| 150 | + expect((result as any).version).toBeUndefined() // unchanged, stripped |
| 151 | + }) |
| 152 | + |
| 153 | + it('should keep top-level assembled fields that changed', () => { |
| 154 | + const last = makeAssembledView({ service: 'old-service' }) |
| 155 | + const current = makeAssembledView({ |
| 156 | + _dd: { |
| 157 | + document_version: 2, |
| 158 | + format_version: 2, |
| 159 | + sdk_name: 'rum', |
| 160 | + configuration: { start_session_replay_recording_manually: false }, |
| 161 | + }, |
| 162 | + view: { |
| 163 | + id: 'view-1', |
| 164 | + name: 'Home', |
| 165 | + url: '/home', |
| 166 | + referrer: '', |
| 167 | + is_active: true, |
| 168 | + action: { count: 1 }, |
| 169 | + error: { count: 0 }, |
| 170 | + long_task: { count: 0 }, |
| 171 | + resource: { count: 0 }, |
| 172 | + time_spent: 100, |
| 173 | + }, |
| 174 | + service: 'new-service', |
| 175 | + }) |
| 176 | + const result = computeAssembledViewDiff(current, last)! |
| 177 | + |
| 178 | + expect(result.service).toBe('new-service') |
| 179 | + }) |
| 180 | + |
| 181 | + it('should not mutate the input events', () => { |
| 182 | + const last = makeAssembledView() |
| 183 | + const current = makeAssembledView({ |
| 184 | + _dd: { |
| 185 | + document_version: 2, |
| 186 | + format_version: 2, |
| 187 | + sdk_name: 'rum', |
| 188 | + configuration: { start_session_replay_recording_manually: false }, |
| 189 | + }, |
| 190 | + view: { |
| 191 | + id: 'view-1', |
| 192 | + name: 'Home', |
| 193 | + url: '/home', |
| 194 | + referrer: '', |
| 195 | + is_active: true, |
| 196 | + action: { count: 1 }, |
| 197 | + error: { count: 0 }, |
| 198 | + long_task: { count: 0 }, |
| 199 | + resource: { count: 0 }, |
| 200 | + time_spent: 100, |
| 201 | + }, |
| 202 | + }) |
| 203 | + const currentService = current.service |
| 204 | + computeAssembledViewDiff(current, last) |
| 205 | + |
| 206 | + expect(current.service).toBe(currentService) |
| 207 | + }) |
| 208 | +}) |
| 209 | + |
| 210 | +describe('startRumBatch partial_view_updates routing', () => { |
| 211 | + beforeEach(() => { |
| 212 | + addExperimentalFeatures([ExperimentalFeature.PARTIAL_VIEW_UPDATES]) |
| 213 | + registerCleanupTask(resetExperimentalFeatures) |
| 214 | + }) |
| 215 | + |
| 216 | + it('PARTIAL_VIEW_UPDATE_CHECKPOINT_INTERVAL should be 100', () => { |
| 217 | + expect(PARTIAL_VIEW_UPDATE_CHECKPOINT_INTERVAL).toBe(100) |
| 218 | + }) |
| 219 | + |
| 220 | + it('PARTIAL_VIEW_UPDATES_NO_CHECKPOINT flag should be defined', () => { |
| 221 | + expect(ExperimentalFeature.PARTIAL_VIEW_UPDATES_NO_CHECKPOINT).toBe('partial_view_updates_no_checkpoint') |
| 222 | + }) |
| 223 | + |
| 224 | + it('PARTIAL_VIEW_UPDATES_NO_CHECKPOINT flag should be disabled by default', () => { |
| 225 | + expect(isExperimentalFeatureEnabled(ExperimentalFeature.PARTIAL_VIEW_UPDATES_NO_CHECKPOINT)).toBe(false) |
| 226 | + }) |
| 227 | +}) |
0 commit comments