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.

3.4 KB
Raw Blame
import type { DeploymentEdition } from '@dify/contracts/api/console/system-features/types.gen'
import type { ReactNode } from 'react'
import { render } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import Zendesk from '../index'

// Shared state for mocks
let mockDeploymentEdition: DeploymentEdition = 'CLOUD'
let mockZendeskWidgetKey: string | undefined = 'test-key'
let mockIsProd = false
let mockNonce: string | null = 'test-nonce'
const mocks = vi.hoisted(() => ({
  getOptionalSystemFeatures: vi.fn(),
  headers: vi.fn(),
  scriptProps: vi.fn(),
}))

// happy-dom cannot render async server components; unwrap memo only to invoke the server boundary.
vi.mock('react', async (importOriginal) => {
  const actual = await importOriginal<typeof import('react')>()
  return {
    ...actual,
    memo: vi.fn((fn) => fn),
  }
})

vi.mock('@/config', () => ({
  get ZENDESK_WIDGET_KEY() {
    return mockZendeskWidgetKey
  },
  get IS_PROD() {
    return mockIsProd
  },
}))

vi.mock('@/features/system-features/server', () => ({
  getOptionalSystemFeatures: mocks.getOptionalSystemFeatures,
}))

// Mock next/headers
vi.mock('@/next/headers', () => ({
  headers: mocks.headers,
}))

type ZendeskScriptProps = {
  nonce?: string
  widgetKey: string
}
vi.mock('../script', () => ({
  ZendeskScript: (props: ZendeskScriptProps) => {
    mocks.scriptProps(props)
    return null
  },
}))

describe('Zendesk', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mockDeploymentEdition = 'CLOUD'
    mockZendeskWidgetKey = 'test-key'
    mockIsProd = false
    mockNonce = 'test-nonce'
    mocks.getOptionalSystemFeatures.mockImplementation(async () => ({
      deployment_edition: mockDeploymentEdition,
    }))
    mocks.headers.mockImplementation(async () => {
      const requestHeaders = new Headers()
      if (mockNonce !== null) requestHeaders.set('x-nonce', mockNonce)
      return requestHeaders
    })
  })

  // Helper to call the async component
  const renderZendesk = async () => {
    const Component = Zendesk as unknown as () => Promise<ReactNode>
    return await Component()
  }

  it.each(['COMMUNITY', 'ENTERPRISE'] as const)(
    'should render nothing when deployment edition is %s',
    async (deploymentEdition) => {
      mockDeploymentEdition = deploymentEdition
      const result = await renderZendesk()
      expect(result).toBeNull()
    },
  )

  it('should render nothing when ZENDESK_WIDGET_KEY is missing', async () => {
    mockZendeskWidgetKey = undefined
    const result = await renderZendesk()
    expect(result).toBeNull()
    expect(mocks.headers).not.toHaveBeenCalled()
    expect(mocks.getOptionalSystemFeatures).not.toHaveBeenCalled()
  })

  it('should render nothing when System Features is unavailable', async () => {
    mocks.getOptionalSystemFeatures.mockResolvedValue(undefined)

    const result = await renderZendesk()

    expect(result).toBeNull()
  })

  it.each([
    { production: false, header: 'test-nonce', expectedNonce: '' },
    { production: true, header: 'prod-nonce', expectedNonce: 'prod-nonce' },
    { production: true, header: null, expectedNonce: '' },
  ])(
    'passes the CSP nonce to the widget runtime: %j',
    async ({ production, header, expectedNonce }) => {
      mockIsProd = production
      mockNonce = header
      render(await renderZendesk())
      expect(mocks.scriptProps).toHaveBeenCalledWith({
        nonce: expectedNonce,
        widgetKey: 'test-key',
      })
    },
  )
})