import { describe, expect, it } from "vitest";
import { initialServerTabState, serverTabReducer } from "../ServerTab";

describe("serverTabReducer", () => {
  it("updates the log level", () => {
    const nextState = serverTabReducer(initialServerTabState, {
      type: "setLogLevel",
      value: "DEBUG",
    });

    expect(nextState.logLevel).toBe("DEBUG");
  });

  it("toggles loading actions correctly", () => {
    const startState = serverTabReducer(initialServerTabState, {
      type: "setFlushing",
      value: true,
    });

    expect(startState.flushing).toBe(true);

    const endState = serverTabReducer(startState, {
      type: "setFlushing",
      value: false,
    });

    expect(endState.flushing).toBe(false);
  });

  it("updates map thresholds and valid payload together", () => {
    const thresholdState = serverTabReducer(initialServerTabState, {
      type: "setMapThresholds",
      high: "100",
      medium: "50",
      low: "10",
    });

    expect(thresholdState.mapThresholdHigh).toBe("100");
    expect(thresholdState.mapThresholdMedium).toBe("50");
    expect(thresholdState.mapThresholdLow).toBe("10");

    const payloadState = serverTabReducer(thresholdState, {
      type: "setMapValidPayload",
      payload: { threshold_high: 100, threshold_medium: 50, threshold_low: 10 },
    });

    expect(payloadState.mapValidPayload).toEqual({
      threshold_high: 100,
      threshold_medium: 50,
      threshold_low: 10,
    });
  });
});
