|
12 | 12 | * - saving a new channel sends exactly one POST (no PATCH churn) |
13 | 13 | */ |
14 | 14 |
|
| 15 | +import "@testing-library/jest-dom/vitest"; |
15 | 16 | import { describe, it, expect, vi, beforeEach } from "vitest"; |
16 | 17 | import { render, screen, waitFor } from "@testing-library/react"; |
17 | 18 | import userEvent from "@testing-library/user-event"; |
@@ -305,6 +306,137 @@ describe("<ChannelsPanel /> create-flow default", () => { |
305 | 306 | expectBefore(displaySettings, groupPolicy); |
306 | 307 | }); |
307 | 308 |
|
| 309 | + async function openQqEditDrawer(groupContext?: Record<string, unknown>) { |
| 310 | + const row = { id: "qq-policy", kind: "qq", name: "qq", enabled: true }; |
| 311 | + api.mockImplementation(async (url, init) => { |
| 312 | + if (init?.method === "PATCH") return row; |
| 313 | + if (url.endsWith("/qq-policy")) { |
| 314 | + return { |
| 315 | + ...row, |
| 316 | + config: { |
| 317 | + app_id: "offline-app", |
| 318 | + secret: "offline-secret", |
| 319 | + group_context: groupContext, |
| 320 | + }, |
| 321 | + }; |
| 322 | + } |
| 323 | + return [row]; |
| 324 | + }); |
| 325 | + render(<ChannelsPanel agentId="ag1" />); |
| 326 | + await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]); |
| 327 | + } |
| 328 | + |
| 329 | + it.each([undefined, true, false])( |
| 330 | + "round-trips QQ command protection %s and the owner allowlist", |
| 331 | + async (ownerOnly) => { |
| 332 | + await openQqEditDrawer({ |
| 333 | + enabled: true, |
| 334 | + owner_ids: ["owner-1"], |
| 335 | + ...(ownerOnly === undefined ? {} : { owner_commands_only: ownerOnly }), |
| 336 | + }); |
| 337 | + const toggle = await screen.findByLabelText( |
| 338 | + "channels.qqGroupOwnerCommandsOnly", |
| 339 | + ); |
| 340 | + await waitFor(() => { |
| 341 | + expect(toggle.getAttribute("aria-checked")).toBe( |
| 342 | + String(ownerOnly ?? true), |
| 343 | + ); |
| 344 | + expect(screen.getByText("owner-1")).toBeInTheDocument(); |
| 345 | + }); |
| 346 | + const owners = screen.getByRole("combobox", { |
| 347 | + name: "channels.qqGroupOwnerIds", |
| 348 | + }); |
| 349 | + if (ownerOnly === false) expect(owners).toBeDisabled(); |
| 350 | + else expect(owners).toBeEnabled(); |
| 351 | + await userEvent.click( |
| 352 | + screen.getByRole("button", { name: "common.save" }), |
| 353 | + ); |
| 354 | + await waitFor(() => { |
| 355 | + const patch = api.mock.calls.find( |
| 356 | + ([, init]) => init?.method === "PATCH", |
| 357 | + ); |
| 358 | + expect(patch).toBeDefined(); |
| 359 | + expect( |
| 360 | + JSON.parse(String(patch![1]!.body)).config.group_context, |
| 361 | + ).toMatchObject({ |
| 362 | + enabled: true, |
| 363 | + owner_commands_only: ownerOnly ?? true, |
| 364 | + owner_ids: ["owner-1"], |
| 365 | + }); |
| 366 | + }); |
| 367 | + }, |
| 368 | + ); |
| 369 | + |
| 370 | + it("keeps the shared allowlist enabled when only tool protection is on", async () => { |
| 371 | + await openQqEditDrawer({ |
| 372 | + enabled: true, |
| 373 | + owner_commands_only: false, |
| 374 | + owner_tools_only: true, |
| 375 | + owner_ids: ["owner-1"], |
| 376 | + }); |
| 377 | + const owners = await screen.findByRole("combobox", { |
| 378 | + name: "channels.qqGroupOwnerIds", |
| 379 | + }); |
| 380 | + expect(owners).toBeEnabled(); |
| 381 | + await userEvent.click( |
| 382 | + screen.getByLabelText("channels.qqGroupOwnerToolsOnly"), |
| 383 | + ); |
| 384 | + expect(owners).toBeDisabled(); |
| 385 | + await userEvent.click(screen.getByRole("button", { name: "common.save" })); |
| 386 | + await waitFor(() => { |
| 387 | + const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH"); |
| 388 | + expect( |
| 389 | + JSON.parse(String(patch![1]!.body)).config.group_context, |
| 390 | + ).toMatchObject({ |
| 391 | + owner_commands_only: false, |
| 392 | + owner_tools_only: false, |
| 393 | + owner_ids: ["owner-1"], |
| 394 | + }); |
| 395 | + }); |
| 396 | + }); |
| 397 | + |
| 398 | + it("enables tool protection for new QQ channels", async () => { |
| 399 | + render(<ChannelsPanel agentId="ag1" />); |
| 400 | + await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]); |
| 401 | + expect( |
| 402 | + await screen.findByLabelText("channels.qqGroupOwnerToolsOnly"), |
| 403 | + ).toBeChecked(); |
| 404 | + }); |
| 405 | + |
| 406 | + it("hides QQ command controls while group context is off", async () => { |
| 407 | + await openQqEditDrawer({ enabled: false, owner_ids: ["stored-owner"] }); |
| 408 | + expect( |
| 409 | + screen.queryByLabelText("channels.qqGroupOwnerCommandsOnly"), |
| 410 | + ).not.toBeInTheDocument(); |
| 411 | + expect( |
| 412 | + screen.queryByLabelText("channels.qqGroupOwnerToolsOnly"), |
| 413 | + ).not.toBeInTheDocument(); |
| 414 | + expect( |
| 415 | + screen.queryByRole("combobox", { name: "channels.qqGroupOwnerIds" }), |
| 416 | + ).not.toBeInTheDocument(); |
| 417 | + |
| 418 | + await userEvent.click( |
| 419 | + screen.getByLabelText("channels.qqGroupContextEnabled"), |
| 420 | + ); |
| 421 | + const toggle = await screen.findByLabelText( |
| 422 | + "channels.qqGroupOwnerCommandsOnly", |
| 423 | + ); |
| 424 | + expect(toggle).toBeChecked(); |
| 425 | + expect(await screen.findByText("stored-owner")).toBeInTheDocument(); |
| 426 | + await userEvent.click(screen.getByRole("button", { name: "common.save" })); |
| 427 | + await waitFor(() => { |
| 428 | + const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH"); |
| 429 | + expect(patch).toBeDefined(); |
| 430 | + expect( |
| 431 | + JSON.parse(String(patch![1]!.body)).config.group_context, |
| 432 | + ).toMatchObject({ |
| 433 | + enabled: true, |
| 434 | + owner_commands_only: true, |
| 435 | + owner_ids: ["stored-owner"], |
| 436 | + }); |
| 437 | + }); |
| 438 | + }); |
| 439 | + |
308 | 440 | it("edits a CLI-created QQ channel whose secret is stored as client_secret", async () => { |
309 | 441 | const row = { |
310 | 442 | id: "qq-channel-2", |
|
0 commit comments