123 lines
5.1 KiB
Go
123 lines
5.1 KiB
Go
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", testTranscriptionConfig+"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)
|
|
}
|
|
}
|