Repository navigation
fix(ui): show one loading state in the agent switcher #3237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vedantlavale
wants to merge
1
commit into
kagent-dev:main
Choose a base branch
from
vedantlavale:fix/3217-agent-switcher-loading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
| import { ThemeProvider } from "@emotion/react"; | ||
| import { MemoryRouter } from "react-router-dom"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { Agent, ApiResource, NamespaceResponse } from "@/api"; | ||
| import { ApiError } from "@/api/ApiError"; | ||
| import { themeFor } from "@/theme/theme"; | ||
| import { AgentSwitcher } from "./AgentSwitcher"; | ||
|
|
||
| const { useNamespaces, useAgentsAcrossNamespaces } = vi.hoisted(() => ({ | ||
| useNamespaces: vi.fn(), | ||
| useAgentsAcrossNamespaces: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/api", () => ({ | ||
| useNamespaces, | ||
| useAgentsAcrossNamespaces, | ||
| harnessRefName: (agent: Agent) => agent.resource.spec.harnessRef.name, | ||
| })); | ||
|
|
||
| type Definitions = { agents: Agent[]; refused: { namespace: string; reason: string }[] }; | ||
|
|
||
| const namespaces: NamespaceResponse[] = [{ name: "kagent", status: "Active" }]; | ||
|
|
||
| function agent(namespace: string, name: string, harness: string): Agent { | ||
| return { | ||
| ref: `${namespace}/${name}`, | ||
| namespace, | ||
| name, | ||
| resource: { | ||
| metadata: { name }, | ||
| spec: { template: { ref: { name: "assistant" } }, harnessRef: { name: harness } }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const CURRENT = agent("kagent", "k8s", "default"); | ||
| const OTHER = agent("kagent", "reporting", "default"); | ||
|
|
||
| function loaded<T>(data: T): ApiResource<T> { | ||
| return { | ||
| data, | ||
| isLoading: false, | ||
| isValidating: false, | ||
| error: undefined, | ||
| isEmpty: Array.isArray(data) ? data.length === 0 : false, | ||
| refresh: async () => {}, | ||
| }; | ||
| } | ||
|
|
||
| function reading<T>(): ApiResource<T> { | ||
| return { | ||
| data: undefined, | ||
| isLoading: true, | ||
| isValidating: true, | ||
| error: undefined, | ||
| isEmpty: false, | ||
| refresh: async () => {}, | ||
| }; | ||
| } | ||
|
|
||
| /** A read held back because its key is not ready: SWR reports this with isLoading false. */ | ||
| function held<T>(): ApiResource<T> { | ||
| return { | ||
| data: undefined, | ||
| isLoading: false, | ||
| isValidating: false, | ||
| error: undefined, | ||
| isEmpty: false, | ||
| refresh: async () => {}, | ||
| }; | ||
| } | ||
|
|
||
| function failing<T>(message: string): ApiResource<T> { | ||
| return { | ||
| ...held<T>(), | ||
| error: new ApiError(message, { kind: "http", url: "kagent.test/Read", status: 500 }), | ||
| }; | ||
| } | ||
|
|
||
| function renderSwitcher() { | ||
| return render( | ||
| <ThemeProvider theme={themeFor("dark")}> | ||
| <MemoryRouter> | ||
| <AgentSwitcher current={{ namespace: "kagent", name: "k8s" }} onPicked={() => {}} /> | ||
| </MemoryRouter> | ||
| </ThemeProvider>, | ||
| ); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("AgentSwitcher", () => { | ||
| it("holds the agents read back until the namespaces are known", () => { | ||
| useNamespaces.mockReturnValue(reading<NamespaceResponse[]>()); | ||
| useAgentsAcrossNamespaces.mockReturnValue(held<Definitions>()); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(useAgentsAcrossNamespaces).toHaveBeenCalledWith(undefined); | ||
| }); | ||
|
|
||
| it("shows a placeholder while the namespaces are read, not an empty cluster", () => { | ||
| useNamespaces.mockReturnValue(reading<NamespaceResponse[]>()); | ||
| useAgentsAcrossNamespaces.mockReturnValue(held<Definitions>()); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getAllByTestId("agent-switcher-loading").length).toBeGreaterThan(0); | ||
| expect(screen.queryByTestId("agent-switcher-empty")).toBeNull(); | ||
| }); | ||
|
|
||
| it("keeps the placeholder while the agents are read", () => { | ||
| useNamespaces.mockReturnValue(loaded(namespaces)); | ||
| useAgentsAcrossNamespaces.mockReturnValue(reading<Definitions>()); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getAllByTestId("agent-switcher-loading").length).toBeGreaterThan(0); | ||
| expect(screen.queryByTestId("agent-switcher-empty")).toBeNull(); | ||
| }); | ||
|
|
||
| it("lists the other agents once both reads land, without the one you are on", () => { | ||
| useNamespaces.mockReturnValue(loaded(namespaces)); | ||
| useAgentsAcrossNamespaces.mockReturnValue( | ||
| loaded<Definitions>({ agents: [CURRENT, OTHER], refused: [] }), | ||
| ); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getByTestId(`agent-switcher-option-${OTHER.name}`)).toBeInTheDocument(); | ||
| expect(screen.queryByTestId(`agent-switcher-option-${CURRENT.name}`)).toBeNull(); | ||
| expect(screen.queryByTestId("agent-switcher-loading")).toBeNull(); | ||
| expect(screen.queryByTestId("agent-switcher-empty")).toBeNull(); | ||
| }); | ||
|
|
||
| it("says the cluster is empty only after both reads have said so", () => { | ||
| useNamespaces.mockReturnValue(loaded(namespaces)); | ||
| useAgentsAcrossNamespaces.mockReturnValue(loaded<Definitions>({ agents: [], refused: [] })); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getByTestId("agent-switcher-empty")).toHaveTextContent( | ||
| "No other agents on this cluster.", | ||
| ); | ||
| expect(screen.queryByTestId("agent-switcher-loading")).toBeNull(); | ||
| }); | ||
|
|
||
| it("reports a failed agents read as a failure, not as an empty cluster", () => { | ||
| useNamespaces.mockReturnValue(loaded(namespaces)); | ||
| useAgentsAcrossNamespaces.mockReturnValue(failing<Definitions>("the read failed")); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getByTestId("agent-switcher-error")).toHaveTextContent( | ||
| "Could not list agents. the read failed", | ||
| ); | ||
| expect(screen.queryByTestId("agent-switcher-empty")).toBeNull(); | ||
| }); | ||
|
|
||
| it("reports a failed namespaces read rather than an empty cluster", () => { | ||
| useNamespaces.mockReturnValue(failing<NamespaceResponse[]>("no namespaces")); | ||
| useAgentsAcrossNamespaces.mockReturnValue(held<Definitions>()); | ||
|
|
||
| renderSwitcher(); | ||
|
|
||
| expect(screen.getByTestId("agent-switcher-error")).toHaveTextContent( | ||
| "Could not list agents. no namespaces", | ||
| ); | ||
| expect(screen.queryByTestId("agent-switcher-empty")).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Level: 🟠 Medium · Blocking
This fixes the race in the switcher only. Two more callers pass
namespaces.data ?? []and fire the read with an empty list while namespaces load:UnmappedConversationsPage.tsx:30-35shows every conversation as unmapped, andAgentsPage.tsx:49-52reads an empty list first. Please fix them here too.🤖 written by Claude