2026-06-14 04:46:53 +00:00
package generatedtext
import (
"strings"
"testing"
)
func TestValidateHourlyNormalizesJSON ( t * testing . T ) {
value , normalized , err := ValidateHourly ( [ ] byte ( ` {
"summary" : " Storm chances increase. " ,
2026-06-14 12:54:32 -05:00
"forecast_discussion" : " A front will keep the region unsettled. " ,
"precipitation_timing" : " Showers are most likely early this afternoon. " ,
"confidence" : " Medium "
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 )
}
want := ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","precipitation_timing":"Showers are most likely early this afternoon.","confidence":"Medium"} `
2026-06-14 04:46:53 +00:00
if string ( normalized ) != want {
t . Fatalf ( "normalized = %s, want %s" , normalized , want )
}
}
func TestValidateHourlyOmitsEmptyConfidence ( t * testing . T ) {
_ , normalized , err := ValidateHourly ( [ ] byte ( ` {
"summary" : "Storm chances increase." ,
2026-06-14 12:54:32 -05:00
"forecast_discussion" : "A front will keep the region unsettled." ,
"precipitation_timing" : " " ,
2026-06-14 04:46:53 +00:00
"confidence" : " "
} ` ) )
if err != nil {
t . Fatalf ( "ValidateHourly() error = %v" , err )
}
2026-06-14 12:54:32 -05:00
want := ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled."} `
2026-06-14 04:46:53 +00:00
if string ( normalized ) != want {
t . Fatalf ( "normalized = %s, want %s" , normalized , want )
}
}
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-06-14 04:46:53 +00:00
want : ` unknown field "extra" ` ,
} ,
{
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" ,
} ,
{
name : "old timing field rejected" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","timing":"Late morning."} ` ,
want : ` unknown field "timing" ` ,
} ,
{
name : "old impacts field rejected" ,
in : ` { "summary":"Storm chances increase.","forecast_discussion":"A front will keep the region unsettled.","impacts":"Brief downpours."} ` ,
want : ` unknown field "impacts" ` ,
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 ) {
_ , _ , err := ValidateHourly ( [ ] byte ( test . in ) )
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 )
}
} )
}
}