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

import { CanPriceField } from "@/shared/ui/can-price-field";

test("CanPriceField renders the native numeric CAN price contract", () => {
  render(<CanPriceField error={undefined} errorId="price-error" onChange={() => undefined} value="0" />);

  const input = screen.getByLabelText("가격");
  expect(input).toHaveAttribute("type", "number");
  expect(input).toHaveAttribute("inputmode", "numeric");
  expect(input).toHaveAttribute("min", "0");
  expect(input).toHaveAttribute("step", "1");
  expect(input).toHaveAccessibleDescription("단위: 캔");
});

test.each([
  "-1",
  "1.5",
])("CanPriceField emits the raw input value %s unchanged", (rawValue) => {
  const onChange = vi.fn<(value: string) => void>();
  render(<CanPriceField error={undefined} errorId="price-error" onChange={onChange} value="0" />);

  fireEvent.change(screen.getByLabelText("가격"), { target: { value: rawValue } });

  expect(onChange).toHaveBeenCalledWith(rawValue);
});

test("CanPriceField links the visible error and unit description to the invalid input", () => {
  render(<CanPriceField error="가격 오류" errorId="price-error" onChange={() => undefined} value="100000" />);

  const input = screen.getByLabelText("가격");
  expect(input).toHaveAttribute("aria-invalid", "true");
  expect(input.getAttribute("aria-describedby")?.split(" ")).toContain("price-error");
  expect(input).toHaveAccessibleDescription("단위: 캔 가격 오류");
  expect(screen.getByRole("alert")).toHaveAttribute("id", "price-error");
});
