194 lines
6.4 KiB
Go
194 lines
6.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/buildinfo"
|
|
)
|
|
|
|
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) {
|
|
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())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCommandVersionOutput(t *testing.T) {
|
|
previous := buildinfo.Override
|
|
t.Cleanup(func() { buildinfo.Override = previous })
|
|
for _, test := range []struct {
|
|
name, override string
|
|
status int
|
|
fragment string
|
|
}{
|
|
{name: "development", fragment: "development"},
|
|
{name: "release", override: "v1.2.3", fragment: "v1.2.3"},
|
|
{name: "invalid release identity", override: "release", status: 1},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
buildinfo.Override = test.override
|
|
var stdout, stderr bytes.Buffer
|
|
code := RunWithOptions([]string{"version"}, &stdout, &stderr, Options{})
|
|
if code != test.status || test.status == 0 && (!strings.Contains(stdout.String(), test.fragment) || stderr.Len() != 0) || test.status != 0 && (stdout.Len() != 0 || stderr.Len() == 0) {
|
|
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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 {
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPipelineValidationDefaultProfileNeedsNoCredentialsOrProvider(t *testing.T) {
|
|
path := writeCommandConfigContent(t, `version: 6
|
|
pipelines:
|
|
demo:
|
|
llm_profile: notarius/dnd-extraction
|
|
input: {module: seriatim}
|
|
artifacts:
|
|
spells:
|
|
extract: {module: dnd/spells}
|
|
`)
|
|
client := &diagnosticClient{}
|
|
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())
|
|
}
|
|
}
|
|
|
|
func TestPipelineListSortsNormalizedIDs(t *testing.T) {
|
|
path := writeCommandConfig(t, " zeta ", "alpha")
|
|
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)
|
|
}
|
|
if code != 0 || !reflect.DeepEqual(ids, []string{"alpha", "zeta"}) {
|
|
t.Fatalf("status=%d ids=%#v stderr=%q", code, ids, stderr.String())
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
func writeResolvableCommandConfig(t *testing.T) string {
|
|
t.Helper()
|
|
return writeCommandConfigContent(t, `version: 6
|
|
pipelines:
|
|
demo:
|
|
input: {module: seriatim}
|
|
artifacts:
|
|
spells:
|
|
extract: {module: dnd/spells}
|
|
`)
|
|
}
|
|
|
|
func writeCommandConfigContent(t *testing.T, content string) string {
|
|
t.Helper()
|
|
return writeProductionContractConfig(t, content)
|
|
}
|