Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): dropped-Escape test flakes and an unhovered usage tooltip (#15424)

Nicholas Tindle2d12331f83f9
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index 4b5c61a0c2..56d7a4c954 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -187,7 +187,14 @@ export const ChatContainer = ({
const isSessionUnavailable =
!!isReconnecting || isLoadingSession || !!isSessionError;
const isLimitReached = useIsUsageLimitReached();
+ // Only a hover made while the limit is reached may open the usage tooltip.
+ // Radix reports hovers while `open` holds it shut, and a lifted limit hides
+ // it without a close event; either would pop it open, unhovered, the next
+ // time the limit is reached.
const [isUsageTooltipOpen, setIsUsageTooltipOpen] = useState(false);
+ useEffect(() => {
+ if (!isLimitReached) setIsUsageTooltipOpen(false);
+ }, [isLimitReached]);
const isInputDisabled =
isSessionUnavailable ||
isLimitReached ||
@@ -429,7 +436,9 @@ export const ChatContainer = ({
)}
<Tooltip
open={Boolean(isLimitReached && isUsageTooltipOpen)}
- onOpenChange={setIsUsageTooltipOpen}
+ onOpenChange={(open) =>
+ setIsUsageTooltipOpen(open && isLimitReached)
+ }
>
<TooltipTrigger asChild>
<div>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
index e0faac4248..08cb78361f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
@@ -214,9 +214,22 @@ describe("ChatContainer", () => {
vi.clearAllMocks();
vi.restoreAllMocks();
vi.unstubAllGlobals();
+ vi.useRealTimers();
});
+ // Radix opens a tooltip on a timer and arms its Escape handler a render
+ // after the tooltip appears. Run the delay on a fake clock inside act() so
+ // the tooltip is fully mounted before the next interaction, however slow
+ // the machine.
+ async function hoverUsageTooltipTrigger(trigger: HTMLElement) {
+ fireEvent.pointerMove(trigger, { pointerType: "mouse" });
+ await act(async () => {
+ vi.runOnlyPendingTimers();
+ });
+ }
+
it("keeps usage tooltips hoverable and dismissible as the limit changes", async () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
const { rerender } = render(<ChatContainer {...baseProps} />);
const input = screen.getByTestId("chat-input");
@@ -229,11 +242,10 @@ describe("ChatContainer", () => {
expect((input as HTMLInputElement).value).toBe("Unsent draft");
expect(screen.queryByRole("tooltip")).toBeNull();
- fireEvent.pointerMove(input.parentElement!, { pointerType: "mouse" });
- expect(await screen.findByRole("tooltip")).toBeDefined();
- await act(async () => {});
+ await hoverUsageTooltipTrigger(input.parentElement!);
+ expect(screen.getByRole("tooltip")).toBeDefined();
fireEvent.keyDown(document, { key: "Escape" });
- await waitFor(() => expect(screen.queryByRole("tooltip")).toBeNull());
+ expect(screen.queryByRole("tooltip")).toBeNull();
mockIsUsageLimitReached.mockReturnValue(false);
rerender(<ChatContainer {...baseProps} />);
@@ -244,6 +256,29 @@ describe("ChatContainer", () => {
consoleWarn.mockRestore();
});
+ it("opens the usage tooltip only for a hover made while the limit is reached", async () => {
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ const { rerender } = render(<ChatContainer {...baseProps} />);
+ const trigger = screen.getByTestId("chat-input").parentElement!;
+
+ // Hovering the composer before the limit is reached.
+ await hoverUsageTooltipTrigger(trigger);
+ fireEvent.pointerLeave(trigger, { pointerType: "mouse" });
+ mockIsUsageLimitReached.mockReturnValue(true);
+ rerender(<ChatContainer {...baseProps} />);
+ expect(screen.queryByRole("tooltip")).toBeNull();
+
+ // The limit lifting while the tooltip shows.
+ await hoverUsageTooltipTrigger(trigger);
+ expect(screen.getByRole("tooltip")).toBeDefined();
+ mockIsUsageLimitReached.mockReturnValue(false);
+ rerender(<ChatContainer {...baseProps} />);
+ fireEvent.pointerLeave(trigger, { pointerType: "mouse" });
+ mockIsUsageLimitReached.mockReturnValue(true);
+ rerender(<ChatContainer {...baseProps} />);
+ expect(screen.queryByRole("tooltip")).toBeNull();
+ });
+
it("renders the blurred usage-limit backdrop only when the limit is reached", () => {
mockIsUsageLimitReached.mockReturnValue(true);
diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/billing/__tests__/trial-canceled-popup.test.tsx b/autogpt_platform/frontend/src/app/(platform)/settings/billing/__tests__/trial-canceled-popup.test.tsx
index 07d2fff608..f59318f640 100644
--- a/autogpt_platform/frontend/src/app/(platform)/settings/billing/__tests__/trial-canceled-popup.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/settings/billing/__tests__/trial-canceled-popup.test.tsx
@@ -165,8 +165,14 @@ describe("post-cancel popup", () => {
mockCancelFlow();
render(<TrialCard />);
const popup = await cancelThroughConfirmation();
- fireEvent.keyDown(popup, { key: "Escape" });
- await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
+ // Radix handles Escape only from a render or two after the dialog
+ // appears, and those renders can still be queued when `findByRole`
+ // resolves, so a press made at once can go unheard. Press until the
+ // dialog closes.
+ await waitFor(() => {
+ fireEvent.keyDown(popup, { key: "Escape" });
+ expect(screen.queryByRole("dialog")).toBeNull();
+ });
expect(screen.getByRole("button", { name: "Resume trial" })).toBeDefined();
});