Centralize pure pipeline profile and binding copies

This commit is contained in:
2026-11-26 02:29:52 +00:00
parent 383b43ab08
commit ad357db22d
17 changed files with 271 additions and 257 deletions

View File

@@ -0,0 +1,63 @@
package pipeline
import (
"encoding/json"
"reflect"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func TestClonePipelineProfilePreservesAndDetachesMutableValues(t *testing.T) {
mode, cache, turns := contracts.ExecutionMode(" permissive "), contracts.CacheMode(" bypass "), 0
profile := PipelineProfile{
ID: " profile ", LLMProfile: " profile-name ", Mode: &mode, Cache: &cache, MaxAdditionalTurns: &turns,
References: map[string]ReferenceSource{"top": {Artifact: &ArtifactReference{Step: " step ", Lane: " lane "}}},
Artifacts: map[string]ArtifactLaneProfile{},
Steps: []PipelineStepProfile{{ID: " step ", References: map[string]ReferenceSource{"external": {Path: " path "}}, Artifacts: map[string]ArtifactLaneProfile{
" lane ": {References: map[string]ReferenceSource{"lane": {Artifact: &ArtifactReference{Step: " source ", Lane: " value "}}},
Extract: ModuleBinding{Module: " extractor ", LLMProfile: " model ", Mode: &mode, Cache: &cache, MaxAdditionalTurns: &turns,
Options: map[string]any{"nested": map[string]any{"list": []any{[]string{"a"}, []byte("b"), json.RawMessage(`{"c":1}`)}}, "empty": []any{}, "scalar": 42},
References: map[string]ReferenceSource{"input": {Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}},
Validators: ValidatorOverride{Set: true, Validators: []ModuleBinding{{Module: " validator ", Options: map[string]any{"bytes": []byte("v")}}}},
}, Validators: []ModuleBinding{{Module: " lane-validator "}}},
}}},
}
copy := ClonePipelineProfile(profile)
if !reflect.DeepEqual(copy, profile) {
t.Fatalf("clone changed values: %#v", copy)
}
*copy.Mode = contracts.ExecutionMode("strict")
*copy.MaxAdditionalTurns = 2
*copy.Cache = contracts.CacheMode("refresh")
copy.References["top"].Artifact.Step = "changed"
step := &copy.Steps[0]
step.References["external"] = ReferenceSource{Path: "changed"}
lane := step.Artifacts[" lane "]
lane.References["lane"].Artifact.Step = "changed"
lane.Extract.References["input"].Artifact.Lane = "changed"
list := lane.Extract.Options["nested"].(map[string]any)["list"].([]any)
list[0].([]string)[0] = "changed"
list[1].([]byte)[0] = 'x'
list[2].(json.RawMessage)[2] = 'x'
lane.Extract.Validators.Validators[0].Options["bytes"].([]byte)[0] = 'x'
lane.Validators[0].Module = "changed"
step.Artifacts[" lane "] = lane
original := profile.Steps[0].Artifacts[" lane "]
originalList := original.Extract.Options["nested"].(map[string]any)["list"].([]any)
if *profile.Mode != " permissive " || *profile.Cache != " bypass " || *profile.MaxAdditionalTurns != 0 || profile.References["top"].Artifact.Step != " step " || profile.Steps[0].References["external"].Path != " path " || original.References["lane"].Artifact.Step != " source " || original.Extract.References["input"].Artifact.Lane != " output " || originalList[0].([]string)[0] != "a" || string(originalList[1].([]byte)) != "b" || string(originalList[2].(json.RawMessage)) != `{"c":1}` || string(original.Extract.Validators.Validators[0].Options["bytes"].([]byte)) != "v" || original.Validators[0].Module != " lane-validator " {
t.Fatal("clone mutation reached the original profile")
}
}
func TestClonePipelineProfilePreservesNilAndEmptyContainers(t *testing.T) {
for _, profile := range []PipelineProfile{
{},
{Artifacts: map[string]ArtifactLaneProfile{}, Steps: []PipelineStepProfile{}, References: map[string]ReferenceSource{}, Input: ModuleBinding{Options: map[string]any{"list": []any{}, "strings": []string{}, "bytes": []byte{}, "raw": json.RawMessage{}}, References: map[string]ReferenceSource{}, Validators: ValidatorOverride{Set: true, Validators: []ModuleBinding{}}}},
{Input: ModuleBinding{Options: map[string]any{"list": []any(nil), "strings": []string(nil), "bytes": []byte(nil), "raw": json.RawMessage(nil)}}},
} {
if copy := ClonePipelineProfile(profile); !reflect.DeepEqual(copy, profile) {
t.Fatalf("clone = %#v, want %#v", copy, profile)
}
}
}