package config import ( "os" "path/filepath" "strings" "testing" ) func TestPipelineCampaignRegistryStrictDecode(t *testing.T) { dir := t.TempDir() pipelinePath := filepath.Join(dir, "pipeline.yml") pipelineYAML := `workspace: root: /tmp/narratio-work campaigns: root: /srv/narratio/campaigns default_campaign_id: dilfs whisperx: transcribe_url: https://example.com/transcribe notification: mode: noop ` if err := os.WriteFile(pipelinePath, []byte(pipelineYAML), 0o644); err != nil { t.Fatalf("write pipeline.yml: %v", err) } cfg, err := LoadPipeline(pipelinePath) if err != nil { t.Fatalf("LoadPipeline() error = %v", err) } if cfg.Campaigns.Root != "/srv/narratio/campaigns" { t.Fatalf("campaigns.root = %q", cfg.Campaigns.Root) } if cfg.Campaigns.DefaultCampaignID != "dilfs" { t.Fatalf("campaigns.default_campaign_id = %q", cfg.Campaigns.DefaultCampaignID) } } func TestCampaignStrictDecodeAcceptsSharedID(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n inputs:\n speakers_file: ./speakers.yml\n glossary_file: ./glossary.yml\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) cfg, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) if err != nil { t.Fatalf("LoadWithSessionOptions() error = %v", err) } if CampaignID(cfg.Campaign) != "sample-campaign" { t.Fatalf("CampaignID() = %q, want sample-campaign", CampaignID(cfg.Campaign)) } } func TestCampaignStrictDecodeRejectsLegacyCampaignField(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "campaign: sample-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: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) _, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) if err == nil { t.Fatal("expected load error, got nil") } if !strings.Contains(configurationErrorText(err), "load campaign config") || !strings.Contains(configurationErrorText(err), "retired campaign field") { t.Fatalf("error = %q, want retired field context", configurationErrorText(err)) } } func TestCampaignStrictDecodeRejectsUnknownFields(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n unknown: true\n inputs: {speakers_file: ./speakers.yml}\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) _, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) if err == nil { t.Fatal("expected load error, got nil") } if !strings.Contains(configurationErrorText(err), "load campaign config") || !strings.Contains(configurationErrorText(err), "decode.unknown_field") { t.Fatalf("error = %q, want campaign strict decode context", configurationErrorText(err)) } } func TestCampaignStrictDecodeAcceptsSessionTemplateFile(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n session_template_file: ./session.template.yml\n inputs:\n speakers_file: ./speakers.yml\n glossary_file: ./glossary.yml\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) cfg, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) if err != nil { t.Fatalf("LoadWithSessionOptions() error = %v", err) } if cfg.Campaign.SessionTemplateFile != filepath.Join(filepath.Dir(campaignPath), "session.template.yml") { t.Fatalf("SessionTemplateFile = %q, want ./session.template.yml", cfg.Campaign.SessionTemplateFile) } } func TestCampaignSessionMergeFillsStableInputs(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./campaign-party.yml}\nnarratio:\n inputs:\n speakers_file: ./campaign-speakers.yml\n glossary_file: ./campaign-glossary.yml\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) 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.Session.Campaign != "sample-campaign" { t.Fatalf("session campaign = %q, want campaign config value", cfg.Session.Campaign) } assertResolvedStableInput(t, cfg.StableInputs.SpeakersFile, "./campaign-speakers.yml", campaignPath, "campaign_config") assertResolvedStableInput(t, cfg.StableInputs.GlossaryFile, "./campaign-glossary.yml", campaignPath, "campaign_config") assertResolvedStableInput(t, cfg.StableInputs.PartyFile, "./campaign-party.yml", campaignPath, "campaign_config") } func TestCampaignSessionMergeSessionOverridesStableInputs(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./campaign-party.yml}\nnarratio:\n inputs:\n speakers_file: ./campaign-speakers.yml\n glossary_file: ./campaign-glossary.yml\n", "session_id: 2026-05-03\ncampaign: sample-campaign\ninputs:\n audio_dir: ./audio\n speakers_file: ./session-speakers.yml\n", ) 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) } assertResolvedStableInput(t, cfg.StableInputs.SpeakersFile, "./session-speakers.yml", sessionPath, "session_config") assertResolvedStableInput(t, cfg.StableInputs.GlossaryFile, "./campaign-glossary.yml", campaignPath, "campaign_config") assertResolvedStableInput(t, cfg.StableInputs.PartyFile, "./campaign-party.yml", campaignPath, "campaign_config") } func TestCampaignWithCanonicalPartyDoesNotRequirePlayersInput(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n inputs:\n speakers_file: ./speakers.yml\n glossary_file: ./glossary.yml\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) if _, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}); err != nil { t.Fatalf("LoadWithSessionOptions() error = %v", err) } } func TestCampaignSessionMismatchFails(t *testing.T) { pipelinePath, campaignPath, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n inputs:\n speakers_file: ./speakers.yml\n glossary_file: ./glossary.yml\n", "session_id: 2026-05-03\ncampaign: other-campaign\ninputs:\n audio_dir: ./audio\n", ) _, err := LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, SessionLoadOptions{}) if err == nil { t.Fatal("expected load error, got nil") } if !strings.Contains(configurationErrorText(err), "campaign mismatch") { t.Fatalf("error = %q, want campaign mismatch context", configurationErrorText(err)) } } func TestLoadMissingCampaignFileFails(t *testing.T) { pipelinePath, _, sessionPath := writeCampaignConfigTestFiles(t, "schema_version: maximumdirect.rpg.campaign.v1\nid: sample-campaign\nreferences: {party: ./party.yml}\nnarratio:\n inputs:\n speakers_file: ./speakers.yml\n glossary_file: ./glossary.yml\n", "session_id: 2026-05-03\ninputs:\n audio_dir: ./audio\n", ) missingCampaignPath := filepath.Join(filepath.Dir(sessionPath), "missing-campaign.yml") _, err := LoadWithSessionOptions(pipelinePath, missingCampaignPath, sessionPath, SessionLoadOptions{}) if err == nil { t.Fatal("expected load error, got nil") } if !strings.Contains(configurationErrorText(err), "load campaign config") { t.Fatalf("error = %q, want campaign load context", configurationErrorText(err)) } } func writeCampaignConfigTestFiles(t *testing.T, campaignYAML, sessionYAML string) (string, string, string) { t.Helper() dir := t.TempDir() pipelinePath := filepath.Join(dir, "pipeline.yml") campaignPath := filepath.Join(dir, "campaign.yml") sessionPath := filepath.Join(dir, "session.yml") pipelineYAML := "workspace:\n root: " + filepath.ToSlash(filepath.Join(dir, "work")) + "\nwhisperx:\n transcribe_url: https://example.com/transcribe\nnotification:\n mode: noop\n" if err := os.WriteFile(pipelinePath, []byte(pipelineYAML), 0o644); err != nil { t.Fatalf("write pipeline.yml: %v", err) } if err := os.WriteFile(campaignPath, []byte(campaignYAML), 0o644); err != nil { t.Fatalf("write campaign.yml: %v", err) } if err := os.WriteFile(sessionPath, []byte(sessionYAML), 0o644); err != nil { t.Fatalf("write session.yml: %v", err) } for _, name := range []string{"party.yml", "campaign-party.yml", "session-party.yml"} { if err := os.WriteFile(filepath.Join(dir, name), []byte(canonicalPartyFixture), 0o644); err != nil { t.Fatalf("write %s: %v", name, err) } } return pipelinePath, campaignPath, sessionPath } func assertResolvedStableInput(t *testing.T, got ResolvedInputFile, wantPath, wantConfigPath, wantSource string) { t.Helper() if wantPath != "" && !filepath.IsAbs(wantPath) { wantPath = filepath.Join(filepath.Dir(wantConfigPath), wantPath) } if got.Path != wantPath || got.ConfigPath != wantConfigPath || got.Source != wantSource { t.Fatalf("resolved input = %#v, want path=%q config_path=%q source=%q", got, wantPath, wantConfigPath, wantSource) } }