Files
narratio/internal/app/prepared_session_roundtrip_test.go

154 lines
6.5 KiB
Go

package app
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/stage"
)
func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
ctx := context.Background()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, t.TempDir())
fragment := filepath.Join(filepath.Dir(sessionPath), "fragment.yml")
mustWriteTestFile(t, sessionPath, "config:\n imports:\n - path: fragment.yml\nsession_id: 2026-05-03\n")
mustWriteTestFile(t, fragment, "title: Published title\ninputs: {audio_dir: audio}\n")
cfg, err := config.LoadWithSessionOptions(pipelinePath, campaignPath, sessionPath, config.SessionLoadOptions{})
if err != nil {
t.Fatal(err)
}
enabled := true
cfg.Pipeline.Publish.Enabled = &enabled
cfg.Pipeline.Publish.UploadRun = &enabled
cfg.Pipeline.Publish.Outputs = nil
fake := &storage.FakeBackend{}
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
env := &stage.Env{Config: cfg, ArtifactStore: store, ObjectStore: fake}
m := manifest.New("2026-05-03", time.Now().UTC())
m.Campaign = "sample-campaign"
m.RunID = "20260519T010203Z-a1b2c3d4"
runRoot := artifacts.SessionRunRootForCampaign(cfg.Pipeline.Workspace.Root, m.Campaign, m.SessionID, m.RunID)
m.LocalWorkDir = runRoot
if err := os.MkdirAll(runRoot, 0o755); err != nil {
t.Fatal(err)
}
prepare, err := stage.Select("prepare")
if err != nil {
t.Fatal(err)
}
if _, err := prepare.Run(ctx, env, m); err != nil {
t.Fatal(err)
}
for _, name := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "analyze"} {
m.MarkStageSucceeded(name, time.Now().UTC(), nil)
}
if err := os.Remove(sessionPath); err != nil {
t.Fatal(err)
}
if err := os.Remove(fragment); err != nil {
t.Fatal(err)
}
publish, err := stage.Select("publish")
if err != nil {
t.Fatal(err)
}
if _, err := publish.Run(ctx, env, m); err != nil {
t.Fatal(err)
}
prefix := artifacts.S3SessionPrefix("dnd", m.Campaign, m.SessionID)
key := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(prefix, m.RunID), "prepare/inputs/session.yml")
if got := string(fake.Objects[key].Data); !strings.Contains(got, "Published title") || strings.Contains(got, "imports:") || strings.Contains(got, "speakers_file:") {
t.Fatalf("published session = %q", got)
}
for _, name := range []string{"campaign.yml", "party.yml"} {
key := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(prefix, m.RunID), "prepare/inputs/"+name)
if data := fake.Objects[key].Data; len(data) == 0 || strings.Contains(string(data), "imports:") {
t.Fatalf("published %s = %q", name, data)
}
}
setSessionConfigSearchPaths(t, []string{sessionPath})
deps, _ := remoteSessionLoaderDependencies(fake)
loaded, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps)
if err != nil {
t.Fatal(err)
}
defer func() { _ = loaded.Close() }()
if loaded.Config.Session.Title != "Published title" || loaded.Config.SessionSource.S3Key != key {
t.Fatalf("remote session = %#v, source=%#v", loaded.Config.Session, loaded.Config.SessionSource)
}
if got := loaded.Config.StableInputs.SpeakersFile; got.Source != "campaign_config" || got.Path != filepath.Join(filepath.Dir(campaignPath), "speakers.yml") {
t.Fatalf("receiving campaign speakers = %#v", got)
}
current, err := discoverRemoteCurrentState(ctx, loaded.Config, fake)
if err != nil {
t.Fatal(err)
}
plan, err := buildRestorePlan(ctx, loaded.Config, current, fake, RestorePlanOptions{Force: true})
if err != nil {
t.Fatal(err)
}
want := map[string]bool{"inputs/session.yml": false, "inputs/campaign.yml": false, "inputs/party.yml": false}
for _, action := range plan.Actions {
if _, ok := want[action.LocalRelativePath]; ok {
want[action.LocalRelativePath] = true
}
}
for path, found := range want {
if !found {
t.Fatalf("restore omitted %q", path)
}
}
paths := store.SessionPathsFor(m.Campaign, m.SessionID)
for _, name := range []string{"session.yml", "campaign.yml", "party.yml"} {
if err := os.Remove(filepath.Join(paths.InputsDir, name)); err != nil {
t.Fatal(err)
}
}
var output strings.Builder
if err := restoreWithDependencies(ctx, []string{m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--force"}, &output, deps); err != nil {
t.Fatalf("restore: %v", err)
}
restored, err := os.ReadFile(filepath.Join(paths.InputsDir, "session.yml"))
if err != nil || string(restored) != string(fake.Objects[key].Data) {
t.Fatalf("restored session = %q, %v", restored, err)
}
committedBytes := string(fake.Objects[key].Data)
if err := sessionInitWithDependencies(ctx, []string{m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--remote", "--force", "--title", "Updated title", "--audio-s3-prefix", "replacement/"}, &output, deps); err != nil {
t.Fatal(err)
}
updated, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps)
if err != nil {
t.Fatal(err)
}
defer func() { _ = updated.Close() }()
if updated.Config.Session.Title != "Updated title" || updated.Config.Session.Inputs.AudioS3 == nil || updated.Config.Session.Inputs.AudioS3.Prefix != "replacement/" {
t.Fatalf("remote edit ignored: %#v", updated.Config.Session)
}
if string(fake.Objects[key].Data) != committedBytes {
t.Fatal("authoring update changed committed input")
}
if _, err := discoverRemoteCurrentState(ctx, updated.Config, fake); err != nil {
t.Fatalf("committed state after authoring update: %v", err)
}
// Remove the authored override to exercise committed-snapshot integrity below.
rootKey := artifacts.S3SessionConfigKey(prefix)
if err := fake.Delete(ctx, rootKey); err != nil {
t.Fatal(err)
}
object := fake.Objects[key]
modified := append([]byte(nil), object.Data...)
modified[0] ^= 1
fake.SeedObject(storage.FakeObject{Key: key, Data: modified, ETag: object.ETag})
if _, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps); err == nil || !strings.Contains(err.Error(), "committed checksum") {
t.Fatalf("changed remote session error = %v", err)
}
}