Files
notarius/internal/cli/command_contract_test.go

192 lines
6.3 KiB
Go
Raw Normal View History

2026-07-18 15:35:58 +00:00
package cli
import (
"bytes"
"context"
2026-07-18 15:35:58 +00:00
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
2026-07-18 15:35:58 +00:00
"strings"
"testing"
2026-08-25 01:55:09 +00:00
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/releasekit/buildinfo"
2026-07-18 15:35:58 +00:00
)
func TestCommandSyntaxErrorsUseStderrAndExitTwo(t *testing.T) {
for _, args := range [][]string{
{"unknown"},
{"pipeline", "unknown"},
{"config", "validate", "--unknown"},
{"run", "demo", "--input-file", "missing", "--cache", "invalid"},
{"pipeline", "validate"},
{"run", "demo", "--input-file", "missing", "--only", "spells"},
} {
t.Run(strings.Join(args, " "), func(t *testing.T) {
2026-07-18 15:35:58 +00:00
var stdout, stderr bytes.Buffer
code := RunWithOptions(args, &stdout, &stderr, commandContractOptions(t))
if code != 2 || stdout.Len() != 0 || stderr.Len() == 0 {
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
2026-07-18 15:35:58 +00:00
}
})
}
}
2026-08-25 01:55:09 +00:00
func TestCommandVersionOutput(t *testing.T) {
var stdout, stderr bytes.Buffer
code := RunWithOptions([]string{"version"}, &stdout, &stderr, Options{})
if code != 0 || !strings.Contains(stdout.String(), buildinfo.Version()) || stderr.Len() != 0 {
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
app, err := NewApplication("v1.2.3", Options{})
if err != nil {
t.Fatal(err)
}
stdout.Reset()
stderr.Reset()
result := app.Execute(context.Background(), []string{"version"}, commandkit.Streams{Out: &stdout, Err: &stderr})
if result.Status != 0 || !strings.Contains(stdout.String(), "v1.2.3") || stderr.Len() != 0 {
t.Fatalf("status=%d stdout=%q stderr=%q", result.Status, stdout.String(), stderr.String())
2026-08-25 01:55:09 +00:00
}
}
2026-07-18 15:35:58 +00:00
func TestConfigDiscoveryPrefersExplicitPathThenEnvironment(t *testing.T) {
explicit := writeCommandConfig(t, "explicit", "alpha")
environment := writeCommandConfig(t, "environment", "beta")
lookup := func(name string) (string, bool) { return environment, name == "NOTARIUS_CONFIG" }
for _, test := range []struct {
name string
flags []string
want []string
}{
{name: "explicit", flags: []string{"--config", explicit}, want: []string{"alpha", "explicit"}},
{name: "environment", want: []string{"beta", "environment"}},
} {
t.Run(test.name, func(t *testing.T) {
var stdout, stderr bytes.Buffer
args := append([]string{"pipeline", "list", "--format", "json"}, test.flags...)
code := RunWithOptions(args, &stdout, &stderr, commandContractOptionsWithLookup(t, lookup))
var ids []string
if err := json.Unmarshal(stdout.Bytes(), &ids); err != nil {
t.Fatal(err)
}
if code != 0 || !reflect.DeepEqual(ids, test.want) {
t.Fatalf("status=%d ids=%#v stderr=%q", code, ids, stderr.String())
}
})
2026-07-18 15:35:58 +00:00
}
}
func TestConfigLoadingFailuresRetainPathContext(t *testing.T) {
root := t.TempDir()
missing := filepath.Join(root, "missing.yml")
malformed := filepath.Join(root, "malformed.yml")
if err := os.WriteFile(malformed, []byte("version: [\n"), 0600); err != nil {
2026-07-18 15:35:58 +00:00
t.Fatal(err)
}
for _, path := range []string{missing, malformed} {
var stdout, stderr bytes.Buffer
code := RunWithOptions([]string{"config", "validate", "--config", path}, &stdout, &stderr, commandContractOptions(t))
if code == 0 || !strings.Contains(stderr.String(), path) {
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
2026-07-18 15:35:58 +00:00
}
}
func TestPipelineValidationSelectsConfiguredCompositionOffline(t *testing.T) {
path := writeResolvableCommandConfig(t)
for _, test := range []struct {
id string
status int
}{
{id: "demo"}, {id: "missing", status: 2},
} {
var stdout, stderr bytes.Buffer
code := RunWithOptions([]string{"pipeline", "validate", test.id, "--config", path, "--format", "json"}, &stdout, &stderr, commandContractOptions(t))
if code != test.status {
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if code == 0 {
var report struct {
Pipeline string `json:"pipeline"`
Valid bool `json:"valid"`
}
if err := json.Unmarshal(stdout.Bytes(), &report); err != nil {
t.Fatal(err)
}
if report.Pipeline != test.id || !report.Valid {
t.Fatalf("validation report=%#v", report)
}
} else if !strings.Contains(stderr.String(), test.id) {
t.Fatalf("missing pipeline context: %s", stderr.String())
}
2026-07-18 15:35:58 +00:00
}
}
func TestPipelineValidationDefaultProfileNeedsNoCredentialsOrProvider(t *testing.T) {
path := writeCommandConfigContent(t, `version: 6
2026-08-03 17:15:07 +00:00
pipelines:
demo:
llm_profile: notarius/dnd-extraction
input: {module: seriatim}
2026-08-03 17:15:07 +00:00
artifacts:
spells:
extract: {module: dnd/spells}
2026-08-03 17:15:07 +00:00
`)
client := &diagnosticClient{}
2026-08-03 17:15:07 +00:00
var stdout, stderr bytes.Buffer
code := RunWithOptions([]string{"pipeline", "validate", "demo", "--config", path}, &stdout, &stderr, Options{LLMClient: client, LookupEnv: emptyLookup})
if code != 0 || client.calls.Load() != 0 {
t.Fatalf("status=%d calls=%d stderr=%q", code, client.calls.Load(), stderr.String())
2026-07-18 15:35:58 +00:00
}
}
func TestPipelineListSortsNormalizedIDs(t *testing.T) {
path := writeCommandConfig(t, " zeta ", "alpha")
2026-07-18 15:35:58 +00:00
var stdout, stderr bytes.Buffer
code := RunWithOptions([]string{"pipeline", "list", "--config", path, "--format", "json"}, &stdout, &stderr, commandContractOptions(t))
var ids []string
if err := json.Unmarshal(stdout.Bytes(), &ids); err != nil {
t.Fatal(err)
2026-07-18 15:35:58 +00:00
}
if code != 0 || !reflect.DeepEqual(ids, []string{"alpha", "zeta"}) {
t.Fatalf("status=%d ids=%#v stderr=%q", code, ids, stderr.String())
2026-07-18 15:35:58 +00:00
}
}
func commandContractOptions(t *testing.T) Options {
return commandContractOptionsWithLookup(t, emptyLookup)
}
func commandContractOptionsWithLookup(t *testing.T, lookup func(string) (string, bool)) Options {
t.Helper()
options := productionCLIOptions(t)
options.LookupEnv = lookup
return options
2026-07-18 15:35:58 +00:00
}
func writeCommandConfig(t *testing.T, firstID, secondID string) string {
t.Helper()
return writeCommandConfigContent(t, fmt.Sprintf("version: 6\npipelines:\n %q:\n input: {module: seriatim}\n %q:\n input: {module: seriatim}\n", firstID, secondID))
2026-07-18 15:35:58 +00:00
}
func writeResolvableCommandConfig(t *testing.T) string {
t.Helper()
return writeCommandConfigContent(t, `version: 6
2026-07-18 15:35:58 +00:00
pipelines:
demo:
input: {module: seriatim}
2026-07-18 15:35:58 +00:00
artifacts:
spells:
extract: {module: dnd/spells}
2026-07-18 15:35:58 +00:00
`)
}
func writeCommandConfigContent(t *testing.T, content string) string {
t.Helper()
return writeProductionContractConfig(t, content)
2026-07-18 15:35:58 +00:00
}