File size: 5,083 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useEffect } from "react";
import { fireEvent, screen, within } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { Form } from "@agentscope-ai/design";
import type { FormInstance } from "antd";
import type { CustomLoopModeConfig } from "@/api/types";
import { renderWithProviders } from "@/test/common_setup";
import { AgentLoopCard, buildCustomLoopMode } from "./AgentLoopCard";

vi.mock("@agentscope-ai/design", async () =>
  vi.importActual<typeof import("antd")>("antd"),
);

vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (_key: string, fallback?: string) => fallback || _key,
  }),
}));

function LoopForm({
  modes = [],
  onForm,
}: {
  modes?: CustomLoopModeConfig[];
  onForm?: (form: FormInstance) => void;
}) {
  const [form] = Form.useForm();
  useEffect(() => {
    onForm?.(form);
  }, [form, onForm]);

  return (
    <Form form={form} initialValues={{ loop: { custom_modes: modes } }}>
      <AgentLoopCard />
    </Form>
  );
}

describe("AgentLoopCard custom mode rendering", () => {
  it("shows a newly created template and its preset gates immediately", async () => {
    let form: FormInstance | undefined;
    renderWithProviders(<LoopForm onForm={(next) => (form = next)} />);

    fireEvent.click(screen.getByLabelText("Create custom loop mode"));
    fireEvent.click(screen.getByRole("button", { name: "OK" }));

    expect(
      await screen.findByRole("tab", { name: "New Loop Mode" }),
    ).toHaveAttribute("aria-selected", "true");
    const editor = within(screen.getByRole("tabpanel"));
    expect(editor.getByText("Iteration limit")).toBeInTheDocument();
    expect(editor.getByText("Token budget")).toBeInTheDocument();
    expect(editor.getByText("Repetition protection")).toBeInTheDocument();
    expect(
      editor.getByText("Qualitative completion check"),
    ).toBeInTheDocument();
    expect(
      editor.queryByText("Available to this agent"),
    ).not.toBeInTheDocument();
    expect(form?.getFieldValue(["loop", "custom_modes", 0, "enabled"])).toBe(
      true,
    );
  }, 15_000);

  it("opens Gate choices from the plus button and enables a blank mode", async () => {
    let form: FormInstance | undefined;
    renderWithProviders(<LoopForm onForm={(next) => (form = next)} />);

    fireEvent.click(screen.getByLabelText("Create custom loop mode"));
    fireEvent.mouseDown(screen.getByRole("combobox"));
    fireEvent.click(await screen.findByText("Blank pipeline"));
    fireEvent.click(screen.getByRole("button", { name: "OK" }));

    const editor = within(await screen.findByRole("tabpanel"));
    expect(form?.getFieldValue(["loop", "custom_modes", 0, "enabled"])).toBe(
      false,
    );

    fireEvent.click(editor.getByRole("button", { name: "Add gate" }));
    fireEvent.click(
      await screen.findByRole("menuitem", { name: "Iteration limit" }),
    );

    expect(editor.getByText("Iteration limit")).toBeInTheDocument();
    expect(form?.getFieldValue(["loop", "custom_modes", 0, "enabled"])).toBe(
      true,
    );
  }, 15_000);

  it("renders Mission defaults as three separate gates", async () => {
    renderWithProviders(<LoopForm />);

    fireEvent.click(screen.getByRole("tab", { name: "Mission" }));

    const mission = within(screen.getByRole("tabpanel"));
    expect(
      mission.getByRole("button", { name: /Mission iteration limit/ }),
    ).toBeInTheDocument();
    expect(
      mission.getByRole("button", { name: /Worker attempts/ }),
    ).toBeInTheDocument();
    const verificationGate = mission.getByRole("button", {
      name: /Mission verification policy/,
    });
    expect(verificationGate).toBeInTheDocument();
    expect(
      mission.queryByText("Verification guidance (optional)"),
    ).not.toBeInTheDocument();

    fireEvent.click(verificationGate);

    expect(
      mission.getByText("Verification guidance (optional)"),
    ).toBeInTheDocument();
    expect(
      mission.getByText("Default test command (optional)"),
    ).toBeInTheDocument();
  }, 15_000);

  it("updates a per-tool call limit name", async () => {
    let form: FormInstance | undefined;
    const mode = buildCustomLoopMode([], "Research", "research", "research", 1);
    const budget = mode.gates.find((gate) => gate.type === "tool_call_budget");
    if (!budget) throw new Error("Research mode must include a tool budget");
    budget.params.per_tool = { "tool-name": 3 };

    renderWithProviders(
      <LoopForm modes={[mode]} onForm={(next) => (form = next)} />,
    );

    fireEvent.click(screen.getByRole("tab", { name: "Research" }));
    fireEvent.click(await screen.findByText("Tool-call budget"));
    const toolName = screen.getByLabelText("Tool name");
    fireEvent.change(toolName, { target: { value: "search" } });

    expect(toolName).toHaveValue("search");

    fireEvent.blur(toolName);

    expect(
      form?.getFieldValue([
        "loop",
        "custom_modes",
        0,
        "gates",
        2,
        "params",
        "per_tool",
      ]),
    ).toEqual({ search: 3 });
  }, 15_000);
});