import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "@/test/common_setup";
import {
DEFAULT_LOOP_MODE,
type LoopModeInfo,
useLoopStore,
} from "../../stores/loopStore";
import { LoopModeSelector } from "./LoopModeSelector";
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string) => key,
i18n: { language: "zh" },
}),
}));
const goal: LoopModeInfo = {
id: "goal",
name: "goal",
slash_command: "goal",
description: "Backend goal description",
source: "builtin",
};
const custom: LoopModeInfo = {
id: "custom:quality",
name: "Quality Review",
slash_command: "quality",
description: "Keep the user's original description.",
source: "custom",
};
const ompUltraqa: LoopModeInfo = {
id: "plugin:ultraqa",
name: "ultraqa",
slash_command: "ultraqa",
description:
"**UltraQA** — automated QA cycle engine\n\n" +
"Usage:\n" +
' `/ultraqa [--tests|--build|--lint|--typecheck|--custom "cmd"]`\n',
name_i18n: {
en: "UltraQA",
"zh-CN": "UltraQA",
},
description_i18n: {
en: "**UltraQA** — automated QA cycle engine",
"zh-CN": "**UltraQA** — 自动化 QA 循环引擎",
},
source: "plugin",
};
describe("LoopModeSelector", () => {
beforeEach(() => {
useLoopStore.setState({
selectedModeId: "default",
availableModes: [DEFAULT_LOOP_MODE, goal, custom, ompUltraqa],
sessionState: "idle",
activeMode: null,
catalogLoading: false,
catalogError: false,
});
});
it("shows localized built-ins and verbatim custom modes", async () => {
const user = userEvent.setup();
renderWithProviders();
await user.click(screen.getByRole("button", { name: "loop.selectorAria" }));
expect(
screen.getAllByText("loop.modes.default.name").length,
).toBeGreaterThan(0);
expect(screen.getByText("loop.modes.goal.description")).toBeInTheDocument();
expect(screen.getByText("Quality Review")).toBeInTheDocument();
expect(
screen.getByText("Keep the user's original description."),
).toBeInTheDocument();
expect(
screen.queryByText("Backend goal description"),
).not.toBeInTheDocument();
const ultraqaLabels = screen.getAllByText("UltraQA");
expect(ultraqaLabels.length).toBeGreaterThanOrEqual(2);
expect(ultraqaLabels.some((node) => node.tagName === "STRONG")).toBe(true);
expect(screen.getByText(/自动化 QA 循环引擎/)).toBeInTheDocument();
expect(
screen.queryByText(/automated QA cycle engine/),
).not.toBeInTheDocument();
expect(screen.queryByText(/\*\*UltraQA\*\*/)).not.toBeInTheDocument();
expect(screen.queryByText(/`\/ultraqa/)).not.toBeInTheDocument();
});
it("selects a custom mode from the compact menu", async () => {
const user = userEvent.setup();
renderWithProviders();
await user.click(screen.getByRole("button", { name: "loop.selectorAria" }));
await user.click(screen.getByText("Quality Review"));
expect(useLoopStore.getState().selectedModeId).toBe("custom:quality");
});
it("shows starting before the first response event", () => {
useLoopStore.getState().setStartingMode(custom);
renderWithProviders();
expect(screen.getByText("Quality Review")).toBeInTheDocument();
expect(screen.getByText("loop.starting")).toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "loop.selectorAria" }),
).not.toBeInTheDocument();
});
it("shows running after the first response event", () => {
useLoopStore.getState().setSessionMode(custom, "running");
renderWithProviders();
expect(screen.getByText("loop.running")).toBeInTheDocument();
});
it("shows that an active mode is waiting for the user", () => {
useLoopStore.getState().setSessionMode(custom, "awaiting_user");
renderWithProviders();
expect(screen.getByText("loop.awaiting_user")).toBeInTheDocument();
});
});