154 lines
6.4 KiB
Go
154 lines
6.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
)
|
|
|
|
func TestInspectionTargetsUseProjectedWorkAndDefinitionClosure(t *testing.T) {
|
|
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enabled := true
|
|
cfg.Pipeline.Trim.Enabled = &enabled
|
|
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{
|
|
"parent": {Enabled: false, PromptID: "parent", OutputPath: "artifacts/parent.md"},
|
|
"child": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"parent"}, Inputs: map[string]config.ScriptoriumInputConfig{"transcript": {Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}, "notes": {Source: "narratio.artifact.parent", Required: false}}},
|
|
"unrelated": {Enabled: false, PromptID: "other", OutputPath: "artifacts/other.md"},
|
|
}
|
|
selection := []string{"child"}
|
|
effective, err := resolveEffectiveArtifacts(cfg, selection)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err := BuildBoundedPlan("trim", "analyze")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
targets, err := executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(targets) != 3 || targets[0].Stage != "trim" || targets[1].Artifact != "parent" || targets[2].Artifact != "child" {
|
|
t.Fatalf("targets = %+v", targets)
|
|
}
|
|
if targets[2].Inputs["notes"].SourceID != "narratio.artifact.parent" || targets[2].Inputs["notes"].Required || targets[2].Inputs["transcript"].SourceID != artifacts.ArtifactTranscriptFinalTrimmed {
|
|
t.Fatalf("bindings = %+v", targets[2].Inputs)
|
|
}
|
|
if targets[2].Context.Vars["session_id"] != "narratio-session-"+cfg.Session.SessionID {
|
|
t.Fatalf("context = %+v", targets[2].Context)
|
|
}
|
|
validation, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
|
if err != nil || !reflect.DeepEqual(targets, validation) {
|
|
t.Fatalf("validation = %+v, error = %v", validation, err)
|
|
}
|
|
for i := range projection.Stages {
|
|
if projection.Stages[i].Name == "analyze" {
|
|
projection.Stages[i].Analysis.ExecutionOrder = projection.Stages[i].Analysis.ExecutionOrder[1:]
|
|
}
|
|
}
|
|
targets, err = executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
|
if err != nil || len(targets) != 2 || targets[1].Artifact != "child" {
|
|
t.Fatalf("reused prerequisite targets = %+v, error = %v", targets, err)
|
|
}
|
|
}
|
|
|
|
func TestInspectionTargetsSkipDisabledTrimAndAbsentScriptorium(t *testing.T) {
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
disabled := false
|
|
cfg.Pipeline.Trim.Enabled = &disabled
|
|
cfg.Pipeline.Scriptorium = nil
|
|
targets, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
|
if err != nil || len(targets) != 0 {
|
|
t.Fatalf("targets = %+v, error = %v", targets, err)
|
|
}
|
|
}
|
|
|
|
func TestInspectionTargetsExpandSelectedFamilyAndIgnoreUnrelatedRange(t *testing.T) {
|
|
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
party, err := config.ParseParty([]byte("schema_version: narratio.party.v1\ncharacters:\n alice:\n player: {name: Player}\n character: {name: Alice, classes: [{name: wizard}]}\n"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg.Pipeline.Scriptorium.Artifacts = nil
|
|
cfg.Pipeline.Scriptorium.ArtifactFamilies = map[string]config.ScriptoriumArtifactFamilyConfig{"character": {Enabled: true, ForEach: "party.characters", PromptID: "character", OutputPathPattern: "artifacts/{character_id}.md", MemberVars: map[string]string{"character_name": "character.name"}}}
|
|
cfg.ArtifactCatalog, err = config.DeriveArtifactCatalog(cfg.Pipeline, config.ResolvedParty{Canonical: party})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
selected, err := normalizeArtifactSelection(cfg, []string{"character"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
effective, err := resolveEffectiveArtifacts(cfg, selected)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err := BuildBoundedPlan("analyze", "analyze")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selected, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
targets, err := executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
|
if err != nil || len(targets) != 1 || targets[0].Artifact != "character_alice" || targets[0].Context.Vars["character_name"] != "Alice" {
|
|
t.Fatalf("targets = %+v, error = %v", targets, err)
|
|
}
|
|
plan, err = BuildBoundedPlan("prepare", "prepare")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
projection, err = projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
targets, err = executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
|
if err != nil || len(targets) != 0 {
|
|
t.Fatalf("unrelated targets = %+v, error = %v", targets, err)
|
|
}
|
|
}
|
|
|
|
func TestDownstreamTargetSelectionForPolish(t *testing.T) {
|
|
cfg := &config.Config{Pipeline: &config.PipelineConfig{}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
stages []projectedStage
|
|
want bool
|
|
}{
|
|
{"selected", []projectedStage{{Name: "polish", Decision: preExecutionDecision{action: stageActionRun}}}, true},
|
|
{"reused", []projectedStage{{Name: "polish", Decision: preExecutionDecision{action: stageActionSkip}}}, false},
|
|
{"unrelated", []projectedStage{{Name: "prepare", Decision: preExecutionDecision{action: stageActionRun}}}, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := executionDownstreamTargets(cfg, "session", &workProjection{Stages: tc.stages})
|
|
if err != nil || got.Polish != tc.want || len(got.Scriptorium) != 0 {
|
|
t.Fatalf("targets: %+v %v", got, err)
|
|
}
|
|
})
|
|
}
|
|
got, err := validationDownstreamTargets(cfg, "session")
|
|
if err != nil || !got.Polish {
|
|
t.Fatalf("validation targets: %+v %v", got, err)
|
|
}
|
|
}
|