Initialize standalone sessions from native local authoring context
This commit is contained in:
@@ -373,6 +373,9 @@ Rules:
|
|||||||
- `--audio-dir` and `--audio-s3-prefix` are mutually exclusive.
|
- `--audio-dir` and `--audio-s3-prefix` are mutually exclusive.
|
||||||
- if campaign `session_template_file` is configured, `session init` renders it.
|
- if campaign `session_template_file` is configured, `session init` renders it.
|
||||||
- generated session YAML must be concrete (no unresolved `{{ ... }}` placeholders).
|
- generated session YAML must be concrete (no unresolved `{{ ... }}` placeholders).
|
||||||
|
- template imports and relative resources resolve in the template's local
|
||||||
|
authoring context. Generated output contains standalone effective values.
|
||||||
|
- relative `--audio-dir` paths resolve from the invocation directory.
|
||||||
|
|
||||||
### `session restore`
|
### `session restore`
|
||||||
|
|
||||||
|
|||||||
@@ -146,6 +146,10 @@ documentation checks. This requires no licensing change or new release gate.
|
|||||||
|
|
||||||
## 5. P2: Give session initialization the correct authoring context
|
## 5. P2: Give session initialization the correct authoring context
|
||||||
|
|
||||||
|
Status: Complete. Rendered templates load with native local context and imports;
|
||||||
|
local and remote products are standalone effective documents. Relative CLI audio,
|
||||||
|
import removal, placeholder/identity, and initialization race checks pass.
|
||||||
|
|
||||||
Stage: 4, interacting with materialization and command validation in Stages
|
Stage: 4, interacting with materialization and command validation in Stages
|
||||||
6, 11, and 12.
|
6, 11, and 12.
|
||||||
|
|
||||||
|
|||||||
@@ -241,16 +241,12 @@ inputs:
|
|||||||
t.Fatalf("read generated session: %v", err)
|
t.Fatalf("read generated session: %v", err)
|
||||||
}
|
}
|
||||||
got := string(data)
|
got := string(data)
|
||||||
for _, want := range []string{
|
generated, err := config.LoadSession(outputPath)
|
||||||
`session_id: "2026-06-07"`,
|
if err != nil {
|
||||||
`previous_session_id: "2026-05-31"`,
|
t.Fatal(err)
|
||||||
`date: "2026-06-07"`,
|
}
|
||||||
`title: "The Black Cabin"`,
|
if generated.SessionID != "2026-06-07" || generated.PreviousSessionID != "2026-05-31" || generated.Date != "2026-06-07" || generated.Title != "The Black Cabin" || generated.Inputs.AudioS3 == nil || generated.Inputs.AudioS3.Prefix != "audio/" {
|
||||||
`prefix: "audio/"`,
|
t.Fatalf("generated session = %#v", generated)
|
||||||
} {
|
|
||||||
if !strings.Contains(got, want) {
|
|
||||||
t.Fatalf("generated session = %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if strings.Contains(got, "{{") {
|
if strings.Contains(got, "{{") {
|
||||||
t.Fatalf("generated session still contains template placeholder: %q", got)
|
t.Fatalf("generated session still contains template placeholder: %q", got)
|
||||||
|
|||||||
@@ -58,6 +58,13 @@ func sessionInitWithDependencies(ctx context.Context, args []string, out io.Writ
|
|||||||
if strings.TrimSpace(audioDir) != "" && strings.TrimSpace(audioS3Prefix) != "" {
|
if strings.TrimSpace(audioDir) != "" && strings.TrimSpace(audioS3Prefix) != "" {
|
||||||
return fmt.Errorf("session init: --audio-dir and --audio-s3-prefix are mutually exclusive")
|
return fmt.Errorf("session init: --audio-dir and --audio-s3-prefix are mutually exclusive")
|
||||||
}
|
}
|
||||||
|
if strings.TrimSpace(audioDir) != "" {
|
||||||
|
var err error
|
||||||
|
audioDir, err = filepath.Abs(audioDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("session init: resolve --audio-dir: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
base, err := loadPipelineCampaignConfigWithDependencies(pipelinePath, campaignPath, campaignFilePath, config.PipelineLoadOptions{Overlay: overlay.pointer()}, secretsDir.pointer(), deps)
|
base, err := loadPipelineCampaignConfigWithDependencies(pipelinePath, campaignPath, campaignFilePath, config.PipelineLoadOptions{Overlay: overlay.pointer()}, secretsDir.pointer(), deps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -83,7 +90,13 @@ func sessionInitWithDependencies(ctx context.Context, args []string, out io.Writ
|
|||||||
if label == "" {
|
if label == "" {
|
||||||
label = "remote session.yml"
|
label = "remote session.yml"
|
||||||
}
|
}
|
||||||
sessionCfg, err := config.LoadSessionBytesWithOptions(label, data, config.SessionLoadOptions{
|
// Generated defaults and CLI resources originate at the invocation. A
|
||||||
|
// configured template supplies its own existing local authoring context.
|
||||||
|
authoringPath := "session.yml"
|
||||||
|
if input.TemplateFile != "" {
|
||||||
|
authoringPath = resolveSessionInitTemplatePath(input.CampaignPath, input.TemplateFile)
|
||||||
|
}
|
||||||
|
sessionCfg, err := config.LoadSessionAuthoringBytes(authoringPath, data, config.SessionLoadOptions{
|
||||||
SessionID: sessionID,
|
SessionID: sessionID,
|
||||||
PreviousSessionID: previousSessionID,
|
PreviousSessionID: previousSessionID,
|
||||||
})
|
})
|
||||||
@@ -97,6 +110,10 @@ func sessionInitWithDependencies(ctx context.Context, args []string, out io.Writ
|
|||||||
if err := config.Validate(cfg); err != nil {
|
if err := config.Validate(cfg); err != nil {
|
||||||
return fmt.Errorf("session init: %w", err)
|
return fmt.Errorf("session init: %w", err)
|
||||||
}
|
}
|
||||||
|
data, err = config.RenderSessionDocument(cfg, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("session init: render effective session: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if !remote {
|
if !remote {
|
||||||
if err := writeLocalFile(output, data, force); err != nil {
|
if err := writeLocalFile(output, data, force); err != nil {
|
||||||
|
|||||||
75
internal/app/session_init_context_test.go
Normal file
75
internal/app/session_init_context_test.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSessionInitMaterializesLocalTemplateImports(t *testing.T) {
|
||||||
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||||
|
writeSessionInitTemplate(t, campaign, "config:\n imports:\n - path: shared.yml\n required: true\nsession_id: '{{ session_id }}'\ninputs:\n audio_s3: {prefix: audio/}\n")
|
||||||
|
fragment := filepath.Join(filepath.Dir(campaign), "shared.yml")
|
||||||
|
mustWriteTestFile(t, fragment, "title: Shared metadata\n")
|
||||||
|
output := filepath.Join(t.TempDir(), "nested", "session.yml")
|
||||||
|
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}, commandDependencies{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.Remove(fragment); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
loaded, err := config.LoadSession(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if loaded.Title != "Shared metadata" || loaded.Inputs.SpeakersFile != "" {
|
||||||
|
t.Fatalf("unexpected standalone session: %#v", loaded)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSessionInitPublishesStandaloneImportedTemplate(t *testing.T) {
|
||||||
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||||
|
writeSessionInitTemplate(t, campaign, "config:\n imports:\n - path: shared.yml\n required: true\nsession_id: '{{ session_id }}'\ninputs: {audio_s3: {prefix: audio/}}\n")
|
||||||
|
fragment := filepath.Join(filepath.Dir(campaign), "shared.yml")
|
||||||
|
mustWriteTestFile(t, fragment, "title: Shared metadata\n")
|
||||||
|
fake := &storage.FakeBackend{}
|
||||||
|
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--remote"}, &bytes.Buffer{}, restoreCommandDependencies(fake)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
key := artifacts.S3SessionConfigKey(artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-06-07"))
|
||||||
|
if err := os.Remove(fragment); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
loaded, err := config.LoadSessionBytesWithOptions("remote session", fake.Objects[key].Data, config.SessionLoadOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if loaded.Title != "Shared metadata" {
|
||||||
|
t.Fatalf("title = %q", loaded.Title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSessionInitPersistsInvocationRelativeAudio(t *testing.T) {
|
||||||
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||||
|
output := filepath.Join(t.TempDir(), "nested", "session.yml")
|
||||||
|
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output, "--audio-dir", "./audio"}, &bytes.Buffer{}, commandDependencies{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
loaded, err := config.LoadSession(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want, err := filepath.Abs("./audio")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if loaded.Inputs.AudioDir != want {
|
||||||
|
t.Fatalf("audio = %q; want %q", loaded.Inputs.AudioDir, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,12 +84,31 @@ func loadCampaignWithConfigKit(path string) (*CampaignConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
|
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
|
||||||
|
return loadSessionSourceWithFilesystem(source, label, opts, imports, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadSessionAuthoringBytes resolves a rendered local document before moving or
|
||||||
|
// publishing it. The path supplies its authoring base without writing that file.
|
||||||
|
func LoadSessionAuthoringBytes(path string, data []byte, opts SessionLoadOptions) (*SessionConfig, error) {
|
||||||
|
absolute, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
filesystem := &configkit.FilesystemContext{BaseDir: filepath.Dir(absolute), Root: string(filepath.Separator)}
|
||||||
|
cfg, _, err := loadSessionSourceWithFilesystem(configkit.Bytes(absolute, data), absolute, opts, true, filesystem)
|
||||||
|
return cfg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadSessionSourceWithFilesystem(source configkit.Source, label string, opts SessionLoadOptions, imports bool, filesystem *configkit.FilesystemContext) (*SessionConfig, string, error) {
|
||||||
invocationDir, err := os.Getwd()
|
invocationDir, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
if filesystem != nil {
|
||||||
|
invocationDir = filesystem.BaseDir
|
||||||
|
}
|
||||||
sources := make(map[string]string)
|
sources := make(map[string]string)
|
||||||
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Definition: sessionDefinition(invocationDir, sources)})
|
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Filesystem: filesystem, Definition: sessionDefinition(invocationDir, sources)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", describeConfigKitLoadError("session", err)
|
return nil, "", describeConfigKitLoadError("session", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user