File size: 8,598 Bytes
96e86e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// @vitest-environment jsdom

import { act } from "react";
import type { ReactNode } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Agent } from "@penclipai/shared";
import { AgentConfigForm } from "./AgentConfigForm";

const translations: Record<string, string> = {
  "agentConfig.identity": "身份信息",
  "agentConfig.name": "名称",
  "agentConfig.agentName": "智能体名称",
  "agentConfig.title": "头衔",
  "agentConfig.titlePlaceholder": "例如:工程副总裁",
  "agentConfig.reportsTo": "汇报对象",
  "agentConfig.chooseManager": "选择上级…",
  "agentConfig.capabilities": "能力",
  "agentConfig.capabilitiesPlaceholder": "描述这个智能体能做什么...",
  "agentConfig.adapter": "适配器",
  "agentConfig.testEnvironment": "检测环境",
  "agentConfig.permissionsAndConfiguration": "权限与配置",
  "agentConfig.command": "命令",
  "agentConfig.model": "模型",
  "agentConfig.thinkingEffort": "思考强度",
  "agentConfig.default": "默认",
  "agentConfig.extraArgs": "额外参数",
  "agentConfig.extraArgsPlaceholder": "用逗号分隔,例如 --verbose, --search",
  "agentConfig.environmentVariables": "环境变量",
  "agentConfig.timeoutSec": "超时时间(秒)",
  "agentConfig.graceSec": "宽限时间(秒)",
  "agentConfig.runPolicy": "运行策略",
  "agentConfig.heartbeatOnInterval": "按间隔触发心跳",
  "agentConfig.advancedRunPolicy": "高级运行策略",
  "agentConfig.wakeOnDemand": "按需唤醒",
  "agentConfig.cooldownSec": "冷却时间(秒)",
  "agentConfig.maxConcurrentRuns": "最大并发运行数",
  "agentConfig.auto": "自动",
};

vi.mock("react-i18next", async (importOriginal) => {
  const actual = await importOriginal<typeof import("react-i18next")>();
  return {
    ...actual,
    useTranslation: () => ({
      t: (key: string, options?: Record<string, unknown>) => translations[key] ?? String(options?.defaultValue ?? key),
    }),
  };
});

const mockAgentsApi = vi.hoisted(() => ({
  list: vi.fn(),
  adapterModels: vi.fn(),
  detectModel: vi.fn(),
  testEnvironment: vi.fn(),
}));

const mockSecretsApi = vi.hoisted(() => ({
  list: vi.fn(),
  create: vi.fn(),
}));

const mockAssetsApi = vi.hoisted(() => ({
  uploadImage: vi.fn(),
}));

vi.mock("../api/agents", () => ({
  agentsApi: mockAgentsApi,
}));

vi.mock("../api/secrets", () => ({
  secretsApi: mockSecretsApi,
}));

vi.mock("../api/assets", () => ({
  assetsApi: mockAssetsApi,
}));

vi.mock("../context/CompanyContext", () => ({
  useCompany: () => ({
    selectedCompanyId: "company-1",
  }),
}));

vi.mock("../adapters", () => ({
  getUIAdapter: () => ({
    ConfigFields: () => null,
    buildAdapterConfig: () => ({}),
  }),
}));

vi.mock("../adapters/metadata", () => ({
  listAdapterOptions: () => [{ value: "claude_local", label: "Claude Code (local)" }],
  listVisibleAdapterTypes: () => ["claude_local"],
}));

vi.mock("../adapters/adapter-display-registry", () => ({
  getAdapterLabel: () => "Claude Code (local)",
}));

vi.mock("../adapters/use-disabled-adapters", () => ({
  useDisabledAdaptersSync: () => new Set<string>(),
}));

vi.mock("../adapters/claude-local/config-fields", () => ({
  ClaudeLocalAdvancedFields: () => null,
}));

vi.mock("./MarkdownEditor", () => ({
  MarkdownEditor: ({ placeholder }: { placeholder?: string }) => <textarea placeholder={placeholder} />,
}));

vi.mock("./ReportsToPicker", () => ({
  ReportsToPicker: ({ chooseLabel }: { chooseLabel: string }) => <button type="button">{chooseLabel}</button>,
}));

vi.mock("./EnvVarEditor", () => ({
  EnvVarEditor: () => <div>ENV</div>,
}));

vi.mock("./PathInstructionsModal", () => ({
  ChoosePathButton: () => <button type="button">path</button>,
}));

vi.mock("@/components/ui/popover", () => ({
  Popover: ({ children }: { children: ReactNode }) => <div>{children}</div>,
  PopoverTrigger: ({ children }: { children: ReactNode }) => <>{children}</>,
  PopoverContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
}));

vi.mock("@/components/ui/tooltip", () => ({
  Tooltip: ({ children }: { children: ReactNode }) => <div>{children}</div>,
  TooltipTrigger: ({ children }: { children: ReactNode }) => <>{children}</>,
  TooltipContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
}));

vi.mock("@/components/ui/toggle-switch", () => ({
  ToggleSwitch: ({ checked }: { checked: boolean }) => <button type="button">{checked ? "on" : "off"}</button>,
}));

vi.mock("@/components/ui/button", () => ({
  Button: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => <button type="button" {...props}>{children}</button>,
}));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;

async function flush() {
  await act(async () => {
    await new Promise((resolve) => setTimeout(resolve, 0));
  });
}

function createAgent(): Agent {
  return {
    id: "agent-1",
    companyId: "company-1",
    name: "CEO",
    role: "ceo",
    title: "VP of Engineering",
    status: "active",
    adapterType: "claude_local",
    adapterConfig: {
      command: "claude",
      model: "",
      env: {},
      timeoutSec: 60,
      graceSec: 15,
    },
    runtimeConfig: {
      heartbeat: {
        enabled: true,
        intervalSec: 300,
        wakeOnDemand: true,
        cooldownSec: 10,
        maxConcurrentRuns: 1,
      },
    },
    reportsTo: null,
    capabilities: "",
    promptTemplate: null,
    budgetMonthlyCents: 0,
    lastHeartbeatAt: null,
    createdAt: new Date("2026-04-01T00:00:00.000Z"),
    updatedAt: new Date("2026-04-01T00:00:00.000Z"),
    hiddenAt: null,
    icon: null,
    permissions: null,
  } as unknown as Agent;
}

function renderForm(container: HTMLDivElement) {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
    },
  });
  const root = createRoot(container);
  act(() => {
    root.render(
      <QueryClientProvider client={queryClient}>
        <AgentConfigForm
          mode="edit"
          agent={createAgent()}
          onSave={vi.fn()}
          sectionLayout="cards"
        />
      </QueryClientProvider>,
    );
  });
  return root;
}

describe("AgentConfigForm localization", () => {
  let container: HTMLDivElement;

  beforeEach(() => {
    container = document.createElement("div");
    document.body.appendChild(container);
    mockAgentsApi.list.mockResolvedValue([]);
    mockAgentsApi.adapterModels.mockResolvedValue([]);
    mockAgentsApi.detectModel.mockResolvedValue({ model: null, candidates: [] });
    mockSecretsApi.list.mockResolvedValue([]);
    mockAssetsApi.uploadImage.mockResolvedValue({ contentPath: "/tmp/image.png" });
  });

  afterEach(() => {
    document.body.innerHTML = "";
  });

  it("renders key configuration labels in Chinese instead of regressed English", async () => {
    const root = renderForm(container);
    await flush();

    expect(container.textContent).toContain("身份信息");
    expect(container.textContent).toContain("名称");
    expect(container.textContent).toContain("头衔");
    expect(container.textContent).toContain("汇报对象");
    expect(container.textContent).toContain("能力");
    expect(container.textContent).toContain("适配器");
    expect(container.textContent).toContain("检测环境");
    expect(container.textContent).toContain("权限与配置");
    expect(container.textContent).toContain("命令");
    expect(container.textContent).toContain("环境变量");
    expect(container.textContent).toContain("超时时间(秒)");
    expect(container.textContent).toContain("运行策略");
    expect(container.textContent).toContain("按间隔触发心跳");
    expect(container.textContent).toContain("高级运行策略");
    const placeholders = Array.from(container.querySelectorAll("input, textarea"))
      .map((element) => element.getAttribute("placeholder"))
      .filter(Boolean);
    expect(placeholders).toContain("智能体名称");
    expect(placeholders).toContain("例如:工程副总裁");
    expect(placeholders).toContain("描述这个智能体能做什么...");

    expect(container.textContent).not.toContain("Identity");
    expect(container.textContent).not.toContain("Reports to");
    expect(container.textContent).not.toContain("Permissions & Configuration");

    act(() => root.unmount());
  });
});