From 2ed2779c6f5fb2a350cc1aad6bc56b905b27a572 Mon Sep 17 00:00:00 2001 From: froppa <5844892+froppa@users.noreply.github.com> Date: Mon, 21 Sep 2026 13:50:23 +0200 Subject: [PATCH] Struktly: Add internal/struktly/ir/parser_test.go covering Parse and sanitize --- internal/struktly/ir/parser_test.go | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 internal/struktly/ir/parser_test.go diff --git a/internal/struktly/ir/parser_test.go b/internal/struktly/ir/parser_test.go new file mode 100644 index 0000000..9b79452 --- /dev/null +++ b/internal/struktly/ir/parser_test.go @@ -0,0 +1,60 @@ +package ir + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParse(t *testing.T) { + // One path and one method: spec.Paths is a map, so handler order is not deterministic. + spec := `info: + title: Pet Store API +paths: + /pets: + get: + summary: List all pets + operationId: list-pets +` + path := filepath.Join(t.TempDir(), "openapi.yaml") + require.NoError(t, os.WriteFile(path, []byte(spec), 0o600)) + + svc, err := Parse(path) + require.NoError(t, err) + require.NotNil(t, svc) + + assert.Equal(t, "PetStoreAPI", svc.Name) + require.Len(t, svc.Handlers, 1) + assert.Equal(t, Handler{ + Name: "ListPets", + Method: "GET", + Path: "/pets", + Description: "List all pets", + }, svc.Handlers[0]) +} + +func TestParseMissingFile(t *testing.T) { + svc, err := Parse(filepath.Join(t.TempDir(), "missing.yaml")) + require.Error(t, err) + assert.Nil(t, svc) +} + +func TestSanitize(t *testing.T) { + tests := []struct { + in, want string + }{ + {"list-pets", "ListPets"}, + {"get_pet_by_id", "GetPetById"}, + {"GET_v2/pets", "GETV2Pets"}, + {"listPets", "ListPets"}, + {"", ""}, + } + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + assert.Equal(t, tt.want, sanitize(tt.in)) + }) + } +}