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.

5.5 KB
Raw Blame
import { InAppNotification } from '~/types'

import { buildNotificationSourcePath, withRecapSourceUrl } from './sidePanelNotificationsLogic'

function makeNotification(overrides: Partial<InAppNotification> = {}): InAppNotification {
    return {
        id: 'test-id',
        team_id: 1,
        notification_type: 'comment_mention',
        priority: 'normal',
        title: 'Test',
        body: '',
        read: false,
        read_at: null,
        resource_type: null,
        resource_id: '',
        target_type: 'user',
        target_id: '1',
        source_url: '',
        source_type: null,
        source_id: null,
        metadata: null,
        created_at: '2026-04-01T00:00:00Z',
        ...overrides,
    }
}

describe('buildNotificationSourcePath', () => {
    it('builds path from source_type and source_id for dashboard', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'dashboard',
                source_id: '42',
            })
        )
        expect(result).toBe('/dashboard/42')
    })

    it('builds path from source_type and source_id for replay', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'replay',
                source_id: 'abc-123',
            })
        )
        expect(result).toBe('/replay/abc-123')
    })

    it('builds path from source_type and source_id for feature_flag', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'feature_flag',
                source_id: '99',
            })
        )
        expect(result).toBe('/feature-flags/99')
    })

    it('builds path from source_type and source_id for insight', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'insight',
                source_id: 'abc123',
            })
        )
        expect(result).toBe('/insights/abc123')
    })

    it('builds path from source_type and source_id for error_tracking', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'error_tracking',
                source_id: 'issue-uuid',
            })
        )
        expect(result).toBe('/error-tracking/issue-uuid')
    })

    it('builds path from source_type and source_id for ticket', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'ticket',
                source_id: 'ticket-uuid',
            })
        )
        expect(result).toBe('/support/tickets/ticket-uuid')
    })

    it('falls back to source_url when source_type is null', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: null,
                source_id: null,
                source_url: '/dashboard/legacy',
            })
        )
        expect(result).toBe('/dashboard/legacy')
    })

    it('uses the source_url deep-link for customer_analytics (no source_id→path mapping)', () => {
        const deepLink = '/customer_analytics/accounts/acc-9/usage'
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'customer_analytics',
                source_id: 'spike-1',
                source_url: deepLink,
            })
        )
        expect(result).toBe(deepLink)
    })

    it('falls back to source_url when source_type is unrecognized', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'unknown_type',
                source_id: '123',
                source_url: '/fallback',
            })
        )
        expect(result).toBe('/fallback')
    })

    it('returns null when no source_type and no source_url', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: null,
                source_id: null,
                source_url: '',
            })
        )
        expect(result).toBeNull()
    })

    it('returns null when source_type exists but source_id is null', () => {
        const result = buildNotificationSourcePath(
            makeNotification({
                source_type: 'dashboard',
                source_id: null,
                source_url: '',
            })
        )
        expect(result).toBeNull()
    })
})

describe('withRecapSourceUrl', () => {
    it('rewrites the /web segment of a digest source_url to /web/recap', () => {
        const result = withRecapSourceUrl(
            makeNotification({ source_url: '/project/2/web?utm_source=web_analytics_digest&utm_medium=in_app' })
        )
        expect(result.source_url).toBe('/project/2/web/recap?utm_source=web_analytics_digest&utm_medium=in_app')
    })

    it('rewrites a bare /web path with no query string', () => {
        const result = withRecapSourceUrl(makeNotification({ source_url: '/project/2/web' }))
        expect(result.source_url).toBe('/project/2/web/recap')
    })

    it('leaves an empty source_url untouched', () => {
        const notification = makeNotification({ source_url: '' })
        expect(withRecapSourceUrl(notification)).toBe(notification)
    })

    it('does not rewrite unrelated paths that merely contain web', () => {
        const result = withRecapSourceUrl(makeNotification({ source_url: '/project/2/web-vitals' }))
        expect(result.source_url).toBe('/project/2/web-vitals')
    })
})