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.

7.1 KB
Raw Blame
import { Meta, StoryObj } from '@storybook/react'
import { HttpResponse } from 'msw'

import { App } from 'scenes/App'
import recordingEventsJson from 'scenes/session-recordings/__mocks__/recording_events_query'
import { recordingMetaJson } from 'scenes/session-recordings/__mocks__/recording_meta'
import { snapshotsAsJSONLines } from 'scenes/session-recordings/__mocks__/recording_snapshots'
import { urls } from 'scenes/urls'

import { mswDecorator } from '~/mocks/browser'
import { Mocks } from '~/mocks/utils'

import { recordingPlaylists } from './__mocks__/recording_playlists'
import { recordings } from './__mocks__/recordings'

// API is set up so that everything except the call to load session recording metadata succeeds
const failureMocks: Mocks = {
    get: {
        '/api/environments/:team_id/session_recordings': ({ request }) => {
            const version = new URL(request.url).searchParams.get('version')
            return [
                200,
                {
                    has_next: false,
                    results: recordings,
                    version,
                },
            ]
        },
        '/api/projects/:team_id/session_recording_playlists': recordingPlaylists,
        '/api/projects/:team_id/session_recording_playlists/:playlist_id': ({ params }) => {
            const playlistId = params.playlist_id

            return [
                200,
                {
                    id: 29,
                    short_id: playlistId,
                    name: 'I am a playlist',
                    derived_name: '(Untitled)',
                    description: '',
                    pinned: false,
                    created_at: '2023-07-31T16:24:38.956943Z',
                    created_by: {
                        id: 1,
                        uuid: '01896512-b4e6-0000-3add-7143ff5174c5',
                        distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',
                        first_name: 'paul',
                        email: '[email protected]',
                        is_email_verified: true,
                    },
                    deleted: false,
                    filters: {
                        events: [],
                        actions: [],
                        date_to: null,
                        date_from: '-7d',
                        properties: [],
                        console_logs: [],
                        session_recording_duration: {
                            key: 'duration',
                            type: 'recording',
                            value: 60,
                            operator: 'gt',
                        },
                    },
                    last_modified_at: '2023-07-31T16:34:15.297322Z',
                    last_modified_by: {
                        id: 1,
                        uuid: '01896512-b4e6-0000-3add-7143ff5174c5',
                        distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',
                        first_name: 'paul',
                        email: '[email protected]',
                        is_email_verified: true,
                    },
                },
            ]
        },
        '/api/projects/:team_id/session_recording_playlists/:playlist_id/recordings': ({ params }) => {
            const playlistId = params.playlist_id
            const response = playlistId === '1234567' ? recordings : []
            return [200, { has_next: false, results: response, version: 1 }]
        },
        '/api/environments/:team_id/session_recordings/:id/snapshots': ({ request }) => {
            // with no sources, returns sources...
            if (new URL(request.url).searchParams.get('source') === 'blob_v2') {
                return new HttpResponse(snapshotsAsJSONLines())
            }
            // with no source requested should return sources
            return [
                200,
                {
                    sources: [
                        {
                            source: 'blob_v2',
                            start_timestamp: '2023-08-11T12:03:36.097000Z',
                            end_timestamp: '2023-08-11T12:04:52.268000Z',
                            blob_key: '0',
                        },
                    ],
                },
            ]
        },
        '/api/environments/:team_id/session_recordings/:id': () => {
            return [404, {}]
        },
        'api/projects/:team/notebooks': {
            count: 0,
            next: null,
            previous: null,
            results: [],
        },
    },
    post: {
        '/api/environments/:team_id/query/:kind': async ({ request }) => {
            const body = (await request.json()) as Record<string, any>
            if (body.query.kind === 'HogQLQuery' && body.query.query.includes('raw_session_replay_events')) {
                return [200, { results: [[7]] }]
            }
            return [200, recordingEventsJson]
        },
        '/api/environments/:team_id/session_recordings/:id/capture_diagnostics': { properties: null },
    },
}

const meta: Meta = {
    component: App,
    title: 'Replay/Tabs/Home/Failure',
    parameters: {
        layout: 'fullscreen',
        viewMode: 'story',
        mockDate: '2023-02-01',
        pageUrl: urls.replay(),
    },
}
export default meta

type Story = StoryObj<{}>
export const NotFound: Story = {
    decorators: [mswDecorator(failureMocks)],
    parameters: {
        testOptions: { waitForLoadersToDisappear: false, waitForSelector: '[data-attr="not-found-recording"]' },
    },
}

export const SnapshotsRateLimited: Story = {
    decorators: [
        // The failed loads raise toasts at varying times, which would make the snapshot differ between runs.
        (Story) => (
            <>
                <style>{'.Toastify { display: none; }'}</style>
                <Story />
            </>
        ),
        mswDecorator({
            ...failureMocks,
            get: {
                ...failureMocks.get,
                '/api/environments/:team_id/session_recordings/:id': recordingMetaJson,
                '/api/environments/:team_id/session_recordings/:id/snapshots': ({ request }) => {
                    if (new URL(request.url).searchParams.get('source') === 'blob_v2') {
                        return [429, { detail: 'Request was throttled.' }]
                    }
                    return [
                        200,
                        {
                            sources: [
                                {
                                    source: 'blob_v2',
                                    start_timestamp: '2023-08-11T12:03:36.097000Z',
                                    end_timestamp: '2023-08-11T12:04:52.268000Z',
                                    blob_key: '0',
                                },
                            ],
                        },
                    ]
                },
            },
        }),
    ],
    parameters: {
        // The player keeps its buffering class in the error state, so the loader wait never finishes.
        testOptions: { waitForLoadersToDisappear: false, waitForSelector: '[data-attr="replay-overlay-retry"]' },
    },
}