Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.1 KB
Raw Blame
import { render } from '@testing-library/react'
import { trackWebAppEvent } from '../web-app-event'
import { WebAppAmplitudeProvider } from '../WebAppAmplitudeProvider'

const { mockClient, mockConsent } = vi.hoisted(() => ({
  mockClient: {
    sendEvent: vi.fn(),
    setOptOut: vi.fn(),
  },
  mockConsent: {
    value: 'unknown' as 'unknown' | 'denied' | 'granted',
  },
}))

vi.mock('@/app/components/base/analytics-consent/consent-store', () => ({
  useAnalyticsConsent: () => mockConsent.value,
}))

vi.mock('../web-app-client', () => ({
  sendWebAppAmplitudeEvent: (...args: unknown[]) => mockClient.sendEvent(...args),
  setWebAppAmplitudeOptOut: (optOut: boolean) => mockClient.setOptOut(optOut),
}))

describe('WebAppAmplitudeProvider', () => {
  beforeEach(() => {
    mockConsent.value = 'unknown'
  })

  it('registers explicit tracking only while analytics consent is granted', () => {
    const { rerender } = render(<WebAppAmplitudeProvider />)

    trackWebAppEvent('webapp_run', { app_mode: 'agent-v2' })
    expect(mockClient.sendEvent).not.toHaveBeenCalled()

    mockConsent.value = 'granted'
    rerender(<WebAppAmplitudeProvider />)

    trackWebAppEvent('webapp_run', { app_mode: 'agent-v2' })

    expect(mockClient.setOptOut).toHaveBeenLastCalledWith(false)
    expect(mockClient.sendEvent).toHaveBeenCalledWith('webapp_run', {
      app_mode: 'agent-v2',
    })

    mockConsent.value = 'denied'
    rerender(<WebAppAmplitudeProvider />)
    mockClient.sendEvent.mockClear()
    trackWebAppEvent('webapp_run', { app_mode: 'chatflow' })

    expect(mockClient.setOptOut).toHaveBeenLastCalledWith(true)
    expect(mockClient.sendEvent).not.toHaveBeenCalled()
  })

  it('unregisters tracking and opts out when the WebApp layout unmounts', () => {
    mockConsent.value = 'granted'
    const { unmount } = render(<WebAppAmplitudeProvider />)

    unmount()
    mockClient.sendEvent.mockClear()
    trackWebAppEvent('webapp_run', { app_mode: 'workflow' })

    expect(mockClient.setOptOut).toHaveBeenLastCalledWith(true)
    expect(mockClient.sendEvent).not.toHaveBeenCalled()
  })
})