import { describe, expect, it } from "vitest";
import {
  confFilesTabReducer,
  initialConfFilesTabState,
} from "../ConfFilesTab";

const sampleFile = { name: "example", filename: "example.conf" };

describe("confFilesTabReducer", () => {
  it("sets files and open items", () => {
    const state = confFilesTabReducer(initialConfFilesTabState, {
      type: "setFiles",
      value: [sampleFile],
    });

    expect(state.files).toEqual([sampleFile]);

    const nextState = confFilesTabReducer(state, {
      type: "setOpenItems",
      value: ["example"],
    });

    expect(nextState.openItems).toEqual(["example"]);
  });

  it("stores content edits independently from loaded content", () => {
    const state = confFilesTabReducer(initialConfFilesTabState, {
      type: "setContent",
      name: "example",
      content: "original",
    });

    const nextState = confFilesTabReducer(state, {
      type: "setEditedContent",
      name: "example",
      content: "modified",
    });

    expect(nextState.contents).toEqual({ example: "original" });
    expect(nextState.editedContents).toEqual({ example: "modified" });
  });

  it("resets create form fields after creation", () => {
    const state = confFilesTabReducer(initialConfFilesTabState, {
      type: "setNewName",
      value: "test",
    });

    const nextState = confFilesTabReducer(state, {
      type: "setNewContent",
      value: "content",
    });

    expect(nextState.newName).toBe("test");
    expect(nextState.newContent).toBe("content");

    const cleared = confFilesTabReducer(nextState, {
      type: "setNewName",
      value: "",
    });
    const clearedContent = confFilesTabReducer(cleared, {
      type: "setNewContent",
      value: "",
    });

    expect(clearedContent.newName).toBe("");
    expect(clearedContent.newContent).toBe("");
  });
});
