From c8ee3fc9142fba80446eb902d3bb6a19fc1bc6ba Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 10 Jul 2026 04:54:49 +0000 Subject: [PATCH] test(ui): cover agent route sync after rename --- ui/src/pages/AgentDetail.progress.test.ts | 45 ++++++++++++++++++++++- ui/src/pages/AgentDetail.tsx | 2 +- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ui/src/pages/AgentDetail.progress.test.ts b/ui/src/pages/AgentDetail.progress.test.ts index 03c62e37229..83c26235567 100644 --- a/ui/src/pages/AgentDetail.progress.test.ts +++ b/ui/src/pages/AgentDetail.progress.test.ts @@ -1,8 +1,11 @@ -import { describe, expect, it } from "vitest"; +import { QueryClient } from "@tanstack/react-query"; +import { describe, expect, it, vi } from "vitest"; +import { queryKeys } from "../lib/queryKeys"; import { buildHeartbeatProgressLogLine, heartbeatProgressLogLineKey, + syncAgentRouteAfterRename, } from "./AgentDetail"; describe("buildHeartbeatProgressLogLine", () => { @@ -59,3 +62,43 @@ describe("heartbeatProgressLogLineKey", () => { ); }); }); + +describe("syncAgentRouteAfterRename", () => { + it("replaces stale agent routes after a rename changes the URL key", () => { + const queryClient = new QueryClient(); + const navigate = vi.fn(); + queryClient.setQueryData(queryKeys.agents.detail("old-agent"), { id: "agent-1" }); + queryClient.setQueryData(queryKeys.agents.detail("renamed-agent"), { id: "agent-1" }); + + const redirected = syncAgentRouteAfterRename( + queryClient, + navigate, + { id: "agent-1", name: "Old Agent", urlKey: "old-agent" }, + { id: "agent-1", name: "Renamed Agent", urlKey: "renamed-agent" }, + "configuration", + ); + + expect(redirected).toBe(true); + expect(navigate).toHaveBeenCalledWith("/agents/renamed-agent/configuration", { replace: true }); + expect(queryClient.getQueryData(queryKeys.agents.detail("old-agent"))).toBeUndefined(); + expect(queryClient.getQueryData(queryKeys.agents.detail("renamed-agent"))).toEqual({ id: "agent-1" }); + }); + + it("does not redirect when the canonical route ref stays the same", () => { + const queryClient = new QueryClient(); + const navigate = vi.fn(); + queryClient.setQueryData(queryKeys.agents.detail("same-agent"), { id: "agent-1" }); + + const redirected = syncAgentRouteAfterRename( + queryClient, + navigate, + { id: "agent-1", name: "Same Agent", urlKey: "same-agent" }, + { id: "agent-1", name: "Same Agent", urlKey: "same-agent" }, + "configuration", + ); + + expect(redirected).toBe(false); + expect(navigate).not.toHaveBeenCalled(); + expect(queryClient.getQueryData(queryKeys.agents.detail("same-agent"))).toEqual({ id: "agent-1" }); + }); +}); diff --git a/ui/src/pages/AgentDetail.tsx b/ui/src/pages/AgentDetail.tsx index 84ca05df491..ab60cae756d 100644 --- a/ui/src/pages/AgentDetail.tsx +++ b/ui/src/pages/AgentDetail.tsx @@ -1646,7 +1646,7 @@ function CostsSection({ * replace the URL with the new canonical reference. Returns true when a * redirect happened. */ -function syncAgentRouteAfterRename( +export function syncAgentRouteAfterRename( queryClient: QueryClient, navigate: NavigateFunction, previous: { id: string; urlKey?: string | null; name?: string | null },