Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

4.6 KB
Raw Blame
import { MOCK_DEFAULT_BASIC_USER, MOCK_SECOND_BASIC_USER } from 'lib/api.mock'

import { expectLogic } from 'kea-test-utils'

import api from 'lib/api'
import { dayjs } from 'lib/dayjs'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { sessionRecordingDataCoordinatorLogic } from 'scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic'
import { sessionRecordingPlayerLogic } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'

import { membersLogic } from '~/models/membersLogic'
import { CommentType } from '~/types'

import { recordingMetaJson } from '../../__mocks__/recording_meta'
import { setupSessionRecordingTest } from '../__mocks__/test-setup'
import { playerCommentModel } from './playerCommentModel'
import { playerCommentOverlayLogic } from './playerFrameCommentOverlayLogic'

jest.mock('../snapshot-processing/DecompressionWorkerManager')

const playerLogicProps = { sessionRecordingId: '1', playerKey: 'playlist', recordingId: '1' }

const mockMembers = [
    {
        id: '1',
        user: MOCK_DEFAULT_BASIC_USER,
        level: 8,
        joined_at: '2020-09-24T15:05:26.758796Z',
        updated_at: '2020-09-24T15:05:26.758837Z',
        is_2fa_enabled: false,
        has_social_auth: false,
        last_login: '2020-09-24T15:05:26.758796Z',
    },
    {
        id: '2',
        user: MOCK_SECOND_BASIC_USER,
        level: 1,
        joined_at: '2021-03-11T19:11:11Z',
        updated_at: '2021-03-11T19:11:11Z',
        is_2fa_enabled: false,
        has_social_auth: false,
        last_login: '2021-03-11T19:11:11Z',
    },
]

describe('playerFrameCommentOverlayLogic', () => {
    let logic: ReturnType<typeof playerCommentOverlayLogic.build>

    beforeEach(() => {
        setupSessionRecordingTest({
            getMocks: {
                '/api/organizations/:organization_id/members/': { results: mockMembers },
            },
        })
        featureFlagLogic.mount()

        sessionRecordingPlayerLogic(playerLogicProps).mount()

        logic = playerCommentOverlayLogic(playerLogicProps)
        logic.mount()
    })

    // Regression test: opening the in-player comment box must populate the org
    // members list so the @-mention autocomplete can resolve teammates. Without
    // this, deep-linking straight to the player and commenting showed an empty
    // "No member matching @" list because nothing else had loaded members yet.
    it('loads org members when the comment overlay opens', async () => {
        await expectLogic(membersLogic, () => {
            logic.actions.setIsCommenting(true)
        }).toDispatchActions(['ensureAllMembersLoaded', 'loadAllMembers', 'loadAllMembersSuccess'])

        expect(membersLogic.values.meFirstMembers).toHaveLength(mockMembers.length)
    })

    // Regression test: currentTimestamp is unset until the player seeks, so a comment made before
    // the playhead moves off 00:00 used to submit an invalid date and fail silently inside the
    // kea-forms submit, saving nothing and reporting nothing.
    it('saves a comment made while the playhead is still at the start', async () => {
        const createSpy = jest.spyOn(api.comments, 'create').mockResolvedValue({} as CommentType)

        await expectLogic(sessionRecordingPlayerLogic(playerLogicProps)).toDispatchActions([
            sessionRecordingDataCoordinatorLogic({ sessionRecordingId: '1' }).actionTypes.loadRecordingMetaSuccess,
        ])
        expect(logic.values.currentPlayerTime).toBe(0)

        logic.actions.setIsCommenting(true)
        logic.actions.setRichContent({
            type: 'doc',
            content: [{ type: 'paragraph', content: [{ type: 'text', text: 'a comment' }] }],
        })

        await expectLogic(logic, () => {
            logic.actions.submitRecordingComment()
        }).toDispatchActions([playerCommentModel.actionTypes.commentEdited, 'submitRecordingCommentSuccess'])

        expect(createSpy).toHaveBeenCalledTimes(1)
        expect(createSpy.mock.calls[0][0].content).toEqual('a comment')
        expect(createSpy.mock.calls[0][0].item_context).toMatchObject({
            milliseconds_into_recording: 0,
            time_in_recording: dayjs(recordingMetaJson.start_time).toISOString(),
        })
    })

    it('does not load all members again when the overlay closes', async () => {
        // Prime the loaded state first so the close path is exercised in isolation.
        await expectLogic(membersLogic, () => {
            logic.actions.setIsCommenting(true)
        }).toDispatchActions(['loadAllMembersSuccess'])

        await expectLogic(membersLogic, () => {
            logic.actions.setIsCommenting(false)
        }).toNotHaveDispatchedActions(['loadAllMembers'])
    })
})