import { describe, expect, it } from "vitest";

import {
  createFacebookProvider,
  FACEBOOK_AUTHORIZATION_SCOPE,
  FACEBOOK_PROFILE_FIELDS,
  mapFacebookProfile,
} from "../facebook_provider";

describe("Facebook provider", () => {
  it("requests public profile without email permission or field", () => {
    const provider = createFacebookProvider();

    expect(provider.options).toMatchObject({
      authorization: {
        params: { scope: "public_profile" },
      },
      userinfo: {
        params: { fields: "id,name,picture.type(large)" },
      },
    });
    expect(FACEBOOK_AUTHORIZATION_SCOPE).not.toContain("email");
    expect(FACEBOOK_PROFILE_FIELDS.split(",")).not.toContain("email");
  });

  it("does not map an email even when Facebook unexpectedly returns one", () => {
    const mapped = mapFacebookProfile({
      id: "facebook-1",
      name: "Elio",
      email: "should-not-be-used@example.com",
      picture: { data: { url: "https://example.com/avatar.jpg" } },
    });

    expect(mapped).toEqual({
      id: "facebook-1",
      name: "Elio",
      email: null,
      image: "https://example.com/avatar.jpg",
    });
  });
});
