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.
feat(frontend): vary daily-limit popup CTA by plan (SECRT-2314) (#12985)
Ubbebc5c2fb0eafe
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitGate.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitGate.tsxindex ac954923f5..ca8c4bb19a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitGate.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitGate.tsx@@ -1,7 +1,10 @@"use client";-import type { CoPilotUsagePublic } from "@/app/api/__generated__/models/coPilotUsagePublic";import { useGetV2GetCopilotUsage } from "@/app/api/__generated__/endpoints/chat/chat";+import { useGetSubscriptionStatus } from "@/app/api/__generated__/endpoints/credits/credits";+import type { CoPilotUsagePublic } from "@/app/api/__generated__/models/coPilotUsagePublic";+import type { SubscriptionStatusResponse } from "@/app/api/__generated__/models/subscriptionStatusResponse";+import type { SubscriptionTier } from "@/app/api/__generated__/models/subscriptionTier";import { toast } from "@/components/molecules/Toast/use-toast";import { useEffect } from "react";import { RateLimitResetDialog } from "./RateLimitResetDialog";@@ -29,6 +32,19 @@ export function RateLimitGate({ rateLimitMessage, onDismiss }: Props) {},});+ // Pulls the user's current tier so the dialog can branch the CTA between+ // "Upgrade plan" (route to /settings/billing) and "Contact us" (top-tier+ // users have no higher self-serve plan to upgrade to).+ const { data: tier } = useGetSubscriptionStatus({+ query: {+ enabled: !!rateLimitMessage,+ select: (res) =>+ res.status === 200+ ? ((res.data as SubscriptionStatusResponse).tier as SubscriptionTier)+ : null,+ },+ });+useEffect(() => {if (!rateLimitMessage) return;if (!usageError) return;@@ -47,6 +63,7 @@ export function RateLimitGate({ rateLimitMessage, onDismiss }: Props) {isOpen={isOpen}onClose={onDismiss}resetsAt={usage?.daily?.resets_at ?? usage?.weekly?.resets_at ?? null}+ tier={tier ?? null}/>);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitResetDialog.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitResetDialog.tsxindex 7ef04c468e..73c2754a37 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitResetDialog.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/RateLimitResetDialog.tsx@@ -1,5 +1,6 @@"use client";+import type { SubscriptionTier } from "@/app/api/__generated__/models/subscriptionTier";import { Button } from "@/components/atoms/Button/Button";import { Text } from "@/components/atoms/Text/Text";import { Dialog } from "@/components/molecules/Dialog/Dialog";@@ -10,11 +11,43 @@ interface Props {isOpen: boolean;onClose: () => void;resetsAt?: string | Date | null;+ tier?: SubscriptionTier | null;}-export function RateLimitResetDialog({ isOpen, onClose, resetsAt }: Props) {+const CONTACT_US_URL = "mailto:[email protected]";+const BILLING_PATH = "/settings/billing";++// Tiers that have no higher self-serve plan to upgrade to — direct these+// users to support instead of the billing page.+const TOP_TIERS: ReadonlySet<SubscriptionTier> = new Set([+ "MAX",+ "BUSINESS",+ "ENTERPRISE",+]);++export function RateLimitResetDialog({+ isOpen,+ onClose,+ resetsAt,+ tier,+}: Props) {const router = useRouter();const resetTimeLabel = resetsAt ? formatResetTime(resetsAt) : null;+ const isTopTier = !!tier && TOP_TIERS.has(tier);++ const ctaLabel = isTopTier ? "Contact us" : "Upgrade plan";+ const bodyTrailer = isTopTier+ ? "or contact us if you need more capacity."+ : "or upgrade your plan.";++ function handleCtaClick() {+ onClose();+ if (isTopTier) {+ window.open(CONTACT_US_URL, "_blank", "noopener,noreferrer");+ return;+ }+ router.push(BILLING_PATH);+ }return (<Dialog@@ -33,21 +66,15 @@ export function RateLimitResetDialog({ isOpen, onClose, resetsAt }: Props) {{resetTimeLabel && resetTimeLabel !== "now"? ` Resets ${resetTimeLabel}.`: ""}{" "}- You can still browse, edit agents, and view results — or upgrade- your plan.+ You can still browse, edit agents, and view results —{" "}+ {bodyTrailer}</Text><Dialog.Footer className="!justify-center"><Button variant="secondary" onClick={onClose}>Wait for reset</Button>- <Button- variant="primary"- onClick={() => {- onClose();- router.push("/settings/billing");- }}- >- Go to billing+ <Button variant="primary" onClick={handleCtaClick}>+ {ctaLabel}</Button></Dialog.Footer></Dialog.Content>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitGate.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitGate.test.tsxindex e70ba89835..62865b18ac 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitGate.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitGate.test.tsx@@ -19,6 +19,12 @@ vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({mockUseGetV2GetCopilotUsage(...args),}));+const mockUseGetSubscriptionStatus = vi.fn();+vi.mock("@/app/api/__generated__/endpoints/credits/credits", () => ({+ useGetSubscriptionStatus: (...args: unknown[]) =>+ mockUseGetSubscriptionStatus(...args),+}));+// Capture props the dialog was rendered with so we can assert on them.const dialogSpy = vi.fn();vi.mock("../RateLimitResetDialog", () => ({@@ -26,6 +32,7 @@ vi.mock("../RateLimitResetDialog", () => ({isOpen: boolean;onClose: () => void;resetsAt?: string | Date | null;+ tier?: string | null;}) => {dialogSpy(props);return <div data-testid="reset-dialog" data-open={String(props.isOpen)} />;@@ -38,9 +45,18 @@ afterEach(() => {cleanup();mockToast.mockReset();mockUseGetV2GetCopilotUsage.mockReset();+ mockUseGetSubscriptionStatus.mockReset();dialogSpy.mockReset();});+function setSubscription(tier: string | null = null) {+ mockUseGetSubscriptionStatus.mockReturnValue({+ data: tier,+ isSuccess: tier !== null,+ isError: false,+ });+}+describe("RateLimitGate", () => {it("disables the usage query when no rate-limit message is present", () => {mockUseGetV2GetCopilotUsage.mockReturnValue({@@ -48,6 +64,7 @@ describe("RateLimitGate", () => {isSuccess: false,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage={null} onDismiss={vi.fn()} />);@@ -64,6 +81,7 @@ describe("RateLimitGate", () => {isSuccess: false,isError: false,});+ setSubscription();render(<RateLimitGate@@ -78,6 +96,23 @@ describe("RateLimitGate", () => {expect(config?.query?.enabled).toBe(true);});+ it("disables the subscription query when no rate-limit message is present", () => {+ mockUseGetV2GetCopilotUsage.mockReturnValue({+ data: undefined,+ isSuccess: false,+ isError: false,+ });+ setSubscription();++ render(<RateLimitGate rateLimitMessage={null} onDismiss={vi.fn()} />);++ expect(mockUseGetSubscriptionStatus).toHaveBeenCalled();+ const [config] = mockUseGetSubscriptionStatus.mock.calls[0] as [+ { query?: { enabled?: boolean } },+ ];+ expect(config?.query?.enabled).toBe(false);+ });+it("opens the reset dialog when usage data is available", () => {const future = new Date(Date.now() + 4 * 60 * 60 * 1000).toISOString();mockUseGetV2GetCopilotUsage.mockReturnValue({@@ -85,6 +120,7 @@ describe("RateLimitGate", () => {isSuccess: true,isError: false,});+ setSubscription("PRO");render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,@@ -96,6 +132,40 @@ describe("RateLimitGate", () => {expect(mockToast).not.toHaveBeenCalled();});+ it("forwards the user's subscription tier to the dialog", () => {+ const future = new Date(Date.now() + 4 * 60 * 60 * 1000).toISOString();+ mockUseGetV2GetCopilotUsage.mockReturnValue({+ data: { daily: { percent_used: 100, resets_at: future }, weekly: null },+ isSuccess: true,+ isError: false,+ });+ setSubscription("MAX");++ render(+ <RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,+ );++ const lastProps = dialogSpy.mock.calls.at(-1)?.[0];+ expect(lastProps.tier).toBe("MAX");+ });++ it("passes null tier when subscription status is unavailable", () => {+ const future = new Date(Date.now() + 4 * 60 * 60 * 1000).toISOString();+ mockUseGetV2GetCopilotUsage.mockReturnValue({+ data: { daily: { percent_used: 100, resets_at: future }, weekly: null },+ isSuccess: true,+ isError: false,+ });+ setSubscription(null);++ render(+ <RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,+ );++ const lastProps = dialogSpy.mock.calls.at(-1)?.[0];+ expect(lastProps.tier).toBeNull();+ });+it("falls back to a toast when usage query errors", () => {const onDismiss = vi.fn();mockUseGetV2GetCopilotUsage.mockReturnValue({@@ -103,6 +173,7 @@ describe("RateLimitGate", () => {isSuccess: false,isError: true,});+ setSubscription();render(<RateLimitGate@@ -130,6 +201,7 @@ describe("RateLimitGate", () => {isSuccess: false,isError: true,});+ setSubscription();render(<RateLimitGate rateLimitMessage={null} onDismiss={vi.fn()} />);@@ -142,6 +214,7 @@ describe("RateLimitGate", () => {isSuccess: false,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,@@ -159,6 +232,7 @@ describe("RateLimitGate", () => {isSuccess: true,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,@@ -180,6 +254,7 @@ describe("RateLimitGate", () => {isSuccess: true,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,@@ -196,6 +271,7 @@ describe("RateLimitGate", () => {isSuccess: true,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,@@ -219,6 +295,7 @@ describe("RateLimitGate", () => {isSuccess: true,isError: false,});+ setSubscription();render(<RateLimitGate rateLimitMessage="limit reached" onDismiss={vi.fn()} />,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitResetDialog.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitResetDialog.test.tsxindex 7eda60dbe5..58b49051f6 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitResetDialog.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/RateLimitResetDialog/__tests__/RateLimitResetDialog.test.tsx@@ -4,7 +4,7 @@ import {screen,fireEvent,} from "@/tests/integrations/test-utils";-import { afterEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const mockPush = vi.fn();vi.mock("next/navigation", () => ({@@ -23,9 +23,21 @@ vi.mock("next/navigation", () => ({import { RateLimitResetDialog } from "../RateLimitResetDialog";+const mockWindowOpen = vi.fn();++beforeEach(() => {+ // jsdom's window.open returns null and pollutes the test output; spy and+ // suppress so we can assert the URL the dialog tries to open.+ vi.spyOn(window, "open").mockImplementation(+ mockWindowOpen as unknown as typeof window.open,+ );+});+afterEach(() => {cleanup();mockPush.mockReset();+ mockWindowOpen.mockReset();+ vi.restoreAllMocks();});describe("RateLimitResetDialog", () => {@@ -62,13 +74,14 @@ describe("RateLimitResetDialog", () => {expect(bodyText.textContent).not.toContain("Resets in");});- it("renders Wait for reset and Go to billing buttons", () => {+ it("renders Wait for reset and Upgrade plan buttons by default", () => {render(<RateLimitResetDialog isOpen={true} onClose={vi.fn()} resetsAt={null} />,);expect(screen.getByText("Wait for reset")).toBeDefined();- expect(screen.getByText("Go to billing")).toBeDefined();+ expect(screen.getByText("Upgrade plan")).toBeDefined();+ expect(screen.queryByText("Contact us")).toBeNull();});it("calls onClose when Wait for reset is clicked", () => {@@ -81,17 +94,66 @@ describe("RateLimitResetDialog", () => {expect(onClose).toHaveBeenCalledTimes(1);});- it("navigates to /settings/billing when Go to billing is clicked", () => {+ it("navigates to /settings/billing when Upgrade plan is clicked (no tier)", () => {const onClose = vi.fn();render(<RateLimitResetDialog isOpen={true} onClose={onClose} resetsAt={null} />,);- fireEvent.click(screen.getByText("Go to billing"));+ fireEvent.click(screen.getByText("Upgrade plan"));expect(onClose).toHaveBeenCalledTimes(1);expect(mockPush).toHaveBeenCalledWith("/settings/billing");+ expect(mockWindowOpen).not.toHaveBeenCalled();});+ it.each(["NO_TIER", "BASIC", "PRO"] as const)(+ "shows Upgrade plan and routes to /settings/billing for tier=%s",+ (tier) => {+ const onClose = vi.fn();+ render(+ <RateLimitResetDialog+ isOpen={true}+ onClose={onClose}+ resetsAt={null}+ tier={tier}+ />,+ );++ expect(screen.getByText("Upgrade plan")).toBeDefined();+ fireEvent.click(screen.getByText("Upgrade plan"));+ expect(onClose).toHaveBeenCalledTimes(1);+ expect(mockPush).toHaveBeenCalledWith("/settings/billing");+ expect(mockWindowOpen).not.toHaveBeenCalled();+ },+ );++ it.each(["MAX", "BUSINESS", "ENTERPRISE"] as const)(+ "shows Contact us and opens mailto for top-tier plan=%s",+ (tier) => {+ const onClose = vi.fn();+ render(+ <RateLimitResetDialog+ isOpen={true}+ onClose={onClose}+ resetsAt={null}+ tier={tier}+ />,+ );++ expect(screen.getByText("Contact us")).toBeDefined();+ expect(screen.queryByText("Upgrade plan")).toBeNull();++ fireEvent.click(screen.getByText("Contact us"));+ expect(onClose).toHaveBeenCalledTimes(1);+ expect(mockWindowOpen).toHaveBeenCalledWith(+ "mailto:[email protected]",+ "_blank",+ "noopener,noreferrer",+ );+ expect(mockPush).not.toHaveBeenCalled();+ },+ );+it("does not render dialog content when closed", () => {render(<RateLimitResetDialog isOpen={false} onClose={vi.fn()} resetsAt={null} />,