From 7dc8e52866aefb288db3626f3872c9d7f7d627c3 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Sun, 29 Nov 2026 09:31:20 +0000 Subject: [PATCH] test: check normalized validation documents --- main_test.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/main_test.go b/main_test.go index ce1076f..8c339a5 100644 --- a/main_test.go +++ b/main_test.go @@ -15,7 +15,9 @@ import ( "testing" "github.com/gofiber/fiber/v3" + publiccodeParser "github.com/publiccodeyml/libpubliccode/v5" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( @@ -164,6 +166,32 @@ func TestUnhandledErrors(t *testing.T) { } } +func assertNormalizedDocument(t *testing.T, response map[string]any) { + t.Helper() + + normalized, ok := response["normalized"].(string) + require.True(t, ok, "normalized must be a YAML string") + require.NotEmpty(t, normalized) + + parser, err := publiccodeParser.NewParser(publiccodeParser.ParserConfig{DisableExternalChecks: true}) + require.NoError(t, err) + parsed, err := parser.ParseStream(strings.NewReader(normalized)) + require.NoError(t, err, "normalized document must remain valid") + document, ok := parsed.(publiccodeParser.PublicCodeV0) + require.True(t, ok) + + assert.Equal(t, "Medusa", document.Name) + require.NotNil(t, document.URL) + assert.Equal(t, "https://github.com/example/project.git", (*url.URL)(document.URL).String()) + assert.Equal(t, "AGPL-3.0-or-later", document.Legal.License) + assert.Equal(t, []string{"web"}, document.Platforms) + description, ok := document.Description["en-GB"] + require.True(t, ok) + assert.Equal(t, "A rather short description which is probably useless\n", description.ShortDescription) + require.NotNil(t, description.Features) + assert.Equal(t, []string{"Just one feature"}, *description.Features) +} + func TestValidateEndpoint(t *testing.T) { validYml := loadTestdata(t, "valid.publiccode.yml") invalidYml := loadTestdata(t, "invalid.publiccode.yml") @@ -205,7 +233,7 @@ func TestValidateEndpoint(t *testing.T) { results, ok := response["results"].([]any) assert.True(t, ok) assert.Len(t, results, 0) - assert.NotNil(t, response["normalized"]) + assertNormalizedDocument(t, response) }, }, { @@ -263,7 +291,7 @@ func TestValidateEndpoint(t *testing.T) { results, ok := response["results"].([]any) assert.True(t, ok) assert.Len(t, results, 0) - assert.NotNil(t, response["normalized"]) + assertNormalizedDocument(t, response) }, }, {