Add NWS forecast discussion fixture coverage
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -214,6 +215,98 @@ func TestForecastDiscussionNormalizerSupportsCrossOfficeLayout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastDiscussionNormalizerSupportsNumberedAndKeyPointsFixtures(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filename string
|
||||
id string
|
||||
source string
|
||||
emittedAt time.Time
|
||||
effectiveAt time.Time
|
||||
messages []string
|
||||
}{
|
||||
{
|
||||
name: "numbered key messages",
|
||||
filename: "forecast_discussion_bgm_numbered_sample.html",
|
||||
id: "evt-discussion-bgm",
|
||||
source: "nws-discussion-bgm-test",
|
||||
emittedAt: time.Date(2026, 4, 10, 17, 31, 0, 0, time.UTC),
|
||||
effectiveAt: time.Date(2026, 4, 10, 17, 30, 0, 0, time.UTC),
|
||||
messages: []string{
|
||||
"Periods of rain are expected through Saturday, with locally heavier amounts possible.",
|
||||
"Cooler temperatures return late this weekend.",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "key points alias",
|
||||
filename: "forecast_discussion_mfr_key_points_sample.html",
|
||||
id: "evt-discussion-mfr",
|
||||
source: "nws-discussion-mfr-test",
|
||||
emittedAt: time.Date(2026, 4, 10, 19, 46, 0, 0, time.UTC),
|
||||
effectiveAt: time.Date(2026, 4, 10, 19, 45, 0, 0, time.UTC),
|
||||
messages: []string{
|
||||
"Gusty winds will develop over exposed ridges, especially during the afternoon.",
|
||||
"Inland valleys remain dry through Saturday.",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
in := event.Event{
|
||||
ID: tt.id,
|
||||
Kind: event.Kind(standards.KindForecastDiscussion),
|
||||
Source: tt.source,
|
||||
EmittedAt: tt.emittedAt,
|
||||
Schema: standards.SchemaRawNWSForecastDiscussionV1,
|
||||
Payload: loadForecastDiscussionFixtureHTML(t, tt.filename),
|
||||
}
|
||||
|
||||
out, err := (ForecastDiscussionNormalizer{}).Normalize(nil, in)
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v", err)
|
||||
}
|
||||
if out == nil {
|
||||
t.Fatalf("Normalize() returned nil output")
|
||||
}
|
||||
if out.ID != in.ID || out.Source != in.Source || !out.EmittedAt.Equal(in.EmittedAt) {
|
||||
t.Fatalf("envelope = %#v, want ID/source/emittedAt from input", out)
|
||||
}
|
||||
if out.Kind != event.Kind(standards.KindForecastDiscussion) {
|
||||
t.Fatalf("Kind = %q, want forecast_discussion", out.Kind)
|
||||
}
|
||||
if out.Schema != standards.SchemaWeatherForecastDiscussionV1 {
|
||||
t.Fatalf("Schema = %q, want %q", out.Schema, standards.SchemaWeatherForecastDiscussionV1)
|
||||
}
|
||||
if out.EffectiveAt == nil || !out.EffectiveAt.Equal(tt.effectiveAt) {
|
||||
t.Fatalf("EffectiveAt = %v, want %s", out.EffectiveAt, tt.effectiveAt.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
payload, ok := out.Payload.(model.WeatherForecastDiscussion)
|
||||
if !ok {
|
||||
t.Fatalf("Payload type = %T, want model.WeatherForecastDiscussion", out.Payload)
|
||||
}
|
||||
if !reflect.DeepEqual(payload.KeyMessages, tt.messages) {
|
||||
t.Fatalf("KeyMessages = %#v, want %#v", payload.KeyMessages, tt.messages)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal(payload) error = %v", err)
|
||||
}
|
||||
var fields map[string]any
|
||||
if err := json.Unmarshal(b, &fields); err != nil {
|
||||
t.Fatalf("json.Unmarshal(payload) error = %v", err)
|
||||
}
|
||||
for _, key := range []string{"nearTerm", "discussion", "aviation", "sections"} {
|
||||
if _, ok := fields[key]; ok {
|
||||
t.Fatalf("unexpected key %q in canonical payload", key)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastDiscussionNormalizerRejectsMissingIssueTime(t *testing.T) {
|
||||
_, err := (ForecastDiscussionNormalizer{}).Normalize(nil, event.Event{
|
||||
ID: "evt-discussion-bad",
|
||||
@@ -283,6 +376,17 @@ func loadForecastDiscussionBOUSampleHTML(t *testing.T) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func loadForecastDiscussionFixtureHTML(t *testing.T, filename string) string {
|
||||
t.Helper()
|
||||
|
||||
path := filepath.Join("..", "..", "providers", "nws", "testdata", filename)
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("os.ReadFile(%q) error = %v", path, err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func loadMixedFormatForecastDiscussionSampleHTML(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user