2026-08-26 23:34:06 +00:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"testing"
|
2026-11-21 23:12:01 +00:00
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
2026-08-26 23:34:06 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-21 22:49:41 +00:00
|
|
|
func TestProducerControlsResolvePrecedenceAndExplicitZero(t *testing.T) {
|
2026-11-21 23:12:01 +00:00
|
|
|
strict, permissive := contracts.ModeStrict, contracts.ModePermissive
|
|
|
|
|
auto, bypass, refresh := contracts.CacheAuto, contracts.CacheBypass, contracts.CacheRefresh
|
2026-11-21 22:49:41 +00:00
|
|
|
zero, five := 0, 5
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
profile PipelineProfile
|
|
|
|
|
binding ModuleBinding
|
2026-11-21 23:12:01 +00:00
|
|
|
invocation *contracts.CacheMode
|
|
|
|
|
want contracts.ProducerControls
|
2026-11-21 22:49:41 +00:00
|
|
|
}{
|
2026-11-22 04:28:48 +00:00
|
|
|
{"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}},
|
2026-11-21 22:49:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 22:49:41 +00:00
|
|
|
func TestProducerControlsRejectInvalidValuesWithoutMaximumBudgets(t *testing.T) {
|
2026-11-21 23:12:01 +00:00
|
|
|
invalidMode, invalidCache := contracts.ExecutionMode("unknown"), contracts.CacheMode("unknown")
|
2026-11-21 22:49:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 22:49:41 +00:00
|
|
|
func TestModuleBindingRoundTripRetainsExplicitControls(t *testing.T) {
|
2026-11-21 23:12:01 +00:00
|
|
|
strict, bypass, zero := contracts.ModeStrict, contracts.CacheBypass, 0
|
2026-11-21 22:49:41 +00:00
|
|
|
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 {
|
2026-11-21 22:49:41 +00:00
|
|
|
t.Fatal(err)
|
2026-08-26 23:34:06 +00:00
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
var output ModuleBinding
|
|
|
|
|
if err := json.Unmarshal(data, &output); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2026-08-26 23:34:06 +00:00
|
|
|
}
|
2026-11-21 22:49:41 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 22:49:41 +00:00
|
|
|
func TestResolvedProducerControlsApplyToEveryLaneOperation(t *testing.T) {
|
|
|
|
|
profile := baselineProfile()
|
2026-11-21 23:12:01 +00:00
|
|
|
permissive, bypass, zero := contracts.ModePermissive, contracts.CacheBypass, 0
|
2026-11-21 22:49:41 +00:00
|
|
|
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
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
if resolved.ChunkControls != (contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 0, Cache: contracts.CacheBypass}) {
|
2026-11-21 22:49:41 +00:00
|
|
|
t.Fatalf("chunk controls = %#v", resolved.ChunkControls)
|
2026-08-26 23:34:06 +00:00
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
for _, step := range resolved.Steps {
|
|
|
|
|
for _, lane := range step.ArtifactLanes {
|
2026-11-21 23:12:01 +00:00
|
|
|
for _, got := range []contracts.ProducerControls{lane.ExtractControls, lane.MergeControls, lane.NormalizeControls} {
|
2026-11-22 04:28:48 +00:00
|
|
|
if got != (contracts.ProducerControls{Mode: contracts.ModePermissive, MaxAdditionalTurns: 2, Cache: contracts.CacheBypass}) {
|
2026-11-21 22:49:41 +00:00
|
|
|
t.Fatalf("lane controls = %#v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-26 23:34:06 +00:00
|
|
|
}
|
|
|
|
|
}
|