Files
notarius/internal/cli/preflight_capture_test.go

151 lines
6.8 KiB
Go
Raw Normal View History

package cli
import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/promptkit"
)
func TestPreflightCapturesEarlyFailureAndUpdatesLatest(t *testing.T) {
probe := &runProbe{}
_, config, input, _ := runFixture(t, probe)
client := &readinessClient{content: `{"status":"ok"}`}
root := t.TempDir()
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, Now: func() time.Time { return time.Unix(42, 0) }, RunIDGenerator: func(time.Time) (string, error) { return "inspection", nil }})
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", "--debug", "--debug-dir", root, "--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 || report.DebugDirectory != filepath.Join(root, "inspection") || client.calls != 0 {
t.Fatalf("outcome=%+v report=%+v error=%v", outcome, report, err)
}
target, err := os.Readlink(filepath.Join(root, "latest"))
if err != nil || target != "inspection" {
t.Fatalf("latest=%q err=%v", target, err)
}
terminal, err := os.ReadFile(filepath.Join(report.DebugDirectory, "outcome.json"))
if err != nil || !strings.Contains(string(terminal), "missing") {
t.Fatalf("capture=%s err=%v", terminal, err)
}
}
func TestPreflightRetainsCancellationAndCaptureFailure(t *testing.T) {
probe := &runProbe{}
_, config, input, _ := runFixture(t, probe)
root := t.TempDir()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
calls := 0
client := captureClient(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
calls++
if err := os.Mkdir(filepath.Join(root, "inspection", "smoke", "002.json"), 0o700); err != nil {
t.Fatal(err)
}
cancel()
return nil, ctx.Err()
})
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, RunIDGenerator: func(time.Time) (string, error) { return "inspection", nil }})
if err != nil {
t.Fatal(err)
}
var out, stderr bytes.Buffer
outcome := app.Execute(ctx, []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass", "--debug", "--debug-dir", root, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
if !errors.Is(outcome.Cause, context.Canceled) || !strings.Contains(outcome.Cause.Error(), "record debug") || calls != 1 {
t.Fatalf("outcome=%+v cause=%v calls=%d stderr=%s", outcome, outcome.Cause, calls, stderr.String())
}
}
func TestPreflightPositiveAssessmentSurvivesCaptureFailure(t *testing.T) {
probe := &runProbe{}
_, config, input, _ := runFixture(t, probe)
root := t.TempDir()
calls := 0
client := captureClient(func(_ context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
calls++
if latest, err := os.Readlink(filepath.Join(root, "latest")); err != nil || latest != "inspection" {
t.Fatalf("latest before provider call = %q, %v", latest, err)
}
if err := os.Mkdir(filepath.Join(root, "inspection", "smoke", "002.json"), 0o700); err != nil {
t.Fatal(err)
}
return &promptkit.GenerateResponse{Content: `{"status":"ok"}`}, nil
})
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, RunIDGenerator: func(time.Time) (string, error) { return "inspection", nil }})
if err != nil {
t.Fatal(err)
}
var out, stderr bytes.Buffer
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass", "--debug", "--debug-dir", root, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
var report preflightReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil || !report.Ready || outcome.Status == 0 || outcome.Cause == nil || calls != 1 {
t.Fatalf("outcome=%+v report=%+v error=%v calls=%d", outcome, report, err, calls)
}
}
func TestPreflightDebugAllocationFailureAndDisable(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(), "file")
if err := os.WriteFile(file, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
args []string
wantReady bool
}{
{[]string{"--debug", "--debug-dir", filepath.Join(file, "bad")}, false},
{[]string{"--debug", "--debug-dir=", "--cache", "bypass"}, true},
{[]string{"--debug=false", "--debug-dir", filepath.Join(file, "bad"), "--cache", "bypass"}, true},
} {
args := append([]string{"preflight", "demo", "--config", config, "--input-file", input, "--format", "json"}, test.args...)
var out, stderr bytes.Buffer
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &stderr})
var report preflightReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil || report.Ready != test.wantReady || (outcome.Status == 0) != test.wantReady || report.DebugDirectory != "" {
t.Fatalf("%v: outcome=%+v report=%+v err=%v stderr=%s", test.args, outcome, report, err, stderr.String())
}
}
}
func TestPreflightCaptureFailureBeforeLastProfileIsNotReady(t *testing.T) {
probe := &runProbe{}
_, config, input, _ := runFixture(t, probe, func(configuration string) string {
return strings.Replace(configuration, "id: inspect-validator\n default_profile: partial", "id: inspect-validator\n default_profile: chosen", 1)
})
root := t.TempDir()
calls := 0
client := captureClient(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
calls++
if err := os.Mkdir(filepath.Join(root, "inspection", "smoke", "004.json"), 0o700); err != nil {
t.Fatal(err)
}
return &promptkit.GenerateResponse{Content: `{"status":"ok"}`}, nil
})
app, err := NewApplication("test", Options{Registries: runRegistries(t, probe), LLMClient: client, RunIDGenerator: func(time.Time) (string, error) { return "inspection", nil }})
if err != nil {
t.Fatal(err)
}
var out, stderr bytes.Buffer
outcome := app.Execute(context.Background(), []string{"preflight", "demo", "--config", config, "--input-file", input, "--cache", "bypass", "--debug", "--debug-dir", root, "--format", "json"}, commandkit.Streams{Out: &out, Err: &stderr})
var report preflightReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil || report.Ready || outcome.Status == 0 || calls != 1 {
t.Fatalf("outcome=%+v report=%+v error=%v calls=%d", outcome, report, err, calls)
}
}