108 lines
4.5 KiB
Go
108 lines
4.5 KiB
Go
|
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestGlossarySelectionsInheritIndependently(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
campaignPath := writePipelineFixture(t, dir, "campaign.yml", `config:
|
||
|
|
imports:
|
||
|
|
- path: config/inputs.yml
|
||
|
|
campaign_id: campaign
|
||
|
|
`)
|
||
|
|
writePipelineFixture(t, dir, "config/inputs.yml", `inputs:
|
||
|
|
speakers_file: speakers.yml
|
||
|
|
autocorrect_file: autocorrect.yml
|
||
|
|
party_file: party.yml
|
||
|
|
setting: coast
|
||
|
|
system: magic
|
||
|
|
glossary_file: vocabulary.yml
|
||
|
|
glossary:
|
||
|
|
entries: [{id: watch, name: Silver Watch, category: organization}]
|
||
|
|
`)
|
||
|
|
campaign, err := LoadCampaign(campaignPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if OptionalString(campaign.Inputs.GlossaryFile) != filepath.Join(dir, "config/vocabulary.yml") {
|
||
|
|
t.Fatal(campaign.Inputs.GlossaryFile)
|
||
|
|
}
|
||
|
|
campaignDoc, err := RenderCampaignDocument(&Config{Campaign: campaign})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !strings.Contains(string(campaignDoc), "Silver Watch") || !strings.Contains(string(campaignDoc), "setting: coast") {
|
||
|
|
t.Fatalf("campaign snapshot: %s", campaignDoc)
|
||
|
|
}
|
||
|
|
opts := SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath}
|
||
|
|
for _, tc := range []struct {
|
||
|
|
name, input, file, setting, system string
|
||
|
|
glossary map[string]any
|
||
|
|
}{
|
||
|
|
{"omitted", "", OptionalString(campaign.Inputs.GlossaryFile), "coast", "magic", campaign.Inputs.Glossary},
|
||
|
|
{"inline only", "glossary: {instruction: Session context.}", OptionalString(campaign.Inputs.GlossaryFile), "coast", "magic", map[string]any{"instruction": "Session context."}},
|
||
|
|
{"file only", "glossary_file: replacement.yml", filepath.Join(dir, "replacement.yml"), "coast", "magic", campaign.Inputs.Glossary},
|
||
|
|
{"explicit empty", "setting: ''\n system: null\n glossary_file: ''\n glossary: {}", "", "", "", map[string]any{}},
|
||
|
|
{"explicit null", "glossary: null", OptionalString(campaign.Inputs.GlossaryFile), "coast", "magic", nil},
|
||
|
|
{"null file", "glossary_file: null", "", "coast", "magic", campaign.Inputs.Glossary},
|
||
|
|
} {
|
||
|
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
|
input := "session_id: session\ninputs:\n audio_dir: audio\n"
|
||
|
|
if tc.input != "" {
|
||
|
|
input += " " + tc.input + "\n"
|
||
|
|
}
|
||
|
|
path := writePipelineFixture(t, dir, "session.yml", input)
|
||
|
|
session, err := LoadSessionWithOptions(path, opts)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(configurationErrorText(err))
|
||
|
|
}
|
||
|
|
got := session.Inputs
|
||
|
|
if OptionalString(got.GlossaryFile) != tc.file || OptionalString(got.Setting) != tc.setting || OptionalString(got.System) != tc.system || !reflect.DeepEqual(got.Glossary, tc.glossary) {
|
||
|
|
t.Fatalf("unexpected selections: %+v", got)
|
||
|
|
}
|
||
|
|
rendered, err := RenderSessionDocument(&Config{Session: session}, "session")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
reloaded, err := LoadSessionBytesWithOptions("materialized", rendered, opts)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("%s: %v", rendered, err)
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(reloaded.Inputs.Glossary, got.Glossary) || !reflect.DeepEqual(reloaded.Inputs.Setting, got.Setting) || !reflect.DeepEqual(reloaded.Inputs.System, got.System) || !reflect.DeepEqual(reloaded.Inputs.GlossaryFile, got.GlossaryFile) {
|
||
|
|
t.Fatalf("round trip: %s -> %+v", rendered, reloaded.Inputs)
|
||
|
|
}
|
||
|
|
if tc.name == "explicit null" && !strings.Contains(string(rendered), "glossary: null") {
|
||
|
|
t.Fatalf("lost null: %s", rendered)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGlossarySelectionsRejectInvalidTypesAndUnknownIDs(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
campaignPath := writePipelineFixture(t, dir, "campaign.yml", "campaign_id: campaign\ninputs: {speakers_file: s, autocorrect_file: a, party_file: p}\n")
|
||
|
|
campaign, err := LoadCampaign(campaignPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
opts := SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath}
|
||
|
|
for _, input := range []string{"glossary: [wrong]", "setting: [wrong]", "glossary_file: [wrong]"} {
|
||
|
|
_, err := LoadSessionBytesWithOptions("session", []byte("session_id: session\ninputs:\n audio_dir: audio\n "+input+"\n"), opts)
|
||
|
|
if err == nil {
|
||
|
|
t.Fatalf("accepted %s", input)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
session, err := LoadSessionBytesWithOptions("session", []byte("session_id: session\ninputs: {audio_dir: audio, setting: missing}\n"), opts)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
loaded := LoadedPipelineCampaign{Pipeline: &PipelineConfig{}, Campaign: campaign, Party: ResolvedParty{Canonical: &CanonicalParty{}}}
|
||
|
|
if _, err := ResolveLoadedPipelineCampaign(loaded, "session", session, SessionSource{}); err == nil || !strings.Contains(err.Error(), "missing") {
|
||
|
|
t.Fatalf("unknown selection: %v", err)
|
||
|
|
}
|
||
|
|
}
|