2026-11-20 05:23:32 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"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"
|
2026-11-20 13:45:17 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
2026-11-20 05:23:32 +00:00
|
|
|
"go.maximumdirect.net/commandkit"
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSessionValidationCommandReturnsFindingsWithoutRuntimeState(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
// A missing stable input is a readiness failure, not malformed configuration.
|
|
|
|
|
missing := cfg.StableInputs.SpeakersFile.Path
|
|
|
|
|
if err := os.Remove(missing); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var out, diagnostic bytes.Buffer
|
|
|
|
|
deps, _ := remoteSessionLoaderDependencies(&storage.FakeBackend{})
|
|
|
|
|
app, err := newApplication(deps, executePlan)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
outcome := app.Execute(context.Background(), []string{"session", "validate", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
|
|
|
if outcome.Status != 1 {
|
|
|
|
|
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
|
|
|
|
}
|
|
|
|
|
var report sessionValidationReport
|
|
|
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if report.Valid || report.SessionID != "2026-05-03" || len(report.Findings) == 0 {
|
|
|
|
|
t.Fatalf("report=%#v", report)
|
|
|
|
|
}
|
|
|
|
|
if !errors.Is(outcome.Cause, os.ErrNotExist) {
|
|
|
|
|
t.Fatalf("missing input cause not retained: %v", outcome.Cause)
|
|
|
|
|
}
|
|
|
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
|
|
|
if _, err := os.Stat(paths.Root); !os.IsNotExist(err) {
|
|
|
|
|
t.Fatalf("preflight created runtime state: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionValidationCommandReportsNativeConfigurationFailure(t *testing.T) {
|
|
|
|
|
pipeline := filepath.Join(t.TempDir(), "invalid.yml")
|
|
|
|
|
mustWriteTestFile(t, pipeline, "unknown_field: value\n")
|
|
|
|
|
var out, diagnostic bytes.Buffer
|
|
|
|
|
outcome := Execute(context.Background(), []string{"session", "validate", "session", "--config", pipeline, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
|
|
|
if outcome.Status != 2 {
|
|
|
|
|
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
|
|
|
|
}
|
|
|
|
|
var report sessionValidationReport
|
|
|
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if report.Valid || len(report.Findings) != 1 || report.Findings[0].Category != "config" {
|
|
|
|
|
t.Fatalf("report=%#v", report)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionArtifactCommandIsLocalWithoutRemoteOption(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
|
|
|
|
app, err := newApplication(commandDependencies{newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
|
|
|
|
t.Fatal("local artifact inspection acquired remote storage")
|
|
|
|
|
return nil, nil
|
|
|
|
|
}}, executePlan)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var out, diagnostic bytes.Buffer
|
|
|
|
|
outcome := app.Execute(context.Background(), []string{"session", "artifacts", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
|
|
|
if outcome.Status != 0 {
|
|
|
|
|
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
|
|
|
|
}
|
|
|
|
|
var report sessionStateReport
|
|
|
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if report.Remote != nil || report.LocalManifest != "missing" || len(report.Artifacts) == 0 {
|
|
|
|
|
t.Fatalf("report=%#v", report)
|
|
|
|
|
}
|
|
|
|
|
found := false
|
|
|
|
|
for _, entry := range report.Artifacts {
|
|
|
|
|
if entry.Source == "narratio.artifact.session_recap" {
|
|
|
|
|
found = entry.Producer == "analyze" && entry.Availability == "unavailable"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
t.Fatal("report lost configured source or producer")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionStateCommandReturnsPartialRemoteFailure(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
failure := errors.New("synthetic remote failure")
|
|
|
|
|
fake := &storage.FakeBackend{}
|
|
|
|
|
deps, _ := remoteSessionLoaderDependencies(fake)
|
|
|
|
|
deps.discoverRemoteCurrentState = func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error) {
|
|
|
|
|
return nil, failure
|
|
|
|
|
}
|
|
|
|
|
app, err := newApplication(deps, executePlan)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, command := range []string{"status", "artifacts"} {
|
|
|
|
|
args := []string{"session", command, "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}
|
|
|
|
|
if command == "artifacts" {
|
|
|
|
|
args = append(args, "--remote")
|
|
|
|
|
}
|
|
|
|
|
var out, diagnostic bytes.Buffer
|
|
|
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
|
|
|
if outcome.Status != 1 || !errors.Is(outcome.Cause, failure) {
|
|
|
|
|
t.Fatalf("command=%s status=%d cause=%v diagnostics=%s", command, outcome.Status, outcome.Cause, diagnostic.String())
|
|
|
|
|
}
|
|
|
|
|
var report sessionStateReport
|
|
|
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if report.Remote == nil || report.Remote.State != "error" || len(report.Artifacts) == 0 || len(report.Findings) == 0 {
|
|
|
|
|
t.Fatalf("partial report=%#v", report)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(fake.Downloads) != 0 || len(fake.Reads) != 0 || len(fake.Uploads) != 0 || len(fake.Deletes) != 0 {
|
|
|
|
|
t.Fatal("inspection touched remote payloads")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPublishedOutputInspectionKeepsCarriedIdentityWithoutPayloadReads(t *testing.T) {
|
|
|
|
|
cfg := restorePlanConfig(t)
|
|
|
|
|
cfg.ArtifactCatalog = &config.ArtifactCatalog{PublishOutputs: []config.PublishOutputRule{{Source: "narratio.transcript.final_trimmed", Dest: "transcripts/full.json"}}}
|
|
|
|
|
fake := &storage.FakeBackend{}
|
|
|
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/full.json": []byte("carried\n")})
|
|
|
|
|
const originRun = "20260512T010203Z-11223344"
|
|
|
|
|
key := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(current.SessionPrefix, originRun), "published/transcripts/original.json")
|
|
|
|
|
fake.SeedObject(storage.FakeObject{Key: key, Data: []byte("carried\n")})
|
|
|
|
|
current.Commit.Artifacts[1].Source = "narratio.transcript.final_trimmed"
|
|
|
|
|
current.Commit.Artifacts[1].DestinationKey = key
|
|
|
|
|
current.Manifest.Stages["publish"] = &manifest.StageRecord{Name: "publish", Status: manifest.StatusSucceeded, Metadata: map[string]any{"publish_output_outcomes": []any{map[string]any{"source": "narratio.transcript.final_trimmed", "status": "carried_locked"}}}}
|
|
|
|
|
reads := len(fake.Reads)
|
|
|
|
|
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, []artifacts.RemoteLock{{Source: "narratio.transcript.final_trimmed"}}, true, "current")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(outputs) != 1 {
|
|
|
|
|
t.Fatalf("outputs=%#v", outputs)
|
|
|
|
|
}
|
|
|
|
|
output := outputs[0]
|
|
|
|
|
if output.State != "published" || output.Outcome != "carried_locked" || output.LockState != "locked" || output.DeclaredKey != key || output.DeclaredDestination != "transcripts/original.json" || output.OriginRun != originRun {
|
|
|
|
|
t.Fatalf("output=%#v", output)
|
|
|
|
|
}
|
|
|
|
|
if len(fake.Reads) != reads || fake.ExistsKeys[len(fake.ExistsKeys)-1] != key {
|
|
|
|
|
t.Fatal("inspection did not use only the exact declared object identity")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSessionStateCommandsLeaveManifestUnchanged(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
|
|
|
m := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
|
|
|
|
m.Campaign = cfg.Session.Campaign
|
|
|
|
|
m.MarkStageSucceeded("prepare", time.Now().UTC(), nil)
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(paths.ManifestPath), 0755); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := (&manifest.LocalStore{}).Save(context.Background(), paths.ManifestPath, m); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
before, err := os.ReadFile(paths.ManifestPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
fake := &storage.FakeBackend{}
|
|
|
|
|
deps, _ := remoteSessionLoaderDependencies(fake)
|
|
|
|
|
app, err := newApplication(deps, executePlan)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, test := range []struct{ command, format string }{{"status", "json"}, {"artifacts", "yaml"}} {
|
|
|
|
|
args := []string{"session", test.command, "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", test.format}
|
|
|
|
|
if test.command == "artifacts" {
|
|
|
|
|
args = append(args, "--remote")
|
|
|
|
|
}
|
|
|
|
|
var out, diagnostic bytes.Buffer
|
|
|
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
|
|
|
if outcome.Status != 0 {
|
|
|
|
|
t.Fatalf("%s status=%d diagnostics=%s", test.command, outcome.Status, diagnostic.String())
|
|
|
|
|
}
|
|
|
|
|
var report map[string]any
|
|
|
|
|
if test.format == "json" {
|
|
|
|
|
err = json.Unmarshal(out.Bytes(), &report)
|
|
|
|
|
} else {
|
|
|
|
|
err = yaml.Unmarshal(out.Bytes(), &report)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if report["local_manifest"] != "present" || report["session_id"] != "2026-05-03" || report["stages"] == nil {
|
|
|
|
|
t.Fatalf("report=%#v", report)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
after, err := os.ReadFile(paths.ManifestPath)
|
|
|
|
|
if err != nil || !bytes.Equal(before, after) {
|
|
|
|
|
t.Fatal("inspection modified manifest")
|
|
|
|
|
}
|
|
|
|
|
if len(fake.Uploads) != 0 || len(fake.Downloads) != 0 || len(fake.Deletes) != 0 {
|
|
|
|
|
t.Fatal("inspection mutated remote state or downloaded payloads")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-20 13:45:17 +00:00
|
|
|
|
|
|
|
|
func TestPublishedOutputInspectionNormalizesAcceptedSources(t *testing.T) {
|
|
|
|
|
const canonical = "narratio.transcript.final_trimmed"
|
|
|
|
|
for _, source := range []string{canonical, " " + canonical + " "} {
|
|
|
|
|
t.Run(source, func(t *testing.T) {
|
|
|
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
|
|
|
|
addPublishOutputsToPipeline(t, pipeline, " outputs:\n - source: \""+source+"\"\n dest: transcripts/full.json\n")
|
|
|
|
|
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := config.Validate(cfg); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
actions, err := stage.PlanPublicationOutputs(nil, cfg.PublicationOutputs(), nil)
|
|
|
|
|
if err != nil || len(actions) != 1 || actions[0].Source != canonical {
|
|
|
|
|
t.Fatalf("publication actions=%#v err=%v", actions, err)
|
|
|
|
|
}
|
|
|
|
|
fake := &storage.FakeBackend{}
|
|
|
|
|
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/full.json": []byte("published\n")})
|
|
|
|
|
declaration := ¤t.Commit.Artifacts[1]
|
|
|
|
|
declaration.Source = canonical
|
|
|
|
|
current.Manifest.Stages["publish"] = &manifest.StageRecord{
|
|
|
|
|
Name: "publish", Status: manifest.StatusSucceeded,
|
|
|
|
|
Metadata: map[string]any{"publish_output_outcomes": []any{map[string]any{"source": canonical, "status": "carried_locked"}}},
|
|
|
|
|
}
|
|
|
|
|
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, []artifacts.RemoteLock{{Source: canonical}}, true, "current")
|
|
|
|
|
if err != nil || len(outputs) != 1 {
|
|
|
|
|
t.Fatalf("outputs=%#v err=%v", outputs, err)
|
|
|
|
|
}
|
|
|
|
|
output := outputs[0]
|
|
|
|
|
if output.Source != canonical || output.State != "published" || output.LockState != "locked" || output.Outcome != "carried_locked" || output.DeclaredKey != declaration.DestinationKey || output.OriginRun != current.RunID {
|
|
|
|
|
t.Fatalf("accepted source lost publication evidence: %#v", output)
|
|
|
|
|
}
|
|
|
|
|
if cfg.PublicationOutputs()[0].Source != source {
|
|
|
|
|
t.Fatal("inspection mutated the source declaration")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|