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.

9.2 KB
Raw Blame
import type { AutoUpdateConfig } from '@/app/components/plugins/reference-setting-modal/auto-update-setting/types'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter } from 'nuqs/adapters/testing'
import {
  AUTO_UPDATE_MODE,
  AUTO_UPDATE_STRATEGY,
} from '@/app/components/plugins/reference-setting-modal/auto-update-setting/types'
import { PluginCategoryEnum } from '@/app/components/plugins/types'
import { toast } from '@/app/notifications'
import { userProfileQueryOptions } from '@/features/account-profile/client'
import { UpdateSettingDialog } from '../update-setting-dialog'

const { getSettings, saveSettings } = vi.hoisted(() => ({
  getSettings: vi.fn(),
  saveSettings: vi.fn(),
}))

// The console client sends through `request`; adapt it back to path + params/body calls.
vi.mock('@/service/base', async (importOriginal) => ({
  ...(await importOriginal<typeof import('@/service/base')>()),
  request: async (url: string, _init: RequestInit, options: { request?: Request }) => {
    const { pathname, searchParams } = new URL(url)
    const path = pathname.replace('/console/api', '')
    const data =
      options.request?.method === 'POST'
        ? await saveSettings(path, { body: await options.request.json() })
        : await getSettings(path, { params: Object.fromEntries(searchParams) })
    return new Response(JSON.stringify(data ?? {}), {
      headers: { 'Content-Type': 'application/json' },
    })
  },
}))

const savedConfig: AutoUpdateConfig = {
  strategy_setting: AUTO_UPDATE_STRATEGY.disabled,
  upgrade_time_of_day: 0,
  upgrade_mode: AUTO_UPDATE_MODE.update_all,
  include_plugins: [],
  exclude_plugins: [],
}

function deferred<T>() {
  let resolve!: (value: T | PromiseLike<T>) => void
  let reject!: (reason: unknown) => void
  const promise = new Promise<T>((resolvePromise, rejectPromise) => {
    resolve = resolvePromise
    reject = rejectPromise
  })
  return { promise, resolve, reject }
}

function renderSettings(category = PluginCategoryEnum.model) {
  const client = new QueryClient({
    defaultOptions: { queries: { retry: false, staleTime: Infinity }, mutations: { retry: false } },
  })
  client.setQueryData(userProfileQueryOptions().queryKey, {
    profile: {
      id: 'user-1',
      name: 'Test User',
      email: '[email protected]',
      avatar_url: null,
      is_password_set: false,
      timezone: 'UTC',
    },
    meta: { currentVersion: null, currentEnv: null },
  })
  const renderCategory = (currentCategory: PluginCategoryEnum) => (
    <QueryClientProvider client={client}>
      <NuqsTestingAdapter>
        <UpdateSettingDialog category={currentCategory} />
      </NuqsTestingAdapter>
    </QueryClientProvider>
  )
  const view = render(renderCategory(category))
  return {
    ...view,
    client,
    changeCategory: (next: PluginCategoryEnum) => view.rerender(renderCategory(next)),
  }
}

const settingsTrigger = () =>
  screen.getByRole('button', { name: /^plugin\.autoUpdate\.autoUpdate / })
const strategy = (value: 'disabled' | 'fixOnly' | 'latest') =>
  screen.getByRole('radio', { name: `plugin.autoUpdate.strategy.${value}.name` })

beforeEach(() => {
  vi.resetAllMocks()
  getSettings.mockImplementation(
    async (_url, { params }: { params: { category: PluginCategoryEnum } }) => ({
      category: params.category,
      auto_upgrade: savedConfig,
    }),
  )
})

it('starts a draft when settings arrive and preserves it across a background refresh', async () => {
  const initialSettings = deferred<{
    category: PluginCategoryEnum
    auto_upgrade: AutoUpdateConfig
  }>()
  getSettings.mockReturnValueOnce(initialSettings.promise)
  const user = userEvent.setup()
  const { client } = renderSettings()
  const trigger = screen.getByRole('button', { name: 'plugin.autoUpdate.autoUpdate' })
  await user.click(trigger)
  expect(screen.getByRole('status')).toHaveTextContent('common.loading')
  expect(screen.queryByRole('button', { name: 'common.operation.save' })).not.toBeInTheDocument()
  initialSettings.resolve({ category: PluginCategoryEnum.model, auto_upgrade: savedConfig })
  await waitFor(() => expect(strategy('disabled')).toBeChecked())
  await user.click(strategy('latest'))
  getSettings.mockResolvedValue({
    category: PluginCategoryEnum.model,
    auto_upgrade: { ...savedConfig, strategy_setting: AUTO_UPDATE_STRATEGY.fixOnly },
  })
  await act(async () => {
    await client.refetchQueries({ predicate: (query) => query.queryKey.includes('autoUpgrade') })
  })
  expect(strategy('latest')).toBeChecked()
  await waitFor(() => expect(trigger).toHaveTextContent('plugin.autoUpdate.strategy.fixOnly.name'))
  await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
  await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
  await user.click(settingsTrigger())
  expect(strategy('fixOnly')).toBeChecked()
})

it('keeps an earlier category rollback separate from the current category session', async () => {
  const pendingSave = deferred<void>()
  saveSettings.mockReturnValue(pendingSave.promise)
  const user = userEvent.setup()
  const { changeCategory } = renderSettings()
  await waitFor(() =>
    expect(settingsTrigger()).toHaveTextContent('plugin.autoUpdate.strategy.disabled.name'),
  )
  await user.click(settingsTrigger())
  await user.click(strategy('latest'))
  await user.click(screen.getByRole('button', { name: 'common.operation.save' }))
  await waitFor(() => expect(saveSettings).toHaveBeenCalledOnce())
  expect(saveSettings).toHaveBeenCalledWith('/workspaces/current/plugin/auto-upgrade/change', {
    body: {
      category: PluginCategoryEnum.model,
      auto_upgrade: { ...savedConfig, strategy_setting: AUTO_UPDATE_STRATEGY.latest },
    },
  })
  getSettings.mockImplementation(
    async (_url, { params }: { params: { category: PluginCategoryEnum } }) => ({
      category: params.category,
      auto_upgrade: { ...savedConfig, strategy_setting: AUTO_UPDATE_STRATEGY.fixOnly },
    }),
  )
  changeCategory(PluginCategoryEnum.tool)
  await waitFor(() =>
    expect(getSettings).toHaveBeenCalledWith('/workspaces/current/plugin/auto-upgrade/fetch', {
      params: { category: PluginCategoryEnum.tool },
    }),
  )
  await act(async () => {
    pendingSave.reject(new Error('Save failed'))
  })
  await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
  await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
  expect(settingsTrigger()).toHaveTextContent('plugin.autoUpdate.strategy.fixOnly.name')
  await user.click(settingsTrigger())
  expect(strategy('fixOnly')).toBeChecked()
  await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
  await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
  changeCategory(PluginCategoryEnum.model)
  await waitFor(() =>
    expect(settingsTrigger()).toHaveTextContent('plugin.autoUpdate.strategy.disabled.name'),
  )
})

it('keeps a failed save open for retry and blocks dismissal while pending', async () => {
  const pendingSave = deferred<void>()
  saveSettings.mockReturnValue(pendingSave.promise)
  const user = userEvent.setup()
  renderSettings()
  await waitFor(() =>
    expect(settingsTrigger()).toHaveTextContent('plugin.autoUpdate.strategy.disabled.name'),
  )
  await user.click(settingsTrigger())
  await user.click(strategy('latest'))
  await user.click(screen.getByRole('button', { name: 'common.operation.save' }))
  const dialog = screen.getByRole('dialog')
  const save = screen.getByRole('button', { name: 'common.operation.save' })
  const cancel = screen.getByRole('button', { name: 'common.operation.cancel' })
  expect(dialog).toBeVisible()
  expect(save).toBeDisabled()
  expect(cancel).toBeDisabled()
  await user.click(save)
  await user.keyboard('{Escape}')
  expect(dialog).toBeVisible()
  expect(saveSettings).toHaveBeenCalledOnce()
  await act(async () => {
    pendingSave.reject(new Error('Save failed'))
  })
  expect(dialog).toBeVisible()
  await waitFor(() => expect(save).toBeEnabled())
  expect(cancel).toBeEnabled()
  expect(strategy('latest')).toBeChecked()
  await user.click(cancel)
  await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
  expect(settingsTrigger()).toHaveTextContent('plugin.autoUpdate.strategy.disabled.name')
  await user.click(settingsTrigger())
  expect(strategy('disabled')).toBeChecked()
  expect(saveSettings).toHaveBeenCalledOnce()
})

it('closes and reports success only after the save succeeds', async () => {
  const pendingSave = deferred<void>()
  saveSettings.mockReturnValue(pendingSave.promise)
  const success = vi.spyOn(toast, 'success')
  const user = userEvent.setup()
  renderSettings()
  await waitFor(() => expect(settingsTrigger()).toBeInTheDocument())
  await user.click(settingsTrigger())
  await user.click(strategy('latest'))
  await user.click(screen.getByRole('button', { name: 'common.operation.save' }))
  expect(screen.getByRole('dialog')).toBeVisible()
  expect(success).not.toHaveBeenCalled()
  await act(async () => {
    pendingSave.resolve()
  })
  await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
  await waitFor(() => expect(success).toHaveBeenCalledWith('common.api.actionSuccess'))
})