199 lines
8.2 KiB
Go
199 lines
8.2 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
)
|
|
|
|
func TestSessionInitWithThreeCampaignDocuments(t *testing.T) {
|
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
|
data, err := os.ReadFile(campaign)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data = bytes.Replace(data, []byte(" glossary_file: ./glossary.yml\n"), nil, 1)
|
|
if err := os.WriteFile(campaign, data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"glossary.yml", "autocorrect.yml"} {
|
|
if err := os.Remove(filepath.Join(filepath.Dir(campaign), name)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
output := filepath.Join(t.TempDir(), "session.yml")
|
|
if err := invokeNativeCommand(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSessionInitSelectsTemplatesInPriorityOrder(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
installation, campaign bool
|
|
want string
|
|
}{
|
|
{"built in", false, false, ""},
|
|
{"installation", true, false, "Installation"},
|
|
{"campaign", true, true, "Campaign"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
|
if tc.installation {
|
|
template := filepath.Join(filepath.Dir(pipeline), "installation.yml")
|
|
mustWriteTestFile(t, template, "session_id: '{{ session_id }}'\ntitle: Installation\ninputs: {audio_s3: {prefix: audio/}}\n")
|
|
data, err := os.ReadFile(pipeline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
updated := strings.Replace(string(data), "campaigns:\n", "campaigns:\n session_template_file: installation.yml\n", 1)
|
|
if err := os.WriteFile(pipeline, []byte(updated), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if tc.campaign {
|
|
writeSessionInitTemplate(t, campaign, "session_id: '{{ session_id }}'\ntitle: Campaign\ninputs: {audio_s3: {prefix: audio/}}\n")
|
|
}
|
|
output := filepath.Join(t.TempDir(), "session.yml")
|
|
if err := invokeNativeCommand(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Title != tc.want {
|
|
t.Fatalf("title = %q, want %q", loaded.Title, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSessionInitDoesNotFallbackFromInvalidSelectedTemplate(t *testing.T) {
|
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
|
data, err := os.ReadFile(pipeline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
updated := strings.Replace(string(data), "campaigns:\n", "campaigns:\n session_template_file: missing.yml\n", 1)
|
|
if err := os.WriteFile(pipeline, []byte(updated), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
output := filepath.Join(t.TempDir(), "session.yml")
|
|
if err := invokeNativeCommand(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}); err == nil {
|
|
t.Fatal("expected missing template error")
|
|
}
|
|
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
|
t.Fatalf("unexpected output: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionInitInstallationTemplateUsesItsOwnSourceContext(t *testing.T) {
|
|
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
|
base := filepath.Dir(pipeline)
|
|
profile := filepath.Join(base, "profiles", "campaigns.yml")
|
|
template := filepath.Join(base, "profiles", "templates", "session.yml")
|
|
mustWriteTestFile(t, profile, "campaigns: {session_template_file: templates/session.yml}\n")
|
|
mustWriteTestFile(t, template, "config:\n imports:\n - path: detail.yml\nsession_id: '{{ session_id }}'\ninputs: {audio_s3: {prefix: audio/}}\n")
|
|
mustWriteTestFile(t, filepath.Join(filepath.Dir(template), "detail.yml"), "title: Imported installation template\n")
|
|
data, err := os.ReadFile(pipeline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(pipeline, append([]byte("config:\n imports:\n - path: profiles/campaigns.yml\n"), data...), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
output := filepath.Join(t.TempDir(), "session.yml")
|
|
if err := invokeNativeCommand(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Title != "Imported installation template" {
|
|
t.Fatalf("title = %q", loaded.Title)
|
|
}
|
|
}
|
|
|
|
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 := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}, executePlan, commandDependencies{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Remove(fragment); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.Title != "Shared metadata" || loaded.Inputs.SpeakersFile != filepath.Join(filepath.Dir(campaign), "speakers.yml") {
|
|
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 := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--remote"}, &bytes.Buffer{}, executePlan, 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, sessionInitLoadOptions(t, campaign))
|
|
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 := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output, "--audio-dir", "./audio"}, &bytes.Buffer{}, executePlan, commandDependencies{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
|
|
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)
|
|
}
|
|
}
|
|
|
|
func sessionInitLoadOptions(t *testing.T, campaignPath string) config.SessionLoadOptions {
|
|
t.Helper()
|
|
campaign, err := config.LoadCampaign(campaignPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return config.SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath}
|
|
}
|