package bls import ( "context" "net/http" "net/http/httptest" "strings" "testing" ) const sampleResp = `{"status":"REQUEST_SUCCEEDED","responseTime":1,"message":[],"Results":{"series":[ {"seriesID":"OEUS150000000000015121204","data":[{"year":"2025","period":"A01","periodName":"Annual","value":"122660"}]}, {"seriesID":"OEUS350000000000015121204","data":[{"year":"2025","period":"A01","periodName":"Annual","value":"142450"}]} ]}}` func TestFetchLatestMapsValues(t *testing.T) { var gotKey, gotLatest bool srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body := make([]byte, r.ContentLength) _, _ = r.Body.Read(body) s := string(body) gotKey = strings.Contains(s, "registrationKey") gotLatest = strings.Contains(s, "\"latest\":true") _, _ = w.Write([]byte(sampleResp)) })) defer srv.Close() c := New("secret", srv.URL) out, err := c.FetchLatest(context.Background(), []string{"OEUS150000000000015121204", "OEUS350000000000015121204"}) if err != nil { t.Fatalf("fetch: %v", err) } if !gotKey || !gotLatest { t.Fatalf("request must send registrationKey and latest: key=%v latest=%v", gotKey, gotLatest) } if len(out) != 2 || out[0].Value != "122660" || out[0].Year != "2025" { t.Fatalf("values mapped wrong: %+v", out) } } func TestFetchLatestThreshold(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(`{"status":"REQUEST_NOT_PROCESSED","message":["You have exceeded the daily threshold of requests."]}`)) })) defer srv.Close() c := New("k", srv.URL) if _, err := c.FetchLatest(context.Background(), []string{"X"}); err != ErrThrottled { t.Fatalf("threshold message must map to ErrThrottled, got %v", err) } } func TestGalapagosSeriesShape(t *testing.T) { specs := GalapagosSeries() // 5 occupations x 2 areas x 2 metrics if len(specs) != 20 { t.Fatalf("want 20 series specs, got %d", len(specs)) } if specs[0].SeriesID != "OEUS150000000000015121204" { t.Fatalf("first series id wrong: %s", specs[0].SeriesID) } }