// Test unitaire PUR — pas de DB, pas de mock.module (helper sans dépendances).
import { expect, test } from "bun:test";
import { hasWebmastersScope, WEBMASTERS_READONLY_SCOPE } from "./scope";

const OTHER =
  "https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/userinfo.profile,openid";

test("null → false", () => {
  expect(hasWebmastersScope(null)).toBe(false);
});

test("chaîne vide → false", () => {
  expect(hasWebmastersScope("")).toBe(false);
});

test("scopes séparés par virgules SANS webmasters (cas prod du bug) → false", () => {
  expect(hasWebmastersScope(OTHER)).toBe(false);
});

test("scopes séparés par virgules AVEC webmasters → true", () => {
  expect(hasWebmastersScope(`${OTHER},${WEBMASTERS_READONLY_SCOPE}`)).toBe(true);
});

test("scopes séparés par espaces AVEC webmasters → true", () => {
  expect(
    hasWebmastersScope(
      `openid https://www.googleapis.com/auth/userinfo.email ${WEBMASTERS_READONLY_SCOPE}`,
    ),
  ).toBe(true);
});

test("séparateurs mélangés (virgules + espaces) → true", () => {
  expect(
    hasWebmastersScope(`openid, ${WEBMASTERS_READONLY_SCOPE} , profile`),
  ).toBe(true);
});

test("le scope webmasters seul → true", () => {
  expect(hasWebmastersScope(WEBMASTERS_READONLY_SCOPE)).toBe(true);
});

test("un préfixe partiel ne matche pas (pas de faux positif de substring)", () => {
  expect(
    hasWebmastersScope("https://www.googleapis.com/auth/webmasters"),
  ).toBe(false);
});
