package pipeline import ( "encoding/json" "testing" ) func TestProducerControlsResolvePrecedenceAndExplicitZero(t *testing.T) { strict, permissive := ModeStrict, ModePermissive auto, bypass, refresh := CacheAuto, CacheBypass, CacheRefresh zero, five := 0, 5 tests := []struct { name string profile PipelineProfile binding ModuleBinding invocation *CacheMode want ProducerControls }{ {"defaults", PipelineProfile{}, ModuleBinding{}, nil, ProducerControls{ModeStrict, 2, CacheAuto}}, {"pipeline", PipelineProfile{Mode: &permissive, MaxAdditionalTurns: &five, Cache: &refresh}, ModuleBinding{}, nil, ProducerControls{ModePermissive, 5, CacheRefresh}}, {"binding", PipelineProfile{Mode: &permissive, MaxAdditionalTurns: &five, Cache: &refresh}, ModuleBinding{Mode: &strict, MaxAdditionalTurns: &zero, Cache: &auto}, nil, ProducerControls{ModeStrict, 0, CacheAuto}}, {"invocation", PipelineProfile{Cache: &refresh}, ModuleBinding{Cache: &auto}, &bypass, ProducerControls{ModeStrict, 2, 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) } }) } } func TestProducerControlsRejectInvalidValuesWithoutMaximumBudgets(t *testing.T) { invalidMode, invalidCache := ExecutionMode("unknown"), 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) } } func TestModuleBindingRoundTripRetainsExplicitControls(t *testing.T) { strict, bypass, zero := ModeStrict, CacheBypass, 0 input := ModuleBinding{Module: "fixture", Mode: &strict, Cache: &bypass, MaxAdditionalTurns: &zero} data, err := json.Marshal(input) if err != nil { t.Fatal(err) } var output ModuleBinding if err := json.Unmarshal(data, &output); err != nil { t.Fatal(err) } 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) } } func TestResolvedProducerControlsApplyToEveryLaneOperation(t *testing.T) { profile := baselineProfile() permissive, bypass, zero := ModePermissive, CacheBypass, 0 profile.Mode = &permissive profile.Chunk.MaxAdditionalTurns = &zero resolved, err := ResolvePipeline(profile, ResolveOptions{CacheOverride: &bypass}, newProfileCatalog(t)) if err != nil { t.Fatal(err) } if resolved.ChunkControls != (ProducerControls{ModePermissive, 0, CacheBypass}) { t.Fatalf("chunk controls = %#v", resolved.ChunkControls) } for _, step := range resolved.Steps { for _, lane := range step.ArtifactLanes { for _, got := range []ProducerControls{lane.ExtractControls, lane.MergeControls, lane.NormalizeControls} { if got != (ProducerControls{ModePermissive, 2, CacheBypass}) { t.Fatalf("lane controls = %#v", got) } } } } }