2026-06-14 04:46:53 +00:00
package generatedtext
import (
"strings"
"testing"
)
2026-08-13 04:10:45 +00:00
func TestValidateHourlyNormalizesFields ( t * testing . T ) {
value , err := ValidateHourly ( [ ] byte ( ` {
2026-06-14 04:46:53 +00:00
"summary" : " Storm chances increase. " ,
2026-06-14 12:54:32 -05:00
"forecast_discussion" : " A front will keep the region unsettled. " ,
2026-08-01 01:25:57 +00:00
"precipitation_timing" : " Showers are most likely early this afternoon. "
2026-06-14 04:46:53 +00:00
} ` ) )
if err != nil {
t . Fatalf ( "ValidateHourly() error = %v" , err )
}
if value . Summary != "Storm chances increase." {
t . Fatalf ( "Summary = %q, want trimmed summary" , value . Summary )
}
2026-06-14 12:54:32 -05:00
if value . ForecastDiscussion != "A front will keep the region unsettled." {
t . Fatalf ( "ForecastDiscussion = %q, want trimmed discussion" , value . ForecastDiscussion )
}
if value . PrecipitationTiming != "Showers are most likely early this afternoon." {
t . Fatalf ( "PrecipitationTiming = %q, want trimmed precipitation timing" , value . PrecipitationTiming )
}
2026-06-14 04:46:53 +00:00
}
2026-08-01 01:25:57 +00:00
func TestValidateHourlyPreservesRequiredEmptyPrecipitationTiming ( t * testing . T ) {
2026-08-13 04:10:45 +00:00
value , err := ValidateHourly ( [ ] byte ( ` {
2026-06-14 04:46:53 +00:00
"summary" : "Storm chances increase." ,
2026-06-14 12:54:32 -05:00
"forecast_discussion" : "A front will keep the region unsettled." ,
2026-08-01 01:25:57 +00:00
"precipitation_timing" : " "
2026-06-14 04:46:53 +00:00
} ` ) )
if err != nil {
t . Fatalf ( "ValidateHourly() error = %v" , err )
}
2026-08-13 04:10:45 +00:00
if value . PrecipitationTiming != "" {
t . Fatalf ( "PrecipitationTiming = %q, want required empty value" , value . PrecipitationTiming )
2026-06-14 04:46:53 +00:00
}
}
func TestValidateHourlyRejectsInvalidInput ( t * testing . T ) {
tests := [ ] struct {
name string
in string
want string
} {
{
name : "malformed" ,
in : ` { ` ,
want : "decode hourly generated text" ,
} ,
{
name : "unknown field" ,
2026-06-14 12:54:32 -05:00
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","extra":"value"} ` ,
2026-08-13 01:47:47 +00:00
want : "unsupported field" ,
2026-06-14 04:46:53 +00:00
} ,
{
name : "missing summary" ,
2026-06-14 12:54:32 -05:00
in : ` { "forecast_discussion":"A front will keep the region unsettled."} ` ,
2026-06-14 04:46:53 +00:00
want : "summary is required" ,
} ,
{
2026-06-14 12:54:32 -05:00
name : "blank forecast discussion" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":" "} ` ,
want : "forecast discussion is required" ,
} ,
2026-08-01 01:25:57 +00:00
{
name : "missing precipitation timing" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled."} ` ,
want : "precipitation timing is required" ,
} ,
{
name : "null precipitation timing" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","precipitation_timing":null} ` ,
want : "precipitation timing must be a string" ,
} ,
{
name : "retired confidence field rejected" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","precipitation_timing":"","confidence":"Medium"} ` ,
2026-08-13 01:47:47 +00:00
want : "unsupported field" ,
2026-08-01 01:25:57 +00:00
} ,
2026-06-14 12:54:32 -05:00
{
name : "old timing field rejected" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","timing":"Late morning."} ` ,
2026-08-13 01:47:47 +00:00
want : "unsupported field" ,
2026-06-14 12:54:32 -05:00
} ,
{
name : "old impacts field rejected" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","impacts":"Brief downpours."} ` ,
2026-08-13 01:47:47 +00:00
want : "unsupported field" ,
2026-06-14 04:46:53 +00:00
} ,
{
name : "multiple values" ,
2026-06-14 12:54:32 -05:00
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled."} { } ` ,
2026-06-14 04:46:53 +00:00
want : "multiple JSON values" ,
} ,
}
for _ , test := range tests {
t . Run ( test . name , func ( t * testing . T ) {
2026-08-13 04:10:45 +00:00
_ , err := ValidateHourly ( [ ] byte ( test . in ) )
2026-06-14 04:46:53 +00:00
if err == nil {
t . Fatal ( "ValidateHourly() error = nil, want error" )
}
if ! strings . Contains ( err . Error ( ) , test . want ) {
t . Fatalf ( "ValidateHourly() error = %v, want %q" , err , test . want )
}
} )
}
}