Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

22.5 KB
Raw Blame
import {
  act,
  fireEvent,
  render,
  screen,
  waitFor,
} from "@testing-library/react";
import { QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createTestQueryClient } from "../utils/testQueryClient";

const mocks = vi.hoisted(() => ({
  save: vi.fn(),
  settings: undefined as
    | {
        firstRunNoticeConfirmed?: boolean;
        newLayoutNoticeConfirmed?: boolean;
        onboardingTourSeen?: boolean;
        whatsNewSeenVersion?: string;
      }
    | undefined,
}));

vi.mock("@/lib/api", () => ({
  settingsApi: { save: mocks.save },
}));
vi.mock("@/lib/query", () => ({
  useSettingsQuery: () => ({ data: mocks.settings }),
}));
vi.mock("@tauri-apps/api/app", () => ({
  getVersion: async () => "4.0.25",
}));

import { FirstRunNoticeDialog } from "@/components/FirstRunNoticeDialog";
import { OnboardingTour, TOUR_STEPS } from "@/components/OnboardingTour";
import { WindowControlsContext } from "@/components/shell/AppPageHeader";
import { startOnboardingTour } from "@/lib/onboardingTour";

function withClient(ui: React.ReactNode) {
  const client = createTestQueryClient();
  return <QueryClientProvider client={client}>{ui}</QueryClientProvider>;
}

const next = () =>
  fireEvent.click(screen.getByRole("button", { name: "tutorial.next" }));

describe("FirstRunNoticeDialog", () => {
  beforeEach(() => {
    mocks.settings = { onboardingTourSeen: undefined };
    mocks.save.mockReset().mockResolvedValue(true);
  });

  it("walks a fresh install through the tour and records it once finished", async () => {
    const onBeforeOpen = vi.fn();
    const onNavigatePage = vi.fn();
    render(
      withClient(
        <FirstRunNoticeDialog
          onBeforeOpen={onBeforeOpen}
          onNavigatePage={onNavigatePage}
        />,
      ),
    );
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
    // 首次启动不切页面:设置是异步读到的,那时切会把用户刚点开的页面拽回去
    expect(onBeforeOpen).not.toHaveBeenCalled();
    expect(onNavigatePage).not.toHaveBeenCalled();

    // 第一步没有「上一步」,「跳过」统一放在小窗右上角
    expect(
      screen.queryByRole("button", { name: "tutorial.back" }),
    ).not.toBeInTheDocument();
    expect(
      screen.getAllByRole("button", { name: "tutorial.skip" }),
    ).toHaveLength(1);

    for (const step of TOUR_STEPS.slice(1)) {
      next();
      expect(screen.getByText(step.titleKey)).toBeInTheDocument();
    }

    expect(onNavigatePage.mock.calls.map((c) => c[0])).toEqual([
      "apps",
      "usage",
      "auth",
      "providers",
    ]);

    // 最后一步没有「下一步」和「跳过」,按钮换成「开始使用」
    expect(
      screen.queryByRole("button", { name: "tutorial.next" }),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByRole("button", { name: "tutorial.skip" }),
    ).not.toBeInTheDocument();
    expect(mocks.save).not.toHaveBeenCalled();

    fireEvent.click(screen.getByRole("button", { name: "tutorial.done" }));
    await waitFor(() =>
      expect(screen.queryByText("tutorial.more.title")).not.toBeInTheDocument(),
    );
    // 教程讲的就是当前版本,「界面改版了」和更新摘要一并记成已看
    expect(mocks.save).toHaveBeenCalledTimes(1);
    expect(mocks.save).toHaveBeenCalledWith(
      expect.objectContaining({
        onboardingTourSeen: true,
        firstRunNoticeConfirmed: true,
        newLayoutNoticeConfirmed: true,
        whatsNewSeenVersion: "4.0.25",
      }),
    );
  });

  it("records the tour as seen when skipped", async () => {
    render(withClient(<FirstRunNoticeDialog />));
    fireEvent.click(screen.getByRole("button", { name: "tutorial.skip" }));
    await waitFor(() =>
      expect(mocks.save).toHaveBeenCalledWith(
        expect.objectContaining({ onboardingTourSeen: true }),
      ),
    );
    expect(screen.queryByText("firstRunNotice.title")).not.toBeInTheDocument();
  });

  it("leaves the update summary for 4.x users to see after the tour", async () => {
    mocks.settings = {
      firstRunNoticeConfirmed: true,
      newLayoutNoticeConfirmed: true,
      whatsNewSeenVersion: "4.0.20",
    };
    render(withClient(<FirstRunNoticeDialog />));
    fireEvent.click(screen.getByRole("button", { name: "tutorial.skip" }));
    await waitFor(() => expect(mocks.save).toHaveBeenCalledTimes(1));
    expect(mocks.save).toHaveBeenCalledWith(
      expect.objectContaining({
        onboardingTourSeen: true,
        whatsNewSeenVersion: "4.0.20",
      }),
    );
  });

  it("records the update summary as seen for 3.x upgraders", async () => {
    mocks.settings = { firstRunNoticeConfirmed: true };
    render(withClient(<FirstRunNoticeDialog />));
    fireEvent.click(screen.getByRole("button", { name: "tutorial.skip" }));
    await waitFor(() =>
      expect(mocks.save).toHaveBeenCalledWith(
        expect.objectContaining({
          newLayoutNoticeConfirmed: true,
          whatsNewSeenVersion: "4.0.25",
        }),
      ),
    );
  });

  it("records the tour as seen when dismissed with Escape", async () => {
    render(withClient(<FirstRunNoticeDialog />));
    fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
    await waitFor(() => expect(mocks.save).toHaveBeenCalledTimes(1));
    expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
  });

  it("goes back a step", () => {
    render(withClient(<FirstRunNoticeDialog />));
    next();
    fireEvent.click(screen.getByRole("button", { name: "tutorial.back" }));
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
  });

  it("moves between steps with the arrow keys", () => {
    render(withClient(<FirstRunNoticeDialog />));
    const dialog = screen.getByRole("dialog");
    fireEvent.keyDown(dialog, { key: "ArrowRight" });
    expect(screen.getByText(TOUR_STEPS[1].titleKey)).toBeInTheDocument();
    fireEvent.keyDown(dialog, { key: "ArrowLeft" });
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
  });

  it("shows once to users who dismissed the old welcome dialog", () => {
    mocks.settings = { firstRunNoticeConfirmed: true };
    render(withClient(<FirstRunNoticeDialog />));
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
  });

  it("stays hidden once the tour was seen", () => {
    mocks.settings = {
      firstRunNoticeConfirmed: true,
      onboardingTourSeen: true,
    };
    render(withClient(<FirstRunNoticeDialog />));
    expect(screen.queryByText("firstRunNotice.title")).not.toBeInTheDocument();
  });

  it("stays hidden until settings load", () => {
    mocks.settings = undefined;
    render(withClient(<FirstRunNoticeDialog />));
    expect(screen.queryByText("firstRunNotice.title")).not.toBeInTheDocument();
  });

  it("replays on request without touching settings", async () => {
    mocks.settings = { onboardingTourSeen: true };
    const onBeforeOpen = vi.fn();
    render(withClient(<FirstRunNoticeDialog onBeforeOpen={onBeforeOpen} />));
    expect(screen.queryByRole("dialog")).not.toBeInTheDocument();

    act(() => startOnboardingTour());
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
    expect(onBeforeOpen).toHaveBeenCalledTimes(1);

    fireEvent.click(screen.getByRole("button", { name: "tutorial.skip" }));
    expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
    expect(mocks.save).not.toHaveBeenCalled();
  });
});

describe("OnboardingTour", () => {
  afterEach(() => {
    document.querySelectorAll("[data-tour]").forEach((el) => el.remove());
  });

  const spotlights = () => screen.queryAllByTestId("tour-spotlight");
  const spotlight = () => {
    const list = spotlights();
    expect(list.length).toBeGreaterThan(0);
    return list[0];
  };

  it("restarts from the first step when reopened", () => {
    const onClose = vi.fn();
    const { rerender } = render(<OnboardingTour open onClose={onClose} />);
    next();
    expect(screen.getByText(TOUR_STEPS[1].titleKey)).toBeInTheDocument();

    rerender(<OnboardingTour open={false} onClose={onClose} />);
    rerender(<OnboardingTour open onClose={onClose} />);
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
  });

  it("cuts the spotlight out around the anchored element", () => {
    const anchor = document.createElement("div");
    anchor.setAttribute("data-tour", "apps");
    anchor.getBoundingClientRect = () =>
      ({
        top: 50,
        left: 10,
        right: 210,
        bottom: 250,
        width: 200,
        height: 200,
      }) as DOMRect;
    document.body.appendChild(anchor);

    render(<OnboardingTour open onClose={vi.fn()} />);
    // 欢迎页没有要圈的元素:不渲染亮框
    expect(spotlights()).toHaveLength(0);

    next();
    expect(screen.getByText("tutorial.apps.title")).toBeInTheDocument();
    // 四周各外扩 6px
    expect(spotlight().style.top).toBe("44px");
    expect(spotlight().style.left).toBe("4px");
    expect(spotlight().style.width).toBe("212px");
    expect(spotlight().style.height).toBe("212px");
  });

  const addAnchor = (
    id: string,
    rect: Partial<DOMRect>,
    parent: HTMLElement = document.body,
  ) => {
    const el = document.createElement("div");
    el.setAttribute("data-tour", id);
    el.getBoundingClientRect = () =>
      ({
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        width: 0,
        height: 0,
        ...rect,
      }) as DOMRect;
    parent.appendChild(el);
    return el;
  };
  const goTo = (stepId: string) => {
    const target = TOUR_STEPS.findIndex((step) => step.id === stepId);
    for (let i = 0; i < target; i++) next();
  };

  it("drops the previous highlight when the next step has nothing to point at", () => {
    // 加供应商按钮在、模式切换不在(如 OpenCode):模式那一步不能还圈着按钮
    addAnchor("add-provider", {
      top: 10,
      left: 600,
      right: 700,
      bottom: 40,
      width: 100,
      height: 30,
    });
    render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("add");
    expect(spotlight().style.width).toBe("112px");

    next();
    expect(screen.getByText("tutorial.modes.title")).toBeInTheDocument();
    expect(spotlights()).toHaveLength(0);
    expect(screen.getByText("tutorial.modes.unavailable")).toBeInTheDocument();
  });

  it("opens on the centered welcome step after closing on a highlighted one", () => {
    addAnchor("nav-settings", {
      top: 400,
      left: 10,
      right: 100,
      bottom: 430,
      width: 90,
      height: 30,
    });
    const onClose = vi.fn();
    const { rerender } = render(<OnboardingTour open onClose={onClose} />);
    goTo("more");
    expect(spotlights()).toHaveLength(1);

    rerender(<OnboardingTour open={false} onClose={onClose} />);
    rerender(<OnboardingTour open onClose={onClose} />);
    expect(screen.getByText("firstRunNotice.title")).toBeInTheDocument();
    expect(spotlights()).toHaveLength(0);
  });

  it("points at the first provider card, or the empty list on a fresh install", () => {
    const empty = addAnchor("provider-empty", {
      top: 100,
      left: 220,
      right: 800,
      bottom: 400,
      width: 580,
      height: 300,
    });
    const { unmount } = render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("providers");
    expect(screen.getByText("tutorial.providers.empty")).toBeInTheDocument();
    expect(
      screen.queryByText("tutorial.providers.body"),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByText("firstRunNotice.bodyOfficial"),
    ).not.toBeInTheDocument();
    expect(spotlight().style.top).toBe("94px");
    unmount();
    empty.remove();

    addAnchor("provider-card", {
      top: 120,
      left: 220,
      right: 800,
      bottom: 180,
      width: 580,
      height: 60,
    });
    render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("providers");
    expect(screen.getByText("tutorial.providers.body")).toBeInTheDocument();
    expect(screen.getByText("firstRunNotice.bodyOfficial")).toBeInTheDocument();
    expect(
      screen.queryByText("tutorial.providers.empty"),
    ).not.toBeInTheDocument();
    expect(spotlight().style.top).toBe("114px");
  });

  it("highlights the mode switch when the app has one", () => {
    addAnchor("mode-tabs", {
      top: 60,
      left: 220,
      right: 420,
      bottom: 96,
      width: 200,
      height: 36,
    });
    render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("modes");
    expect(spotlight().style.width).toBe("212px");
    expect(screen.getByText("tutorial.modes.directTitle")).toBeInTheDocument();
    expect(screen.getByText("tutorial.modes.routeTitle")).toBeInTheDocument();
    expect(screen.getByText("tutorial.modes.stackTitle")).toBeInTheDocument();
    expect(
      screen.queryByText("tutorial.modes.unavailable"),
    ).not.toBeInTheDocument();
  });

  it("falls back to a centered card when the anchor is missing", () => {
    render(<OnboardingTour open onClose={vi.fn()} />);
    next();
    expect(screen.getByText("tutorial.apps.title")).toBeInTheDocument();
    expect(spotlights()).toHaveLength(0);
  });

  it("allows skipping via the header skip button on any step", () => {
    const onClose = vi.fn();
    render(<OnboardingTour open onClose={onClose} />);
    goTo("add");
    fireEvent.click(screen.getByRole("button", { name: "tutorial.skip" }));
    expect(onClose).toHaveBeenCalledTimes(1);
  });

  it("jumps directly to a step when clicking progress dots", () => {
    render(<OnboardingTour open onClose={vi.fn()} />);
    const dots = screen.getByRole("group").querySelectorAll("button");
    expect(dots).toHaveLength(TOUR_STEPS.length);
    fireEvent.click(dots[3]);
    expect(screen.getByText(TOUR_STEPS[3].titleKey)).toBeInTheDocument();
  });

  it("highlights Settings on the last step", () => {
    addAnchor("nav-settings", {
      top: 470,
      left: 102,
      right: 192,
      bottom: 498,
      width: 90,
      height: 28,
    });

    render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("more");
    const list = spotlights();
    expect(list).toHaveLength(1);
    expect(list[0].style.left).toBe("96px");
    expect(list[0].style.width).toBe("102px");
  });

  it("navigates across apps, usage, and auth steps and returns to providers on back or close", () => {
    const onNavigatePage = vi.fn();
    const onClose = vi.fn();
    addAnchor("apps-row", {
      top: 100,
      left: 224,
      right: 960,
      bottom: 160,
      width: 736,
      height: 60,
    });
    addAnchor("apps-actions", {
      top: 10,
      left: 780,
      right: 980,
      bottom: 42,
      width: 200,
      height: 32,
    });
    addAnchor("usage-filters", {
      top: 56,
      left: 224,
      right: 960,
      bottom: 96,
      width: 736,
      height: 40,
    });
    addAnchor("usage-actions", {
      top: 10,
      left: 800,
      right: 980,
      bottom: 42,
      width: 180,
      height: 32,
    });
    addAnchor("auth-toggles", {
      top: 68,
      left: 224,
      right: 960,
      bottom: 180,
      width: 736,
      height: 112,
    });
    addAnchor("auth-refresh", {
      top: 10,
      left: 860,
      right: 980,
      bottom: 42,
      width: 120,
      height: 32,
    });

    const { rerender } = render(
      <OnboardingTour open onClose={onClose} onNavigatePage={onNavigatePage} />,
    );
    expect(onNavigatePage).not.toHaveBeenCalled();

    goTo("appsPage");
    expect(onNavigatePage).toHaveBeenLastCalledWith("apps");
    expect(spotlights()).toHaveLength(2);

    // 从「应用」退回「共享资源」时切回供应商页
    fireEvent.click(screen.getByRole("button", { name: "tutorial.back" }));
    expect(onNavigatePage).toHaveBeenLastCalledWith("providers");

    next();
    expect(onNavigatePage).toHaveBeenLastCalledWith("apps");
    next();
    expect(onNavigatePage).toHaveBeenLastCalledWith("usage");
    expect(spotlights()).toHaveLength(2);

    next();
    expect(onNavigatePage).toHaveBeenLastCalledWith("auth");
    expect(spotlights()).toHaveLength(2);

    // 在授权中心步骤直接关闭时也切回供应商页
    rerender(
      <OnboardingTour
        open={false}
        onClose={onClose}
        onNavigatePage={onNavigatePage}
      />,
    );
    expect(onNavigatePage).toHaveBeenLastCalledWith("providers");
  });

  it("clips overflowing anchors to their scrollable ancestor bounds", () => {
    const scroller = document.createElement("div");
    scroller.style.overflowY = "auto";
    scroller.getBoundingClientRect = () =>
      ({
        top: 44,
        left: 0,
        right: 200,
        bottom: 300,
        width: 200,
        height: 256,
      }) as DOMRect;
    document.body.appendChild(scroller);

    addAnchor(
      "apps",
      {
        top: 44,
        left: 8,
        right: 192,
        bottom: 480,
        width: 184,
        height: 436,
      },
      scroller,
    );

    render(<OnboardingTour open onClose={vi.fn()} />);
    next();
    expect(spotlight().style.top).toBe("38px");
    expect(spotlight().style.height).toBe("268px");
    scroller.remove();
  });

  it("includes nav-plugins in the shared sidebar spotlight group", () => {
    addAnchor("nav-mcp", {
      top: 260,
      left: 8,
      right: 192,
      bottom: 288,
      width: 184,
      height: 28,
    });
    addAnchor("nav-skills", {
      top: 288,
      left: 8,
      right: 192,
      bottom: 316,
      width: 184,
      height: 28,
    });
    addAnchor("nav-plugins", {
      top: 316,
      left: 8,
      right: 192,
      bottom: 344,
      width: 184,
      height: 28,
    });
    addAnchor("nav-prompts", {
      top: 344,
      left: 8,
      right: 192,
      bottom: 372,
      width: 184,
      height: 28,
    });
    addAnchor("nav-sessions", {
      top: 372,
      left: 8,
      right: 192,
      bottom: 400,
      width: 184,
      height: 28,
    });

    render(<OnboardingTour open onClose={vi.fn()} />);
    goTo("shared");
    const box = spotlight();
    expect(box.style.top).toBe("254px");
    expect(box.style.height).toBe("152px");
  });

  it("renders ModalDragStrip window controls in the overlay outside the dialog card", () => {
    render(
      <WindowControlsContext.Provider
        value={<button type="button">WindowClose</button>}
      >
        <OnboardingTour open onClose={vi.fn()} />
      </WindowControlsContext.Provider>,
    );
    const closeWin = screen.getByRole("button", { name: "WindowClose" });
    const overlay = screen.getByTestId("onboarding-tour");
    const dialog = screen.getByRole("dialog");
    expect(overlay).toContainElement(closeWin);
    expect(dialog).not.toContainElement(closeWin);
  });

  it("handles navigation keys globally and blocks Ctrl+F, Ctrl+, and Ctrl+\\ while open", () => {
    const onClose = vi.fn();
    render(<OnboardingTour open onClose={onClose} />);

    fireEvent.keyDown(document.body, { key: "ArrowRight" });
    expect(screen.getByText(TOUR_STEPS[1].titleKey)).toBeInTheDocument();

    fireEvent.keyDown(document.body, { key: "ArrowLeft" });
    expect(screen.getByText(TOUR_STEPS[0].titleKey)).toBeInTheDocument();

    for (const key of ["f", ",", "\\"]) {
      const blocked = new KeyboardEvent("keydown", {
        key,
        ctrlKey: true,
        bubbles: true,
        cancelable: true,
      });
      window.dispatchEvent(blocked);
      expect(blocked.defaultPrevented).toBe(true);
    }

    fireEvent.keyDown(document.body, { key: "Escape" });
    expect(onClose).toHaveBeenCalledTimes(1);
  });

  it("highlights both the app list and the bottom Apps button on the apps step", () => {
    addAnchor("apps", {
      top: 50,
      left: 8,
      right: 192,
      bottom: 250,
      width: 184,
      height: 200,
    });
    addAnchor("nav-apps", {
      top: 520,
      left: 8,
      right: 100,
      bottom: 548,
      width: 92,
      height: 28,
    });

    render(<OnboardingTour open onClose={vi.fn()} />);
    next();
    const list = spotlights();
    expect(list).toHaveLength(2);
    expect(list[0].style.top).toBe("44px");
    expect(list[1].style.top).toBe("514px");

    const dialog = screen.getByRole("dialog");
    expect(dialog).toHaveAttribute("aria-describedby");
    expect(
      document.getElementById(dialog.getAttribute("aria-describedby")!),
    ).toHaveTextContent("tutorial.apps.body");
  });

  it("keeps Tab trapped and Escape working after clicking the backdrop or dialog card", () => {
    const onClose = vi.fn();
    render(
      <>
        <button type="button">OutsideAction</button>
        <OnboardingTour open onClose={onClose} />
      </>,
    );

    const outside = screen.getByRole("button", { name: "OutsideAction" });
    const dialog = screen.getByRole("dialog");
    const backdrop = screen.getByTestId("tour-backdrop");
    const primary = screen.getByRole("button", { name: "tutorial.next" });

    expect(primary).toHaveFocus();

    const mouseDown = new MouseEvent("mousedown", {
      bubbles: true,
      cancelable: true,
    });
    backdrop.dispatchEvent(mouseDown);
    expect(mouseDown.defaultPrevented).toBe(true);
    fireEvent.click(backdrop);
    expect(primary).toHaveFocus();

    fireEvent.keyDown(document.body, { key: "Tab" });
    expect(dialog).toContainElement(document.activeElement as HTMLElement);
    expect(outside).not.toHaveFocus();

    // 即使点击卡片非按钮区域使焦点落到卡片容器或 body,Tab 仍会回到卡片按钮内,Esc 仍能关闭
    dialog.focus();
    expect(dialog).toHaveFocus();
    fireEvent.keyDown(dialog, { key: "Tab" });
    expect(Array.from(dialog.querySelectorAll("button"))).toContain(
      document.activeElement,
    );

    fireEvent.keyDown(document.body, { key: "Tab", shiftKey: true });
    expect(Array.from(dialog.querySelectorAll("button"))).toContain(
      document.activeElement,
    );

    fireEvent.keyDown(dialog, { key: "Escape" });
    expect(onClose).toHaveBeenCalledTimes(1);
  });

  it("snaps centered card coordinates to whole pixels on odd viewports", () => {
    const prevWidth = window.innerWidth;
    const prevHeight = window.innerHeight;
    Object.defineProperty(window, "innerWidth", {
      configurable: true,
      value: 1001,
    });
    Object.defineProperty(window, "innerHeight", {
      configurable: true,
      value: 701,
    });
    try {
      render(<OnboardingTour open onClose={vi.fn()} />);
      const dialog = screen.getByRole("dialog");
      expect(dialog.style.left).toBe("321px");
      expect(dialog.style.top).toBe("241px");
    } finally {
      Object.defineProperty(window, "innerWidth", {
        configurable: true,
        value: prevWidth,
      });
      Object.defineProperty(window, "innerHeight", {
        configurable: true,
        value: prevHeight,
      });
    }
  });
});