fix(qq): clarify QR binding states and setup actions (#6340)
* fix(qq): clarify QR binding states and setup actions * fix(qq): confirm rebinding instead of showing a persistent warning * fix(qq): make rebind credential warning conditional * docs(qq): align rebind guide with conditional warning
This commit is contained in:
1 parent
51a0fc83a2
commit
130f4edefe
27 files changed
+634
-168
No files matched your search
@@ -239,16 +239,20 @@ it("previews QQ QR states and retries without connecting to the portal", async (
|
||||
render(<Gallery />);
|
||||
await screen.findByRole("heading", { name: "QQConnectFlowView · QQ QR connection" });
|
||||
const preview = within(screen.getByRole("region", { name: "Component preview" }));
|
||||
expect(await preview.findByRole("img", { name: "QQ login QR code" })).toBeVisible();
|
||||
expect(await preview.findByRole("img", { name: "QQ binding QR code" })).toBeVisible();
|
||||
await userEvent.click(preview.getByRole("button", { name: "Cancel" }));
|
||||
expect(preview.queryByRole("img", { name: "QQ login QR code" })).not.toBeInTheDocument();
|
||||
await userEvent.click(preview.getByRole("button", { name: "Connect" }));
|
||||
expect(await preview.findByRole("img", { name: "QQ login QR code" })).toBeVisible();
|
||||
expect(preview.queryByRole("img", { name: "QQ binding QR code" })).not.toBeInTheDocument();
|
||||
await userEvent.click(preview.getByRole("button", { name: "Bind QQ bot" }));
|
||||
expect(await preview.findByRole("img", { name: "QQ binding QR code" })).toBeVisible();
|
||||
await userEvent.click(preview.getByRole("combobox", { name: "QQ connection state" }));
|
||||
await userEvent.click(screen.getByRole("option", { name: "succeeded" }));
|
||||
expect(preview.getByText("QQ is connected.")).toBeVisible();
|
||||
expect(preview.getByText(/previous App Secret stops working/)).toBeVisible();
|
||||
expect(preview.getByRole("button", { name: "Scan again" })).toBeVisible();
|
||||
expect(preview.getByText("Configured")).toBeVisible();
|
||||
expect(preview.queryByText(/Rebinding may interrupt other apps/)).not.toBeInTheDocument();
|
||||
await userEvent.click(preview.getByRole("button", { name: "Rebind bot" }));
|
||||
const confirmation = screen.getByRole("alertdialog", { name: "Rebind bot" });
|
||||
expect(confirmation).toHaveTextContent("Rebinding may interrupt other apps using this QQ bot. If the App Secret is reset, the old secret becomes invalid and those apps need the new secret.");
|
||||
await userEvent.click(within(confirmation).getByRole("button", { name: "Cancel" }));
|
||||
expect(preview.getByText("Configured")).toBeVisible();
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch.mock.calls[0][0]).toBe("/__nanobot_components");
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import QRCode from "qrcode";
|
||||
|
||||
import i18n from "@/i18n";
|
||||
import type { ChannelConnectPayload, NanobotFeatureInfo } from "@/lib/types";
|
||||
|
||||
import { QQConnectFlow } from "../../../nanobot/channels/qq/webui/QQConnectFlow";
|
||||
import { QQConnectFlowView } from "../../../nanobot/channels/qq/webui/QQConnectFlowView";
|
||||
|
||||
const { requestMutation } = vi.hoisted(() => ({ requestMutation: vi.fn() }));
|
||||
vi.mock("@/providers/ClientProvider", () => {
|
||||
const client = { requestMutation };
|
||||
return { useClient: () => ({ client }) };
|
||||
});
|
||||
|
||||
const pending: ChannelConnectPayload = {
|
||||
session_id: "qq-test",
|
||||
status: "pending",
|
||||
qr_url: "https://example.invalid/qq-bind",
|
||||
retrying: false,
|
||||
};
|
||||
const actions = () => ({
|
||||
busy: false,
|
||||
error: null,
|
||||
onStart: vi.fn(),
|
||||
onCancel: vi.fn(),
|
||||
onPoll: vi.fn(async () => null),
|
||||
onManualSetup: vi.fn(),
|
||||
});
|
||||
|
||||
beforeEach(() => requestMutation.mockReset());
|
||||
afterEach(() => { cleanup(); vi.useRealTimers(); vi.restoreAllMocks(); });
|
||||
|
||||
it("keeps the QR and manual setup available while retrying, then returns to waiting", async () => {
|
||||
const props = actions();
|
||||
const { rerender } = render(<QQConnectFlowView {...props} connect={pending} />);
|
||||
const qr = await screen.findByRole("img", { name: "QQ binding QR code" });
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Waiting for scan…");
|
||||
rerender(<QQConnectFlowView {...props} connect={{ ...pending, retrying: true }} />);
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Retrying…");
|
||||
expect(qr).toBeInTheDocument();
|
||||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Enter credentials manually" }));
|
||||
expect(props.onManualSetup).toHaveBeenCalledOnce();
|
||||
rerender(<QQConnectFlowView {...props} connect={pending} />);
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Waiting for scan…");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
||||
expect(props.onCancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["expired", "二维码已过期", "刷新二维码"],
|
||||
["failed", "绑定失败", "重试"],
|
||||
["cancelled", "已取消", "绑定机器人"],
|
||||
] as const)("shows a localized %s state and a working recovery action", async (status, label, action) => {
|
||||
await i18n.changeLanguage("zh-CN");
|
||||
const props = actions();
|
||||
render(<QQConnectFlowView {...props} connect={{ ...pending, status, message: "Raw portal response" }} />);
|
||||
expect(screen.getByRole("status")).toHaveTextContent(label);
|
||||
expect(screen.queryByRole("img")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText("Raw portal response")).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: action }));
|
||||
expect(props.onStart).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("offers retry after QR preparation fails without showing a successful binding", () => {
|
||||
const props = { ...actions(), error: "Unable to start QQ QR login: portal error" };
|
||||
render(<QQConnectFlowView {...props} connect={null} autoStart />);
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("Binding failed");
|
||||
expect(screen.queryByText("Configured")).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
|
||||
expect(props.onStart).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("clears a transport retry indication when polling recovers before the scan finishes", async () => {
|
||||
const feature: NanobotFeatureInfo = {
|
||||
name: "qq", display_name: "QQ", type: "channel", enabled: false,
|
||||
configured: false, installed: true, ready: false, status: "not_enabled",
|
||||
install_supported: true, requires_restart: false,
|
||||
};
|
||||
requestMutation.mockResolvedValueOnce({ ...pending, interval_ms: 2500 })
|
||||
.mockRejectedValueOnce(new Error("connection interrupted"))
|
||||
.mockResolvedValueOnce(pending);
|
||||
vi.spyOn(QRCode, "toDataURL").mockImplementation(async () => "data:image/png;base64,preview");
|
||||
vi.useFakeTimers();
|
||||
await act(async () => {
|
||||
render(<QQConnectFlow token="test" feature={feature} onFeaturesUpdate={vi.fn()} />);
|
||||
});
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Waiting for scan…");
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(900); });
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Retrying…");
|
||||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(1600); });
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Waiting for scan…");
|
||||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||||
expect(requestMutation).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("keeps cancellation errors distinct from poll recovery and clears them after cancelling", async () => {
|
||||
const feature: NanobotFeatureInfo = {
|
||||
name: "qq", display_name: "QQ", type: "channel", enabled: false,
|
||||
configured: false, installed: true, ready: false, status: "not_enabled",
|
||||
install_supported: true, requires_restart: false,
|
||||
};
|
||||
requestMutation.mockResolvedValueOnce({ ...pending, interval_ms: 2500 })
|
||||
.mockRejectedValueOnce(new Error("connection interrupted"))
|
||||
.mockRejectedValueOnce(new Error("cancel request failed"))
|
||||
.mockRejectedValueOnce(new Error("connection still interrupted"))
|
||||
.mockResolvedValueOnce(pending)
|
||||
.mockResolvedValueOnce({ session_id: pending.session_id, status: "cancelled", retrying: false });
|
||||
vi.spyOn(QRCode, "toDataURL").mockImplementation(async () => "data:image/png;base64,preview");
|
||||
vi.useFakeTimers();
|
||||
await act(async () => {
|
||||
render(<QQConnectFlow token="test" feature={feature} onFeaturesUpdate={vi.fn()} />);
|
||||
});
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(900); });
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Retrying…");
|
||||
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Cancel" })); });
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("Could not cancel");
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(1600); });
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("Could not cancel");
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(2500); });
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("Could not cancel");
|
||||
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Cancel" })); });
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Cancelled");
|
||||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "Bind QQ bot" })).toBeEnabled();
|
||||
});
|
||||
@@ -2114,20 +2114,83 @@ describe("Settings channels", () => {
|
||||
renderSettingsView({ initialSection: "channels" });
|
||||
fireEvent.click(await screen.findByRole("button", { name: "View QQ settings" }));
|
||||
|
||||
expect(await screen.findByRole("img", { name: "QQ login QR code" })).toBeInTheDocument();
|
||||
expect(await screen.findByRole("img", { name: "QQ binding QR code" })).toBeInTheDocument();
|
||||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||||
"settings.channel.connect.start", { channel: "qq" }, 150_000,
|
||||
);
|
||||
await waitFor(() => expect(requestMutationMock).toHaveBeenCalledWith(
|
||||
"settings.channel.connect.poll", { channel: "qq", session_id: "qq-1" }, 150_000,
|
||||
));
|
||||
expect(await screen.findByText("QQ is connected.")).toBeInTheDocument();
|
||||
expect(screen.getByText(/previous App Secret stops working/)).toBeInTheDocument();
|
||||
expect(await screen.findByText("Configured")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText(/Rebinding may interrupt other apps/)).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Advanced" }));
|
||||
expect(screen.getByLabelText("App ID")).toHaveValue("1024");
|
||||
expect(screen.getByLabelText("Acknowledgement message")).toHaveValue("⏳ Processing...");
|
||||
});
|
||||
|
||||
it("keeps saved QQ setup distinct from a running connection when reopening settings", async () => {
|
||||
const qqFeature: NanobotFeatureInfo = {
|
||||
...catalogFeature("qq", false),
|
||||
display_name: "QQ",
|
||||
webui: "webui/index.tsx",
|
||||
configured: true,
|
||||
runtime_status: "stopped",
|
||||
config_values: { "channels.qq.appId": "1024" },
|
||||
configured_fields: ["channels.qq.appId", "channels.qq.secret"],
|
||||
setup: channelSetupContract("qq"),
|
||||
};
|
||||
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/settings") return jsonResponse(settingsPayload());
|
||||
if (url === "/api/settings/nanobot-features") {
|
||||
return jsonResponse({ features: [qqFeature], enabled_count: 0 });
|
||||
}
|
||||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||||
}));
|
||||
|
||||
renderSettingsView({ initialSection: "channels" });
|
||||
fireEvent.click(await screen.findByRole("button", { name: "View QQ settings" }));
|
||||
expect(await screen.findByText("Configured")).toBeInTheDocument();
|
||||
expect(within(screen.getByRole("dialog")).getByText("Not running")).toBeInTheDocument();
|
||||
expect(within(screen.getByRole("dialog")).queryByText("Running")).not.toBeInTheDocument();
|
||||
expect(screen.getByText("1024")).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "Rebind bot" })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("img", { name: "QQ binding QR code" })).not.toBeInTheDocument();
|
||||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Enter credentials manually" }));
|
||||
expect(screen.getByRole("group", { name: "Manual credentials" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("group", { name: "Access" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("group", { name: "Behavior" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("group", { name: "Advanced" })).toBeInTheDocument();
|
||||
expect(screen.getByLabelText("App ID")).toHaveValue("1024");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "View QQ settings" }));
|
||||
const rebind = await screen.findByRole("button", { name: "Rebind bot" });
|
||||
expect(screen.queryByText(/Rebinding may interrupt other apps/)).not.toBeInTheDocument();
|
||||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||||
rebind.focus();
|
||||
fireEvent.click(rebind);
|
||||
const confirmation = await screen.findByRole("alertdialog", { name: "Rebind bot" });
|
||||
expect(confirmation).toHaveTextContent("Rebinding may interrupt other apps using this QQ bot. If the App Secret is reset, the old secret becomes invalid and those apps need the new secret.");
|
||||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||||
fireEvent.click(within(confirmation).getByRole("button", { name: "Cancel" }));
|
||||
await waitFor(() => expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument());
|
||||
expect(screen.queryByText(/Rebinding may interrupt other apps/)).not.toBeInTheDocument();
|
||||
expect(rebind).toHaveFocus();
|
||||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||||
|
||||
requestMutationMock.mockResolvedValue({ status: "pending", session_id: "qq-rebind", qr_url: "https://example.com/qq-rebind" });
|
||||
fireEvent.click(rebind);
|
||||
fireEvent.click(within(await screen.findByRole("alertdialog", { name: "Rebind bot" }))
|
||||
.getByRole("button", { name: "Rebind bot" }));
|
||||
expect(await screen.findByRole("img", { name: "QQ binding QR code" })).toBeInTheDocument();
|
||||
expect(requestMutationMock).toHaveBeenCalledTimes(1);
|
||||
expect(requestMutationMock).toHaveBeenCalledWith("settings.channel.connect.start", { channel: "qq" }, 150_000);
|
||||
expect(screen.queryByRole("alertdialog")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps the WebUI websocket channel enabled without opening a setup dialog", async () => {
|
||||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
|
||||
Reference in new issue
Block a user