178 lines
8.2 KiB
Go
178 lines
8.2 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestNativeDiscoveryDoesNotAcquireConfiguration(t *testing.T) {
|
|
root := t.TempDir()
|
|
t.Chdir(root)
|
|
if err := os.WriteFile(filepath.Join(root, "pipeline.yml"), []byte("invalid: ["), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
application, err := newApplication(commandDependencies{
|
|
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
|
t.Fatal("discovery loaded pipeline configuration")
|
|
return nil, nil
|
|
},
|
|
newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
|
t.Fatal("discovery initialized object storage")
|
|
return nil, nil
|
|
},
|
|
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
|
|
t.Fatal("discovery bootstrapped credentials")
|
|
return nil, nil
|
|
},
|
|
}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, args := range [][]string{nil, {"help"}, {"--help"}, {"session"}, {"artifacts"}, {"version"}, {"help", "run"}, {"help", "config", "show"}, {"session", "locks"}, {"config", "scopes"}, {"completion"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}} {
|
|
var out, diagnostics bytes.Buffer
|
|
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics})
|
|
if outcome.Status != 0 || out.Len() == 0 || diagnostics.Len() != 0 {
|
|
t.Fatalf("%v: outcome=%+v stdout=%q stderr=%q", args, outcome, out.String(), diagnostics.String())
|
|
}
|
|
}
|
|
for _, query := range []struct {
|
|
args []string
|
|
fragment string
|
|
}{
|
|
{[]string{"__complete", "run", "--config", "missing.yml", "--secrets-dir", "missing-secrets", "--stage", "tr"}, "transcribe\n"},
|
|
{[]string{"__complete", "session", "plan", "--from", "an"}, "analyze\n"},
|
|
{[]string{"__complete", "run", "--through", "pub"}, "publish\n"},
|
|
{[]string{"__complete", "config", "show", "--scope", ""}, "party\n"},
|
|
{[]string{"__complete", "run", "--config", ""}, ":0\n"},
|
|
{[]string{"__complete", "session", "status", "--session-file", ""}, ":0\n"},
|
|
{[]string{"__complete", "session", "plan", "--party-file", ""}, ":0\n"},
|
|
{[]string{"__complete", "config", "diff", "--scope", "party", "--against-party-file", ""}, ":0\n"},
|
|
{[]string{"__complete", "session", "init", "--output", ""}, ":0\n"},
|
|
{[]string{"__complete", "session", "init", "--audio-dir", ""}, ":16\n"},
|
|
{[]string{"__complete", "run", "--secrets-dir", ""}, ":16\n"},
|
|
} {
|
|
var out, diagnostics bytes.Buffer
|
|
outcome := application.Execute(context.Background(), query.args, commandkit.Streams{Out: &out, Err: &diagnostics})
|
|
if outcome.Status != 0 || diagnostics.Len() != 0 || !strings.Contains(out.String(), query.fragment) {
|
|
t.Fatalf("query=%v outcome=%#v reply=%q diagnostic=%s", query.args, outcome, out.String(), diagnostics.String())
|
|
}
|
|
}
|
|
if outcome := application.Execute(context.Background(), []string{"unknown"}, commandkit.Streams{}); outcome.Status != 2 {
|
|
t.Fatalf("invalid invocation: %+v", outcome)
|
|
}
|
|
}
|
|
|
|
func TestNativeSelectionRetainsPresenceAndCallerContext(t *testing.T) {
|
|
flags := newConfigurationFlags(func(in *configurationSelection) *configurationSelection { return in })
|
|
type contextKey struct{}
|
|
ctx := context.WithValue(context.Background(), contextKey{}, "caller")
|
|
application, err := commandkit.New(commandkit.Identity{Name: "narratio"}, commandkit.NewCommand(commandkit.CommandSpec[configurationSelection]{
|
|
Name: "inspect",
|
|
Flags: []commandkit.Flag[configurationSelection]{flags.Config, flags.Overlay, flags.Campaign, flags.CampaignFile, flags.Session, flags.SessionFile, flags.PartyFile, flags.SecretsDir},
|
|
CheckInput: func(in configurationSelection) error {
|
|
if !in.Config.Set || in.Config.Value != "" || !in.Campaign.Set || !in.CampaignFile.Set || in.PartyFile.Set {
|
|
t.Fatalf("selection lost presence or complementary selectors: %+v", in)
|
|
}
|
|
return nil
|
|
},
|
|
Run: func(inv commandkit.Invocation, in configurationSelection) commandkit.Outcome {
|
|
if inv.Context.Value(contextKey{}) != "caller" {
|
|
t.Fatal("caller context lost")
|
|
}
|
|
return commandkit.Success()
|
|
},
|
|
}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outcome := application.Execute(ctx, []string{"inspect", "--config=", "--campaign", "campaign", "--campaign-file", "campaign.yml"}, commandkit.Streams{})
|
|
if outcome.Status != 0 {
|
|
t.Fatalf("selection outcome: %+v", outcome)
|
|
}
|
|
}
|
|
|
|
func TestOperationOutcomePreservesCausesAndOperationalPrecedence(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "pipeline.yml")
|
|
if err := os.WriteFile(path, []byte("invalid: ["), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, invalid := config.LoadPipeline(path)
|
|
if invalid == nil {
|
|
t.Fatal("expected invalid configuration")
|
|
}
|
|
input := &inputError{message: "invalid identity selection"}
|
|
operational := errors.New("storage unavailable")
|
|
_, catalogInput := config.DeriveArtifactCatalog(&config.PipelineConfig{Scriptorium: &config.ScriptoriumConfig{Artifacts: map[string]config.ScriptoriumArtifactConfig{
|
|
"result": {OutputPath: "artifacts/result.md", DependsOn: []string{"missing"}},
|
|
}}}, config.ResolvedParty{})
|
|
var catalogError *config.ArtifactCatalogInputError
|
|
if !errors.As(catalogInput, &catalogError) || errors.Unwrap(catalogError) == nil {
|
|
t.Fatalf("catalog validation lost its typed input cause: %v", catalogInput)
|
|
}
|
|
for _, tc := range []struct {
|
|
err error
|
|
status int
|
|
}{
|
|
{nil, 0}, {input, 2}, {invalid, 2},
|
|
{catalogInput, 2},
|
|
{errors.Join(catalogInput, operational), 1},
|
|
{errors.Join(catalogInput, context.Canceled), 1},
|
|
{&config.SessionIdentityError{Source: "session.yml", Field: "session_id", Expected: "one", Actual: "two"}, 2},
|
|
{fmt.Errorf("request: %w", errors.Join(input, invalid)), 2},
|
|
{errors.Join(input, operational), 1},
|
|
{fmt.Errorf("request: %w", errors.Join(invalid, context.Canceled)), 1},
|
|
} {
|
|
outcome := operationOutcome(tc.err)
|
|
if outcome.Status != tc.status || (tc.err != nil && !errors.Is(outcome.Cause, tc.err)) {
|
|
t.Fatalf("%v: outcome=%+v, want status %d and original cause", tc.err, outcome, tc.status)
|
|
}
|
|
}
|
|
application, err := commandkit.New(commandkit.Identity{Name: "narratio"}, commandkit.NewCommand(commandkit.CommandSpec[struct{}]{
|
|
Name: "fail", Run: func(commandkit.Invocation, struct{}) commandkit.Outcome { return operationOutcome(operational) },
|
|
}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var diagnostics bytes.Buffer
|
|
outcome := application.Execute(context.Background(), []string{"fail", "--quiet"}, commandkit.Streams{Out: io.Discard, Err: &diagnostics})
|
|
if outcome.Status != 1 || !errors.Is(outcome.Cause, operational) || strings.Count(diagnostics.String(), operational.Error()) != 1 {
|
|
t.Fatalf("failure delivery: outcome=%+v diagnostics=%q", outcome, diagnostics.String())
|
|
}
|
|
}
|
|
|
|
func TestNativeTreeRejectsRemovedRoutesAndMissingOperandsBeforeAcquisition(t *testing.T) {
|
|
application, err := newApplication(commandDependencies{
|
|
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
|
t.Fatal("invalid input loaded configuration")
|
|
return nil, nil
|
|
},
|
|
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
|
|
t.Fatal("invalid input bootstrapped credentials")
|
|
return nil, nil
|
|
},
|
|
}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cases := [][]string{{"run"}, {"artifacts", "regenerate"}, {"session", "init"}, {"session", "validate"}, {"session", "status"}, {"session", "plan"}, {"session", "artifacts"}, {"session", "restore"}, {"session", "clean"}, {"session", "locks", "list"}, {"session", "locks", "add"}, {"session", "locks", "remove"}, {"run-stage"}, {"analyze"}, {"publish"}, {"regenerate-artifacts"}, {"clean"}, {"plan"}, {"status"}, {"resume"}, {"lock"}, {"unlock"}, {"config", "artifacts"}, {"run", "session", "--session-id", "session"}, {"session", "status", "session", "--manifest", "manifest.json"}}
|
|
for _, args := range cases {
|
|
var out, diagnostic bytes.Buffer
|
|
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
|
if outcome.Status != 2 {
|
|
t.Fatalf("args=%v status=%d diagnostic=%s", args, outcome.Status, diagnostic.String())
|
|
}
|
|
}
|
|
}
|