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.

3.4 KB
Raw Blame
import { MOCK_DEFAULT_ORGANIZATION } from 'lib/api.mock'

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { BindLogic } from 'kea'

import { OrganizationMembershipLevel } from 'lib/constants'
import { maxLogic } from 'scenes/max/maxLogic'
import { organizationLogic } from 'scenes/organizationLogic'

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

import { HOMEPAGE_TAB_ID } from './constants'
import { HomepageAiInput } from './HomepageInput'

jest.mock('scenes/max/components/SidebarQuestionInput', () => ({
    SidebarQuestionInput: () => <div data-attr="mock-question-input" />,
}))

describe('HomepageAiInput', () => {
    const APPROVE_LABEL = 'I allow AI analysis in this organization'
    const patchOrganization = jest.fn()

    function renderInput(membershipLevel: OrganizationMembershipLevel): HTMLElement {
        initKeaTests(true, undefined, undefined, {
            ...MOCK_DEFAULT_ORGANIZATION,
            is_ai_data_processing_approved: false,
            membership_level: membershipLevel,
        })

        const { container } = render(
            <BindLogic logic={maxLogic} props={{ panelId: HOMEPAGE_TAB_ID }}>
                <HomepageAiInput />
            </BindLogic>
        )
        return container
    }

    beforeEach(() => {
        patchOrganization.mockClear()
        useMocks({
            patch: {
                '/api/organizations/:id': async ({ request }) => {
                    patchOrganization()
                    return [
                        200,
                        {
                            ...MOCK_DEFAULT_ORGANIZATION,
                            ...((await request.json()) as Partial<typeof MOCK_DEFAULT_ORGANIZATION>),
                        },
                    ]
                },
            },
            post: {
                '/api/organizations/:id/request_ai_access/': () => [200, { success: true }],
            },
        })
    })

    afterEach(cleanup)

    it('approves AI data processing once, and only after the admin confirms the BAA disclaimer', async () => {
        const container = renderInput(OrganizationMembershipLevel.Admin)

        fireEvent.click(screen.getByText(APPROVE_LABEL))
        fireEvent.click(await screen.findByText('Cancel'))
        await waitFor(() => expect(screen.queryByText(/Business Associate Agreement/)).toBeNull())
        expect(patchOrganization).not.toHaveBeenCalled()

        fireEvent.click(screen.getByText(APPROVE_LABEL))
        expect(await screen.findByText(/Business Associate Agreement/)).toBeTruthy()
        const confirmButton = screen.getByText('Enable AI analysis')
        fireEvent.click(confirmButton)
        fireEvent.click(confirmButton)

        await waitFor(() =>
            expect(organizationLogic.values.currentOrganization?.is_ai_data_processing_approved).toBe(true)
        )
        expect(patchOrganization).toHaveBeenCalledTimes(1)
        expect(container.querySelector('[data-attr="mock-question-input"]')).toBeTruthy()
    })

    it('lets a member ask an admin to approve, instead of dead-ending on the disabled reason', async () => {
        renderInput(OrganizationMembershipLevel.Member)

        expect(screen.queryByText(APPROVE_LABEL)).toBeNull()
        fireEvent.click(screen.getByText('Request access'))

        await waitFor(() => expect(screen.getByText(/Request sent\./)).toBeTruthy())
    })
})