import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { FinanceCoinSellerExchangeRateDialog } from "./FinanceCoinSellerExchangeRateDialog.jsx";

test("edits app amount tiers and user whitelist rates", async () => {
    const onClose = vi.fn();
    const onLoad = vi.fn(async () => ({
        appCode: "lalu",
        tiers: [{ coinsPerUsd: 80000, maxUsdAmount: "200.00", minUsdAmount: "100.00" }],
        whitelist: [],
    }));
    const onSave = vi.fn(async () => ({}));

    render(
        <FinanceCoinSellerExchangeRateDialog
            apps={[{ appCode: "lalu", appName: "Lalu" }]}
            initialAppCode="lalu"
            open
            onClose={onClose}
            onLoad={onLoad}
            onSave={onSave}
        />,
    );

    await waitFor(() => expect(screen.getByRole("spinbutton", { name: "区间金币汇率" })).toHaveValue(80000));
    fireEvent.click(screen.getByRole("button", { name: "添加白名单" }));
    fireEvent.change(screen.getByRole("textbox", { name: "白名单用户 ID" }), { target: { value: "123456" } });
    fireEvent.change(screen.getByRole("spinbutton", { name: "白名单金币汇率" }), {
        target: { value: "100000" },
    });
    fireEvent.click(screen.getByRole("button", { name: "保存" }));

    await waitFor(() =>
        expect(onSave).toHaveBeenCalledWith("lalu", {
            tiers: [{ coinsPerUsd: 80000, maxUsdAmount: 200, minUsdAmount: 100 }],
            whitelist: [{ coinsPerUsd: 100000, userId: "123456" }],
        }),
    );
    expect(onClose).toHaveBeenCalled();
});
