import { describe, it, expect } from "vitest";
import {
  feedbackReducer,
  initialFeedbackState,
  type FeedbackState,
} from "./useFeedback";

describe("feedbackReducer", () => {
  it("starts in idle with no error", () => {
    expect(initialFeedbackState).toEqual({ status: "idle", errorCode: null });
  });

  it("transitions idle → sending on SEND_START", () => {
    const next = feedbackReducer(initialFeedbackState, { type: "SEND_START" });
    expect(next).toEqual({ status: "sending", errorCode: null });
  });

  it("clears a previous error code when re-sending", () => {
    const prev: FeedbackState = { status: "error", errorCode: "rate_limit" };
    const next = feedbackReducer(prev, { type: "SEND_START" });
    expect(next.errorCode).toBeNull();
  });

  it("transitions sending → success", () => {
    const prev: FeedbackState = { status: "sending", errorCode: null };
    const next = feedbackReducer(prev, { type: "SEND_SUCCESS" });
    expect(next).toEqual({ status: "success", errorCode: null });
  });

  it("transitions sending → error and records the code", () => {
    const prev: FeedbackState = { status: "sending", errorCode: null };
    const next = feedbackReducer(prev, {
      type: "SEND_ERROR",
      code: "network_error",
    });
    expect(next).toEqual({ status: "error", errorCode: "network_error" });
  });

  it("RESET returns to the initial state regardless of prior state", () => {
    const prev: FeedbackState = { status: "error", errorCode: "invalid" };
    const next = feedbackReducer(prev, { type: "RESET" });
    expect(next).toEqual(initialFeedbackState);
  });
});
