Compare commits

...
2 changed files with 57 additions and 5 deletions
@@ -1,5 +1,5 @@
import { toast } from '@langgenius/dify-ui/toast'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defaultAgentSoulConfigFormState } from '@/features/agent-v2/agent-composer/form-state'
@@ -120,6 +120,41 @@ describe('AgentEnvEditor', () => {
expect(screen.getByDisplayValue('SECOND_KEY')).toBeInTheDocument()
})
it('should preserve a newly added variable key when key and value change in the same batch', async () => {
const user = userEvent.setup()
render(
<AgentComposerProvider
initialDraft={{
...defaultAgentSoulConfigFormState,
envVariables: [{
id: 'env-1',
key: 'API_KEY',
value: 'secret-value',
scope: 'plain',
}],
}}
>
<AgentEnvEditor />
</AgentComposerProvider>,
)
await user.click(screen.getByRole('button', { name: 'agentV2.agentDetail.configure.advancedSettings.envEditor.add' }))
const keyInputs = screen.getAllByPlaceholderText('agentV2.agentDetail.configure.advancedSettings.envEditor.keyPlaceholder')
const valueInputs = screen.getAllByPlaceholderText('agentV2.agentDetail.configure.advancedSettings.envEditor.valuePlaceholder')
const newKeyInput = keyInputs[1]!
const newValueInput = valueInputs[1]!
act(() => {
fireEvent.change(newKeyInput, { target: { value: 'SECOND_KEY' } })
fireEvent.change(newValueInput, { target: { value: 'plain' } })
})
expect(newKeyInput).toHaveValue('SECOND_KEY')
expect(newValueInput).toHaveValue('plain')
})
it('should import dotenv variables into the env table when a file is selected', async () => {
const user = userEvent.setup()
const { container } = renderAgentEnvEditor()
@@ -436,6 +436,23 @@ export function AgentEnvEditor() {
const envEditorTableId = 'agent-configure-env-editor-table'
const visibleEnvVariables = envVariables.length > 0 ? envVariables : [starterVariable]
const updateVariable = (id: string, updater: (variable: EnvVariable) => EnvVariable) => {
setEnvVariables((currentEnvVariables) => {
const existingVariable = currentEnvVariables.find(variable => variable.id === id)
if (existingVariable) {
return currentEnvVariables.map(variable => (
variable.id === id ? updater(variable) : variable
))
}
if (id === starterVariable.id)
return [updater(starterVariable)]
return currentEnvVariables
})
}
const addVariable = ({
focusField = 'key',
scope,
@@ -448,8 +465,8 @@ export function AgentEnvEditor() {
...(scope ? { scope } : {}),
}
addEnvVariable({
starterVariable,
setEnvVariables(currentEnvVariables => [
...(currentEnvVariables.length > 0 ? currentEnvVariables : [starterVariable]),
variable,
})
setFocusedVariable({ id: variable.id, field: focusField })
@@ -470,7 +487,7 @@ export function AgentEnvEditor() {
if (importedVariables.length === 0)
return
importEnvVariables(importedVariables)
setEnvVariables(currentEnvVariables => [...currentEnvVariables, ...importedVariables])
}
const updateVariableKey = (id: string, key: string) => {
setEnvVariableKey({ id, key, starterVariable })
@@ -482,7 +499,7 @@ export function AgentEnvEditor() {
setEnvVariableValue({ id, starterVariable, value })
}
const deleteVariable = (id: string) => {
removeEnvVariable(id)
setEnvVariables(currentEnvVariables => currentEnvVariables.filter(variable => variable.id !== id))
}
return (