Derive artifact catalogs from the selected party without changing configuration
This commit is contained in:
125
internal/config/catalog_derivation_test.go
Normal file
125
internal/config/catalog_derivation_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCatalogDerivationKeepsNativeInputsAndRostersIndependent(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Chdir(dir)
|
||||
pipelinePath, campaignPath, sessionPath := writeArtifactFamilyConfig(t, dir, artifactFamilyPipelineYAML(`
|
||||
character_notes:
|
||||
enabled: true
|
||||
for_each: party.characters
|
||||
prompt_id: notes
|
||||
output_path_pattern: artifacts/{character_id}.md
|
||||
member_vars:
|
||||
player: player.name
|
||||
publish:
|
||||
enabled: true
|
||||
required: true
|
||||
`))
|
||||
writePipelineFixture(t, dir, "party.yml", canonicalPartyFixture)
|
||||
explicit := "selected.yml"
|
||||
writePipelineFixture(t, dir, explicit, `schema_version: narratio.party.v1
|
||||
characters:
|
||||
other:
|
||||
player: {name: Selected Player}
|
||||
character: {name: Other, classes: [{name: ranger}]}
|
||||
`)
|
||||
pipeline, err := LoadPipeline(pipelinePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
campaign, err := LoadCampaign(campaignPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
beforePipeline, err := json.Marshal(pipeline)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
beforeCampaign, err := json.Marshal(campaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var beforeShow bytes.Buffer
|
||||
if err := pipeline.ConfigKitResult().WriteEffective(&beforeShow); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defaultContext, err := LoadPipelineCampaign(pipelinePath, pipeline, campaignPath, campaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selectedContext, err := LoadPipelineCampaignWithPartyFile(pipelinePath, pipeline, campaignPath, campaign, &explicit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected := selectedContext.ArtifactCatalog
|
||||
if len(selected.Artifacts) != 1 || selected.Artifacts["character_notes_other"].Vars["player"] != "Selected Player" {
|
||||
t.Fatalf("selected catalog = %+v", selected)
|
||||
}
|
||||
if _, found := defaultContext.ArtifactCatalog.Artifacts["character_notes_other"]; found {
|
||||
t.Fatal("second roster changed the first catalog")
|
||||
}
|
||||
if !strings.Contains(string(selectedContext.Party.Players), "Selected Player") || strings.Contains(string(defaultContext.Party.Players), "Selected Player") {
|
||||
t.Fatal("players did not follow the independently selected roster")
|
||||
}
|
||||
if result := selectedContext.Party.Canonical.ConfigKitResult(); result == nil || result.Config.Characters["other"].Player.Name != "Selected Player" || result.Sources()[0].Path != filepath.Join(dir, explicit) {
|
||||
t.Fatalf("party lost its actual selected native result: %+v", result)
|
||||
}
|
||||
afterPipeline, _ := json.Marshal(pipeline)
|
||||
afterCampaign, _ := json.Marshal(campaign)
|
||||
var afterShow bytes.Buffer
|
||||
if err := pipeline.ConfigKitResult().WriteEffective(&afterShow); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(beforePipeline, afterPipeline) || !bytes.Equal(beforeCampaign, afterCampaign) || beforeShow.String() != afterShow.String() {
|
||||
t.Fatal("catalog derivation changed completed native configuration")
|
||||
}
|
||||
if len(pipeline.Scriptorium.ArtifactFamilies) != 1 || len(pipeline.Scriptorium.Artifacts) != 0 {
|
||||
t.Fatal("domain definitions leaked into pipeline configuration")
|
||||
}
|
||||
if len(selected.PublishOutputs) != len(pipeline.Publish.Outputs)+1 {
|
||||
t.Fatal("family publication actions were not derived separately")
|
||||
}
|
||||
cfg, err := LoadSessionWithPipelineCampaignOptions(selectedContext, sessionPath, SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Validate(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.StableInputs.PartyFile.Path != filepath.Join(dir, explicit) || cfg.StableInputs.PartyFile.Source != "party_file" || cfg.Campaign.Inputs.PartyFile != filepath.Join(dir, "party.yml") {
|
||||
t.Fatalf("explicit party source or unchanged campaign default: %+v", cfg.StableInputs.PartyFile)
|
||||
}
|
||||
repeated, err := DeriveArtifactCatalog(pipeline, selectedContext.Party)
|
||||
if err != nil || !reflect.DeepEqual(selected, repeated) {
|
||||
t.Fatalf("repeated derivation changed catalog: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitPartySelectionNeverFallsBack(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
campaignPath := writePipelineFixture(t, dir, "campaign.yml", "campaign_id: campaign\ninputs: {speakers_file: speakers.yml, autocorrect_file: autocorrect.yml, glossary_file: glossary.yml, party_file: party.yml}\n")
|
||||
writePipelineFixture(t, dir, "party.yml", canonicalPartyFixture)
|
||||
campaign, err := LoadCampaign(campaignPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid := writePipelineFixture(t, dir, "invalid.yml", "schema_version: invalid\n")
|
||||
for _, path := range []string{"", filepath.Join(dir, "missing.yml"), invalid} {
|
||||
if _, err := ResolveParty(campaignPath, campaign, &path); err == nil {
|
||||
t.Fatalf("explicit party %q fell back", path)
|
||||
}
|
||||
}
|
||||
path := filepath.Join(dir, "party.yml")
|
||||
if party, err := ResolveParty("", nil, &path); err != nil || party.Canonical.ConfigKitResult() == nil {
|
||||
t.Fatalf("exact party selection acquired unrelated campaign context: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user