// frontend/tests/inbox/CaptureComposer.test.tsx
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import CaptureComposer from "@/components/inbox/CaptureComposer";

describe("CaptureComposer", () => {
  it("Enter로 text 제출 후 입력 비움", () => {
    const onSubmit = vi.fn();
    render(<CaptureComposer onSubmit={onSubmit} />);
    const input = screen.getByLabelText("인박스에 빠르게 캡처") as HTMLInputElement;
    fireEvent.change(input, { target: { value: "엄마 생신 선물 알아보기" } });
    fireEvent.keyDown(input, { key: "Enter" });
    expect(onSubmit).toHaveBeenCalledWith("엄마 생신 선물 알아보기", "text");
    expect(input.value).toBe("");
  });

  it("mic 버튼: getUserMedia 미지원 → voice 폴백 문구", () => {
    // jsdom: navigator.mediaDevices 미정의 → 즉시 폴백
    const onSubmit = vi.fn();
    render(<CaptureComposer onSubmit={onSubmit} />);
    fireEvent.click(screen.getByLabelText(/음성으로 캡처/));
    expect(onSubmit).toHaveBeenCalledWith(expect.stringContaining("음성 메모"), "voice");
  });

  it("image 버튼: 파일 선택 → caption 실패 시 image 폴백", async () => {
    const onSubmit = vi.fn();
    const { container } = render(<CaptureComposer onSubmit={onSubmit} />);
    const file = new File(["x"], "shot.jpg", { type: "image/jpeg" });
    const fileInput = container.querySelector(
      'input[type="file"]',
    ) as HTMLInputElement;
    fireEvent.change(fileInput, { target: { files: [file] } });
    // fetch 미정의(jsdom) → captionImage throw → IMAGE_FALLBACK 으로 image 제출
    await vi.waitFor(() =>
      expect(onSubmit).toHaveBeenCalledWith(expect.any(String), "image"),
    );
  });

  it("빈 입력은 제출 무시", () => {
    const onSubmit = vi.fn();
    render(<CaptureComposer onSubmit={onSubmit} />);
    fireEvent.keyDown(screen.getByLabelText("인박스에 빠르게 캡처"), { key: "Enter" });
    expect(onSubmit).not.toHaveBeenCalled();
  });
});
