import { describe, expect, it } from "vitest";
import { createContext } from "./createContext";

describe("createContext", () => {
  it("uses permissions array attribute when present", () => {
    const ctx = createContext({
      user: {
        id: "user-1",
        attributes: {
          permissions: ["user-management:createUser", "user-management:activateUser"],
        },
      },
    });

    expect(ctx.actorId).toBe("user-1");
    expect(ctx.permissions).toEqual(["user-management:createUser", "user-management:activateUser"]);
  });

  it("returns empty permissions when attributes exists but permissions is absent", () => {
    const ctx = createContext({
      user: {
        id: "user-1",
        attributes: {},
      },
    });

    expect(ctx.actorId).toBe("user-1");
    expect(ctx.permissions).toEqual([]);
  });

  it("returns empty permissions when attributes is null", () => {
    const ctx = createContext({
      user: { id: "user-1", attributes: null },
    });

    expect(ctx.actorId).toBe("user-1");
    expect(ctx.permissions).toEqual([]);
  });
});
