import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { environment } from '../../../environments/environment';
import { TeamPermissionsApi } from './team-permissions-api';

describe('TeamPermissionsApi', () => {
  let api: TeamPermissionsApi;
  let httpMock: HttpTestingController;

  const permissions = {
    transactionCreateMinRole: 2,
    transactionReverseMinRole: 2,
    inviteMinRole: 3,
    memberManageMinRole: 3,
    penaltyManageMinRole: 3,
    publicAccessManageMinRole: 3,
  };

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [provideHttpClient(), provideHttpClientTesting()],
    });
    api = TestBed.inject(TeamPermissionsApi);
    httpMock = TestBed.inject(HttpTestingController);
  });

  afterEach(() => httpMock.verify());

  it('loads the team permissions', () => {
    api.getPermissions(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/permissions`);
    expect(request.request.method).toBe('GET');
    request.flush(permissions);
  });

  it('updates only the changed permissions', () => {
    api.updatePermissions(5, { inviteMinRole: 4 }).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/permissions`);
    expect(request.request.method).toBe('PATCH');
    expect(request.request.body).toEqual({ inviteMinRole: 4 });
    request.flush({ ...permissions, inviteMinRole: 4 });
  });
});
