import { describe, expect, it } from "vitest";
import type { TenantSummary } from "./adminApi";
import { buildTenantFullTree } from "./tenantTree";

function tenant(
  id: string,
  slug: string,
  parentId?: string,
  type = "USER_GROUP",
): TenantSummary {
  return {
    id,
    type,
    name: slug,
    slug,
    description: "",
    status: "active",
    parentId,
    memberCount: 0,
    totalMemberCount: 0,
    createdAt: "2026-06-10T00:00:00.000Z",
    updatedAt: "2026-06-10T00:00:00.000Z",
  };
}

describe("buildTenantFullTree", () => {
  it("treats a self-parent hanmac-family tenant as a root", () => {
    const result = buildTenantFullTree([
      tenant(
        "hanmac-family-id",
        "hanmac-family",
        "hanmac-family-id",
        "COMPANY_GROUP",
      ),
      tenant("saman-id", "saman", "hanmac-family-id", "COMPANY"),
      tenant("platform-id", "platform", "saman-id"),
    ]);

    expect(result.subTree).toHaveLength(1);
    expect(result.subTree[0]?.id).toBe("hanmac-family-id");
    expect(result.subTree[0]?.children[0]?.id).toBe("saman-id");
    expect(result.subTree[0]?.children[0]?.children[0]?.id).toBe("platform-id");
  });
});
