|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "go.uber.org/mock/gomock" |
| 13 | + |
| 14 | + "github.com/TuanKiri/weather-mcp-server/internal/server/services/mock" |
| 15 | + viewModels "github.com/TuanKiri/weather-mcp-server/internal/server/view/models" |
| 16 | + "github.com/TuanKiri/weather-mcp-server/pkg/weatherapi/models" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCurrentWeather(t *testing.T) { |
| 20 | + testCases := map[string]struct { |
| 21 | + city string |
| 22 | + errString string |
| 23 | + wait string |
| 24 | + setupWeatherAPI func(weatherAPI *mock.MockWeatherAPIProvider) |
| 25 | + setupRenderer func(renderer *mock.MockTemplateRenderer) |
| 26 | + }{ |
| 27 | + "city_not_found": { |
| 28 | + city: "Tokyo", |
| 29 | + errString: "weather API not available. Code: 400", |
| 30 | + setupWeatherAPI: func(weatherAPI *mock.MockWeatherAPIProvider) { |
| 31 | + weatherAPI.EXPECT(). |
| 32 | + Current(context.Background(), "Tokyo"). |
| 33 | + Return(nil, errors.New("weather API not available. Code: 400")) |
| 34 | + }, |
| 35 | + }, |
| 36 | + "successful_result": { |
| 37 | + city: "London", |
| 38 | + wait: "{Location:London, United Kingdom " + |
| 39 | + "Icon:https://cdn.weatherapi.com/weather/64x64/day/113.png " + |
| 40 | + "Condition:Sunny " + |
| 41 | + "Temperature:18 " + |
| 42 | + "Humidity:45 " + |
| 43 | + "WindSpeed:4}", |
| 44 | + setupWeatherAPI: func(weatherAPI *mock.MockWeatherAPIProvider) { |
| 45 | + weatherAPI.EXPECT(). |
| 46 | + Current(context.Background(), "London"). |
| 47 | + Return(&models.CurrentResponse{ |
| 48 | + Location: models.Location{ |
| 49 | + Name: "London", |
| 50 | + Country: "United Kingdom", |
| 51 | + }, |
| 52 | + Current: models.Current{ |
| 53 | + TempC: 18.4, |
| 54 | + WindKph: 4, |
| 55 | + Humidity: 45, |
| 56 | + Condition: models.Condition{ |
| 57 | + Text: "Sunny", |
| 58 | + Icon: "//cdn.weatherapi.com/weather/64x64/day/113.png", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, nil) |
| 62 | + }, |
| 63 | + setupRenderer: func(renderer *mock.MockTemplateRenderer) { |
| 64 | + renderer.EXPECT(). |
| 65 | + ExecuteTemplate( |
| 66 | + gomock.AssignableToTypeOf(&bytes.Buffer{}), |
| 67 | + "weather.html", |
| 68 | + gomock.AssignableToTypeOf(viewModels.CurrentWeather{}), |
| 69 | + ). |
| 70 | + Do(func(wr io.Writer, _ string, data any) error { |
| 71 | + value, _ := data.(viewModels.CurrentWeather) |
| 72 | + wr.Write(fmt.Appendf([]byte{}, "%+v", value)) |
| 73 | + return nil |
| 74 | + }) |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + ctrl := gomock.NewController(t) |
| 80 | + defer ctrl.Finish() |
| 81 | + |
| 82 | + renderer := mock.NewMockTemplateRenderer(ctrl) |
| 83 | + weatherAPI := mock.NewMockWeatherAPIProvider(ctrl) |
| 84 | + |
| 85 | + svc := New(renderer, weatherAPI) |
| 86 | + |
| 87 | + for name, tc := range testCases { |
| 88 | + t.Run(name, func(t *testing.T) { |
| 89 | + if tc.setupWeatherAPI != nil { |
| 90 | + tc.setupWeatherAPI(weatherAPI) |
| 91 | + } |
| 92 | + |
| 93 | + if tc.setupRenderer != nil { |
| 94 | + tc.setupRenderer(renderer) |
| 95 | + } |
| 96 | + |
| 97 | + data, err := svc.Weather().Current(context.Background(), tc.city) |
| 98 | + if err != nil { |
| 99 | + assert.EqualError(t, err, tc.errString) |
| 100 | + } |
| 101 | + |
| 102 | + assert.Equal(t, tc.wait, data) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments