Validate sessions with prepared downstream inputs
This commit is contained in:
@@ -5,13 +5,18 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
"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/preflight"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
@@ -33,7 +38,9 @@ func TestSessionValidationSharesPromptFindingsAndLeavesNoWorkspace(t *testing.T)
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := &scriptorium.FakeRunner{RequirementsResult: &scriptorium.RequirementsReport{Complete: false, Inputs: []scriptorium.RequirementInput{{Name: "conditional", Kind: "conditional", Explanation: "unknown"}}}}
|
||||
seedValidationPrepared(t, cfg)
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return &audita.FakeRunner{}, nil }
|
||||
deps.newScriptorium = func() scriptorium.Runner { return fake }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err != nil || !report.Valid || len(fake.RequirementsRequests) == 0 {
|
||||
@@ -53,8 +60,8 @@ func TestSessionValidationSharesPromptFindingsAndLeavesNoWorkspace(t *testing.T)
|
||||
}
|
||||
}
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, err := os.Stat(paths.Root); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("validation created workspace: %v", err)
|
||||
if _, err := os.Stat(filepath.Join(paths.RunsDir, ".validation", "polish")); err != nil {
|
||||
t.Fatalf("probe namespace: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +111,9 @@ func TestSessionValidationReportsOptionalSourceAndDisabledPrerequisite(t *testin
|
||||
child.Inputs["recap"] = config.ScriptoriumInputConfig{Source: "narratio.artifact.session_recap", Required: false}
|
||||
cfg.ArtifactCatalog.Artifacts["player_handout"] = child
|
||||
fake := &validationReportRunner{}
|
||||
seedValidationPrepared(t, cfg)
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return &audita.FakeRunner{}, nil }
|
||||
deps.newScriptorium = func() scriptorium.Runner { return fake }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err != nil || !report.Valid {
|
||||
@@ -134,9 +143,128 @@ func TestSessionValidationSkipsInspectorWithoutTargets(t *testing.T) {
|
||||
cfg.Pipeline.Trim.Enabled = &disabled
|
||||
cfg.Pipeline.Scriptorium = nil
|
||||
cfg.ArtifactCatalog = nil
|
||||
seedValidationPrepared(t, cfg)
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return &audita.FakeRunner{}, nil }
|
||||
deps.newScriptorium = func() scriptorium.Runner { t.Fatal("unexpected inspector"); return nil }
|
||||
if _, err := inspectSessionValidation(context.Background(), cfg, deps); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func seedValidationPrepared(t *testing.T, cfg *config.Config) artifacts.SessionPaths {
|
||||
t.Helper()
|
||||
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
||||
paths, err := store.EnsureLayoutFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
||||
m.Campaign = cfg.Session.Campaign
|
||||
for _, d := range artifactpolicy.GlossaryInputSources() {
|
||||
path := filepath.Join(paths.InputsDir, d.Filename)
|
||||
mustWriteTestFile(t, path, "synthetic: {entries: []}\n")
|
||||
checksum, err := store.Checksum(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Inputs = append(m.Inputs, manifest.InputRecord{Kind: d.ManifestKind, Path: path, Checksum: checksum})
|
||||
}
|
||||
m.MarkStageSucceeded("prepare", time.Now().UTC(), nil)
|
||||
if err := (&manifest.LocalStore{}).Save(context.Background(), paths.ManifestPath, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func TestSessionValidationUsesPreparedGlossariesWithoutChangingState(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 := seedValidationPrepared(t, cfg)
|
||||
before, err := os.ReadFile(paths.ManifestPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source := cfg.StableInputs.GlossaryFile.Path
|
||||
if err := os.Remove(source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := &audita.FakeRunner{}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return fake, nil }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err != nil || !report.Valid || len(fake.PreflightRequests) != 1 {
|
||||
t.Fatalf("report=%+v calls=%d err=%v", report, len(fake.PreflightRequests), err)
|
||||
}
|
||||
req := fake.PreflightRequests[0]
|
||||
root := artifacts.SessionValidationPolishDir(paths)
|
||||
if req.OutputProcessedPath != filepath.Join(root, "outputs", "transcripts", "polished.json") || req.ReportDir != filepath.Join(root, "logs") || req.WorkDir != filepath.Join(root, "debug") || len(req.GlossaryFiles) != 4 {
|
||||
t.Fatalf("probe request: %+v", req)
|
||||
}
|
||||
after, err := os.ReadFile(paths.ManifestPath)
|
||||
if err != nil || !bytes.Equal(before, after) {
|
||||
t.Fatalf("manifest changed: %v", err)
|
||||
}
|
||||
entries, err := os.ReadDir(paths.RunsDir)
|
||||
if err != nil || len(entries) != 1 || entries[0].Name() != ".validation" {
|
||||
t.Fatalf("runs: %+v %v", entries, err)
|
||||
}
|
||||
if _, err := os.Stat(req.OutputProcessedPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("processing output exists: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionValidationReportsPreparedAndProviderFailures(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)
|
||||
}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
fake := &audita.FakeRunner{}
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return fake, nil }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err == nil || report.Valid || !strings.Contains(err.Error(), "run prepare") || len(fake.PreflightRequests) != 0 {
|
||||
t.Fatalf("missing evidence: %+v %v", report, err)
|
||||
}
|
||||
paths := seedValidationPrepared(t, cfg)
|
||||
failure := errors.New("synthetic provider failure")
|
||||
fake.PreflightErr = failure
|
||||
report, err = inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if !errors.Is(err, failure) || report.Valid || !findingMatches(report.Findings, "ERROR", "audita", "synthetic provider failure") {
|
||||
t.Fatalf("provider: %+v %v", report, err)
|
||||
}
|
||||
fake.PreflightErr = nil
|
||||
if err := os.RemoveAll(filepath.Join(paths.RunsDir, ".validation")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(paths.RunsDir, ".validation"), []byte("conflict"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count := len(fake.PreflightRequests)
|
||||
report, err = inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err == nil || report.Valid || len(fake.PreflightRequests) != count {
|
||||
t.Fatalf("destination: %+v %v", report, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionValidationStopsChecksOnCancellation(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)
|
||||
}
|
||||
seedValidationPrepared(t, cfg)
|
||||
fake := &audita.FakeRunner{}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return fake, nil }
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
report, err := inspectSessionValidation(ctx, cfg, deps)
|
||||
if !errors.Is(err, context.Canceled) || report.Valid || len(fake.PreflightRequests) != 0 {
|
||||
t.Fatalf("cancelled: %+v %v", report, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user