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.1 KB
Raw Blame
import type { Mock } from 'vite-plus/test'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { createZendeskRuntime } from '../runtime'

describe('Zendesk runtime', () => {
  let api: Mock
  let runtime: ReturnType<typeof createZendeskRuntime>

  beforeEach(() => {
    api = vi.fn()
    runtime = createZendeskRuntime(() => api)
  })

  it('queues fields and opens only after the single requested load is ready', async () => {
    const fieldsCallback = vi.fn()
    runtime.setConversationFields(
      [{ id: 'email', value: '[email protected]' }],
      'CLOUD',
      fieldsCallback,
    )

    const firstOpen = runtime.open('CLOUD')
    const secondOpen = runtime.open('CLOUD')

    expect(runtime.getSnapshot()).toEqual({ attempt: 1, status: 'loading' })
    expect(api).not.toHaveBeenCalled()

    runtime.markReady()
    await Promise.all([firstOpen, secondOpen])

    expect(api).toHaveBeenCalledWith(
      'messenger:set',
      'conversationFields',
      [{ id: 'email', value: '[email protected]' }],
      expect.any(Function),
    )
    const flushedFieldsCallback = api.mock.calls[0]![3] as () => void
    flushedFieldsCallback()
    expect(fieldsCallback).toHaveBeenCalledOnce()
    expect(api).toHaveBeenNthCalledWith(2, 'messenger', 'show')
    expect(api).toHaveBeenNthCalledWith(3, 'messenger', 'open')
    expect(runtime.getSnapshot()).toEqual({ attempt: 1, status: 'ready' })
  })

  it('flushes only the latest pending value for each field', async () => {
    runtime.setConversationFields(
      [
        { id: 'email', value: '[email protected]' },
        { id: 'workspace', value: 'workspace-1' },
      ],
      'CLOUD',
    )
    runtime.setConversationFields(
      [
        { id: 'email', value: '[email protected]' },
        { id: 'plan', value: 'professional-plan' },
      ],
      'CLOUD',
    )

    const openPromise = runtime.open('CLOUD')
    runtime.markReady()
    await openPromise

    expect(api).toHaveBeenNthCalledWith(
      1,
      'messenger:set',
      'conversationFields',
      [
        { id: 'email', value: '[email protected]' },
        { id: 'workspace', value: 'workspace-1' },
        { id: 'plan', value: 'professional-plan' },
      ],
      undefined,
    )
    expect(api).toHaveBeenCalledTimes(3)
  })

  it('can retry a failed load and report the original failure', async () => {
    const failedOpen = runtime.open('CLOUD')
    runtime.markFailed()

    await expect(failedOpen).rejects.toThrow('Failed to load Zendesk')
    expect(runtime.getSnapshot()).toEqual({ attempt: 1, status: 'error' })

    const retriedOpen = runtime.open('CLOUD')
    expect(runtime.getSnapshot()).toEqual({ attempt: 2, status: 'loading' })

    runtime.markReady()
    await retriedOpen

    expect(api).toHaveBeenCalledWith('messenger', 'show')
    expect(api).toHaveBeenCalledWith('messenger', 'open')
  })

  it('does not load or queue fields outside Cloud deployments', async () => {
    runtime.setConversationFields([{ id: 'plan', value: 'sandbox-plan' }], 'COMMUNITY')
    await runtime.open('COMMUNITY')

    expect(runtime.getSnapshot()).toEqual({ attempt: 0, status: 'idle' })
    expect(api).not.toHaveBeenCalled()
  })
})