Files
notarius/internal/framework/pipeline/validation_policy_test.go

91 lines
4.0 KiB
Go
Raw Normal View History

2026-08-26 23:34:06 +00:00
package pipeline
import (
"encoding/json"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
2026-08-26 23:34:06 +00:00
)
func TestProducerControlsResolvePrecedenceAndExplicitZero(t *testing.T) {
strict, permissive := contracts.ModeStrict, contracts.ModePermissive
auto, bypass, refresh := contracts.CacheAuto, contracts.CacheBypass, contracts.CacheRefresh
zero, five := 0, 5
tests := []struct {
name string
profile PipelineProfile
binding ModuleBinding
invocation *contracts.CacheMode
want contracts.ProducerControls
}{
{"defaults", PipelineProfile{}, ModuleBinding{}, nil, contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 2, Cache: contracts.CacheAuto}},
{"pipeline", PipelineProfile{Mode: &permissive, MaxAdditionalTurns: &five, Cache: &refresh}, ModuleBinding{}, nil, contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 5, Cache: contracts.CacheRefresh}},
{"binding", PipelineProfile{Mode: &permissive, MaxAdditionalTurns: &five, Cache: &refresh}, ModuleBinding{Mode: &strict, MaxAdditionalTurns: &zero, Cache: &auto}, nil, contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 0, Cache: contracts.CacheAuto}},
{"invocation", PipelineProfile{Cache: &refresh}, ModuleBinding{Cache: &auto}, &bypass, contracts.ProducerControls{Mode: contracts.ModeStrict, MaxAdditionalTurns: 2, Cache: contracts.CacheBypass}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := ResolveProducerControls(test.binding, test.profile, test.invocation)
if err != nil {
t.Fatal(err)
}
if got != test.want {
t.Fatalf("controls = %#v, want %#v", got, test.want)
}
})
2026-08-26 23:34:06 +00:00
}
}
func TestProducerControlsRejectInvalidValuesWithoutMaximumBudgets(t *testing.T) {
invalidMode, invalidCache := contracts.ExecutionMode("unknown"), contracts.CacheMode("unknown")
negative, large := -1, 1000000
for _, binding := range []ModuleBinding{{Mode: &invalidMode}, {Cache: &invalidCache}, {MaxAdditionalTurns: &negative}} {
if _, err := ResolveProducerControls(binding, PipelineProfile{}, nil); err == nil {
t.Fatalf("accepted invalid controls: %#v", binding)
}
}
got, err := ResolveProducerControls(ModuleBinding{MaxAdditionalTurns: &large}, PipelineProfile{}, nil)
if err != nil || got.MaxAdditionalTurns != large {
t.Fatalf("large allowance = %#v, %v", got, err)
2026-08-26 23:34:06 +00:00
}
}
func TestModuleBindingRoundTripRetainsExplicitControls(t *testing.T) {
strict, bypass, zero := contracts.ModeStrict, contracts.CacheBypass, 0
input := ModuleBinding{Module: "fixture", Mode: &strict, Cache: &bypass, MaxAdditionalTurns: &zero}
data, err := json.Marshal(input)
2026-08-26 23:34:06 +00:00
if err != nil {
t.Fatal(err)
2026-08-26 23:34:06 +00:00
}
var output ModuleBinding
if err := json.Unmarshal(data, &output); err != nil {
t.Fatal(err)
2026-08-26 23:34:06 +00:00
}
if output.Mode == nil || *output.Mode != strict || output.Cache == nil || *output.Cache != bypass || output.MaxAdditionalTurns == nil || *output.MaxAdditionalTurns != 0 {
t.Fatalf("controls lost in round-trip: %s", data)
2026-08-26 23:34:06 +00:00
}
}
func TestResolvedProducerControlsApplyToEveryLaneOperation(t *testing.T) {
profile := baselineProfile()
permissive, bypass, zero := contracts.ModePermissive, contracts.CacheBypass, 0
profile.Mode = &permissive
profile.Chunk.MaxAdditionalTurns = &zero
resolved, err := ResolvePipeline(profile, ResolveOptions{CacheOverride: &bypass}, newProfileCatalog(t))
if err != nil {
t.Fatal(err)
2026-08-26 23:34:06 +00:00
}
if resolved.ChunkControls != (contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 0, Cache: contracts.CacheBypass}) {
t.Fatalf("chunk controls = %#v", resolved.ChunkControls)
2026-08-26 23:34:06 +00:00
}
for _, step := range resolved.Steps {
for _, lane := range step.ArtifactLanes {
for _, got := range []contracts.ProducerControls{lane.ExtractControls, lane.MergeControls, lane.NormalizeControls} {
if got != (contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 2, Cache: contracts.CacheBypass}) {
t.Fatalf("lane controls = %#v", got)
}
}
}
2026-08-26 23:34:06 +00:00
}
}