File size: 4,171 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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(<LoopModeSelector />);

    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(<LoopModeSelector />);

    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(<LoopModeSelector />);

    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(<LoopModeSelector />);

    expect(screen.getByText("loop.running")).toBeInTheDocument();
  });

  it("shows that an active mode is waiting for the user", () => {
    useLoopStore.getState().setSessionMode(custom, "awaiting_user");

    renderWithProviders(<LoopModeSelector />);

    expect(screen.getByText("loop.awaiting_user")).toBeInTheDocument();
  });
});