170 lines
6.3 KiB
Go
170 lines
6.3 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"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 TestSessionPlanUsesS3EvidenceWithoutChangingSession(t *testing.T) {
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||
|
|
replaceInFileOrFatal(t, session, "audio_dir: ./audio", "audio_s3:\n prefix: audio/")
|
||
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
key := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.SessionID) + "audio/alice.flac"
|
||
|
|
remote := &storage.FakeBackend{}
|
||
|
|
remote.SeedObject(storage.FakeObject{Key: key, Data: []byte("original audio")})
|
||
|
|
prepare, err := stage.Select("prepare")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
run := func() *RunSummary {
|
||
|
|
t.Helper()
|
||
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{prepare}, RunOptions{Env: &Env{ObjectStore: remote}})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return summary
|
||
|
|
}
|
||
|
|
first := run()
|
||
|
|
mstore := &manifest.LocalStore{}
|
||
|
|
m, err := mstore.Load(context.Background(), first.ManifestPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
m.MarkStageSucceeded("transcribe", time.Now().UTC(), nil)
|
||
|
|
seedCurrentSemanticEvidence(t, cfg, m, "transcribe")
|
||
|
|
if err := mstore.Save(context.Background(), first.ManifestPath, m); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
args := []string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--through", "transcribe", "--format", "json"}
|
||
|
|
deps := fakeCommandDependencies(remote, nil)
|
||
|
|
plan := func(wantCode int, d commandDependencies, extra ...string) sessionPlanReport {
|
||
|
|
t.Helper()
|
||
|
|
before := snapshotSessionTree(t, filepath.Dir(first.ManifestPath))
|
||
|
|
reads, uploads, deletes := len(remote.Reads), len(remote.Uploads), len(remote.Deletes)
|
||
|
|
var out, diagnostics bytes.Buffer
|
||
|
|
code := executeWithDependencies(append(append([]string{}, args...), extra...), &out, &diagnostics, d, nil)
|
||
|
|
if code != wantCode {
|
||
|
|
t.Fatalf("plan code=%d diagnostics=%s", code, diagnostics.String())
|
||
|
|
}
|
||
|
|
if code != 0 && !strings.Contains(diagnostics.String(), "synthetic store initialization failure") {
|
||
|
|
t.Fatalf("missing initialization failure: %s", diagnostics.String())
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(before, snapshotSessionTree(t, filepath.Dir(first.ManifestPath))) {
|
||
|
|
t.Fatal("planning changed session files")
|
||
|
|
}
|
||
|
|
if reads != len(remote.Reads) || uploads != len(remote.Uploads) || deletes != len(remote.Deletes) {
|
||
|
|
t.Fatal("planning transferred or mutated remote objects")
|
||
|
|
}
|
||
|
|
if code != 0 {
|
||
|
|
return sessionPlanReport{}
|
||
|
|
}
|
||
|
|
return decodeNativeReport[sessionPlanReport](t, out.Bytes())
|
||
|
|
}
|
||
|
|
report := plan(0, deps)
|
||
|
|
if report.Stages[0].Action != "skip" || report.Stages[1].Action != "skip" {
|
||
|
|
t.Fatalf("unchanged plan=%+v", report.Stages)
|
||
|
|
}
|
||
|
|
if result := run(); result.StageResults[0].Outcome != "reused" {
|
||
|
|
t.Fatalf("unchanged execution=%+v", result.StageResults)
|
||
|
|
}
|
||
|
|
remote.ListErr = errors.New("synthetic listing outage")
|
||
|
|
report = plan(0, deps)
|
||
|
|
if report.Stages[0].Action != "run" || !strings.Contains(report.Stages[0].Reason, "could not be verified") || !strings.Contains(report.Stages[0].Reason, "synthetic listing outage") || report.Stages[1].Action != "run" {
|
||
|
|
t.Fatalf("unverified plan=%+v", report.Stages)
|
||
|
|
}
|
||
|
|
remote.ListErr = nil
|
||
|
|
failure := errors.New("synthetic store initialization failure")
|
||
|
|
failing := deps
|
||
|
|
failing.newObjectStore = func(context.Context, *config.Config) (storage.ObjectStore, error) { return nil, failure }
|
||
|
|
plan(1, failing)
|
||
|
|
report = plan(0, failing, "--force")
|
||
|
|
if report.Stages[0].Action != "run" {
|
||
|
|
t.Fatalf("forced plan=%+v", report.Stages)
|
||
|
|
}
|
||
|
|
report = plan(0, failing, "--from", "transcribe")
|
||
|
|
if len(report.Stages) != 1 || report.Stages[0].Name != "transcribe" {
|
||
|
|
t.Fatalf("downstream plan=%+v", report.Stages)
|
||
|
|
}
|
||
|
|
remote.SeedObject(storage.FakeObject{Key: key, Data: []byte("changed audio")})
|
||
|
|
report = plan(0, deps)
|
||
|
|
if report.Stages[0].Action != "run" || !strings.Contains(report.Stages[0].Reason, "changed") || report.Stages[1].Action != "run" {
|
||
|
|
t.Fatalf("changed plan=%+v", report.Stages)
|
||
|
|
}
|
||
|
|
if result := run(); result.StageResults[0].Outcome != "completed" {
|
||
|
|
t.Fatalf("changed execution=%+v", result.StageResults)
|
||
|
|
}
|
||
|
|
m, err = mstore.Load(context.Background(), first.ManifestPath)
|
||
|
|
if err != nil || m.Stages["transcribe"].Status != manifest.StatusStale {
|
||
|
|
t.Fatalf("dependent state=%+v error=%v", m.Stages["transcribe"], err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func snapshotSessionTree(t *testing.T, root string) map[string]string {
|
||
|
|
t.Helper()
|
||
|
|
files := map[string]string{}
|
||
|
|
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
rel, err := filepath.Rel(root, path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if entry.IsDir() {
|
||
|
|
files[rel] = "directory"
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if entry.Type()&os.ModeSymlink != 0 {
|
||
|
|
target, err := os.Readlink(path)
|
||
|
|
files[rel] = "link:" + target
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
files[rel] = string(data)
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return files
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSessionPlanDoesNotInitializeStoreForNewOrLocalPreparation(t *testing.T) {
|
||
|
|
for _, source := range []string{"new S3", "local"} {
|
||
|
|
t.Run(source, func(t *testing.T) {
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||
|
|
if source == "new S3" {
|
||
|
|
replaceInFileOrFatal(t, session, "audio_dir: ./audio", "audio_s3:\n prefix: audio/")
|
||
|
|
}
|
||
|
|
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||
|
|
deps.newObjectStore = func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
||
|
|
return nil, errors.New("unexpected store initialization")
|
||
|
|
}
|
||
|
|
var out, diagnostic bytes.Buffer
|
||
|
|
code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--through", "prepare"}, &out, &diagnostic, deps, nil)
|
||
|
|
if code != 0 {
|
||
|
|
t.Fatalf("code=%d diagnostic=%s", code, diagnostic.String())
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|