import { TestBed } from '@angular/core/testing';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient } from '@angular/common/http';
import { GameApiService } from './game-api.service';

describe('GameApiService', () => {
  let service: GameApiService;
  let http: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [GameApiService, provideHttpClient(), provideHttpClientTesting()],
    });

    service = TestBed.inject(GameApiService);
    http = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    http.verify();
  });

  it('uses relative API URLs for all read requests', () => {
    service.getCharacter().subscribe();
    service.getCurrentLocation().subscribe();
    service.getCurrentTravel().subscribe();

    const requests = http.match((request) => request.method === 'GET');

    expect(requests.map((request) => request.request.url)).toEqual([
      '/api/characters/me',
      '/api/world/current-location',
      '/api/travel/current',
    ]);
    expect(requests.every((request) => !request.request.url.includes('://'))).toBe(true);

    for (const request of requests) {
      request.flush({});
    }
  });

  it('posts only the target location ID when starting travel', () => {
    service.startTravel('target-uuid').subscribe();

    const request = http.expectOne('/api/travel');
    expect(request.request.method).toBe('POST');
    expect(request.request.body).toEqual({ targetLocationId: 'target-uuid' });
    request.flush({ status: 'IDLE' });
  });
});
