announcements_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // announcements_test.go — avisos externos: selección del aviso vigente
  2. // (starts/expires) y fetch con servidor de prueba (fail-silent fuera).
  3. package httpapi
  4. import (
  5. "context"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "time"
  10. )
  11. func TestActiveAnnouncementWindow(t *testing.T) {
  12. now := time.Date(2026, 9, 12, 10, 0, 0, 0, time.UTC)
  13. list := []Announcement{
  14. {ID: "futuro", Starts: "2026-10-01"},
  15. {ID: "caducado", Expires: "2026-09-01"},
  16. {ID: "sin-ventana", Title: map[string]string{"en": "always"}},
  17. {ID: "vigente-con-fin", Starts: "2026-09-01", Expires: "2026-11-01"},
  18. }
  19. got := activeAnnouncement(list, now)
  20. if got == nil || got.ID != "sin-ventana" {
  21. t.Fatalf("debe elegir el primero vigente sin ventana: %+v", got)
  22. }
  23. solo := []Announcement{{ID: "vigente-con-fin", Starts: "2026-09-01", Expires: "2026-11-01"}}
  24. if got := activeAnnouncement(solo, now); got == nil || got.ID != "vigente-con-fin" {
  25. t.Fatalf("ventana válida debe activarse: %+v", got)
  26. }
  27. if got := activeAnnouncement(nil, now); got != nil {
  28. t.Fatalf("lista vacía: sin aviso, got %+v", got)
  29. }
  30. }
  31. func TestFetchAnnouncementsPicksActive(t *testing.T) {
  32. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  33. w.Header().Set("Content-Type", "application/json")
  34. w.Write([]byte(`{"announcements":[{"id":"caducado","expires":"2020-01-01"},{"id":"vivo","title":{"es":"hola","en":"hello"}}]}`))
  35. }))
  36. defer srv.Close()
  37. a, err := fetchAnnouncements(context.Background(), srv.URL)
  38. if err != nil {
  39. t.Fatalf("fetch: %v", err)
  40. }
  41. if a == nil || a.ID != "vivo" || a.Title["en"] != "hello" {
  42. t.Fatalf("aviso activo incorrecto: %+v", a)
  43. }
  44. }
  45. func TestFetchAnnouncementsFailsSilent(t *testing.T) {
  46. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  47. w.WriteHeader(http.StatusInternalServerError)
  48. }))
  49. defer srv.Close()
  50. if _, err := fetchAnnouncements(context.Background(), srv.URL); err == nil {
  51. t.Fatal("un 500 debe devolver error (el caller lo traga con log)")
  52. }
  53. }