190 lines
7.3 KiB
Go
190 lines
7.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
"go.yaml.in/yaml/v4"
|
|
)
|
|
|
|
type readinessClient struct {
|
|
calls int
|
|
content string
|
|
}
|
|
|
|
func (client *readinessClient) Generate(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
client.calls++
|
|
return &promptkit.GenerateResponse{Content: client.content}, nil
|
|
}
|
|
|
|
func TestPreflightRoutesAndReports(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, config, input, _ := runFixture(t, probe)
|
|
client := &readinessClient{content: `{"status":"ok"}`}
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, test := range []struct {
|
|
args []string
|
|
format string
|
|
}{
|
|
{[]string{"preflight", "demo"}, "json"},
|
|
{[]string{"pipeline", "preflight", "demo"}, "yaml"},
|
|
} {
|
|
args := append(test.args, "--config", config, "--input-file", input, "--cache", "bypass", "--format", test.format)
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status != 0 {
|
|
t.Fatalf("%v: %+v %s", args, outcome, stderr.String())
|
|
}
|
|
var report preflightReport
|
|
if test.format == "json" {
|
|
err = json.Unmarshal(out.Bytes(), &report)
|
|
} else {
|
|
err = yaml.Unmarshal(out.Bytes(), &report)
|
|
}
|
|
if err != nil || !report.Ready || report.Pipeline != "demo" || len(report.Checks) == 0 {
|
|
t.Fatalf("report=%+v error=%v output=%s", report, err, out.String())
|
|
}
|
|
for _, check := range report.Checks {
|
|
if check.ID == "reachability" || check.ID == "authentication" || check.Result == "warning" {
|
|
t.Fatalf("unavailable HTTP diagnostics created a check or warning: %+v", check)
|
|
}
|
|
}
|
|
}
|
|
if client.calls != 2 || probe.parsed != 2 {
|
|
t.Fatalf("calls=%d parses=%d", client.calls, probe.parsed)
|
|
}
|
|
client.content = `{"status":"bad"}`
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass", "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
var failed preflightReport
|
|
if err := json.Unmarshal(out.Bytes(), &failed); err != nil || outcome.Status == 0 || failed.Ready || len(failed.Checks) == 0 {
|
|
t.Fatalf("outcome=%+v report=%+v error=%v", outcome, failed, err)
|
|
}
|
|
if outcome.Cause == nil {
|
|
t.Fatal("failed readiness lost its cause")
|
|
}
|
|
}
|
|
|
|
func TestPreflightLocalFailureStopsBeforeProvider(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, config, input, _ := runFixture(t, probe)
|
|
client := &readinessClient{content: `{"status":"ok"}`}
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input + "-missing", "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
var report preflightReport
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil || outcome.Status == 0 || report.Ready || client.calls != 0 {
|
|
t.Fatalf("outcome=%+v report=%+v error=%v calls=%d", outcome, report, err, client.calls)
|
|
}
|
|
if !errors.Is(outcome.Cause, context.Canceled) && outcome.Cause == nil {
|
|
t.Fatal("missing input cause")
|
|
}
|
|
out.Reset()
|
|
stderr.Reset()
|
|
outcome = app.Execute(context.Background(), []string{"preflight", "demo", "--config", config}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status == 0 || out.Len() != 0 || client.calls != 0 {
|
|
t.Fatalf("missing required flag: %+v %q", outcome, out.String())
|
|
}
|
|
}
|
|
|
|
func TestPreflightHelpIsLazy(t *testing.T) {
|
|
app, err := NewApplication("test", Options{LookupEnv: func(string) (string, bool) { t.Fatal("help loaded configuration"); return "", false }})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, args := range [][]string{{"preflight", "--help"}, {"pipeline", "preflight", "--help"}} {
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status != 0 || !strings.Contains(out.String(), "--input-file") {
|
|
t.Fatalf("%v: %+v %s", args, outcome, stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreflightNativeTextVisibility(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, config, input, _ := runFixture(t, probe)
|
|
client := &readinessClient{content: `{"status":"ok"}`}
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, test := range []struct {
|
|
flag string
|
|
summary, detail bool
|
|
}{{"", true, false}, {"--verbose", true, true}, {"--quiet", false, false}} {
|
|
args := []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass"}
|
|
if test.flag != "" {
|
|
args = append(args, test.flag)
|
|
}
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
|
|
if outcome.Status != 0 || strings.Contains(out.String(), "is ready") != test.summary || strings.Contains(stderr.String(), "Source input is valid") != test.detail {
|
|
t.Fatalf("%s: outcome=%+v stdout=%q stderr=%q", test.flag, outcome, out.String(), stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreflightReportDeliveryFailureDoesNotRepeatChecks(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, config, input, _ := runFixture(t, probe)
|
|
client := &readinessClient{content: `{"status":"ok"}`}
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cause := errors.New("output unavailable")
|
|
var stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass", "--format", "json"}, commandkit.Streams{Out: failingResultWriter{cause: cause}, Err: &stderr})
|
|
if outcome.Status == 0 || client.calls != 1 || probe.parsed != 1 {
|
|
t.Fatalf("outcome=%+v calls=%d parses=%d", outcome, client.calls, probe.parsed)
|
|
}
|
|
}
|
|
|
|
func TestPreflightCacheFailureWarnsWithoutPreventingReadiness(t *testing.T) {
|
|
probe := &runProbe{}
|
|
_, config, input, _ := runFixture(t, probe)
|
|
client := &readinessClient{content: `{"status":"ok"}`}
|
|
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file := filepath.Join(t.TempDir(), "not-a-directory")
|
|
if err := os.WriteFile(file, []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encoded, err := json.Marshal(filepath.Join(file, "cache"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out, stderr bytes.Buffer
|
|
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--set", "/cache_directory=" + string(encoded), "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
|
|
var report preflightReport
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil || outcome.Status != 0 || !report.Ready {
|
|
t.Fatalf("outcome=%+v report=%+v error=%v stderr=%s", outcome, report, err, stderr.String())
|
|
}
|
|
found := false
|
|
for _, check := range report.Checks {
|
|
if check.ID == "cache" && check.Result == "warning" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("missing cache warning: %+v", report)
|
|
}
|
|
}
|