Route executions through neutral snapshots

This commit is contained in:
2026-10-11 01:25:45 +00:00
parent 24e8cd8629
commit 7e5f84bf16
12 changed files with 217 additions and 191 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
)
func TestAppParseUsesRegisteredBindingsWithTheAdapter(t *testing.T) {
@@ -33,7 +34,7 @@ func TestAppParseUsesRegisteredBindingsWithTheAdapter(t *testing.T) {
t.Fatal(err)
}
options := internalcobra.ParseOptions{Context: context.Background()}
options := commandmeta.ParseOptions{Context: context.Background()}
result := app.parse([]string{"config", "show", "one", "--scope", "app"}, options)
if result.Failure != nil {
t.Fatalf("parse failed: %s", result.Failure.Message)
@@ -47,6 +48,32 @@ func TestAppParseUsesRegisteredBindingsWithTheAdapter(t *testing.T) {
}
}
func TestRoutingSnapshotPairsParsedKeyWithItsRunner(t *testing.T) {
command := NewCommand(CommandSpec[struct{}]{Name: "run", Run: func(Invocation, struct{}) Outcome { return Success() }})
app, err := New(Identity{Name: "example"}, command)
if err != nil {
t.Fatal(err)
}
snapshot := app.routeSnapshot()
session := internalcobra.Prepare(snapshot.metadata, commandmeta.ParseOptions{Context: context.Background()})
parsed := session.Parse([]string{"run"})
if parsed.Failure != nil {
t.Fatalf("parse failed: %s", parsed.Failure.Message)
}
if parsed.Key == 0 || snapshot.runners[parsed.Key] == nil || !snapshot.runners[parsed.Key].hasRun() {
t.Fatalf("parsed key %d has no matching runner in its snapshot", parsed.Key)
}
if app.routeSnapshot() != snapshot {
t.Fatal("unchanged registry rebuilt its routing snapshot")
}
if err := app.Register(NewCommand(CommandSpec[struct{}]{Name: "late"})); err != nil {
t.Fatal(err)
}
if app.routeSnapshot() == snapshot {
t.Fatal("successful registration kept stale routing metadata")
}
}
func TestExecutionRefreshesHelpMetadataAfterRegistration(t *testing.T) {
first := NewCommand(CommandSpec[struct{}]{
Name: "first",