Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 8, 2024
1 parent 262f377 commit e86a694
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 94 deletions.
155 changes: 77 additions & 78 deletions src/__tests__/extensions/replay/sessionrecording.test.ts

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/__tests__/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import * as globals from '../utils/globals'
import { ENABLE_PERSON_PROCESSING, USER_STATE } from '../constants'
import { createPosthogInstance, defaultPostHog } from './helpers/posthog-instance'
import { PostHogConfig, RemoteConfig } from '../types'
import { PostHog } from '../posthog-core'
import { OnlyValidKeys, PostHog } from '../posthog-core'
import { PostHogPersistence } from '../posthog-persistence'
import { SessionIdManager } from '../sessionid'
import { RequestQueue } from '../request-queue'
import { SessionRecording } from '../entrypoints/sessionrecording'
import { PostHogFeatureFlags } from '../posthog-featureflags'
import { SessionRecordingLoader } from '../extensions/replay/session-recording-loader'

describe('posthog core', () => {
const baseUTCDateTime = new Date(Date.UTC(2020, 0, 1, 0, 0, 0))
Expand Down Expand Up @@ -825,7 +825,10 @@ describe('posthog core', () => {
posthog.sessionRecording = {
afterDecideResponse: jest.fn(),
startIfEnabledOrStop: jest.fn(),
} as unknown as SessionRecording
} as OnlyValidKeys<
Partial<SessionRecordingLoader>,
Partial<SessionRecordingLoader>
> as SessionRecordingLoader
posthog.persistence = {
register: jest.fn(),
update_config: jest.fn(),
Expand Down Expand Up @@ -1136,7 +1139,7 @@ describe('posthog core', () => {
instance._send_request = jest.fn()
instance._loaded()

expect(instance._send_request.mock.calls[0][0]).toMatchObject({
expect(jest.mocked(instance._send_request).mock.calls[0][0]).toMatchObject({
url: 'http://localhost/decide/?v=3',
})
expect(instance.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true)
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/sessionid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('Session ID manager', () => {
disabled: false,
}
;(sessionStore.is_supported as jest.Mock).mockReturnValue(true)
// @ts-expect-error - typescript gets confused about type of Date here
jest.spyOn(global, 'Date').mockImplementation(() => new originalDate(now))
;(uuidv7 as jest.Mock).mockReturnValue('newUUID')
;(uuid7ToTimestampMs as jest.Mock).mockReturnValue(timestamp)
Expand Down
12 changes: 9 additions & 3 deletions src/__tests__/site-apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mockLogger } from './helpers/mock-logger'
import { SiteApps } from '../site-apps'
import { PostHogPersistence } from '../posthog-persistence'
import { RequestRouter } from '../utils/request-router'
import { PostHog } from '../posthog-core'
import { OnlyValidKeys, PostHog } from '../posthog-core'
import { DecideResponse, PostHogConfig, Properties, CaptureResult } from '../types'
import { assignableWindow } from '../utils/globals'
import '../entrypoints/external-scripts-loader'
Expand Down Expand Up @@ -131,7 +131,10 @@ describe('SiteApps', () => {
siteAppsInstance.enabled = true
siteAppsInstance.loaded = false

const eventPayload = { event: 'test_event', properties: { prop1: 'value1' } } as CaptureResult
const eventPayload = { event: 'test_event', properties: { prop1: 'value1' } } as OnlyValidKeys<
Partial<CaptureResult>,
Partial<CaptureResult>
> as CaptureResult

jest.spyOn(siteAppsInstance, 'globalsForEvent').mockReturnValue({ some: 'globals' })

Expand All @@ -147,7 +150,10 @@ describe('SiteApps', () => {

siteAppsInstance.missedInvocations = new Array(1000).fill({})

const eventPayload = { event: 'test_event', properties: { prop1: 'value1' } } as CaptureResult
const eventPayload = { event: 'test_event', properties: { prop1: 'value1' } } as OnlyValidKeys<
Partial<CaptureResult>,
Partial<CaptureResult>
> as CaptureResult

jest.spyOn(siteAppsInstance, 'globalsForEvent').mockReturnValue({ some: 'globals' })

Expand Down
8 changes: 1 addition & 7 deletions src/entrypoints/session-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
SessionRecordingStatus,
SessionRecordingUrlTrigger,
SessionStartReason,
SnapshotBuffer,
TriggerType,
} from '../types'
import {
Expand Down Expand Up @@ -84,13 +85,6 @@ const ACTIVE_SOURCES = [

type TriggerStatus = 'trigger_activated' | 'trigger_pending' | 'trigger_disabled'

export interface SnapshotBuffer {
size: number
data: any[]
sessionId: string
windowId: string
}

interface QueuedRRWebEvent {
rrwebMethod: () => void
attempt: number
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/replay/sessionrecording-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { eventWithTime, listenerHandler, pluginEvent } from '@rrweb/types'
import type { record } from '@rrweb/record'

import { isObject } from '../../utils/type-utils'
import { SnapshotBuffer } from '../../entrypoints/sessionrecording'
import { SnapshotBuffer } from '../../types'

// taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references
export function circularReferenceReplacer() {
Expand Down
6 changes: 5 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ Globals should be all caps
* That's a really tricky mistake to spot.
* The OnlyValidKeys type ensures that only keys that are valid in the PostHogConfig type are allowed.
*/
type OnlyValidKeys<T, Shape> = T extends Shape ? (Exclude<keyof T, keyof Shape> extends never ? T : never) : never
export type OnlyValidKeys<T, Shape> = T extends Shape
? Exclude<keyof T, keyof Shape> extends never
? T
: never
: never

const instances: Record<string, PostHog> = {}

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ export type SessionRecordingStatus = 'disabled' | 'sampled' | 'active' | 'buffer

export type TriggerType = 'url' | 'event'

export interface SnapshotBuffer {
size: number
data: any[]
sessionId: string
windowId: string
}

export enum Compression {
GZipJS = 'gzip-js',
Base64 = 'base64',
Expand Down

0 comments on commit e86a694

Please sign in to comment.