2026-06-15 14:45:49 +00:00
package generatedtext
import (
"strings"
"testing"
)
func TestValidateTodayNormalizesJSON ( t * testing . T ) {
value , normalized , err := ValidateToday ( [ ] byte ( ` {
"summary" : " Showers are likely today. " ,
"forecast_discussion" : [
" A front will keep rain chances elevated. " ,
"" ,
" Temperatures stay mild through the afternoon. "
] ,
2026-08-01 01:25:57 +00:00
"precipitation_timing" : " Rain is most likely during the afternoon. "
2026-06-15 14:45:49 +00:00
} ` ) )
if err != nil {
t . Fatalf ( "ValidateToday() error = %v" , err )
}
if value . Summary != "Showers are likely today." {
t . Fatalf ( "Summary = %q, want trimmed summary" , value . Summary )
}
if strings . Join ( value . ForecastDiscussion , "|" ) != "A front will keep rain chances elevated.|Temperatures stay mild through the afternoon." {
t . Fatalf ( "ForecastDiscussion = %#v, want trimmed non-empty paragraphs" , value . ForecastDiscussion )
}
if value . PrecipitationTiming != "Rain is most likely during the afternoon." {
t . Fatalf ( "PrecipitationTiming = %q, want trimmed precipitation timing" , value . PrecipitationTiming )
}
2026-08-01 01:25:57 +00:00
want := ` { "summary":"Showers are likely today.","forecast_discussion":["A front will keep rain chances elevated.","Temperatures stay mild through the afternoon."],"precipitation_timing":"Rain is most likely during the afternoon."} `
2026-06-15 14:45:49 +00:00
if string ( normalized ) != want {
t . Fatalf ( "normalized = %s, want %s" , normalized , want )
}
}
2026-08-01 01:25:57 +00:00
func TestValidateTodayPreservesRequiredEmptyPrecipitationTiming ( t * testing . T ) {
2026-06-15 14:45:49 +00:00
_ , normalized , err := ValidateToday ( [ ] byte ( ` {
"summary" : "Showers are likely today." ,
"forecast_discussion" : [ "A front will keep rain chances elevated." ] ,
2026-08-01 01:25:57 +00:00
"precipitation_timing" : " "
2026-06-15 14:45:49 +00:00
} ` ) )
if err != nil {
t . Fatalf ( "ValidateToday() error = %v" , err )
}
2026-08-01 01:25:57 +00:00
want := ` { "summary":"Showers are likely today.","forecast_discussion":["A front will keep rain chances elevated."],"precipitation_timing":""} `
2026-06-15 14:45:49 +00:00
if string ( normalized ) != want {
t . Fatalf ( "normalized = %s, want %s" , normalized , want )
}
}
func TestValidateTodayRejectsInvalidInput ( t * testing . T ) {
tests := [ ] struct {
name string
in string
want string
} {
{
name : "malformed" ,
in : ` { ` ,
want : "decode today generated text" ,
} ,
{
name : "unknown field" ,
in : ` { "summary":"Showers are likely today.","forecast_discussion":["A front will keep rain chances elevated."],"extra":"value"} ` ,
want : ` unknown field "extra" ` ,
} ,
{
name : "missing summary" ,
in : ` { "forecast_discussion":["A front will keep rain chances elevated."]} ` ,
want : "summary is required" ,
} ,
{
name : "blank summary" ,
in : ` { "summary":" ","forecast_discussion":["A front will keep rain chances elevated."]} ` ,
want : "summary is required" ,
} ,
{
name : "missing forecast discussion" ,
in : ` { "summary":"Showers are likely today."} ` ,
want : "forecast discussion is required" ,
} ,
{
name : "blank forecast discussion" ,
in : ` { "summary":"Showers are likely today.","forecast_discussion":[" ",""]} ` ,
want : "forecast discussion is required" ,
} ,
{
name : "forecast discussion wrong type" ,
in : ` { "summary":"Showers are likely today.","forecast_discussion":"A front will keep rain chances elevated."} ` ,
want : "cannot unmarshal string" ,
} ,
{
name : "multiple values" ,
in : ` { "summary":"Showers are likely today.","forecast_discussion":["A front will keep rain chances elevated."]} { } ` ,
want : "multiple JSON values" ,
} ,
}
for _ , test := range tests {
t . Run ( test . name , func ( t * testing . T ) {
_ , _ , err := ValidateToday ( [ ] byte ( test . in ) )
if err == nil {
t . Fatal ( "ValidateToday() error = nil, want error" )
}
if ! strings . Contains ( err . Error ( ) , test . want ) {
t . Fatalf ( "ValidateToday() error = %v, want %q" , err , test . want )
}
} )
}
}