// [DEF:frontend.src.lib.components.layout.__tests__.sidebarNavigation:Module]
// @TIER: STANDARD
// @SEMANTICS: tests, sidebar, navigation, rbac, permissions
// @PURPOSE: Verifies RBAC-based sidebar category and subitem visibility.
// @LAYER: UI (Tests)
// @RELATION: TESTS -> frontend/src/lib/components/layout/sidebarNavigation.js

import { describe, it, expect } from "vitest";
import { buildSidebarCategories } from "../sidebarNavigation.js";

const i18nState = {
  nav: {
    dashboards: "Dashboards",
    overview: "Overview",
    datasets: "Datasets",
    all_datasets: "All datasets",
    storage: "Storage",
    backups: "Backups",
    repositories: "Repositories",
    reports: "Reports",
    profile: "Profile",
    admin: "Admin",
    admin_users: "User management",
    admin_roles: "Role management",
    settings: "Settings",
  },
};

function makeUser(roles) {
  return { roles };
}

describe("sidebarNavigation", () => {
  it("shows only categories available to a non-admin user", () => {
    const user = makeUser([
      {
        name: "Operator",
        permissions: [
          { resource: "plugin:migration", action: "READ" },
          { resource: "tasks", action: "READ" },
        ],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const categoryIds = categories.map((category) => category.id);

    expect(categoryIds).toEqual(["dashboards", "datasets", "reports", "profile"]);
  });

  it("hides admin category when user has no admin permissions", () => {
    const user = makeUser([
      {
        name: "Viewer",
        permissions: [{ resource: "plugin:migration", action: "READ" }],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const adminCategory = categories.find((category) => category.id === "admin");

    expect(adminCategory).toBeUndefined();
  });

  it("shows full admin category for admin role", () => {
    const user = makeUser([
      {
        name: "Admin",
        permissions: [],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const adminCategory = categories.find((category) => category.id === "admin");

    expect(adminCategory).toBeDefined();
    expect(adminCategory.subItems.map((item) => item.path)).toEqual([
      "/admin/users",
      "/admin/roles",
      "/settings",
    ]);
  });

  it("keeps profile visible even without explicit plugin permissions", () => {
    const user = makeUser([
      {
        name: "Basic",
        permissions: [],
      },
    ]);

    const categories = buildSidebarCategories(i18nState, user);
    const categoryIds = categories.map((category) => category.id);

    expect(categoryIds).toEqual(["profile"]);
  });
});

// [/DEF:frontend.src.lib.components.layout.__tests__.sidebarNavigation:Module]