Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️[RUMF-1517] split rum test utils #2117

Merged
merged 6 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/rum/src/domain/record/observers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { initObservers } from './observers'
export { InputCallback, initInputObserver } from './inputObserver'
export { initMutationObserver, MutationCallBack, RumMutationRecord } from './mutationObserver'
export { DEFAULT_CONFIGURATION } from './observers.specHelper'
export { DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createNewEvent } from '@datadog/browser-core/test'
import { PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_MASK_USER_INPUT } from '../../../constants'
import { serializeDocument, SerializationContextStatus } from '../serialization'
import { createElementsScrollPositions } from '../elementsScrollPositions'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from '../../../../test'
import type { InputCallback } from './inputObserver'
import { initInputObserver } from './inputObserver'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'

describe('initInputObserver', () => {
let stopInputObserver: () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { createNewEvent } from '@datadog/browser-core/test'
import { IncrementalSource, MouseInteractionType, RecordType } from '../../../types'
import { serializeDocument, SerializationContextStatus } from '../serialization'
import { createElementsScrollPositions } from '../elementsScrollPositions'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from '../../../../test'
import type { MouseInteractionCallBack } from './mouseInteractionObserver'
import { initMouseInteractionObserver } from './mouseInteractionObserver'
import type { RecordIds } from './recordIds'
import { initRecordIds } from './recordIds'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'

describe('initMouseInteractionObserver', () => {
let mouseInteractionCallbackSpy: jasmine.Spy<MouseInteractionCallBack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createNewEvent } from '@datadog/browser-core/test'
import { SerializationContextStatus, serializeDocument } from '../serialization'
import { createElementsScrollPositions } from '../elementsScrollPositions'
import { IncrementalSource } from '../../../types'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from '../../../../test'
import type { MousemoveCallBack } from './moveObserver'
import { initMoveObserver } from './moveObserver'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'

describe('initMoveObserver', () => {
let mouseMoveCallbackSpy: jasmine.Spy<MousemoveCallBack>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DefaultPrivacyLevel, isIE } from '@datadog/browser-core'
import type { RumConfiguration } from '@datadog/browser-rum-core'
import { collectAsyncCalls } from '@datadog/browser-core/test'
import { createMutationPayloadValidator, DEFAULT_SHADOW_ROOT_CONTROLLER } from '../../../../test'
import { createMutationPayloadValidator } from '../../../../test'
import {
NodePrivacyLevel,
PRIVACY_ATTR_NAME,
Expand All @@ -16,6 +16,7 @@ import { createElementsScrollPositions } from '../elementsScrollPositions'
import type { ShadowRootCallBack } from '../shadowRootsController'
import { sortAddedAndMovedNodes, initMutationObserver } from './mutationObserver'
import type { MutationCallBack } from './mutationObserver'
import { DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'

describe('startMutationCollection', () => {
let sandbox: HTMLElement
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { noop } from '@datadog/browser-core'
import type { RumConfiguration } from '@datadog/browser-rum-core'
import type { ShadowRootsController } from '../shadowRootsController'
import { NodePrivacyLevel } from '../../../constants'

export const DEFAULT_SHADOW_ROOT_CONTROLLER: ShadowRootsController = {
flush: noop,
stop: noop,
addShadowRoot: noop,
removeShadowRoot: noop,
}
export const DEFAULT_CONFIGURATION = { defaultPrivacyLevel: NodePrivacyLevel.ALLOW } as RumConfiguration
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isIE } from '@datadog/browser-core'
import { isFirefox } from '@datadog/browser-core/test'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from '../../../../test'
import { serializeDocument, SerializationContextStatus } from '../serialization'
import { createElementsScrollPositions } from '../elementsScrollPositions'
import type { StyleSheetCallback } from './styleSheetObserver'
import { initStyleSheetObserver, getPathToNestedCSSRule } from './styleSheetObserver'
import { DEFAULT_CONFIGURATION, DEFAULT_SHADOW_ROOT_CONTROLLER } from './observers.specHelper'

describe('initStyleSheetObserver', () => {
let stopStyleSheetObserver: () => void
Expand Down
8 changes: 6 additions & 2 deletions packages/rum/src/domain/segmentCollection/segment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TimeStamp } from '@datadog/browser-core'
import { noop, setDebugMode, display, isIE } from '@datadog/browser-core'
import { MockWorker, parseSegment } from '../../../test'
import type { CreationReason, BrowserRecord, SegmentContext } from '../../types'
import { MockWorker } from '../../../test'
import type { CreationReason, BrowserRecord, SegmentContext, BrowserSegment } from '../../types'
import { RecordType } from '../../types'
import { getReplayStats, resetReplayStats } from '../replayStats'
import { Segment } from './segment'
Expand Down Expand Up @@ -240,3 +240,7 @@ describe('Segment', () => {
return new Segment(worker, context, creationReason, initialRecord, onWrote, onFlushed)
}
})

function parseSegment(bytes: Uint8Array) {
return JSON.parse(new TextDecoder().decode(bytes)) as BrowserSegment
}
6 changes: 5 additions & 1 deletion packages/rum/test/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './utils'
export * from './mockWorker'
export * from './recordsPerFullSnapshot'
export * from './mutationPayloadValidator'
export * from './nodes'
export * from './segments'
136 changes: 136 additions & 0 deletions packages/rum/test/mockWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import type { DeflateWorker, DeflateWorkerAction, DeflateWorkerListener } from '../src/domain/segmentCollection'

export class MockWorker implements DeflateWorker {
readonly pendingMessages: DeflateWorkerAction[] = []
private rawBytesCount = 0
private deflatedData: Uint8Array[] = []
private listeners: {
message: DeflateWorkerListener[]
error: Array<(error: unknown) => void>
} = { message: [], error: [] }

addEventListener(eventName: 'message', listener: DeflateWorkerListener): void
addEventListener(eventName: 'error', listener: (error: ErrorEvent) => void): void
addEventListener(eventName: 'message' | 'error', listener: any): void {
const index = this.listeners[eventName].indexOf(listener)
if (index < 0) {
this.listeners[eventName].push(listener)
}
}

removeEventListener(eventName: 'message', listener: DeflateWorkerListener): void
removeEventListener(eventName: 'error', listener: (error: ErrorEvent) => void): void
removeEventListener(eventName: 'message' | 'error', listener: any): void {
const index = this.listeners[eventName].indexOf(listener)
if (index >= 0) {
this.listeners[eventName].splice(index, 1)
}
}

postMessage(message: DeflateWorkerAction): void {
this.pendingMessages.push(message)
}

terminate(): void {
// do nothing
}

get pendingData() {
return this.pendingMessages.map((message) => ('data' in message ? message.data : '')).join('')
}

get messageListenersCount() {
return this.listeners.message.length
}

processAllMessages(): void {
while (this.pendingMessages.length) {
this.processNextMessage()
}
}

dropNextMessage(): void {
this.pendingMessages.shift()
}

processNextMessage(): void {
const message = this.pendingMessages.shift()
if (message) {
switch (message.action) {
case 'init':
this.listeners.message.forEach((listener) =>
listener({
data: {
type: 'initialized',
},
})
)
break
case 'write':
{
const additionalBytesCount = this.pushData(message.data)
this.listeners.message.forEach((listener) =>
listener({
data: {
type: 'wrote',
id: message.id,
compressedBytesCount: uint8ArraysSize(this.deflatedData),
additionalBytesCount,
},
})
)
}
break
case 'flush':
{
const additionalBytesCount = this.pushData(message.data)
this.listeners.message.forEach((listener) =>
listener({
data: {
type: 'flushed',
id: message.id,
result: mergeUint8Arrays(this.deflatedData),
rawBytesCount: this.rawBytesCount,
additionalBytesCount,
},
})
)
this.deflatedData.length = 0
this.rawBytesCount = 0
}
break
}
}
}

dispatchErrorEvent() {
const error = new ErrorEvent('worker')
this.listeners.error.forEach((listener) => listener(error))
}

dispatchErrorMessage(error: Error | string) {
this.listeners.message.forEach((listener) => listener({ data: { type: 'errored', error } }))
}

private pushData(data?: string) {
const encodedData = new TextEncoder().encode(data)
this.rawBytesCount += encodedData.length
// In the mock worker, for simplicity, we'll just use the UTF-8 encoded string instead of deflating it.
this.deflatedData.push(encodedData)
return encodedData.length
}
}

function uint8ArraysSize(arrays: Uint8Array[]) {
return arrays.reduce((sum, bytes) => sum + bytes.length, 0)
}

function mergeUint8Arrays(arrays: Uint8Array[]) {
const result = new Uint8Array(uint8ArraysSize(arrays))
let offset = 0
for (const bytes of arrays) {
result.set(bytes, offset)
offset += bytes.byteLength
}
return result
}
Loading