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.

1.4 KB
Raw Blame
import type { DifyClientConfig } from '../src/types/common'
import { vi } from 'vite-plus/test'
import { HttpClient } from '../src/http/client'

type FetchMock = ReturnType<typeof vi.fn>
type RequestSpy = ReturnType<typeof vi.fn>

type HttpClientWithFetchMock = {
  client: HttpClient
  fetchMock: FetchMock
}

type HttpClientWithSpies = HttpClientWithFetchMock & {
  request: RequestSpy
  requestStream: RequestSpy
  requestBinaryStream: RequestSpy
}

export const createHttpClient = (
  configOverrides: Partial<DifyClientConfig> = {},
): HttpClientWithFetchMock => {
  const fetchMock = vi.fn()
  vi.stubGlobal('fetch', fetchMock)
  const client = new HttpClient({ apiKey: 'test', ...configOverrides })
  return { client, fetchMock }
}

export const createHttpClientWithSpies = (
  configOverrides: Partial<DifyClientConfig> = {},
): HttpClientWithSpies => {
  const { client, fetchMock } = createHttpClient(configOverrides)
  const request = vi
    .spyOn(client, 'request')
    .mockResolvedValue({ data: 'ok', status: 200, headers: {} })
  const requestStream = vi
    .spyOn(client, 'requestStream')
    .mockResolvedValue({ data: null, status: 200, headers: {} } as never)
  const requestBinaryStream = vi
    .spyOn(client, 'requestBinaryStream')
    .mockResolvedValue({ data: null, status: 200, headers: {} } as never)
  return {
    client,
    fetchMock,
    request,
    requestStream,
    requestBinaryStream,
  }
}