import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { ConnectionCenter, ConnectionEditor } from "./components";
import { MemoryMockConnectionAdapter } from "./mockAdapter";

describe("连接中心组件", () => {
  it("表单拒绝无效配置，秘密字段使用 password 类型", () => {
    render(<ConnectionEditor onSave={() => undefined} onCancel={() => undefined} />);
    expect(screen.getByLabelText("认证秘密")).toHaveAttribute("type", "password");
    fireEvent.click(screen.getByRole("button", { name: "保存到内存" }));
    expect(screen.getByRole("alert")).toHaveTextContent("配置错误");
  });

  it("通过 mock adapter 完成创建并明确未建立网络连接", async () => {
    render(<ConnectionCenter adapter={new MemoryMockConnectionAdapter()} />);
    fireEvent.click(await screen.findByRole("button", { name: "＋ 新建连接" }));
    fireEvent.change(screen.getByLabelText("名称"), { target: { value: "Atlas" } });
    fireEvent.change(screen.getByLabelText("地址"), { target: { value: "https://atlas.example" } });
    fireEvent.click(screen.getByRole("button", { name: "保存到内存" }));
    await waitFor(() => expect(screen.getByText("Atlas")).toBeInTheDocument());
    expect(screen.getByText(/尚未建立网络连接/)).toBeInTheDocument();
  });
});
