import { afterEach, describe, expect, it } from "vitest";
import { useSidebarStore } from "@/store/sidebar";

describe("useSidebarStore hydration", () => {
  afterEach(() => {
    localStorage.clear();
  });

  it("模块初始化时应保持默认首帧状态，避免先读持久化状态", () => {
    const state = useSidebarStore.getState();

    expect(state.open).toBe(false);
    expect(state.width).toBe(280);
    expect(state.viewMode).toBe("section");
  });

  it("手动 hydrate 后才应用持久化状态", () => {
    localStorage.setItem(
      "sidebar-ui",
      JSON.stringify({
        state: {
          open: true,
          width: 360,
          collapsedSections: {
            starred: false,
            public: false,
            shared: false,
            private: true,
            templates: false,
          },
          trashConfirm: false,
          viewMode: "filesystem",
        },
        version: 4,
      }),
    );

    const store = useSidebarStore;
    expect(store.persist.hasHydrated()).toBe(false);
    void store.persist.rehydrate();

    expect(store.getState().open).toBe(true);
    expect(store.getState().width).toBe(360);
    expect(store.getState().viewMode).toBe("filesystem");
    expect(store.persist.hasHydrated()).toBe(true);
  });
});
