import { InMemoryLocationRepository } from "../adapters/inMemoryLocationsRepository";
import Location from "../entities/location";
import WeatherService, { Forecast } from "../entities/weatherService";
import { DummyPresenter } from "./createLocation.test";
import getForecasts, { GetForecastsResult } from "./getForecasts";

class DummyWeatherService implements WeatherService {
  getForecastFor(lat: number, lon: number): Promise<Forecast> {
    return Promise.resolve({
      weather: "Sunny",
      temperature: 12,
      min: 10,
      max: 16,
    });
  }
}

describe("use case getForecasts", () => {
  it("retrieve all locations forecasts", async () => {
    const repository = new InMemoryLocationRepository([
      new Location("Rouen", 49.443232, 1.099971),
      new Location("Bourg-Achard", 49.35322, 0.81623),
    ]);
    const presenter = new DummyPresenter<GetForecastsResult[]>();
    const weatherService = new DummyWeatherService();

    await getForecasts(presenter, repository, weatherService);

    expect(presenter.result).not.toBeUndefined();
    expect(presenter.result).toHaveLength(2);

    let forecast = presenter.result![0];

    expect(forecast.location.name).toEqual("Rouen");
    expect(forecast.forecast.max).toEqual(16);
    expect(forecast.forecast.min).toEqual(10);
    expect(forecast.forecast.temperature).toEqual(12);
    expect(forecast.forecast.weather).toEqual("Sunny");

    forecast = presenter.result![1];

    expect(forecast.location.name).toEqual("Bourg-Achard");
    expect(forecast.forecast.max).toEqual(16);
    expect(forecast.forecast.min).toEqual(10);
    expect(forecast.forecast.temperature).toEqual(12);
    expect(forecast.forecast.weather).toEqual("Sunny");
  });
});
