Enterprise agent platform for building, deploying, and operating AI teammates.
import React from "react"
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { resolveTranslation, type TranslationKey } from "@/i18n/translations"
// Issue #802: the `agent` tool category (multi-agent delegation) is a
// Workforce concern and must not be assignable from the agent builder.
// Covers the two frontend guarantees: the category never appears in the
// tool selector, and a legacy agent that still has it saved neither
// shows it as selected nor writes it back on save.
const apiRequestMock = vi.hoisted(() => vi.fn())
const toastErrorMock = vi.hoisted(() => vi.fn())
const useBuilderSettingsMock = vi.hoisted(() => vi.fn())
vi.mock("@/lib/agent-builder-settings-extension", () => ({
useAgentBuilderSettingsExtension: useBuilderSettingsMock,
}))
vi.mock("@/lib/api-wrapper", async () => {
const actual = await vi.importActual<typeof import("@/lib/api-wrapper")>(
"@/lib/api-wrapper"
)
return { ...actual, apiRequest: apiRequestMock }
})
vi.mock("@/lib/utils", async () => {
const actual = await vi.importActual<typeof import("@/lib/utils")>("@/lib/utils")
return {
...actual,
getApiUrl: () => "http://api.local",
getUploadApiUrl: () => "http://api.local",
getWsUrl: () => "ws://api.local",
}
})
vi.mock("@/contexts/app-context-chat", () => ({
useApp: () => ({
state: {
messages: [],
traceEvents: [],
currentTask: null,
isProcessing: false,
isHistoryLoading: false,
taskId: null,
filePreview: { isOpen: false },
dagExecution: null,
steps: [],
},
setTaskId: vi.fn(),
sendMessage: vi.fn(),
dispatch: vi.fn(),
closeFilePreview: vi.fn(),
pauseTask: vi.fn(),
resumeTask: vi.fn(),
openFilePreview: vi.fn(),
requestStatus: vi.fn(),
}),
}))
vi.mock("@/contexts/auth-context", () => ({
useAuth: () => ({ token: "token", user: { id: "1", is_admin: false } }),
}))
vi.mock("@/contexts/i18n-context", () => ({
useI18n: () => ({
locale: "en",
t: (key: string, vars?: Record<string, string>) =>
key === "builds.configForm.tools.alwaysAvailable"
? resolveTranslation("en", key as TranslationKey, vars)
: vars?.appName ? `${key}:${vars.appName}` : key,
}),
}))
vi.mock("@/contexts/mcp-apps-context", () => ({
useMcpApps: () => ({ apps: [], getAppIcon: () => null }),
}))
vi.mock("@/lib/branding", () => ({
getBrandingFromEnv: () => ({ appName: "Xagent" }),
}))
vi.mock("sonner", () => ({ toast: { error: toastErrorMock, success: vi.fn() } }))
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
useSearchParams: () => ({ get: () => null }),
}))
vi.mock("@/components/layout/resizable-three-column-layout", () => ({
ResizableThreeColumnLayout: ({ middlePanel }: { middlePanel: React.ReactNode }) => (
<div>{middlePanel}</div>
),
}))
vi.mock("@/components/task/task-conversation-panel", () => ({
TaskConversationPanel: () => null,
}))
vi.mock("@/components/build/agent-builder-chat", () => ({ AgentBuilderChat: () => null }))
vi.mock("@/components/kb/knowledge-base-creation-dialog", () => ({
KnowledgeBaseCreationDialog: () => null,
}))
vi.mock("@/components/mcp/connect-mcp-dialog", () => ({
ConnectMcpDialog: () => null,
}))
vi.mock("@/components/chat/FileMentionDropdown", () => ({ FileMentionDropdown: () => null }))
vi.mock("@/hooks/use-file-mention", () => ({
useFileMention: () => ({
checkTrigger: vi.fn(),
isOpen: false,
items: [],
selectedIndex: 0,
selectItem: vi.fn(),
close: vi.fn(),
}),
}))
// Render each MultiSelect's option values so tests can assert on what the
// tool-category selector actually offers.
vi.mock("@/components/ui/multi-select", () => ({
MultiSelect: (props: any) => (
<div data-testid="multi-select" data-placeholder={props.placeholder} data-option-labels={(props.options || []).map((option: any) => option.label).join("|")}>
{(props.options || []).map((o: any) => o.value).join("|")}
</div>
),
}))
vi.mock("@/components/ui/select", () => ({ Select: () => null }))
vi.mock("@/components/build/build-file-preview-sheet", () => ({
BuildFilePreviewSheet: () => null,
}))
import { AgentBuilder } from "./agent-builder"
const AGENT_ID = "5"
const AVAILABLE_TOOLS = [
{ name: "calculator", description: "", category: "basic_tools", enabled: true },
{ name: "web_search", description: "", category: "web_tools", enabled: true },
{ name: "agent_7", description: "", category: "agent", enabled: true },
{ name: "misc_tool", description: "", category: "other", enabled: true },
]
function agentResponse(toolCategories: string[]) {
return {
id: Number(AGENT_ID),
user_id: 1,
team_id: null,
name: "Legacy Agent",
description: "",
instructions: "You are a legacy agent.",
execution_mode: "balanced",
models: { general: "10" },
knowledge_bases: [],
skills: [],
tool_categories: toolCategories,
suggested_prompts: [],
logo_url: null,
status: "draft",
created_at: "2026-01-01T00:00:00Z",
updated_at: "2026-01-01T00:00:00Z",
widget_enabled: false,
allowed_domains: [],
share_enabled: false,
share_updated_at: null,
can_edit: true,
}
}
function installApi(
toolCategories: string[],
putResponse: () => Response | Promise<Response> = () =>
new Response(JSON.stringify(agentResponse(toolCategories)), { status: 200 }),
toolsBody: unknown = { tools: AVAILABLE_TOOLS },
skills: unknown[] = []
) {
apiRequestMock.mockImplementation((url: string, opts?: { method?: string }) => {
if (opts?.method === "PUT")
return Promise.resolve(putResponse())
if (url.endsWith("/api/kb/collections"))
return Promise.resolve(new Response(JSON.stringify({ collections: [] }), { status: 200 }))
if (url.endsWith("/api/skills/"))
return Promise.resolve(new Response(JSON.stringify(skills), { status: 200 }))
if (url.endsWith("/api/tools/available?include_usage=false"))
return Promise.resolve(new Response(JSON.stringify(toolsBody), { status: 200 }))
if (url.endsWith("/api/models/?category=llm"))
return Promise.resolve(new Response(JSON.stringify([]), { status: 200 }))
if (url.endsWith("/api/models/user-default"))
return Promise.resolve(new Response(JSON.stringify([]), { status: 200 }))
if (url.includes(`/api/agents/${AGENT_ID}/triggers`))
return Promise.resolve(new Response(JSON.stringify([]), { status: 200 }))
if (url.endsWith(`/api/agents/${AGENT_ID}`))
return Promise.resolve(
new Response(JSON.stringify(agentResponse(toolCategories)), { status: 200 })
)
if (url.includes("/api/mcp/servers"))
return Promise.resolve(new Response(JSON.stringify([]), { status: 200 }))
return Promise.resolve(new Response(JSON.stringify({}), { status: 200 }))
})
}
const toolCategorySelector = () =>
screen
.getAllByTestId("multi-select")
.find((el) => el.getAttribute("data-placeholder") === "builds.configForm.tools.placeholder")
beforeEach(() => {
apiRequestMock.mockReset()
toastErrorMock.mockReset()
useBuilderSettingsMock.mockReset().mockReturnValue({})
;(globalThis as any).WebSocket = vi.fn()
})
afterEach(() => cleanup())
describe("AgentBuilder host settings", () => {
it("renders the extension, gates publishing, and acknowledges only the submitted settings", async () => {
let finishSave!: (response: Response) => void
const response = new Promise<Response>(resolve => { finishSave = resolve })
installApi(["basic_tools"], () => response)
const markSaved = vi.fn()
const panel = <div>Host settings panel</div>
useBuilderSettingsMock.mockReturnValue({ panel, changes: {}, markSaved })
const { rerender } = render(<AgentBuilder agentId={AGENT_ID} />)
expect(await screen.findByText("Host settings panel")).toBeInTheDocument()
const update = screen.getByRole("button", { name: "builds.editor.header.update" })
const publish = screen.getByRole("button", { name: "builds.editor.header.publish" })
await waitFor(() => expect(publish).toBeEnabled())
expect(update).toBeDisabled()
const submitted = { policy: "strict" }
useBuilderSettingsMock.mockReturnValue({ panel, changes: submitted, markSaved })
rerender(<AgentBuilder agentId={AGENT_ID} />)
expect(update).toBeEnabled()
expect(publish).toBeDisabled()
fireEvent.click(update)
const put = apiRequestMock.mock.calls.find(([, options]) => options?.method === "PUT")
expect(JSON.parse(put?.[1].body)).toMatchObject({ host_settings: submitted })
expect(markSaved).not.toHaveBeenCalled()
useBuilderSettingsMock.mockReturnValue({ panel, changes: { policy: "new draft" }, markSaved })
rerender(<AgentBuilder agentId={AGENT_ID} />)
finishSave(Response.json(agentResponse(["basic_tools"])))
await waitFor(() => {
expect(markSaved).toHaveBeenCalledExactlyOnceWith(submitted)
expect(update).toBeEnabled()
})
expect(publish).toBeDisabled()
})
it("keeps the settings dirty when the save is rejected", async () => {
installApi(["basic_tools"], () => Response.json({ detail: "Invalid policy" }, { status: 422 }))
const markSaved = vi.fn()
useBuilderSettingsMock.mockReturnValue({ changes: { policy: "invalid" }, markSaved })
render(<AgentBuilder agentId={AGENT_ID} />)
const update = screen.getByRole("button", { name: "builds.editor.header.update" })
await waitFor(() => expect(update).toBeEnabled())
fireEvent.click(update)
await waitFor(() => expect(toastErrorMock.mock.calls.at(-1)?.[0]).toBe("Invalid policy"))
expect(markSaved).not.toHaveBeenCalled()
expect(update).toBeEnabled()
})
})
describe("AgentBuilder agent tool category (issue #802)", () => {
it("does not offer unassignable categories even when such tools exist", async () => {
installApi(["basic_tools"])
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => {
const selector = toolCategorySelector()
expect(selector).toBeTruthy()
expect(selector!.textContent).toContain("basic_tools")
})
const offered = toolCategorySelector()!.textContent!.split("|")
expect(offered).not.toContain("agent")
expect(offered).not.toContain("other")
})
it("saves a legacy agent without writing the agent category back", async () => {
installApi(["basic_tools", "agent"])
render(<AgentBuilder agentId={AGENT_ID} />)
// Wait for the agent to load (name lands in the header input).
await waitFor(() =>
expect(
screen.getByPlaceholderText("builds.configForm.name.placeholder")
).toHaveValue("Legacy Agent")
)
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
const putCall = apiRequestMock.mock.calls.find(
([, opts]) => (opts as any)?.method === "PUT"
)
expect(putCall).toBeTruthy()
const body = JSON.parse((putCall![1] as any).body)
expect(body.tool_categories).toEqual(["basic_tools"])
})
})
})
describe("AgentBuilder update error handling (issue #956)", () => {
it.each([
{
name: "a plain string detail",
payload: { detail: " Agent update failed with string detail " },
expected: "Agent update failed with string detail",
},
{
name: "a top-level message",
payload: { message: " Agent update failed with top-level message " },
expected: "Agent update failed with top-level message",
},
{
name: "a detail object without a readable message",
payload: { detail: { code: 123 } },
expected: "builds.editor.error.unknown",
},
{
name: "a detail array without readable entries",
payload: {
detail: [1, true, null, { msg: " " }, { message: " " }],
},
expected: "builds.editor.error.unknown",
},
])("handles $name without unmounting the builder", async ({ payload, expected }) => {
installApi(
["basic_tools"],
() =>
new Response(JSON.stringify(payload), {
status: 422,
headers: { "Content-Type": "application/json" },
})
)
render(<AgentBuilder agentId={AGENT_ID} />)
const nameInput = await screen.findByPlaceholderText(
"builds.configForm.name.placeholder"
)
fireEvent.change(nameInput, { target: { value: "Updated Agent" } })
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
expect(toastErrorMock).toHaveBeenCalled()
expect(toastErrorMock.mock.calls.at(-1)?.[0]).toBe(expected)
})
expect(screen.getByDisplayValue("Updated Agent")).toBeInTheDocument()
})
it("renders a structured detail message without unmounting the builder", async () => {
installApi(
["basic_tools"],
() =>
new Response(
JSON.stringify({
detail: {
message: "Agent update failed",
context: [],
},
}),
{ status: 422, headers: { "Content-Type": "application/json" } }
)
)
render(<AgentBuilder agentId={AGENT_ID} />)
const nameInput = await screen.findByPlaceholderText(
"builds.configForm.name.placeholder"
)
fireEvent.change(nameInput, { target: { value: "Updated Agent" } })
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
expect(toastErrorMock).toHaveBeenCalled()
expect(toastErrorMock.mock.calls.at(-1)?.[0]).toBe("Agent update failed")
})
expect(screen.getByDisplayValue("Updated Agent")).toBeInTheDocument()
})
it("uses the localized fallback for an empty response without unmounting the builder", async () => {
installApi(["basic_tools"], () => new Response(null, { status: 500 }))
render(<AgentBuilder agentId={AGENT_ID} />)
const nameInput = await screen.findByPlaceholderText(
"builds.configForm.name.placeholder"
)
fireEvent.change(nameInput, { target: { value: "Updated Agent" } })
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
expect(toastErrorMock).toHaveBeenCalled()
expect(toastErrorMock.mock.calls.at(-1)?.[0]).toBe(
"builds.editor.error.unknown"
)
})
expect(screen.getByDisplayValue("Updated Agent")).toBeInTheDocument()
})
it("renders FastAPI validation detail messages without unmounting the builder", async () => {
installApi(
["basic_tools"],
() =>
new Response(
JSON.stringify({
detail: [
{ msg: " Name must be 200 characters or fewer " },
" Invalid model selection ",
{ message: " Unsupported execution mode " },
{ msg: " " },
],
}),
{ status: 422, headers: { "Content-Type": "application/json" } }
)
)
render(<AgentBuilder agentId={AGENT_ID} />)
const nameInput = await screen.findByPlaceholderText(
"builds.configForm.name.placeholder"
)
fireEvent.change(nameInput, { target: { value: "Updated Agent" } })
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
expect(toastErrorMock).toHaveBeenCalled()
expect(toastErrorMock.mock.calls.at(-1)?.[0]).toBe(
"Name must be 200 characters or fewer; Invalid model selection; Unsupported execution mode"
)
})
expect(screen.getByDisplayValue("Updated Agent")).toBeInTheDocument()
})
})
describe("AgentBuilder extra built-in tools (issue #306)", () => {
const PREFIX = "The agent may also use these built-in tools: "
const block = () => screen.queryByText((text) => text.startsWith(PREFIX))
const toolsBody = (tools: Record<string, unknown>[]) => ({
tools: [
{ name: "calculator", description: "", category: "basic_tools", enabled: true, always_available: false },
...tools,
],
skill_loader_tool: "skill_loader_x",
})
it("lists backend-flagged tools and adds the skill loader only once a skill is selected", async () => {
installApi(
["basic_tools"],
undefined,
toolsBody([{ name: "clock_a", description: "", category: "other", enabled: true, always_available: true }]),
[{ name: "writer" }]
)
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => expect(block()?.textContent).toBe(`${PREFIX}clock_a`))
fireEvent.click(document.getElementById("selectAllSkills")!)
await waitFor(() => expect(block()?.textContent).toBe(`${PREFIX}clock_a, skill_loader_x`))
})
it("lists an always-available tool even when an admin disabled it", async () => {
installApi(
["basic_tools"],
undefined,
toolsBody([{ name: "clock_off", description: "", category: "other", enabled: false, always_available: true }])
)
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => expect(block()?.textContent).toContain("clock_off"))
})
it("hides the block for an agent saved with zero tools", async () => {
installApi(
[],
undefined,
toolsBody([{ name: "clock_a", description: "", category: "other", enabled: true, always_available: true }])
)
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => {
expect(screen.getByPlaceholderText("builds.configForm.name.placeholder")).toHaveValue("Legacy Agent")
expect(toolCategorySelector()?.textContent).toContain("basic_tools")
})
expect(block()).toBeNull()
fireEvent.click(document.getElementById("selectAllTools")!)
await waitFor(() => expect(block()).not.toBeNull())
})
it.each([
{ source: "a knowledge base", patch: { knowledge_bases: ["kb1"] } },
{ source: "an MCP connector", patch: { tool_categories: ["mcp:foo"] } },
])("lists always-available tools when only $source configures tools", async ({ patch }) => {
installApi(
[],
undefined,
toolsBody([{ name: "clock_a", description: "", category: "other", enabled: true, always_available: true }])
)
const baseImpl = apiRequestMock.getMockImplementation()!
apiRequestMock.mockImplementation(async (url: string, opts?: { method?: string }) =>
url.endsWith(`/api/agents/${AGENT_ID}`)
? new Response(JSON.stringify({ ...agentResponse([]), ...patch }), { status: 200 })
: baseImpl(url, opts)
)
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => expect(block()?.textContent).toBe(`${PREFIX}clock_a`))
})
it("lists only the skill loader for a zero-tool agent with a skill selected", async () => {
installApi(
[],
undefined,
toolsBody([{ name: "clock_a", description: "", category: "other", enabled: true, always_available: true }]),
[{ name: "writer" }]
)
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => {
expect(screen.getByPlaceholderText("builds.configForm.name.placeholder")).toHaveValue("Legacy Agent")
expect(toolCategorySelector()?.textContent).toContain("basic_tools")
})
fireEvent.click(document.getElementById("selectAllSkills")!)
await waitFor(() => expect(block()?.textContent).toBe(`${PREFIX}skill_loader_x`))
})
})
it("renders and saves migrated categories using only the canonical groups", async () => {
installApi(["basic_tools", "code_execution", "web_tools"], undefined, {
tools: [
{ name: "ask_user_question", description: "Ask", category: "basic_tools", enabled: true },
{ name: "execute_python_code", description: "Python", category: "code_execution", enabled: true },
{ name: "api_call", description: "HTTP", category: "web_tools", enabled: true },
{ name: "fetch_web_content", description: "Fetch", category: "web_tools", enabled: true },
],
categories: [
{ id: "basic_tools", label: "Basic Tools" },
{ id: "code_execution", label: "Code Execution" },
{ id: "web_tools", label: "Web Tools" },
],
})
render(<AgentBuilder agentId={AGENT_ID} />)
await waitFor(() => expect(screen.getByDisplayValue("Legacy Agent")).toBeInTheDocument())
expect(toolCategorySelector()?.textContent).toBe("basic_tools|code_execution|web_tools")
expect(toolCategorySelector()?.getAttribute("data-option-labels")).not.toContain("existingPermissions")
fireEvent.change(screen.getByDisplayValue("Legacy Agent"), { target: { value: "Renamed Legacy Agent" } })
fireEvent.click(screen.getByText("builds.editor.header.update"))
await waitFor(() => {
const put = apiRequestMock.mock.calls.find(([, options]) => (options as any)?.method === "PUT")
expect(put).toBeTruthy()
expect(JSON.parse((put![1] as any).body).tool_categories).toEqual(["basic_tools", "code_execution", "web_tools"])
})
})