import { fireEvent, render, waitFor } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { uploadFile, uploadImage } from "@/shared/api/upload";
import { ToastProvider } from "@/shared/ui/ToastProvider.jsx";
import { UploadField } from "./UploadField.jsx";

vi.mock("@/shared/api/upload", () => ({
    uploadFile: vi.fn(async () => ({ url: "https://media.haiyihy.com/admin/files/effect.bin" })),
    uploadImage: vi.fn(async () => ({ url: "https://media.haiyihy.com/admin/images/effect.png" })),
}));

test("file upload field does not restrict picker type and uses generic upload endpoint", async () => {
    const onChange = vi.fn();
    const onFileSelected = vi.fn();
    const { container } = render(
        <ToastProvider>
            <UploadField kind="file" label="动效素材" value="" onChange={onChange} onFileSelected={onFileSelected} />
        </ToastProvider>,
    );
    const input = container.querySelector('input[type="file"]');

    expect(input).not.toHaveAttribute("accept");

    fireEvent.change(input, {
        target: {
            files: [new File(["effect"], "effect.bin", { type: "application/octet-stream" })],
        },
    });

    await waitFor(() => expect(uploadFile).toHaveBeenCalledTimes(1));
    expect(onFileSelected).toHaveBeenCalledWith(expect.objectContaining({ name: "effect.bin" }));
    expect(uploadImage).not.toHaveBeenCalled();
    expect(onChange).toHaveBeenCalledWith("https://media.haiyihy.com/admin/files/effect.bin");
});
