Load campaign and session imports from their sources

This commit is contained in:
2026-11-17 22:15:05 +00:00
parent c3023cb609
commit 1657c9f65d
16 changed files with 326 additions and 209 deletions

View File

@@ -59,12 +59,12 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
return retainedCommandConfig(cfg), nil
}
discoveredSession, err := discoverSessionConfigPathWithCandidates(config.DefaultSessionConfigSearchPaths)
discoveredSession, discoveredPath, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, sessionOpts)
if err != nil {
return nil, err
}
if discoveredSession.Path != "" {
cfg, err := config.LoadSessionWithPipelineCampaignOptions(*base, discoveredSession.Path, sessionOpts)
if discoveredSession != nil {
cfg, err := config.ResolveLoadedPipelineCampaign(*base, discoveredPath, discoveredSession, config.SessionSource{Source: "session_config", LocalPath: discoveredPath})
if err != nil {
return nil, err
}
@@ -73,7 +73,7 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
sessionID := strings.TrimSpace(sessionOpts.SessionID)
if sessionID == "" {
return nil, missingSessionConfigError(discoveredSession.Searched, "remote session loading requires a session_id")
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session_id")
}
rootPrefix := ""
@@ -88,16 +88,16 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
}
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, nil, deps)
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
}
sessionInfo, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey)
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, err.Error())
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
}
sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml")
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
}
cleanup = func() error {
if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil {

View File

@@ -257,6 +257,29 @@ inputs:
}
}
func TestExecuteSessionInitUsesImportedTemplateLocation(t *testing.T) {
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
dir := filepath.Dir(campaign)
original, err := os.ReadFile(campaign)
if err != nil {
t.Fatal(err)
}
mustWriteTestFile(t, filepath.Join(dir, "fragments", "template-settings.yml"), "session_template_file: templates/session.yml\n")
mustWriteTestFile(t, filepath.Join(dir, "fragments", "templates", "session.yml"), "session_id: '{{ session_id }}'\ninputs:\n audio_s3: {prefix: audio/}\n")
if err := os.WriteFile(campaign, append([]byte("config:\n imports:\n - path: fragments/template-settings.yml\n"), original...), 0o644); err != nil {
t.Fatal(err)
}
output := filepath.Join(t.TempDir(), "session.yml")
var stdout, stderr bytes.Buffer
if code := Execute([]string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &stdout, &stderr); code != 0 {
t.Fatalf("exit = %d; stderr = %q", code, stderr.String())
}
data, err := os.ReadFile(output)
if err != nil || !strings.Contains(string(data), "2026-06-07") {
t.Fatalf("generated session = %q, %v", data, err)
}
}
func TestExecuteSessionInitRemoteRendersCampaignTemplate(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)

View File

@@ -46,6 +46,24 @@ inputs:
}
}
func TestInvalidDiscoveredSessionDoesNotTriggerRemoteFallback(t *testing.T) {
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
selected := filepath.Join(t.TempDir(), "session.yml")
if err := os.WriteFile(selected, []byte("unexpected: value\n"), 0o644); err != nil {
t.Fatal(err)
}
setSessionConfigSearchPaths(t, []string{selected})
storeCalls := 0
deps := fakeCommandDependencies(&storage.FakeBackend{}, &storeCalls)
var stdout, stderr bytes.Buffer
if code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign}, &stdout, &stderr, deps, executePlan); code == 0 {
t.Fatal("invalid session was accepted")
}
if storeCalls != 0 || !strings.Contains(stderr.String(), "decode.unknown_field") {
t.Fatalf("store calls=%d, stderr=%q", storeCalls, stderr.String())
}
}
func TestRemoteSessionLoadingRetainsCampaignCanonicalParty(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)

View File

@@ -131,7 +131,7 @@ func TestRunStageAcceptsPositionalSessionIDAndParsesStageName(t *testing.T) {
}
func TestResolveSessionConfigPathErrorIncludesSearchedPaths(t *testing.T) {
_, err := resolveSessionConfigPathWithCandidates("", []string{"/usr/local/etc/narratio/session.yml", "/etc/narratio/session.yml"})
err := missingSessionConfigError([]string{"/usr/local/etc/narratio/session.yml", "/etc/narratio/session.yml"}, "")
if err == nil {
t.Fatal("expected error, got nil")
}

View File

@@ -1,57 +1,10 @@
package app
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
func resolveSessionConfigPathWithCandidates(flagValue string, candidates []string) (string, error) {
if explicit := strings.TrimSpace(flagValue); explicit != "" {
return explicit, nil
}
resolved, err := discoverSessionConfigPathWithCandidates(candidates)
if err != nil {
return "", err
}
if resolved.Path != "" {
return resolved.Path, nil
}
return "", missingSessionConfigError(resolved.Searched, "")
}
type sessionConfigDiscovery struct {
Path string
Searched []string
}
func discoverSessionConfigPathWithCandidates(candidates []string) (sessionConfigDiscovery, error) {
ordered := make([]string, 0, len(candidates))
for _, raw := range candidates {
path := strings.TrimSpace(raw)
if path == "" {
continue
}
ordered = append(ordered, path)
info, err := os.Stat(path)
if err == nil {
if info.IsDir() {
continue
}
return sessionConfigDiscovery{Path: filepath.Clean(path), Searched: ordered}, nil
}
if errors.Is(err, os.ErrNotExist) {
continue
}
return sessionConfigDiscovery{}, fmt.Errorf("check default session config %q: %w", path, err)
}
return sessionConfigDiscovery{Searched: ordered}, nil
}
func missingSessionConfigError(searched []string, remoteDetail string) error {
ordered := append([]string(nil), searched...)
if len(ordered) == 0 {

View File

@@ -1,68 +0,0 @@
package app
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestResolveSessionConfigPathWithCandidatesExplicitWins(t *testing.T) {
got, err := resolveSessionConfigPathWithCandidates(" ./custom/session.yml ", []string{"./session.yml", "/a", "/b"})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != "./custom/session.yml" {
t.Fatalf("resolved path = %q, want explicit path", got)
}
}
func TestResolveSessionConfigPathWithCandidatesUsesFirstExisting(t *testing.T) {
dir := t.TempDir()
first := filepath.Join(dir, "first.yml")
second := filepath.Join(dir, "second.yml")
if err := os.WriteFile(second, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write second default: %v", err)
}
got, err := resolveSessionConfigPathWithCandidates("", []string{first, second})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != filepath.Clean(second) {
t.Fatalf("resolved path = %q, want %q", got, filepath.Clean(second))
}
}
func TestResolveSessionConfigPathWithCandidatesPrecedence(t *testing.T) {
dir := t.TempDir()
first := filepath.Join(dir, "first.yml")
second := filepath.Join(dir, "second.yml")
if err := os.WriteFile(first, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write first default: %v", err)
}
if err := os.WriteFile(second, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write second default: %v", err)
}
got, err := resolveSessionConfigPathWithCandidates("", []string{first, second})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != filepath.Clean(first) {
t.Fatalf("resolved path = %q, want first candidate %q", got, filepath.Clean(first))
}
}
func TestResolveSessionConfigPathWithCandidatesMissing(t *testing.T) {
_, err := resolveSessionConfigPathWithCandidates("", []string{"/does/not/exist/one.yml", "/does/not/exist/two.yml"})
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "no default session config found") {
t.Fatalf("error = %q, want missing-defaults context", err.Error())
}
if !strings.Contains(err.Error(), "pass --session") {
t.Fatalf("error = %q, want explicit-path guidance", err.Error())
}
}