import { describe, expect, it } from "vitest";
import { createDefaultDashboardLayout } from "@capakraken/shared/schemas";
import { shouldHydrateDashboardFromDb } from "./useDashboardLayout.js";

describe("shouldHydrateDashboardFromDb", () => {
  it("hydrates from the database on first load when no local changes happened yet", () => {
    expect(shouldHydrateDashboardFromDb({
      remoteLayout: createDefaultDashboardLayout(),
      hasHydratedFromDb: false,
      hasLocalChangesBeforeHydration: false,
    })).toBe(true);
  });

  it("skips initial database hydration after a local first-load change", () => {
    expect(shouldHydrateDashboardFromDb({
      remoteLayout: createDefaultDashboardLayout(),
      hasHydratedFromDb: false,
      hasLocalChangesBeforeHydration: true,
    })).toBe(false);
  });

  it("does not re-hydrate once the initial database handoff already happened", () => {
    expect(shouldHydrateDashboardFromDb({
      remoteLayout: createDefaultDashboardLayout(),
      hasHydratedFromDb: true,
      hasLocalChangesBeforeHydration: false,
    })).toBe(false);
  });

  it("does not hydrate when the backend has no stored layout", () => {
    expect(shouldHydrateDashboardFromDb({
      remoteLayout: null,
      hasHydratedFromDb: false,
      hasLocalChangesBeforeHydration: false,
    })).toBe(false);
  });
});
