package generatedtext import ( "strings" "testing" ) func TestValidateDailyNormalizesJSON(t *testing.T) { value, normalized, err := ValidateDaily([]byte(`{ "summary": " Showers are possible during the selected day. ", "forecast_discussion": [ " A front will keep rain chances in the forecast. ", "", " Temperatures stay seasonable by afternoon. " ], "precipitation_timing": " Rain is most likely during the afternoon. ", "confidence": " Medium " }`)) if err != nil { t.Fatalf("ValidateDaily() error = %v", err) } if value.Summary != "Showers are possible during the selected day." { t.Fatalf("Summary = %q, want trimmed summary", value.Summary) } if strings.Join(value.ForecastDiscussion, "|") != "A front will keep rain chances in the forecast.|Temperatures stay seasonable by 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) } want := `{"summary":"Showers are possible during the selected day.","forecast_discussion":["A front will keep rain chances in the forecast.","Temperatures stay seasonable by afternoon."],"precipitation_timing":"Rain is most likely during the afternoon.","confidence":"Medium"}` if string(normalized) != want { t.Fatalf("normalized = %s, want %s", normalized, want) } } func TestValidateDailyOmitsEmptyOptionalFields(t *testing.T) { _, normalized, err := ValidateDaily([]byte(`{ "summary": "Showers are possible during the selected day.", "forecast_discussion": ["A front will keep rain chances in the forecast."], "precipitation_timing": " ", "confidence": " " }`)) if err != nil { t.Fatalf("ValidateDaily() error = %v", err) } want := `{"summary":"Showers are possible during the selected day.","forecast_discussion":["A front will keep rain chances in the forecast."]}` if string(normalized) != want { t.Fatalf("normalized = %s, want %s", normalized, want) } } func TestValidateDailyRejectsInvalidInput(t *testing.T) { tests := []struct { name string in string want string }{ { name: "malformed", in: `{`, want: "decode daily generated text", }, { name: "unknown field", in: `{"summary":"Showers are possible during the selected day.","forecast_discussion":["A front will keep rain chances in the forecast."],"extra":"value"}`, want: `unknown field "extra"`, }, { name: "missing summary", in: `{"forecast_discussion":["A front will keep rain chances in the forecast."]}`, want: "summary is required", }, { name: "blank summary", in: `{"summary":" ","forecast_discussion":["A front will keep rain chances in the forecast."]}`, want: "summary is required", }, { name: "missing forecast discussion", in: `{"summary":"Showers are possible during the selected day."}`, want: "forecast discussion is required", }, { name: "blank forecast discussion", in: `{"summary":"Showers are possible during the selected day.","forecast_discussion":[" ",""]}`, want: "forecast discussion is required", }, { name: "forecast discussion wrong type", in: `{"summary":"Showers are possible during the selected day.","forecast_discussion":"A front will keep rain chances in the forecast."}`, want: "cannot unmarshal string", }, { name: "multiple values", in: `{"summary":"Showers are possible during the selected day.","forecast_discussion":["A front will keep rain chances in the forecast."]} {}`, want: "multiple JSON values", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { _, _, err := ValidateDaily([]byte(test.in)) if err == nil { t.Fatal("ValidateDaily() error = nil, want error") } if !strings.Contains(err.Error(), test.want) { t.Fatalf("ValidateDaily() error = %v, want %q", err, test.want) } }) } }