241 lines
9.8 KiB
Go
241 lines
9.8 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCanonicalPartyResolvesFromCampaignAndDerivesVirtualPlayers(t *testing.T) {
|
|
dir := t.TempDir()
|
|
partyPath := filepath.Join(dir, "roster", "party.yml")
|
|
if err := os.Mkdir(filepath.Dir(partyPath), 0o755); err != nil {
|
|
t.Fatalf("create roster directory: %v", err)
|
|
}
|
|
partyYAML := []byte(`schema_version: narratio.party.v1
|
|
characters:
|
|
arannis:
|
|
player: {name: Eric}
|
|
character:
|
|
name: Arannis
|
|
classes: [{name: wizard, level: 8}]
|
|
`)
|
|
if err := os.WriteFile(partyPath, partyYAML, 0o644); err != nil {
|
|
t.Fatalf("write party: %v", err)
|
|
}
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, `campaign_id: campaign
|
|
inputs:
|
|
speakers_file: speakers.yml
|
|
autocorrect_file: autocorrect.yml
|
|
glossary_file: glossary.yml
|
|
party_file: roster/party.yml
|
|
`, `session_id: session
|
|
campaign: campaign
|
|
inputs:
|
|
audio_dir: audio
|
|
`)
|
|
|
|
cfg, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatalf("LoadWithSessionOptions() error = %v", err)
|
|
}
|
|
if err := Validate(cfg); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
if cfg.Party.Mode != PartyModeCanonical || cfg.Party.Canonical == nil {
|
|
t.Fatalf("party = %#v, want canonical party", cfg.Party)
|
|
}
|
|
if got, want := cfg.Party.Source.Path, partyPath; got != want {
|
|
t.Fatalf("party source path = %q, want %q", got, want)
|
|
}
|
|
if cfg.Party.Source.Source != "campaign_config" || cfg.Party.Source.ConfigPath != campaignPath {
|
|
t.Fatalf("party provenance = %#v, want campaign source", cfg.Party.Source)
|
|
}
|
|
if !bytes.Equal(cfg.Party.Canonical.Raw, partyYAML) {
|
|
t.Fatal("canonical party bytes were not retained")
|
|
}
|
|
if got := cfg.StableInputs.PlayersFile; got.Source != "derived_from_party" || got.Path != "" || got.ConfigPath != "" {
|
|
t.Fatalf("derived players input = %#v, want virtual party projection source", got)
|
|
}
|
|
}
|
|
|
|
func TestCanonicalPartyRejectsSeparatePlayersAndSessionPartyOverrides(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
campaignExtra string
|
|
sessionExtra string
|
|
want string
|
|
}{
|
|
{name: "campaign players", campaignExtra: " players_file: players.yml\n", want: "campaign.inputs.players_file is not allowed"},
|
|
{name: "session players", sessionExtra: " players_file: players.yml\n", want: "session.inputs.players_file is not allowed"},
|
|
{name: "session party", sessionExtra: " party_file: party-override.yml\n", want: "session.inputs.party_file cannot override"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePartyResolutionFile(t, filepath.Join(dir, "party.yml"), canonicalPartyFixture)
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, "campaign_id: campaign\ninputs:\n speakers_file: speakers.yml\n autocorrect_file: autocorrect.yml\n glossary_file: glossary.yml\n party_file: party.yml\n"+test.campaignExtra, "session_id: session\ncampaign: campaign\ninputs:\n audio_dir: audio\n"+test.sessionExtra)
|
|
_, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{})
|
|
if err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("LoadWithSessionOptions() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLegacyPartyPreservesCampaignAndSessionInputOverrides(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePartyResolutionFile(t, filepath.Join(dir, "party.yml"), "legacy: campaign\n")
|
|
writePartyResolutionFile(t, filepath.Join(dir, "session-party.yml"), "legacy: session\n")
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, `campaign_id: campaign
|
|
inputs:
|
|
speakers_file: speakers.yml
|
|
autocorrect_file: autocorrect.yml
|
|
glossary_file: glossary.yml
|
|
players_file: campaign-players.yml
|
|
party_file: party.yml
|
|
`, `session_id: session
|
|
campaign: campaign
|
|
inputs:
|
|
audio_dir: audio
|
|
players_file: session-players.yml
|
|
party_file: session-party.yml
|
|
`)
|
|
|
|
cfg, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatalf("LoadWithSessionOptions() error = %v", err)
|
|
}
|
|
if err := Validate(cfg); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
if cfg.Party.Mode != PartyModeLegacy || cfg.Party.Canonical != nil {
|
|
t.Fatalf("party = %#v, want opaque legacy party", cfg.Party)
|
|
}
|
|
if cfg.Party.Source.Source != "session_config" || filepath.Base(cfg.Party.Source.Path) != "session-party.yml" {
|
|
t.Fatalf("party source = %#v, want session override", cfg.Party.Source)
|
|
}
|
|
if got := cfg.StableInputs.PlayersFile; got.Path != "session-players.yml" || got.Source != "session_config" {
|
|
t.Fatalf("players input = %#v, want session override", got)
|
|
}
|
|
}
|
|
|
|
func TestLegacyPartyAcceptsAnEffectiveSessionPlayersOverride(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePartyResolutionFile(t, filepath.Join(dir, "party.yml"), "legacy: campaign\n")
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, `campaign_id: campaign
|
|
inputs:
|
|
speakers_file: speakers.yml
|
|
autocorrect_file: autocorrect.yml
|
|
glossary_file: glossary.yml
|
|
party_file: party.yml
|
|
`, `session_id: session
|
|
campaign: campaign
|
|
inputs:
|
|
audio_dir: audio
|
|
players_file: session-players.yml
|
|
`)
|
|
cfg, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{})
|
|
if err != nil {
|
|
t.Fatalf("LoadWithSessionOptions() error = %v", err)
|
|
}
|
|
if got := cfg.StableInputs.PlayersFile; got.Path != "session-players.yml" || got.Source != "session_config" {
|
|
t.Fatalf("players input = %#v, want effective session override", got)
|
|
}
|
|
}
|
|
|
|
func TestLegacyPartyRequiresPlayersAndPartyMustBeReadableRegularFile(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
partySetup func(t *testing.T, dir string)
|
|
partyFile string
|
|
playersFile string
|
|
want string
|
|
}{
|
|
{name: "missing players", partySetup: func(t *testing.T, dir string) {
|
|
writePartyResolutionFile(t, filepath.Join(dir, "party.yml"), "legacy: party\n")
|
|
}, partyFile: "party.yml", want: "players_file is required with a legacy party"},
|
|
{name: "missing party file", partySetup: func(t *testing.T, dir string) {}, partyFile: "missing.yml", playersFile: "players.yml", want: "read party input"},
|
|
{name: "non-regular party file", partySetup: func(t *testing.T, dir string) {
|
|
if err := os.Mkdir(filepath.Join(dir, "party-dir"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}, partyFile: "party-dir", playersFile: "players.yml", want: "read party input"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
test.partySetup(t, dir)
|
|
players := ""
|
|
if test.playersFile != "" {
|
|
players = " players_file: " + test.playersFile + "\n"
|
|
}
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, "campaign_id: campaign\ninputs:\n speakers_file: speakers.yml\n autocorrect_file: autocorrect.yml\n glossary_file: glossary.yml\n"+players+" party_file: "+test.partyFile+"\n", "session_id: session\ncampaign: campaign\ninputs:\n audio_dir: audio\n")
|
|
_, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{})
|
|
if err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("LoadWithSessionOptions() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveAndLoadedCampaignShareCanonicalPartyResolution(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePartyResolutionFile(t, filepath.Join(dir, "party.yml"), canonicalPartyFixture)
|
|
pipelinePath, campaignPath, sessionPath := writePartyResolutionConfig(t, dir, "campaign_id: campaign\ninputs:\n speakers_file: speakers.yml\n autocorrect_file: autocorrect.yml\n glossary_file: glossary.yml\n party_file: party.yml\n", "session_id: session\ncampaign: campaign\ninputs:\n audio_dir: audio\n")
|
|
pipeline, err := LoadPipeline(pipelinePath)
|
|
if err != nil {
|
|
t.Fatalf("LoadPipeline() error = %v", err)
|
|
}
|
|
campaign, err := LoadCampaign(campaignPath)
|
|
if err != nil {
|
|
t.Fatalf("LoadCampaign() error = %v", err)
|
|
}
|
|
session, err := LoadSession(sessionPath)
|
|
if err != nil {
|
|
t.Fatalf("LoadSession() error = %v", err)
|
|
}
|
|
loaded, err := LoadPipelineCampaign(pipelinePath, pipeline, campaignPath, campaign)
|
|
if err != nil {
|
|
t.Fatalf("LoadPipelineCampaign() error = %v", err)
|
|
}
|
|
partial, err := ResolveLoadedPipelineCampaign(loaded, "", nil, SessionSource{})
|
|
if err != nil || partial.Party.Mode != PartyModeCanonical {
|
|
t.Fatalf("ResolveLoadedPipelineCampaign() = %#v, %v; want canonical partial config", partial, err)
|
|
}
|
|
direct, err := Resolve(pipelinePath, pipeline, campaignPath, campaign, sessionPath, session, SessionSource{Source: "session_config", LocalPath: sessionPath})
|
|
if err != nil {
|
|
t.Fatalf("Resolve() error = %v", err)
|
|
}
|
|
if direct.Party.Mode != PartyModeCanonical || direct.StableInputs.PlayersFile.Source != "derived_from_party" {
|
|
t.Fatalf("Resolve() party = %#v, players = %#v", direct.Party, direct.StableInputs.PlayersFile)
|
|
}
|
|
}
|
|
|
|
const canonicalPartyFixture = `schema_version: narratio.party.v1
|
|
characters:
|
|
arannis:
|
|
player: {name: Eric}
|
|
character:
|
|
name: Arannis
|
|
classes: [{name: wizard}]
|
|
`
|
|
|
|
func writePartyResolutionConfig(t *testing.T, dir, campaign, session string) (string, string, string) {
|
|
t.Helper()
|
|
pipelinePath := writePartyResolutionFile(t, filepath.Join(dir, "pipeline.yml"), "workspace:\n root: "+filepath.ToSlash(filepath.Join(dir, "work"))+"\nwhisperx:\n transcribe_url: https://example.test/transcribe\nnotification:\n mode: noop\n")
|
|
campaignPath := writePartyResolutionFile(t, filepath.Join(dir, "campaign.yml"), campaign)
|
|
sessionPath := writePartyResolutionFile(t, filepath.Join(dir, "session.yml"), session)
|
|
return pipelinePath, campaignPath, sessionPath
|
|
}
|
|
|
|
func writePartyResolutionFile(t *testing.T, path, contents string) string {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
|
|
t.Fatalf("write %s: %v", path, err)
|
|
}
|
|
return path
|
|
}
|