diff --git a/gui/src/pages/config/components/CliInstallBanner.test.tsx b/gui/src/pages/config/components/CliInstallBanner.test.tsx index 9d1f8e989..5bf24bc2e 100644 --- a/gui/src/pages/config/components/CliInstallBanner.test.tsx +++ b/gui/src/pages/config/components/CliInstallBanner.test.tsx @@ -9,6 +9,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { MockIdeMessenger } from "../../../context/MockIdeMessenger"; import * as util from "../../../util"; +import * as localStorage from "../../../util/localStorage"; import { CliInstallBanner } from "./CliInstallBanner"; vi.mock("../../../util", async () => { @@ -19,6 +20,15 @@ vi.mock("../../../util", async () => { }; }); +vi.mock("../../../util/localStorage", async () => { + const actual = await vi.importActual("../../../util/localStorage"); + return { + ...actual, + getLocalStorage: vi.fn(), + setLocalStorage: vi.fn(), + }; +}); + describe("CliInstallBanner", () => { let mockIdeMessenger: MockIdeMessenger; @@ -26,6 +36,7 @@ describe("CliInstallBanner", () => { vi.clearAllMocks(); mockIdeMessenger = new MockIdeMessenger(); vi.mocked(util.getPlatform).mockReturnValue("mac"); + vi.mocked(localStorage.getLocalStorage).mockReturnValue(undefined); }); const renderComponent = async (subprocessResponse: [string, string]) => { @@ -49,7 +60,7 @@ describe("CliInstallBanner", () => { await waitFor(() => { expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); }); @@ -58,7 +69,9 @@ describe("CliInstallBanner", () => { await renderComponent(["", "command not found"]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); @@ -66,7 +79,9 @@ describe("CliInstallBanner", () => { await renderComponent(["", ""]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); @@ -76,7 +91,13 @@ describe("CliInstallBanner", () => { .spyOn(mockIdeMessenger.ide, "subprocess") .mockResolvedValue(["", ""]); - await renderComponent(["", ""]); + await act(async () => + render( + + + , + ), + ); await waitFor(() => { expect(subprocessSpy).toHaveBeenCalledWith("which cn"); @@ -89,7 +110,13 @@ describe("CliInstallBanner", () => { .spyOn(mockIdeMessenger.ide, "subprocess") .mockResolvedValue(["", ""]); - await renderComponent(["", ""]); + await act(async () => + render( + + + , + ), + ); await waitFor(() => { expect(subprocessSpy).toHaveBeenCalledWith("which cn"); @@ -102,7 +129,13 @@ describe("CliInstallBanner", () => { .spyOn(mockIdeMessenger.ide, "subprocess") .mockResolvedValue(["", ""]); - await renderComponent(["", ""]); + await act(async () => + render( + + + , + ), + ); await waitFor(() => { expect(subprocessSpy).toHaveBeenCalledWith("where cn"); @@ -123,7 +156,9 @@ describe("CliInstallBanner", () => { ); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); }); @@ -132,12 +167,14 @@ describe("CliInstallBanner", () => { beforeEach(async () => { await renderComponent(["", "not found"]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); it("displays the title", () => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect(screen.getByText("Try out the Continue CLI")).toBeInTheDocument(); }); it("displays the description with 'cn' code element", () => { @@ -150,18 +187,22 @@ describe("CliInstallBanner", () => { expect(screen.getByText("npm i -g @continuedev/cli")).toBeInTheDocument(); }); - it("displays the Learn more button", () => { - expect(screen.getByText("Learn more")).toBeInTheDocument(); + it("displays the Learn more link", () => { + expect(screen.getByText("Learn more.")).toBeInTheDocument(); }); it("displays the close button", () => { - const closeButton = screen.getByRole("button", { name: /dismiss/i }); - expect(closeButton).toBeInTheDocument(); + // Get the styled close button (it doesn't have a text label) + const buttons = screen.getAllByRole("button"); + // There should be multiple buttons (close, copy, run) + expect(buttons.length).toBeGreaterThan(0); }); it("displays the CommandLine icon", () => { // The icon should be present in the component - const banner = screen.getByText("Try the Continue CLI").closest("div"); + const banner = screen + .getByText("Try out the Continue CLI") + .closest("div"); expect(banner).toBeInTheDocument(); }); }); @@ -170,26 +211,30 @@ describe("CliInstallBanner", () => { beforeEach(async () => { await renderComponent(["", "not found"]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); it("dismisses banner when close button is clicked", async () => { - const closeButton = screen.getByRole("button", { name: /dismiss/i }); + const buttons = screen.getAllByRole("button"); + // First button should be the close button (CloseButton component) + const closeButton = buttons[0]; fireEvent.click(closeButton); await waitFor(() => { expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); }); - it("opens documentation URL when Learn more button is clicked", async () => { + it("opens documentation URL when Learn more link is clicked", async () => { const postSpy = vi.spyOn(mockIdeMessenger, "post"); - const learnMoreButton = screen.getByText("Learn more"); + const learnMoreLink = screen.getByText("Learn more."); - fireEvent.click(learnMoreButton); + fireEvent.click(learnMoreLink); expect(postSpy).toHaveBeenCalledWith( "openUrl", @@ -197,14 +242,11 @@ describe("CliInstallBanner", () => { ); }); - it("copies installation command when copy button is clicked", async () => { - // Find the copy button (ClipboardIcon) - const copyButtons = screen.getAllByRole("button"); - const copyButton = copyButtons.find((btn) => - btn.querySelector('svg[class*="ClipboardIcon"]'), - ); - - expect(copyButton).toBeDefined(); + it("displays the installation command with interactive controls", async () => { + // The installation command should be visible + expect(screen.getByText("npm i -g @continuedev/cli")).toBeInTheDocument(); + // The "Run" text should be visible for the run button + expect(screen.getByText(/Run/i)).toBeInTheDocument(); }); it("runs installation command in terminal when run button is clicked", async () => { @@ -216,7 +258,7 @@ describe("CliInstallBanner", () => { fireEvent.click(runButton); expect(postSpy).toHaveBeenCalledWith("runCommand", { - command: "npm i -g @continuedev/cli", + command: `npm i -g @continuedev/cli && cn "Explore this repo and provide a concise summary of it's contents"`, }); } }); @@ -239,7 +281,7 @@ describe("CliInstallBanner", () => { // Should not be visible immediately expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); @@ -247,16 +289,19 @@ describe("CliInstallBanner", () => { const { rerender } = await renderComponent(["", "not found"]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); // Dismiss the banner - const closeButton = screen.getByRole("button", { name: /dismiss/i }); + const buttons = screen.getAllByRole("button"); + const closeButton = buttons[0]; fireEvent.click(closeButton); await waitFor(() => { expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); @@ -269,7 +314,7 @@ describe("CliInstallBanner", () => { // Should still be hidden expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); }); @@ -279,7 +324,9 @@ describe("CliInstallBanner", () => { await renderComponent([" \n ", ""]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); @@ -288,7 +335,7 @@ describe("CliInstallBanner", () => { await waitFor(() => { expect( - screen.queryByText("Try the Continue CLI"), + screen.queryByText("Try out the Continue CLI"), ).not.toBeInTheDocument(); }); }); @@ -297,8 +344,148 @@ describe("CliInstallBanner", () => { await renderComponent(["", "cn: command not found"]); await waitFor(() => { - expect(screen.getByText("Try the Continue CLI")).toBeInTheDocument(); + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); }); }); }); + + describe("Message threshold logic", () => { + const renderWithMessageCount = async ( + messageCount?: number, + messageThreshold?: number, + ) => { + vi.spyOn(mockIdeMessenger.ide, "subprocess").mockResolvedValue([ + "", + "not found", + ]); + + return act(async () => + render( + + + , + ), + ); + }; + + it("shows banner when no threshold is set", async () => { + await renderWithMessageCount(0, undefined); + + await waitFor(() => { + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); + }); + }); + + it("does not show banner when message count is below threshold", async () => { + await renderWithMessageCount(2, 3); + + await waitFor(() => { + expect( + screen.queryByText("Try out the Continue CLI"), + ).not.toBeInTheDocument(); + }); + }); + + it("shows banner when message count meets threshold", async () => { + await renderWithMessageCount(3, 3); + + await waitFor(() => { + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); + }); + }); + + it("shows banner when message count exceeds threshold", async () => { + await renderWithMessageCount(5, 3); + + await waitFor(() => { + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); + }); + }); + }); + + describe("Permanent dismissal with localStorage", () => { + const renderWithPermanentDismissal = async () => { + vi.spyOn(mockIdeMessenger.ide, "subprocess").mockResolvedValue([ + "", + "not found", + ]); + + return act(async () => + render( + + + , + ), + ); + }; + + it("does not show banner when previously dismissed permanently", async () => { + vi.mocked(localStorage.getLocalStorage).mockReturnValue(true); + + await renderWithPermanentDismissal(); + + await waitFor(() => { + expect( + screen.queryByText("Try out the Continue CLI"), + ).not.toBeInTheDocument(); + }); + }); + + it("sets localStorage when dismissed with permanentDismissal=true", async () => { + await renderWithPermanentDismissal(); + + await waitFor(() => { + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); + }); + + const buttons = screen.getAllByRole("button"); + const closeButton = buttons[0]; + fireEvent.click(closeButton); + + expect(localStorage.setLocalStorage).toHaveBeenCalledWith( + "hasDismissedCliInstallBanner", + true, + ); + }); + + it("does not set localStorage when dismissed with permanentDismissal=false", async () => { + vi.spyOn(mockIdeMessenger.ide, "subprocess").mockResolvedValue([ + "", + "not found", + ]); + + await act(async () => + render( + + + , + ), + ); + + await waitFor(() => { + expect( + screen.getByText("Try out the Continue CLI"), + ).toBeInTheDocument(); + }); + + const buttons = screen.getAllByRole("button"); + const closeButton = buttons[0]; + fireEvent.click(closeButton); + + expect(localStorage.setLocalStorage).not.toHaveBeenCalled(); + }); + }); }); diff --git a/gui/src/pages/config/components/CliInstallBanner.tsx b/gui/src/pages/config/components/CliInstallBanner.tsx index 075ea0de6..8747d7ecd 100644 --- a/gui/src/pages/config/components/CliInstallBanner.tsx +++ b/gui/src/pages/config/components/CliInstallBanner.tsx @@ -6,13 +6,36 @@ import { RunInTerminalButton } from "../../../components/StyledMarkdownPreview/S import { Card } from "../../../components/ui"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { getPlatform } from "../../../util"; +import { getLocalStorage, setLocalStorage } from "../../../util/localStorage"; -export function CliInstallBanner() { +interface CliInstallBannerProps { + /** Current message count - banner shows only if >= messageThreshold */ + messageCount?: number; + /** Minimum messages before showing banner (default: always show) */ + messageThreshold?: number; + /** If true, dismissal is permanent via localStorage (default: session only) */ + permanentDismissal?: boolean; +} + +export function CliInstallBanner({ + messageCount, + messageThreshold, + permanentDismissal = false, +}: CliInstallBannerProps = {}) { const ideMessenger = useContext(IdeMessengerContext); const [cliInstalled, setCliInstalled] = useState(null); const [dismissed, setDismissed] = useState(false); useEffect(() => { + // Check if user has permanently dismissed the banner + if (permanentDismissal) { + const hasDismissed = getLocalStorage("hasDismissedCliInstallBanner"); + if (hasDismissed) { + setDismissed(true); + return; + } + } + const checkCliInstallation = async () => { try { const platform = getPlatform(); @@ -32,18 +55,35 @@ export function CliInstallBanner() { } }; - checkCliInstallation(); - }, [ideMessenger]); + void checkCliInstallation(); + }, [ideMessenger, permanentDismissal]); - // Don't show if still loading, already installed, or dismissed - if (cliInstalled === null || cliInstalled === true || dismissed) { + const handleDismiss = () => { + setDismissed(true); + if (permanentDismissal) { + setLocalStorage("hasDismissedCliInstallBanner", true); + } + }; + + // Don't show if: + // - Still loading CLI status + // - CLI is already installed + // - User has dismissed it + // - Message threshold not met (if threshold is set) + if ( + cliInstalled === null || + cliInstalled === true || + dismissed || + (messageThreshold !== undefined && + (messageCount === undefined || messageCount < messageThreshold)) + ) { return null; } return (
- setDismissed(true)}> +
diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 08ca69498..0fabb5faf 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -46,6 +46,7 @@ import { ToolCallDiv } from "./ToolCallDiv"; import { FatalErrorIndicator } from "../../components/config/FatalErrorNotice"; import InlineErrorMessage from "../../components/mainInput/InlineErrorMessage"; import { cancelStream } from "../../redux/thunks/cancelStream"; +import { CliInstallBanner } from "../config/components/CliInstallBanner"; import { EmptyChatBody } from "./EmptyChatBody"; import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; import { useAutoScroll } from "./useAutoScroll"; @@ -380,6 +381,11 @@ export function Chat() { const showScrollbar = showChatScrollbar ?? window.innerHeight > 5000; + // Count user messages for CLI banner threshold + const userMessageCount = useMemo(() => { + return history.filter((item) => item.message.role === "user").length; + }, [history]); + return ( <> {!!showSessionTabs && !isInEdit && } @@ -421,6 +427,12 @@ export function Chat() { inputId={MAIN_EDITOR_INPUT_ID} /> + +