138 lines
6.3 KiB
Go
138 lines
6.3 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||
|
|
)
|
||
|
|
|
||
|
|
type observingInspector struct {
|
||
|
|
scriptorium.FakeRunner
|
||
|
|
inspect func(scriptorium.RequirementsRequest)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *observingInspector) InspectRequirements(ctx context.Context, req scriptorium.RequirementsRequest) (scriptorium.RequirementsReport, error) {
|
||
|
|
if r.inspect != nil {
|
||
|
|
r.inspect(req)
|
||
|
|
}
|
||
|
|
return r.FakeRunner.InspectRequirements(ctx, req)
|
||
|
|
}
|
||
|
|
|
||
|
|
type warningRecorder struct{ warnings []string }
|
||
|
|
|
||
|
|
func (*warningRecorder) Info(string, ...any) {}
|
||
|
|
func (r *warningRecorder) Warn(message string, _ ...any) { r.warnings = append(r.warnings, message) }
|
||
|
|
func (*warningRecorder) Debug(string, ...any) {}
|
||
|
|
func (*warningRecorder) Progress(string, ...any) {}
|
||
|
|
|
||
|
|
func preflightTestConfig(t *testing.T) *config.Config {
|
||
|
|
t.Helper()
|
||
|
|
cfg := testConfig(t)
|
||
|
|
configured := map[string]config.ScriptoriumArtifactConfig{"recap": {Enabled: true, PromptID: "recap", OutputPath: "artifacts/recap.md"}}
|
||
|
|
cfg.Pipeline.Scriptorium = &config.ScriptoriumConfig{Binary: "scriptorium", Timeout: "1m", Artifacts: configured}
|
||
|
|
cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: configured}
|
||
|
|
return cfg
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreflightRunsBeforeAttemptsAndWarningsAllowExecution(t *testing.T) {
|
||
|
|
cfg := preflightTestConfig(t)
|
||
|
|
var earlierRuns int
|
||
|
|
reporter := &warningRecorder{}
|
||
|
|
runner := &observingInspector{inspect: func(req scriptorium.RequirementsRequest) {
|
||
|
|
if earlierRuns != 0 {
|
||
|
|
t.Fatal("inspection ran after first attempt")
|
||
|
|
}
|
||
|
|
if req.PromptID != "recap" {
|
||
|
|
t.Fatalf("request = %+v", req)
|
||
|
|
}
|
||
|
|
}}
|
||
|
|
runner.RequirementsResult = &scriptorium.RequirementsReport{Complete: false}
|
||
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &earlierRuns}, mustSelectStage(t, "analyze")}, RunOptions{Env: &Env{Scriptorium: runner}, Reporter: reporter})
|
||
|
|
if err != nil || summary == nil || !summary.Complete || earlierRuns != 1 || len(runner.RequirementsRequests) != 1 || len(runner.RunRequests) != 1 || len(reporter.warnings) == 0 {
|
||
|
|
t.Fatalf("summary = %+v, error = %v, warnings = %v", summary, err, reporter.warnings)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreflightFailurePreservesCompletedAuthorityOnForce(t *testing.T) {
|
||
|
|
cfg := preflightTestConfig(t)
|
||
|
|
analyze := mustSelectStage(t, "analyze")
|
||
|
|
initial := &scriptorium.FakeRunner{}
|
||
|
|
if _, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Env: &Env{Scriptorium: initial}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
reused := &scriptorium.FakeRunner{RequirementsErr: errors.New("unexpected inspection")}
|
||
|
|
if _, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Env: &Env{Scriptorium: reused}}); err != nil || len(reused.RequirementsRequests) != 0 {
|
||
|
|
t.Fatalf("reusable work inspected: requests = %+v, error = %v", reused.RequirementsRequests, err)
|
||
|
|
}
|
||
|
|
store := &manifest.LocalStore{}
|
||
|
|
before, err := store.Load(context.Background(), manifestPathFor(cfg))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
output := filepath.Join(cfg.Pipeline.Workspace.Root, "work", cfg.Session.Campaign, cfg.Session.SessionID, "artifacts", "recap.md")
|
||
|
|
priorBytes, err := os.ReadFile(output)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
runner := &scriptorium.FakeRunner{RequirementsResult: &scriptorium.RequirementsReport{Complete: true, Inputs: []scriptorium.RequirementInput{{Name: "transcript", Kind: "required"}}}}
|
||
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Force: true, Env: &Env{Scriptorium: runner}})
|
||
|
|
if err == nil || !strings.Contains(err.Error(), `required input "transcript"`) || summary == nil || summary.StageResults[0].Attempted || len(runner.RunRequests) != 0 {
|
||
|
|
t.Fatalf("summary = %+v, error = %v", summary, err)
|
||
|
|
}
|
||
|
|
after, err := store.Load(context.Background(), manifestPathFor(cfg))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(before.Stages["analyze"], after.Stages["analyze"]) {
|
||
|
|
t.Fatalf("analysis authority changed: before=%+v after=%+v", before.Stages["analyze"], after.Stages["analyze"])
|
||
|
|
}
|
||
|
|
afterBytes, err := os.ReadFile(output)
|
||
|
|
if err != nil || !reflect.DeepEqual(priorBytes, afterBytes) {
|
||
|
|
t.Fatalf("output changed, error = %v", err)
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(summaryRunRoot(cfg, summary.RunID)); !errors.Is(err, os.ErrNotExist) {
|
||
|
|
t.Fatalf("preflight created run directory: %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreflightReusesInvocationOwnedRunner(t *testing.T) {
|
||
|
|
cfg := preflightTestConfig(t)
|
||
|
|
fake := &scriptorium.FakeRunner{}
|
||
|
|
created := 0
|
||
|
|
deps := invocationDependencies{newScriptorium: func() scriptorium.Runner { created++; return fake }}
|
||
|
|
plan := BoundedPlan{stages: []stage.Stage{mustSelectStage(t, "analyze")}}
|
||
|
|
_, err := executePlanWithDependencies(context.Background(), cfg, plan, RunOptions{}, deps)
|
||
|
|
if err != nil || created != 1 || len(fake.RequirementsRequests) != 1 || len(fake.RunRequests) != 1 {
|
||
|
|
t.Fatalf("created = %d, inspection = %d, generation = %d, error = %v", created, len(fake.RequirementsRequests), len(fake.RunRequests), err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func summaryRunRoot(cfg *config.Config, runID string) string {
|
||
|
|
return filepath.Join(cfg.Pipeline.Workspace.Root, "work", cfg.Session.Campaign, cfg.Session.SessionID, "runs", runID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreflightInspectionFailureAndUnrelatedSelection(t *testing.T) {
|
||
|
|
cfg := preflightTestConfig(t)
|
||
|
|
want := errors.New("inspector unavailable")
|
||
|
|
runner := &scriptorium.FakeRunner{RequirementsErr: want}
|
||
|
|
var attempts int
|
||
|
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &attempts}, mustSelectStage(t, "analyze")}, RunOptions{Env: &Env{Scriptorium: runner}})
|
||
|
|
if !errors.Is(err, want) || attempts != 0 || summary == nil || summary.StageResults[0].Attempted {
|
||
|
|
t.Fatalf("summary = %+v, attempts = %d, error = %v", summary, attempts, err)
|
||
|
|
}
|
||
|
|
runner.RequirementsRequests = nil
|
||
|
|
_, err = executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &attempts}}, RunOptions{Force: true, Env: &Env{Scriptorium: runner}})
|
||
|
|
if err != nil || len(runner.RequirementsRequests) != 0 {
|
||
|
|
t.Fatalf("unrelated run error = %v, requests = %+v", err, runner.RequirementsRequests)
|
||
|
|
}
|
||
|
|
}
|