import { TestBed } from '@angular/core/testing';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient } from '@angular/common/http';
import { ImageService } from './image.service';

describe('ImageService', () => {
  let service: ImageService;
  let httpMock: HttpTestingController;

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

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

  it('should include tags query param when filter is set', () => {
    service.list(['cat', 'funny'], 50, 0).subscribe();
    const req = httpMock.expectOne((r) => r.url === '/api/v1/images');
    expect(req.request.params.get('tags')).toBe('cat,funny');
    req.flush({ items: [], total: 0, limit: 50, offset: 0 });
  });

  it('should omit tags query param when filter is empty', () => {
    service.list([], 50, 0).subscribe();
    const req = httpMock.expectOne((r) => r.url === '/api/v1/images');
    expect(req.request.params.has('tags')).toBeFalse();
    req.flush({ items: [], total: 0, limit: 50, offset: 0 });
  });

  it('should send correct offset and limit params', () => {
    service.list([], 25, 75).subscribe();
    const req = httpMock.expectOne((r) => r.url === '/api/v1/images');
    expect(req.request.params.get('limit')).toBe('25');
    expect(req.request.params.get('offset')).toBe('75');
    req.flush({ items: [], total: 0, limit: 25, offset: 75 });
  });
});
