Complete session defaults within the ConfigKit lifecycle
This commit is contained in:
@@ -225,7 +225,7 @@ func TestCampaignSessionMismatchFails(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected load error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "does not match campaign config") {
|
||||
if !strings.Contains(err.Error(), "campaign mismatch") {
|
||||
t.Fatalf("error = %q, want campaign mismatch context", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,20 @@ func (cfg *CampaignConfig) ConfigKitResult() *configkit.Result[CampaignConfig] {
|
||||
return cfg.resolution.campaign
|
||||
}
|
||||
|
||||
// ConfigKitResult returns the session's actual completed native load result.
|
||||
// Constructed session values have no result.
|
||||
func (cfg *SessionConfig) ConfigKitResult() *configkit.Result[SessionConfig] {
|
||||
if cfg == nil || cfg.resolution == nil {
|
||||
return nil
|
||||
}
|
||||
return cfg.resolution.session
|
||||
}
|
||||
|
||||
func sessionFieldExplicit(cfg *SessionConfig, name string) bool {
|
||||
result := cfg.ConfigKitResult()
|
||||
return result != nil && result.Metadata().IsExplicit(configkit.Keys("inputs", name))
|
||||
}
|
||||
|
||||
func documentResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string, sources map[string]string) {
|
||||
if info, ok := resolution.EntryInfo(path); ok && len(info.Origin.Locations) == 1 {
|
||||
if source, ok := resolution.Source(info.Origin.Locations[0].SourceID); ok {
|
||||
@@ -57,8 +71,14 @@ func campaignDefinition(invocationDir string, sources map[string]string) configk
|
||||
}, Validate: validateCampaignConfigKit}
|
||||
}
|
||||
|
||||
func sessionDefinition(invocationDir string, sources map[string]string) configkit.Definition[SessionConfig] {
|
||||
func sessionDefinition(invocationDir string, sources map[string]string, label string, opts SessionLoadOptions) configkit.Definition[SessionConfig] {
|
||||
hasSource := false
|
||||
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
|
||||
info, ok := resolution.EntryInfo(configkit.Keys())
|
||||
hasSource = ok && len(info.Origin.Locations) > 0
|
||||
if !hasSource {
|
||||
return nil
|
||||
}
|
||||
inputs := configkit.Keys("inputs")
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
@@ -73,10 +93,51 @@ func sessionDefinition(invocationDir string, sources map[string]string) configki
|
||||
for index := range cfg.Inputs.AudioFiles {
|
||||
documentResource(resolution, inputs.Key("audio_files").Index(index), &cfg.Inputs.AudioFiles[index], invocationDir, sources)
|
||||
}
|
||||
applyCampaignSessionDefaults(cfg, resolution, opts)
|
||||
return nil
|
||||
}, Validate: func(cfg *SessionConfig, validation *configkit.Validation) {
|
||||
// Optional discovery with no selected document leaves acquisition to
|
||||
// the caller's next source. There is no session to validate yet.
|
||||
if !hasSource {
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
validation.Error(configkit.Keys(), "cannot inspect session configuration")
|
||||
return
|
||||
}
|
||||
if err := rejectSessionTemplatePlaceholders(label, string(data)); err != nil {
|
||||
validation.Error(configkit.Keys(), err.Error())
|
||||
return
|
||||
}
|
||||
if path, err := sessionConfigurationError(cfg); err != nil {
|
||||
validation.Error(path, err.Error())
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
func applyCampaignSessionDefaults(cfg *SessionConfig, resolution *configkit.Resolution, opts SessionLoadOptions) {
|
||||
if opts.Campaign == nil {
|
||||
return
|
||||
}
|
||||
campaign := opts.Campaign
|
||||
reason := fmt.Sprintf("default from campaign %q (%s)", CampaignID(campaign), opts.CampaignPath)
|
||||
configkit.Default(resolution, configkit.Keys("campaign"), &cfg.Campaign, CampaignID(campaign), reason)
|
||||
for _, field := range []struct {
|
||||
name, value string
|
||||
target *string
|
||||
}{
|
||||
{"speakers_file", campaign.Inputs.SpeakersFile, &cfg.Inputs.SpeakersFile},
|
||||
{"autocorrect_file", campaign.Inputs.AutocorrectFile, &cfg.Inputs.AutocorrectFile},
|
||||
{"glossary_file", campaign.Inputs.GlossaryFile, &cfg.Inputs.GlossaryFile},
|
||||
{"spell_catalog_file", campaign.Inputs.SpellCatalogFile, &cfg.Inputs.SpellCatalogFile},
|
||||
} {
|
||||
if field.value != "" {
|
||||
configkit.Default(resolution, configkit.Keys("inputs", field.name), field.target, field.value, reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadCampaignWithConfigKit(path string) (*CampaignConfig, error) {
|
||||
invocationDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -117,7 +178,7 @@ func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts
|
||||
invocationDir = filesystem.BaseDir
|
||||
}
|
||||
sources := make(map[string]string)
|
||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources)})
|
||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources, label, opts)})
|
||||
if err != nil {
|
||||
return nil, "", describeConfigKitLoadError("session", err)
|
||||
}
|
||||
@@ -129,19 +190,12 @@ func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts
|
||||
if selected == "" {
|
||||
selected = label
|
||||
}
|
||||
cfg := result.Config
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("inspect session config: %w", err)
|
||||
}
|
||||
if err := rejectSessionTemplatePlaceholders(selected, string(data)); err != nil {
|
||||
return nil, "", fmt.Errorf("load session config: %w", err)
|
||||
}
|
||||
if err := validateSessionIdentity(selected, &cfg, opts); err != nil {
|
||||
cfg := &result.Config
|
||||
if err := validateSessionIdentity(selected, cfg, opts); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
cfg.resolution = &documentResolution{session: result, sources: sources}
|
||||
return &cfg, filepath.Clean(selected), nil
|
||||
return cfg, filepath.Clean(selected), nil
|
||||
}
|
||||
|
||||
// DiscoverSession loads the first selected local candidate, or returns nil when
|
||||
|
||||
@@ -42,18 +42,18 @@ inputs:
|
||||
t.Fatalf("audio[%d] = %q", i, got)
|
||||
}
|
||||
}
|
||||
if cfg.SessionDocument.Inputs.GlossaryFile != "" || cfg.SessionDocument.Inputs.SpeakersFile != filepath.Join(dir, "session/override.yml") {
|
||||
t.Fatalf("detached session = %#v", cfg.SessionDocument)
|
||||
if cfg.SessionDocument.Inputs.GlossaryFile != filepath.Join(dir, "campaign/glossary.yml") || cfg.SessionDocument.Inputs.SpeakersFile != filepath.Join(dir, "session/override.yml") {
|
||||
t.Fatalf("completed session = %#v", cfg.SessionDocument)
|
||||
}
|
||||
if cfg.CampaignDocument.Inputs.GlossaryFile != filepath.Join(dir, "campaign/glossary.yml") {
|
||||
t.Fatalf("detached campaign = %#v", cfg.CampaignDocument)
|
||||
t.Fatalf("completed campaign = %#v", cfg.CampaignDocument)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionDiscoveryDistinguishesAbsenceFromInvalidSelection(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
second := writePipelineFixture(t, dir, "second.yml", "session_id: valid\n")
|
||||
second := writePipelineFixture(t, dir, "second.yml", "session_id: valid\n"+standaloneSessionFields)
|
||||
if cfg, path, err := DiscoverSession([]string{first, second}, SessionLoadOptions{}); err != nil || cfg == nil || path != second {
|
||||
t.Fatalf("selected = %#v %q %v", cfg, path, err)
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func TestRemoteSessionResourceBaseIgnoresDiagnosticLabel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, label := range []string{"s3://bucket/session.yml", filepath.Join(t.TempDir(), "download.yml")} {
|
||||
cfg, err := LoadSessionBytesWithOptions(label, []byte("session_id: test\ninputs: {audio_dir: ./audio}\n"), SessionLoadOptions{})
|
||||
cfg, err := LoadSessionBytesWithOptions(label, []byte("session_id: test\n"+standaloneSessionFields), SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func TestEquivalentDocumentLayoutsRenderIdentically(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := LoadSession(files[1])
|
||||
session, err := LoadSessionWithOptions(files[1], SessionLoadOptions{Campaign: campaign, CampaignPath: files[0]})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -56,8 +56,11 @@ func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
|
||||
SpeakersFile string `yaml:"speakers_file,omitempty"`
|
||||
AutocorrectFile string `yaml:"autocorrect_file,omitempty"`
|
||||
GlossaryFile string `yaml:"glossary_file,omitempty"`
|
||||
SpellCatalogFile string `yaml:"spell_catalog_file,omitempty"`
|
||||
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3, doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.GlossaryFile, doc.Inputs.SpellCatalogFile}
|
||||
SpellCatalogFile *string `yaml:"spell_catalog_file,omitempty"`
|
||||
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3, doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.GlossaryFile, nil}
|
||||
if doc.Inputs.SpellCatalogFile != "" || sessionFieldExplicit(doc, "spell_catalog_file") {
|
||||
inputs.SpellCatalogFile = &doc.Inputs.SpellCatalogFile
|
||||
}
|
||||
value := struct {
|
||||
SessionID string `yaml:"session_id"`
|
||||
PreviousSessionID string `yaml:"previous_session_id,omitempty"`
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -69,8 +68,10 @@ func LoadSession(path string) (*SessionConfig, error) {
|
||||
return LoadSessionWithOptions(path, SessionLoadOptions{})
|
||||
}
|
||||
|
||||
// SessionLoadOptions configures expected session identity checks.
|
||||
// SessionLoadOptions supplies campaign defaults and expected session identities.
|
||||
type SessionLoadOptions struct {
|
||||
Campaign *CampaignConfig
|
||||
CampaignPath string
|
||||
SessionID string
|
||||
PreviousSessionID string
|
||||
Overlay *string
|
||||
@@ -89,19 +90,35 @@ func LoadSessionBytesWithOptions(label string, data []byte, opts SessionLoadOpti
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
func validateSessionIdentity(label string, cfg *SessionConfig, opts SessionLoadOptions) error {
|
||||
if strings.TrimSpace(opts.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != strings.TrimSpace(opts.SessionID) {
|
||||
return fmt.Errorf("load session config: session file %q: session_id mismatch: --session-id %q does not match session_id %q", label, strings.TrimSpace(opts.SessionID), strings.TrimSpace(cfg.SessionID))
|
||||
// SessionIdentityError identifies an explicit identity assertion that the
|
||||
// completed session configuration does not satisfy.
|
||||
type SessionIdentityError struct {
|
||||
Source string
|
||||
Field string
|
||||
Expected string
|
||||
Actual string
|
||||
}
|
||||
|
||||
func (e *SessionIdentityError) Error() string {
|
||||
if e.Actual == "" {
|
||||
return fmt.Sprintf("load session config: session file %q: %s is required when %q is selected", e.Source, e.Field, e.Expected)
|
||||
}
|
||||
if expected := strings.TrimSpace(opts.PreviousSessionID); expected != "" {
|
||||
actual := strings.TrimSpace(cfg.PreviousSessionID)
|
||||
if actual == "" {
|
||||
return fmt.Errorf("load session config: session file %q: previous_session_id is required when --previous-session-id %q is provided", label, expected)
|
||||
}
|
||||
if actual != expected {
|
||||
return fmt.Errorf("load session config: session file %q: previous_session_id mismatch: --previous-session-id %q does not match previous_session_id %q", label, expected, actual)
|
||||
return fmt.Sprintf("load session config: session file %q: %s mismatch: selected %q does not match %q", e.Source, e.Field, e.Expected, e.Actual)
|
||||
}
|
||||
|
||||
func validateSessionIdentity(label string, cfg *SessionConfig, opts SessionLoadOptions) error {
|
||||
for _, assertion := range []struct{ field, expected, actual string }{
|
||||
{"session_id", opts.SessionID, cfg.SessionID},
|
||||
{"previous_session_id", opts.PreviousSessionID, cfg.PreviousSessionID},
|
||||
} {
|
||||
expected := strings.TrimSpace(assertion.expected)
|
||||
if expected != "" && strings.TrimSpace(assertion.actual) != expected {
|
||||
return &SessionIdentityError{Source: label, Field: assertion.field, Expected: expected, Actual: assertion.actual}
|
||||
}
|
||||
}
|
||||
if opts.Campaign != nil && cfg.Campaign != CampaignID(opts.Campaign) {
|
||||
return &SessionIdentityError{Source: label, Field: "campaign", Expected: CampaignID(opts.Campaign), Actual: cfg.Campaign}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -192,6 +209,8 @@ func LoadPipelineCampaignWithParty(pipelinePath string, pipeline *PipelineConfig
|
||||
// LoadSessionWithPipelineCampaignOptions loads one local session and combines
|
||||
// it with an already loaded pipeline and campaign.
|
||||
func LoadSessionWithPipelineCampaignOptions(loaded LoadedPipelineCampaign, sessionPath string, sessionOpts SessionLoadOptions) (*Config, error) {
|
||||
sessionOpts.Campaign = loaded.Campaign
|
||||
sessionOpts.CampaignPath = loaded.CampaignPath
|
||||
sessionCfg, err := LoadSessionWithOptions(sessionPath, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -230,20 +249,13 @@ func ResolveLoadedPipelineCampaign(loaded LoadedPipelineCampaign, sessionPath st
|
||||
SessionSource: sessionSource,
|
||||
Party: loaded.Party,
|
||||
}
|
||||
campaignDocument := *loaded.Campaign
|
||||
cfg.CampaignDocument = &campaignDocument
|
||||
cfg.CampaignDocument = loaded.Campaign
|
||||
cfg.SessionDocument = sessionCfg
|
||||
if sessionCfg == nil {
|
||||
return cfg, nil
|
||||
}
|
||||
sessionDocument := *sessionCfg
|
||||
sessionDocument.Inputs.AudioFiles = slices.Clone(sessionCfg.Inputs.AudioFiles)
|
||||
if sessionCfg.Inputs.AudioS3 != nil {
|
||||
audio := *sessionCfg.Inputs.AudioS3
|
||||
sessionDocument.Inputs.AudioS3 = &audio
|
||||
}
|
||||
cfg.SessionDocument = &sessionDocument
|
||||
|
||||
stableInputs, err := mergeCampaignSession(loaded.Campaign, sessionCfg, loaded.CampaignPath, sessionPath)
|
||||
stableInputs, err := deriveStableInputs(loaded.Campaign, sessionCfg, loaded.CampaignPath, sessionPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -284,86 +296,44 @@ func campaignSessionPaths(paths ...string) (campaignPath, sessionPath string, er
|
||||
return campaignPath, sessionPath, nil
|
||||
}
|
||||
|
||||
func mergeCampaignSession(campaignCfg *CampaignConfig, sessionCfg *SessionConfig, campaignPath, sessionPath string) (ResolvedStableInputs, error) {
|
||||
func deriveStableInputs(campaignCfg *CampaignConfig, sessionCfg *SessionConfig, campaignPath, sessionPath string) (ResolvedStableInputs, error) {
|
||||
if campaignCfg == nil {
|
||||
return ResolvedStableInputs{}, fmt.Errorf("campaign config is required")
|
||||
}
|
||||
if sessionCfg == nil {
|
||||
return ResolvedStableInputs{}, fmt.Errorf("session config is required")
|
||||
}
|
||||
if campaignCfg.Inputs.SpellCatalogFile != "" && strings.TrimSpace(campaignCfg.Inputs.SpellCatalogFile) == "" {
|
||||
return ResolvedStableInputs{}, fmt.Errorf("campaign.inputs.spell_catalog_file must be non-empty when provided")
|
||||
}
|
||||
if sessionCfg.Inputs.SpellCatalogFile != "" && strings.TrimSpace(sessionCfg.Inputs.SpellCatalogFile) == "" {
|
||||
return ResolvedStableInputs{}, fmt.Errorf("session.inputs.spell_catalog_file must be non-empty when provided")
|
||||
}
|
||||
|
||||
campaignName := CampaignID(campaignCfg)
|
||||
sessionCampaign := strings.TrimSpace(sessionCfg.Campaign)
|
||||
if sessionCampaign != "" && campaignName != "" && sessionCampaign != campaignName {
|
||||
return ResolvedStableInputs{}, fmt.Errorf(
|
||||
"campaign/session config invalid: session campaign %q does not match campaign config %q",
|
||||
sessionCampaign,
|
||||
campaignName,
|
||||
)
|
||||
}
|
||||
if sessionCampaign == "" {
|
||||
sessionCfg.Campaign = campaignName
|
||||
}
|
||||
|
||||
stable := ResolvedStableInputs{
|
||||
SpeakersFile: selectStableInput(
|
||||
campaignCfg.Inputs.SpeakersFile,
|
||||
sessionCfg.Inputs.SpeakersFile,
|
||||
campaignPath,
|
||||
sessionPath,
|
||||
),
|
||||
AutocorrectFile: selectStableInput(
|
||||
campaignCfg.Inputs.AutocorrectFile,
|
||||
sessionCfg.Inputs.AutocorrectFile,
|
||||
campaignPath,
|
||||
sessionPath,
|
||||
),
|
||||
GlossaryFile: selectStableInput(
|
||||
campaignCfg.Inputs.GlossaryFile,
|
||||
sessionCfg.Inputs.GlossaryFile,
|
||||
campaignPath,
|
||||
sessionPath,
|
||||
),
|
||||
PartyFile: selectStableInput(
|
||||
campaignCfg.Inputs.PartyFile,
|
||||
"",
|
||||
campaignPath,
|
||||
sessionPath,
|
||||
),
|
||||
SpellCatalogFile: selectStableInput(
|
||||
campaignCfg.Inputs.SpellCatalogFile,
|
||||
sessionCfg.Inputs.SpellCatalogFile,
|
||||
campaignPath,
|
||||
sessionPath,
|
||||
),
|
||||
if sessionCfg.Campaign != CampaignID(campaignCfg) {
|
||||
return ResolvedStableInputs{}, &SessionIdentityError{Source: sessionPath, Field: "campaign", Expected: CampaignID(campaignCfg), Actual: sessionCfg.Campaign}
|
||||
}
|
||||
stable := ResolvedStableInputs{}
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
input *ResolvedInputFile
|
||||
name, value string
|
||||
target *ResolvedInputFile
|
||||
campaignOnly bool
|
||||
}{
|
||||
{"speakers_file", &stable.SpeakersFile}, {"autocorrect_file", &stable.AutocorrectFile},
|
||||
{"glossary_file", &stable.GlossaryFile}, {"party_file", &stable.PartyFile},
|
||||
{"spell_catalog_file", &stable.SpellCatalogFile},
|
||||
{"speakers_file", sessionCfg.Inputs.SpeakersFile, &stable.SpeakersFile, false},
|
||||
{"autocorrect_file", sessionCfg.Inputs.AutocorrectFile, &stable.AutocorrectFile, false},
|
||||
{"glossary_file", sessionCfg.Inputs.GlossaryFile, &stable.GlossaryFile, false},
|
||||
{"party_file", campaignCfg.Inputs.PartyFile, &stable.PartyFile, true},
|
||||
{"spell_catalog_file", sessionCfg.Inputs.SpellCatalogFile, &stable.SpellCatalogFile, false},
|
||||
} {
|
||||
metadata := campaignCfg.resolution
|
||||
if field.input.Source == "session_config" {
|
||||
metadata = sessionCfg.resolution
|
||||
reference := ResolvedInputFile{Path: field.value, ConfigPath: sessionPath, Source: "session_config"}
|
||||
metadata := sessionCfg.resolution
|
||||
fromCampaign := field.campaignOnly
|
||||
if sessionCfg.ConfigKitResult() != nil && !sessionFieldExplicit(sessionCfg, field.name) {
|
||||
fromCampaign = true
|
||||
}
|
||||
if fromCampaign {
|
||||
reference.ConfigPath = campaignPath
|
||||
reference.Source = "campaign_config"
|
||||
metadata = campaignCfg.resolution
|
||||
}
|
||||
if metadata != nil && metadata.sources["/inputs/"+field.name] != "" {
|
||||
field.input.ConfigPath = metadata.sources["/inputs/"+field.name]
|
||||
reference.ConfigPath = metadata.sources["/inputs/"+field.name]
|
||||
}
|
||||
*field.target = reference
|
||||
}
|
||||
|
||||
sessionCfg.Inputs.SpeakersFile = stable.SpeakersFile.Path
|
||||
sessionCfg.Inputs.AutocorrectFile = stable.AutocorrectFile.Path
|
||||
sessionCfg.Inputs.GlossaryFile = stable.GlossaryFile.Path
|
||||
sessionCfg.Inputs.SpellCatalogFile = stable.SpellCatalogFile.Path
|
||||
return stable, nil
|
||||
}
|
||||
|
||||
@@ -375,21 +345,6 @@ func CampaignID(cfg *CampaignConfig) string {
|
||||
return strings.TrimSpace(cfg.CampaignID)
|
||||
}
|
||||
|
||||
func selectStableInput(campaignValue, sessionValue, campaignPath, sessionPath string) ResolvedInputFile {
|
||||
if strings.TrimSpace(sessionValue) != "" {
|
||||
return ResolvedInputFile{
|
||||
Path: sessionValue,
|
||||
ConfigPath: sessionPath,
|
||||
Source: "session_config",
|
||||
}
|
||||
}
|
||||
return ResolvedInputFile{
|
||||
Path: campaignValue,
|
||||
ConfigPath: campaignPath,
|
||||
Source: "campaign_config",
|
||||
}
|
||||
}
|
||||
|
||||
var sessionTemplatePlaceholderPattern = regexp.MustCompile(`\{\{[^}]*\}\}`)
|
||||
var sessionTemplateVariablePattern = regexp.MustCompile(`\{\{\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\}\}`)
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ func TestResolveAndLoadedCampaignShareCanonicalPartyResolution(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCampaign() error = %v", err)
|
||||
}
|
||||
session, err := LoadSession(sessionPath)
|
||||
session, err := LoadSessionWithOptions(sessionPath, SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadSession() error = %v", err)
|
||||
}
|
||||
|
||||
122
internal/config/session_defaults_test.go
Normal file
122
internal/config/session_defaults_test.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestSessionDefaultsCompleteBeforeNativeHandoff(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pipeline := writePipelineFixture(t, dir, "pipeline.yml", "workspace: {root: work}\n")
|
||||
campaign := writePipelineFixture(t, dir, "campaign.yml", "config:\n imports:\n - path: campaign/resources.yml\ncampaign_id: campaign\n")
|
||||
fragment := writePipelineFixture(t, dir, "campaign/resources.yml", `inputs:
|
||||
speakers_file: speakers.yml
|
||||
autocorrect_file: autocorrect.yml
|
||||
glossary_file: glossary.yml
|
||||
party_file: party.yml
|
||||
spell_catalog_file: spells.yml
|
||||
`)
|
||||
writePipelineFixture(t, dir, "campaign/party.yml", canonicalPartyFixture)
|
||||
session := writePipelineFixture(t, dir, "session.yml", "config:\n imports:\n - path: session/resources.yml\nsession_id: session\n")
|
||||
sessionFragment := writePipelineFixture(t, dir, "session/resources.yml", "inputs:\n audio_dir: audio\n speakers_file: override.yml\n spell_catalog_file: ''\n")
|
||||
cfg, err := LoadWithSessionOptions(pipeline, campaign, session, SessionLoadOptions{SessionID: "session"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result := cfg.Session.ConfigKitResult()
|
||||
if result == nil || cfg.Session != &result.Config {
|
||||
t.Fatal("session lost its actual native result")
|
||||
}
|
||||
if len(result.Sources()) != 2 {
|
||||
t.Fatalf("session sources include unrelated campaign inputs: %+v", result.Sources())
|
||||
}
|
||||
if cfg.Session.Campaign != "campaign" || cfg.Session.Inputs.GlossaryFile != filepath.Join(dir, "campaign/glossary.yml") || cfg.Session.Inputs.SpellCatalogFile != "" {
|
||||
t.Fatalf("session defaults/explicit clearing: %+v", cfg.Session)
|
||||
}
|
||||
if cfg.StableInputs.GlossaryFile.Source != "campaign_config" || cfg.StableInputs.GlossaryFile.ConfigPath != fragment || cfg.StableInputs.SpeakersFile.ConfigPath != sessionFragment || cfg.StableInputs.SpellCatalogFile.Source != "session_config" {
|
||||
t.Fatalf("stable input references: %+v", cfg.StableInputs)
|
||||
}
|
||||
explanation, ok := result.Explain(configkit.Keys("inputs", "glossary_file"))
|
||||
if !ok || explanation.Origin.Layer != configkit.LayerResolution {
|
||||
t.Fatalf("default origin: %+v", explanation)
|
||||
}
|
||||
found := false
|
||||
for _, change := range explanation.History {
|
||||
if strings.Contains(strings.Join(change.Reasons, " "), campaign) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("default history lacks campaign context: %+v", explanation.History)
|
||||
}
|
||||
var shown bytes.Buffer
|
||||
if err := result.WriteEffective(&shown); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded SessionConfig
|
||||
if err := yaml.Unmarshal(shown.Bytes(), &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Campaign != cfg.Session.Campaign || !reflect.DeepEqual(decoded.Inputs, cfg.Session.Inputs) {
|
||||
t.Fatal("native show and domain session differ")
|
||||
}
|
||||
before := shown.String()
|
||||
if _, err := deriveStableInputs(cfg.Campaign, cfg.Session, campaign, session); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
shown.Reset()
|
||||
if err := result.WriteEffective(&shown); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if shown.String() != before {
|
||||
t.Fatal("reference derivation changed native configuration")
|
||||
}
|
||||
materialized, err := RenderSessionDocument(cfg, "session")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reloaded, err := LoadSessionBytesWithOptions("materialized", materialized, SessionLoadOptions{Campaign: cfg.Campaign, CampaignPath: campaign})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(reloaded.Inputs, cfg.Session.Inputs) {
|
||||
t.Fatalf("standalone rendering lost effective values or explicit clearing: %s", materialized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionDefaultsRespectExplicitEmptyInputsAndIdentity(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")
|
||||
campaign, err := LoadCampaign(campaignPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opts := SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath, SessionID: "session"}
|
||||
for _, data := range []string{
|
||||
"session_id: session\ninputs: {audio_dir: audio, glossary_file: ''}\n",
|
||||
"session_id: session\ncampaign: ''\ninputs: {audio_dir: audio}\n",
|
||||
"session_id: session\ninputs: null\n",
|
||||
} {
|
||||
_, err := LoadSessionBytesWithOptions("session.yml", []byte(data), opts)
|
||||
var native *configkit.LoadError
|
||||
if !errors.As(err, &native) {
|
||||
t.Fatalf("explicit invalid value did not retain a native validation failure: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = LoadSessionBytesWithOptions("session.yml", []byte("session_id: other\ninputs: {audio_dir: audio}\n"), opts)
|
||||
var mismatch *SessionIdentityError
|
||||
if !errors.As(err, &mismatch) || mismatch.Field != "session_id" || mismatch.Actual != "other" {
|
||||
t.Fatalf("identity assertion was concealed by defaults: %v", err)
|
||||
}
|
||||
_, err = LoadSessionBytesWithOptions("session.yml", []byte("session_id: session\ncampaign: other\ninputs: {audio_dir: audio}\n"), opts)
|
||||
if !errors.As(err, &mismatch) || mismatch.Field != "campaign" {
|
||||
t.Fatalf("campaign assertion was concealed: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -116,11 +116,7 @@ inputs:
|
||||
func TestLoadSessionWithOptionsRequiresExpectedPreviousSession(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
sessionPath := filepath.Join(dir, "session.yml")
|
||||
sessionYAML := `session_id: 2026-05-03
|
||||
campaign: sample-campaign
|
||||
inputs:
|
||||
audio_dir: ./audio
|
||||
`
|
||||
sessionYAML := "session_id: 2026-05-03\n" + standaloneSessionFields
|
||||
if err := os.WriteFile(sessionPath, []byte(sessionYAML), 0o644); err != nil {
|
||||
t.Fatalf("write session.yml: %v", err)
|
||||
}
|
||||
@@ -196,7 +192,7 @@ inputs:
|
||||
}
|
||||
|
||||
func TestLoadSessionBytesWithOptionsStrictDecode(t *testing.T) {
|
||||
cfg, err := LoadSessionBytesWithOptions("s3://bucket/session.yml", []byte("session_id: 2026-05-03\n"), SessionLoadOptions{})
|
||||
cfg, err := LoadSessionBytesWithOptions("s3://bucket/session.yml", []byte("session_id: 2026-05-03\n"+standaloneSessionFields), SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadSessionBytesWithOptions() error = %v", err)
|
||||
}
|
||||
@@ -214,7 +210,7 @@ func TestLoadSessionBytesWithOptionsStrictDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoadSessionBytesWithOptionsMismatchFails(t *testing.T) {
|
||||
_, err := LoadSessionBytesWithOptions("s3://bucket/session.yml", []byte("session_id: 2026-05-03\n"), SessionLoadOptions{SessionID: "2026-04-04"})
|
||||
_, err := LoadSessionBytesWithOptions("s3://bucket/session.yml", []byte("session_id: 2026-05-03\n"+standaloneSessionFields), SessionLoadOptions{SessionID: "2026-04-04"})
|
||||
if err == nil {
|
||||
t.Fatal("expected mismatch error, got nil")
|
||||
}
|
||||
@@ -226,7 +222,7 @@ func TestLoadSessionBytesWithOptionsMismatchFails(t *testing.T) {
|
||||
func TestLoadSessionBytesWithOptionsRequiresExpectedPreviousSession(t *testing.T) {
|
||||
_, err := LoadSessionBytesWithOptions(
|
||||
"s3://bucket/session.yml",
|
||||
[]byte("session_id: 2026-05-03\n"),
|
||||
[]byte("session_id: 2026-05-03\n"+standaloneSessionFields),
|
||||
SessionLoadOptions{PreviousSessionID: "2026-04-26"},
|
||||
)
|
||||
if err == nil || !strings.Contains(err.Error(), "previous_session_id is required") {
|
||||
@@ -248,3 +244,13 @@ func assertConcreteSessionTemplateError(t *testing.T, err error, vars ...string)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Minimal concrete session fields for tests that exercise source acquisition or
|
||||
// identity assertions independently of campaign defaulting.
|
||||
const standaloneSessionFields = `campaign: sample-campaign
|
||||
inputs:
|
||||
audio_dir: audio
|
||||
speakers_file: speakers.yml
|
||||
autocorrect_file: autocorrect.yml
|
||||
glossary_file: glossary.yml
|
||||
`
|
||||
|
||||
@@ -707,10 +707,9 @@ storage:
|
||||
pipelinePath, sessionPath := writeConfigFiles(t, pipelineYAML, tt.sessionYAML)
|
||||
|
||||
cfg, err := Load(pipelinePath, sessionPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error = %v", err)
|
||||
if err == nil {
|
||||
err = Validate(cfg)
|
||||
}
|
||||
err = Validate(cfg)
|
||||
if tt.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("Validate() error = %v, want to contain %q", err, tt.wantErr)
|
||||
|
||||
@@ -62,33 +62,38 @@ func validateCache(cfg CacheConfig) error {
|
||||
}
|
||||
|
||||
func validateSession(cfg *SessionConfig) error {
|
||||
_, err := sessionConfigurationError(cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func sessionConfigurationError(cfg *SessionConfig) (configkit.Path, error) {
|
||||
if err := validateSessionIdentifier("session.session_id", cfg.SessionID, true); err != nil {
|
||||
return err
|
||||
return configkit.Keys("session_id"), err
|
||||
}
|
||||
if err := validateSessionIdentifier("session.previous_session_id", cfg.PreviousSessionID, false); err != nil {
|
||||
return err
|
||||
return configkit.Keys("previous_session_id"), err
|
||||
}
|
||||
if strings.TrimSpace(cfg.PreviousSessionID) != "" && strings.TrimSpace(cfg.PreviousSessionID) == strings.TrimSpace(cfg.SessionID) {
|
||||
return fmt.Errorf("session.previous_session_id must not equal session.session_id")
|
||||
return configkit.Keys("previous_session_id"), fmt.Errorf("session.previous_session_id must not equal session.session_id")
|
||||
}
|
||||
if strings.TrimSpace(cfg.Campaign) == "" {
|
||||
return fmt.Errorf("session.campaign is required")
|
||||
return configkit.Keys("campaign"), fmt.Errorf("session.campaign is required")
|
||||
}
|
||||
if err := pathsafe.ValidateOpaqueSegment(cfg.Campaign); err != nil {
|
||||
return fmt.Errorf("session.campaign: %w", err)
|
||||
return configkit.Keys("campaign"), fmt.Errorf("session.campaign: %w", err)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(cfg.Inputs.SpeakersFile) == "" {
|
||||
return fmt.Errorf("session.inputs.speakers_file is required")
|
||||
return configkit.Keys("inputs", "speakers_file"), fmt.Errorf("session.inputs.speakers_file is required")
|
||||
}
|
||||
if strings.TrimSpace(cfg.Inputs.AutocorrectFile) == "" {
|
||||
return fmt.Errorf("session.inputs.autocorrect_file is required")
|
||||
return configkit.Keys("inputs", "autocorrect_file"), fmt.Errorf("session.inputs.autocorrect_file is required")
|
||||
}
|
||||
if strings.TrimSpace(cfg.Inputs.GlossaryFile) == "" {
|
||||
return fmt.Errorf("session.inputs.glossary_file is required")
|
||||
return configkit.Keys("inputs", "glossary_file"), fmt.Errorf("session.inputs.glossary_file is required")
|
||||
}
|
||||
if cfg.Inputs.SpellCatalogFile != "" && strings.TrimSpace(cfg.Inputs.SpellCatalogFile) == "" {
|
||||
return fmt.Errorf("session.inputs.spell_catalog_file must be non-empty when provided")
|
||||
return configkit.Keys("inputs", "spell_catalog_file"), fmt.Errorf("session.inputs.spell_catalog_file must be non-empty when provided")
|
||||
}
|
||||
|
||||
hasAudioDir := strings.TrimSpace(cfg.Inputs.AudioDir) != ""
|
||||
@@ -96,20 +101,20 @@ func validateSession(cfg *SessionConfig) error {
|
||||
hasAudioS3 := cfg.Inputs.AudioS3 != nil
|
||||
if hasAudioS3 {
|
||||
if strings.TrimSpace(cfg.Inputs.AudioS3.Prefix) == "" {
|
||||
return fmt.Errorf("session.inputs.audio_s3.prefix is required when session.inputs.audio_s3 is configured")
|
||||
return configkit.Keys("inputs", "audio_s3", "prefix"), fmt.Errorf("session.inputs.audio_s3.prefix is required when session.inputs.audio_s3 is configured")
|
||||
}
|
||||
if err := validateRelativeSafePath("session.inputs.audio_s3.prefix", cfg.Inputs.AudioS3.Prefix); err != nil {
|
||||
return err
|
||||
return configkit.Keys("inputs", "audio_s3", "prefix"), err
|
||||
}
|
||||
}
|
||||
if hasAudioS3 && (hasAudioDir || hasAudioFiles) {
|
||||
return fmt.Errorf("session.inputs.audio_dir/audio_files and session.inputs.audio_s3 are mutually exclusive")
|
||||
return configkit.Keys("inputs", "audio_dir"), fmt.Errorf("session.inputs.audio_dir/audio_files and session.inputs.audio_s3 are mutually exclusive")
|
||||
}
|
||||
if !hasAudioDir && !hasAudioFiles && !hasAudioS3 {
|
||||
return fmt.Errorf("session.inputs requires audio_dir, at least one audio_files entry, or audio_s3")
|
||||
return configkit.Keys("inputs"), fmt.Errorf("session.inputs requires audio_dir, at least one audio_files entry, or audio_s3")
|
||||
}
|
||||
|
||||
return nil
|
||||
return configkit.Keys(), nil
|
||||
}
|
||||
|
||||
func validateResolvedPartyInputs(cfg *Config) error {
|
||||
|
||||
Reference in New Issue
Block a user