From 429869bef8076bfd86fc578e786a407437fae032 Mon Sep 17 00:00:00 2001 From: froppa <5844892+froppa@users.noreply.github.com> Date: Mon, 21 Sep 2026 14:39:16 +0200 Subject: [PATCH] Struktly: Add internal/struktly/policy/checker_test.go covering Load and Check --- internal/struktly/policy/checker_test.go | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 internal/struktly/policy/checker_test.go diff --git a/internal/struktly/policy/checker_test.go b/internal/struktly/policy/checker_test.go new file mode 100644 index 0000000..144ddd6 --- /dev/null +++ b/internal/struktly/policy/checker_test.go @@ -0,0 +1,46 @@ +package policy + +import ( + "os" + "path/filepath" + "testing" + + "github.com/froppa/stackkit/internal/struktly/ir" + "github.com/stretchr/testify/require" +) + +const fixture = `max_handlers: 2 +required: + - tests +` + +// loadFixture writes the policy fixture into a temp dir and loads it with Load. +func loadFixture(t *testing.T) *Policy { + t.Helper() + path := filepath.Join(t.TempDir(), "policy.yaml") + require.NoError(t, os.WriteFile(path, []byte(fixture), 0o600)) + + p, err := Load(path) + require.NoError(t, err) + require.Equal(t, 2, p.MaxHandlers) + require.Equal(t, []string{"tests"}, p.Required) + return p +} + +func TestCheckWithinLimit(t *testing.T) { + p := loadFixture(t) + + for _, n := range []int{1, 2} { + svc := &ir.Service{Name: "svc", Handlers: make([]ir.Handler, n)} + require.NoError(t, Check(p, svc), "handlers=%d", n) + } +} + +func TestCheckTooManyHandlers(t *testing.T) { + p := loadFixture(t) + svc := &ir.Service{Name: "svc", Handlers: make([]ir.Handler, 3)} + + err := Check(p, svc) + require.Error(t, err) + require.ErrorContains(t, err, "policy violation: too many handlers") +}