Files
Maven/internal/weather/openmeteo_test.go
T
claude c62c7034fa weather asks the dictionary before guessing case (V-530)
locationCandidates reversed endings by hand to turn "в Казани" into the
nominative the geocoder wants. internal/morph knows the answer for the places
it has, so it goes first and the reversals stay behind it for the ones it does
not: "Твери" and "Перми" come back unchanged.

The four-rune floor was there to stop a two-letter stem, so it now tests the
stem instead. "Уфе" was under the floor and "Уфа" was never tried.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 21:18:52 +04:00

118 lines
3.3 KiB
Go

package weather
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestOpenMeteoProvider(t *testing.T) {
var geoReqCount, weatherReqCount int
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v1/search" {
geoReqCount++
json.NewEncoder(w).Encode(geoResponse{
Results: []geoResult{{
Name: "Moscow", Latitude: 55.7558, Longitude: 37.6173, Country: "Russia",
}},
})
return
}
if r.URL.Path == "/v1/forecast" {
weatherReqCount++
json.NewEncoder(w).Encode(forecastResponse{
CurrentWeather: currentWeather{
Temperature: 22.5,
WMO: 0,
WindSpeed: 3.2,
},
})
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer mock.Close()
p := NewOpenMeteoProvider()
transport := &mockTransport{
geoURL: mock.URL + "/v1/search",
forecastURL: mock.URL + "/v1/forecast",
}
p.SetClient(&http.Client{Transport: transport})
w, err := p.CurrentWeather(context.Background(), "Moscow")
if err != nil {
t.Fatalf("CurrentWeather: %v", err)
}
if w.Location != "Moscow" {
t.Errorf("location = %q, want Moscow", w.Location)
}
if w.Temperature != 22.5 {
t.Errorf("temperature = %f, want 22.5", w.Temperature)
}
if w.Condition != "ясно" {
t.Errorf("condition = %q, want ясно", w.Condition)
}
}
type mockTransport struct {
geoURL string
forecastURL string
}
func (t *mockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
var targetURL string
if r.URL.Host == "geocoding-api.open-meteo.com" {
targetURL = t.geoURL + "?" + r.URL.RawQuery
} else if r.URL.Host == "api.open-meteo.com" {
targetURL = t.forecastURL + "?" + r.URL.RawQuery
} else {
return nil, fmt.Errorf("unexpected host: %s", r.URL.Host)
}
req, _ := http.NewRequestWithContext(r.Context(), r.Method, targetURL, nil)
return http.DefaultTransport.RoundTrip(req)
}
func TestStubProvider(t *testing.T) {
p := NewStubProvider()
_, err := p.CurrentWeather(context.Background(), "Moscow")
if err != ErrNotConfigured {
t.Fatalf("StubProvider: want ErrNotConfigured, got %v", err)
}
}
// TestLocationCandidates — he speaks the prepositional case and the geocoder
// wants the nominative (Vikunja #421).
//
// The dictionary answers before the reversals now, so the nominative it knows
// comes second and anything derived by hand follows (V-530). "Уфе" used to fall
// under a four-rune floor and was asked as spoken, so "Уфа" was never tried.
func TestLocationCandidates(t *testing.T) {
cases := map[string][]string{
"Москве": {"Москве", "Москва", "Москв"},
"Казани": {"Казани", "Казань", "Казан"},
"Лондоне": {"Лондоне", "Лондон", "Лондона"},
"Тбилиси": {"Тбилиси", "Тбились", "Тбилис"},
"Berlin": {"Berlin"},
"Уфе": {"Уфе", "Уфа", "Уф"},
// The dictionary does not know it, so the soft-sign reversal answers.
"Твери": {"Твери", "Тверь", "Твер"},
}
for in, want := range cases {
got := locationCandidates(in)
if len(got) != len(want) {
t.Errorf("locationCandidates(%q) = %v, want %v", in, got, want)
continue
}
for i := range got {
if got[i] != want[i] {
t.Errorf("locationCandidates(%q) = %v, want %v", in, got, want)
break
}
}
}
}