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.

refactor(web): query installation bootstrap status (#44382)

Codadaded133ad2043
diff --git a/web/app/install/installForm.spec.tsx b/web/app/install/installForm.spec.tsx
index 21bb44a431a..87295135507 100644
--- a/web/app/install/installForm.spec.tsx
+++ b/web/app/install/installForm.spec.tsx
@@ -1,9 +1,10 @@
import type { ReactElement } from 'react'
-import { fireEvent, screen, waitFor } from '@testing-library/react'
+import { act, fireEvent, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
-import { consoleClient } from '@/service/console'
+import ErrorBoundary from '@/app/components/base/error-boundary'
+import { consoleClient, consoleQuery } from '@/service/console'
import { expectLoadingButton } from '@/test/button'
-import { renderWithConsoleQuery } from '@/test/console/query-data'
+import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
import { encryptPassword } from '@/utils/encryption'
import InstallForm from './installForm'
@@ -18,15 +19,18 @@ vi.mock('@/next/navigation', () => ({
vi.mock('@/service/console', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/service/console')>()
- const { withConsoleClientResponses } = await import('@/test/console/service-responses')
+ const { withConsoleClientResponses, withConsoleResponses } =
+ await import('@/test/console/service-responses')
+ const responses = {
+ 'setup.get': vi.fn(),
+ 'setup.post': vi.fn(),
+ 'init.get': vi.fn(),
+ 'login.post': vi.fn(),
+ }
return {
...actual,
- consoleClient: withConsoleClientResponses(actual.consoleClient, {
- 'setup.get': vi.fn(),
- 'setup.post': vi.fn(),
- 'init.get': vi.fn(),
- 'login.post': vi.fn(),
- }),
+ consoleClient: withConsoleClientResponses(actual.consoleClient, responses),
+ consoleQuery: withConsoleResponses(actual.consoleQuery, responses),
}
})
@@ -35,6 +39,14 @@ const mockFetchInitValidateStatus = vi.mocked(consoleClient.init.get)
const mockSetup = vi.mocked(consoleClient.setup.post)
const mockLogin = vi.mocked(consoleClient.login.post)
+const createDeferredResponse = <T,>() => {
+ let resolve: (value: T) => void
+ const promise = new Promise<T>((onResolve) => {
+ resolve = onResolve
+ })
+ return { promise, resolve: (value: T) => resolve(value) }
+}
+
const prepareLoadedState = () => {
mockFetchSetupStatus.mockResolvedValue({ step: 'not_started' })
mockFetchInitValidateStatus.mockResolvedValue({
@@ -64,6 +76,93 @@ describe('InstallForm', () => {
expect(screen.getByRole('button', { name: /login\.installBtn/ })).toBeInTheDocument()
})
+ it('waits for both bootstrap checks before showing the account form', async () => {
+ const setupStatus =
+ createDeferredResponse<Awaited<ReturnType<typeof consoleClient.setup.get>>>()
+ const initStatus = createDeferredResponse<Awaited<ReturnType<typeof consoleClient.init.get>>>()
+ mockFetchSetupStatus.mockReturnValue(setupStatus.promise)
+ mockFetchInitValidateStatus.mockReturnValue(initStatus.promise)
+
+ render(<InstallForm />)
+
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+ expect(mockFetchInitValidateStatus).not.toHaveBeenCalled()
+
+ await act(async () => setupStatus.resolve({ step: 'not_started' }))
+ await waitFor(() => expect(mockFetchInitValidateStatus).toHaveBeenCalledTimes(1))
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+
+ await act(async () => initStatus.resolve({ status: 'finished' }))
+ expect(await screen.findByLabelText('login.email')).toBeInTheDocument()
+ })
+
+ it('redirects to initialization without showing the account form', async () => {
+ mockFetchInitValidateStatus.mockResolvedValue({ status: 'not_started' })
+
+ render(<InstallForm />)
+
+ await waitFor(() => expect(mockPush).toHaveBeenCalledWith('/init'))
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+ expect(mockReplace).not.toHaveBeenCalled()
+ })
+
+ it('revalidates cached bootstrap state before displaying or redirecting', async () => {
+ const queryClient = createConsoleQueryClient()
+ queryClient.setQueryData(consoleQuery.setup.get.queryKey(), { step: 'finished' })
+ queryClient.setQueryData(consoleQuery.init.get.queryKey(), { status: 'not_started' })
+ const setupStatus =
+ createDeferredResponse<Awaited<ReturnType<typeof consoleClient.setup.get>>>()
+ const initStatus = createDeferredResponse<Awaited<ReturnType<typeof consoleClient.init.get>>>()
+ mockFetchSetupStatus.mockReturnValue(setupStatus.promise)
+ mockFetchInitValidateStatus.mockReturnValue(initStatus.promise)
+
+ renderWithConsoleQuery(<InstallForm />, { queryClient })
+
+ expect(mockPush).not.toHaveBeenCalled()
+ expect(mockFetchInitValidateStatus).not.toHaveBeenCalled()
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+
+ await act(async () => setupStatus.resolve({ step: 'not_started' }))
+ await waitFor(() => expect(mockFetchInitValidateStatus).toHaveBeenCalledTimes(1))
+ expect(mockPush).not.toHaveBeenCalled()
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+
+ await act(async () => initStatus.resolve({ status: 'finished' }))
+ expect(await screen.findByLabelText('login.email')).toBeInTheDocument()
+ expect(mockPush).not.toHaveBeenCalled()
+ expect(queryClient.getQueryData(consoleQuery.setup.get.queryKey())).toEqual({
+ step: 'not_started',
+ })
+ expect(queryClient.getQueryData(consoleQuery.init.get.queryKey())).toEqual({
+ status: 'finished',
+ })
+ })
+
+ it.each(['setup', 'init'] as const)(
+ 'passes a failed %s check to the error boundary',
+ async (stage) => {
+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
+ const failedCheck = stage === 'setup' ? mockFetchSetupStatus : mockFetchInitValidateStatus
+ failedCheck.mockRejectedValue(new Error('Bootstrap unavailable'))
+
+ try {
+ render(
+ <ErrorBoundary fallback={<div role="alert">Bootstrap unavailable</div>}>
+ <InstallForm />
+ </ErrorBoundary>,
+ )
+
+ expect(await screen.findByRole('alert')).toHaveTextContent('Bootstrap unavailable')
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
+ expect(mockPush).not.toHaveBeenCalled()
+ expect(failedCheck).toHaveBeenCalledTimes(1)
+ if (stage === 'setup') expect(mockFetchInitValidateStatus).not.toHaveBeenCalled()
+ } finally {
+ consoleError.mockRestore()
+ }
+ },
+ )
+
it('should reveal and hide the password with an accessible action', async () => {
const user = userEvent.setup()
render(<InstallForm />)
@@ -242,5 +341,7 @@ describe('InstallForm', () => {
await waitFor(() => {
expect(mockPush).toHaveBeenCalledWith('/signin')
})
+ expect(mockFetchInitValidateStatus).not.toHaveBeenCalled()
+ expect(screen.queryByLabelText('login.email')).not.toBeInTheDocument()
})
})
diff --git a/web/app/install/installForm.tsx b/web/app/install/installForm.tsx
index 6bc92ac10d4..05fc0ca7b6d 100644
--- a/web/app/install/installForm.tsx
+++ b/web/app/install/installForm.tsx
@@ -12,7 +12,7 @@ import { Form } from '@langgenius/dify-ui/form'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Input } from '@langgenius/dify-ui/input'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
-import { useQueryClient } from '@tanstack/react-query'
+import { useQuery, useQueryClient } from '@tanstack/react-query'
import * as React from 'react'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
@@ -41,8 +41,27 @@ const InstallForm = () => {
const { push, replace } = useRouter()
const queryClient = useQueryClient()
const [showPassword, setShowPassword] = React.useState(false)
- const [loading, setLoading] = React.useState(true)
const [isSubmitting, setIsSubmitting] = React.useState(false)
+ const setupStatus = useQuery(
+ consoleQuery.setup.get.queryOptions({
+ staleTime: 0,
+ retry: false,
+ throwOnError: true,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ }),
+ )
+ const initStatus = useQuery(
+ consoleQuery.init.get.queryOptions({
+ enabled:
+ setupStatus.isSuccess && !setupStatus.isFetching && setupStatus.data.step !== 'finished',
+ staleTime: 0,
+ retry: false,
+ throwOnError: true,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ }),
+ )
const handleSubmit = async (value: AccountFormValues) => {
if (isSubmitting) return
@@ -79,19 +98,35 @@ const InstallForm = () => {
}
useEffect(() => {
- consoleClient.setup.get().then((res) => {
- if (res.step === 'finished') {
- push('/signin')
- } else {
- consoleClient.init.get().then((res) => {
- if (res.status === 'not_started') push('/init')
- })
- }
- setLoading(false)
- })
- }, [push])
+ if (!setupStatus.isSuccess || setupStatus.isFetching) return
+ if (setupStatus.data.step === 'finished') {
+ push('/signin')
+ } else if (
+ initStatus.isSuccess &&
+ !initStatus.isFetching &&
+ initStatus.data.status === 'not_started'
+ ) {
+ push('/init')
+ }
+ }, [
+ push,
+ setupStatus.isSuccess,
+ setupStatus.isFetching,
+ setupStatus.data,
+ initStatus.isSuccess,
+ initStatus.isFetching,
+ initStatus.data,
+ ])
+
+ const canSetup =
+ setupStatus.isSuccess &&
+ !setupStatus.isFetching &&
+ setupStatus.data.step !== 'finished' &&
+ initStatus.isSuccess &&
+ !initStatus.isFetching &&
+ initStatus.data.status !== 'not_started'
- return loading ? (
+ return !canSetup ? (
<LoadingPlaceholder />
) : (
<>