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 { PublicAccessApi } from './public-access-api';

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

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

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

  it('loads the sharing status', () => {
    api.getStatus(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/public-access`);
    expect(request.request.method).toBe('GET');
    request.flush({ enabled: false, token: null });
  });

  it('updates the sharing status', () => {
    api.setEnabled(5, true).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/public-access`);
    expect(request.request.method).toBe('PATCH');
    expect(request.request.body).toEqual({ enabled: true });
    request.flush({ enabled: true, token: 'token' });
  });

  it('rotates the sharing token', () => {
    api.rotate(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/public-access/rotate`);
    expect(request.request.method).toBe('POST');
    expect(request.request.body).toEqual({});
    request.flush({ enabled: true, token: 'new-token' });
  });
});
