Materialize effective campaign and session inputs
This commit is contained in:
@@ -95,3 +95,36 @@ func TestRemoteSessionResourceBaseIgnoresDiagnosticLabel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEquivalentDocumentLayoutsRenderIdentically(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
firstCampaign := writePipelineFixture(t, dir, "first-campaign.yml", "campaign_id: sample\ninputs: {speakers_file: shared/speakers.yml}\n")
|
||||
secondCampaign := writePipelineFixture(t, dir, "second-campaign.yml", "config:\n imports:\n - path: shared/campaign.yml\ncampaign_id: sample\n")
|
||||
writePipelineFixture(t, dir, "shared/campaign.yml", "inputs: {speakers_file: speakers.yml}\n")
|
||||
firstSession := writePipelineFixture(t, dir, "first-session.yml", "session_id: sample\ninputs: {audio_dir: shared/audio}\n")
|
||||
secondSession := writePipelineFixture(t, dir, "second-session.yml", "config:\n imports:\n - path: shared/session.yml\nsession_id: sample\n")
|
||||
writePipelineFixture(t, dir, "shared/session.yml", "inputs: {audio_dir: audio}\n")
|
||||
var campaigns, sessions [2][]byte
|
||||
for i, files := range [][2]string{{firstCampaign, firstSession}, {secondCampaign, secondSession}} {
|
||||
campaign, err := LoadCampaign(files[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := LoadSession(files[1])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &Config{Campaign: campaign, Session: session, CampaignDocument: campaign, SessionDocument: session}
|
||||
campaigns[i], err = RenderCampaignDocument(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions[i], err = RenderSessionDocument(cfg, "sample")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if string(campaigns[0]) != string(campaigns[1]) || string(sessions[0]) != string(sessions[1]) {
|
||||
t.Fatalf("equivalent documents differ: %q / %q; %q / %q", campaigns[0], campaigns[1], sessions[0], sessions[1])
|
||||
}
|
||||
}
|
||||
|
||||
70
internal/config/document_render.go
Normal file
70
internal/config/document_render.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type renderedCampaignInputs struct {
|
||||
SpeakersFile string `yaml:"speakers_file,omitempty"`
|
||||
AutocorrectFile string `yaml:"autocorrect_file,omitempty"`
|
||||
GlossaryFile string `yaml:"glossary_file,omitempty"`
|
||||
PartyFile string `yaml:"party_file,omitempty"`
|
||||
SpellCatalogFile string `yaml:"spell_catalog_file,omitempty"`
|
||||
}
|
||||
|
||||
// RenderCampaignDocument serializes the loaded effective campaign document.
|
||||
func RenderCampaignDocument(cfg *Config) ([]byte, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("campaign document is required")
|
||||
}
|
||||
doc := cfg.CampaignDocument
|
||||
if doc == nil {
|
||||
doc = cfg.Campaign
|
||||
}
|
||||
if doc == nil {
|
||||
return nil, fmt.Errorf("campaign document is required")
|
||||
}
|
||||
value := struct {
|
||||
CampaignID string `yaml:"campaign_id"`
|
||||
SessionTemplateFile string `yaml:"session_template_file,omitempty"`
|
||||
Inputs renderedCampaignInputs `yaml:"inputs"`
|
||||
}{doc.CampaignID, doc.SessionTemplateFile, renderedCampaignInputs{
|
||||
doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.GlossaryFile, doc.Inputs.PartyFile, doc.Inputs.SpellCatalogFile,
|
||||
}}
|
||||
return yaml.Marshal(value)
|
||||
}
|
||||
|
||||
// RenderSessionDocument serializes the session's own fields and actual identity.
|
||||
func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
|
||||
if cfg == nil || cfg.Session == nil {
|
||||
return nil, fmt.Errorf("session document is required")
|
||||
}
|
||||
doc := cfg.SessionDocument
|
||||
if doc == nil {
|
||||
doc = cfg.Session
|
||||
}
|
||||
id := cfg.Session.SessionID
|
||||
if id == "" {
|
||||
id = sessionID
|
||||
}
|
||||
inputs := struct {
|
||||
AudioDir string `yaml:"audio_dir,omitempty"`
|
||||
AudioFiles []string `yaml:"audio_files,omitempty"`
|
||||
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
|
||||
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}
|
||||
value := struct {
|
||||
SessionID string `yaml:"session_id"`
|
||||
PreviousSessionID string `yaml:"previous_session_id,omitempty"`
|
||||
Campaign string `yaml:"campaign,omitempty"`
|
||||
Date string `yaml:"date,omitempty"`
|
||||
Title string `yaml:"title,omitempty"`
|
||||
Inputs any `yaml:"inputs"`
|
||||
}{id, cfg.Session.PreviousSessionID, cfg.Session.Campaign, doc.Date, doc.Title, inputs}
|
||||
return yaml.Marshal(value)
|
||||
}
|
||||
@@ -48,15 +48,7 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
return nil, fmt.Errorf("prepare: ensure workdir layout: %w", err)
|
||||
}
|
||||
|
||||
campaignSrc := env.Config.CampaignPath
|
||||
if err := requireFile(campaignSrc, "campaign.yml"); err != nil {
|
||||
return nil, fmt.Errorf("prepare: %w", err)
|
||||
}
|
||||
|
||||
sessionSrc := env.Config.SessionPath
|
||||
if err := requireFile(sessionSrc, "session.yml"); err != nil {
|
||||
return nil, fmt.Errorf("prepare: %w", err)
|
||||
}
|
||||
sessionDir := filepath.Dir(sessionSrc)
|
||||
|
||||
speakersInput := stableInputSource(env.Config.StableInputs.SpeakersFile, env.Config.Session.Inputs.SpeakersFile, sessionSrc)
|
||||
@@ -131,14 +123,22 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
}
|
||||
|
||||
campaignDst := filepath.Join(paths.InputsDir, "campaign.yml")
|
||||
campaignChecksum, err := copyFileIfChanged(env.ArtifactStore, campaignSrc, campaignDst)
|
||||
campaignBytes, err := config.RenderCampaignDocument(env.Config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("prepare: render campaign.yml: %w", err)
|
||||
}
|
||||
campaignChecksum, err := writeBytesIfChanged(env.ArtifactStore, campaignDst, campaignBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("prepare: materialize campaign.yml: %w", err)
|
||||
}
|
||||
registerConfigInput("campaign_config", campaignDst, campaignChecksum, "campaign_config")
|
||||
|
||||
sessionDst := filepath.Join(paths.InputsDir, "session.yml")
|
||||
sessionChecksum, err := copyFileIfChanged(env.ArtifactStore, sessionSrc, sessionDst)
|
||||
sessionBytes, err := config.RenderSessionDocument(env.Config, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("prepare: render session.yml: %w", err)
|
||||
}
|
||||
sessionChecksum, err := writeBytesIfChanged(env.ArtifactStore, sessionDst, sessionBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("prepare: materialize session.yml: %w", err)
|
||||
}
|
||||
|
||||
78
internal/stage/prepare_documents_test.go
Normal file
78
internal/stage/prepare_documents_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
)
|
||||
|
||||
func TestPrepareMaterializesImportedDocumentsWithoutAuthoringFiles(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
dir := filepath.Dir(env.Config.CampaignPath)
|
||||
campaignFragment := filepath.Join(dir, "fragments", "campaign.yml")
|
||||
sessionFragment := filepath.Join(dir, "fragments", "session.yml")
|
||||
writeFile(t, env.Config.CampaignPath, "config:\n imports:\n - path: fragments/campaign.yml\ncampaign_id: sample-campaign\n")
|
||||
writeFile(t, campaignFragment, "inputs:\n speakers_file: ../speakers.yml\n autocorrect_file: ../autocorrect.yml\n glossary_file: ../glossary.yml\n party_file: ../party.yml\n")
|
||||
writeFile(t, env.Config.SessionPath, "config:\n imports:\n - path: fragments/session.yml\nsession_id: 2026-05-03\n")
|
||||
writeFile(t, sessionFragment, "title: Imported title\ninputs:\n audio_dir: ../audio\n")
|
||||
writeFile(t, filepath.Join(dir, "audio", "one.flac"), "audio")
|
||||
campaign, err := config.LoadCampaign(env.Config.CampaignPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := config.LoadSession(env.Config.SessionPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := config.LoadPipelineCampaign(env.Config.PipelinePath, env.Config.Pipeline, env.Config.CampaignPath, campaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolved, err := config.ResolveLoadedPipelineCampaign(loaded, env.Config.SessionPath, session, config.SessionSource{Source: "session_config.s3", S3Bucket: "test-bucket", S3Key: "sessions/test/session.yml", S3Size: 17, S3ETag: "synthetic-etag"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env.Config = resolved
|
||||
for _, name := range []string{env.Config.CampaignPath, env.Config.SessionPath, campaignFragment, sessionFragment} {
|
||||
if err := os.Remove(name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
campaignBytes, err := os.ReadFile(filepath.Join(paths.InputsDir, "campaign.yml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessionBytes, err := os.ReadFile(filepath.Join(paths.InputsDir, "session.yml"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(campaignBytes)+string(sessionBytes), "imports:") {
|
||||
t.Fatal("prepared documents retain imports")
|
||||
}
|
||||
preparedCampaign, err := config.LoadCampaign(filepath.Join(paths.InputsDir, "campaign.yml"))
|
||||
if err != nil || preparedCampaign.Inputs.SpeakersFile != filepath.Join(dir, "speakers.yml") {
|
||||
t.Fatalf("prepared campaign = %#v, %v", preparedCampaign, err)
|
||||
}
|
||||
preparedSession, err := config.LoadSession(filepath.Join(paths.InputsDir, "session.yml"))
|
||||
if err != nil || preparedSession.Title != "Imported title" || preparedSession.Inputs.SpeakersFile != "" {
|
||||
t.Fatalf("prepared session = %#v, %v", preparedSession, err)
|
||||
}
|
||||
record := findManifestInput(t, m.Inputs, "session_config")
|
||||
if record.Source != "session_config.s3" || record.S3Bucket != "test-bucket" || record.S3ETag != "synthetic-etag" {
|
||||
t.Fatalf("session provenance = %#v", record)
|
||||
}
|
||||
assertPrepareResume(t, env, m, true)
|
||||
env.Config.SessionDocument.Title = "Changed imported title"
|
||||
assertPrepareResume(t, env, m, false)
|
||||
env.Config.SessionDocument.Title = "Imported title"
|
||||
env.Config.CampaignDocument.SessionTemplateFile = "/new/template.yml"
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
@@ -71,6 +71,15 @@ func preparedInputRecordsCurrent(env *Env, records []manifest.InputRecord) bool
|
||||
func currentPrepareSourceInputs(ctx context.Context, env *Env, m *manifest.Manifest) ([]prepareExpectedInput, error) {
|
||||
cfg := env.Config
|
||||
expected := make([]prepareExpectedInput, 0, 8)
|
||||
campaign, err := config.RenderCampaignDocument(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session, err := config.RenderSessionDocument(cfg, m.SessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expected = append(expected, prepareExpectedInput{kind: "campaign_config", checksum: checksumPrepareBytes(campaign)}, prepareExpectedInput{kind: "session_config", checksum: checksumPrepareBytes(session)})
|
||||
appendFile := func(kind string, input config.ResolvedInputFile, fallback string) error {
|
||||
resolved := stableInputSource(input, fallback, cfg.SessionPath)
|
||||
sourcePath, err := resolveConfigRelativePath(resolved)
|
||||
@@ -197,6 +206,7 @@ func prepareSourceInputsMatch(expected []prepareExpectedInput, records []manifes
|
||||
return false
|
||||
}
|
||||
managedKinds := map[string]struct{}{
|
||||
"campaign_config": {}, "session_config": {},
|
||||
"audio": {}, "speakers": {}, "autocorrect": {}, "glossary": {},
|
||||
"players": {}, "party": {}, "spell_catalog": {},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user