Strengthen startup and error regression tests

This commit is contained in:
2026-11-25 17:31:38 +00:00
parent 93ab0e5969
commit 989f4edeca
4 changed files with 54 additions and 10 deletions

View File

@@ -182,6 +182,10 @@ lifetime change is required; the current server already shares one engine.
## Stage 5: Correct Startup, Removed-Flag, And Error Test Blind Spots
**Status: complete.** Native engine capture fails before listening with typed
causes, removed flags are identified in otherwise-valid invocations, and HTTP
error tests retain classification and privacy without pinning generic prose.
**Findings:** 11–13.
**Files:** `internal/adapter/cli/lifecycle_test.go`, `app_test.go`, and HTTP
error/timeout/report tests in `handler_test.go`.

View File

@@ -31,13 +31,15 @@ func TestNativeMetadataWithoutConfiguration(t *testing.T) {
}
func TestRemovedSourceAndSchemaFlagsAreRejected(t *testing.T) {
for _, command := range [][]string{{"run"}, {"serve"}, {"config", "show"}, {"promptkit", "list", "prompts"}} {
nativePromptFixture(t, "http://127.0.0.1:1/v1")
for _, command := range [][]string{{"run", "support/summary"}, {"serve"}, {"config", "show"}, {"promptkit", "list", "prompts"}} {
for _, flag := range []string{"--promptkit-source=missing.yml", "--schema-dir=schemas"} {
args := append(append([]string{}, command...), flag)
args := append(append([]string{}, command...), "--config=app.yml", flag)
var output, diagnostics bytes.Buffer
outcome := Execute(context.Background(), commandkit.Identity{Name: "scriptorium"}, args, &output, &diagnostics)
if outcome.Kind != commandkit.OutcomeUsage || outcome.Status != 2 {
t.Fatalf("%v: %+v", args, outcome)
flagName := strings.SplitN(flag, "=", 2)[0]
if outcome.Kind != commandkit.OutcomeUsage || outcome.Status != 2 || !strings.Contains(diagnostics.String(), flagName) {
t.Fatalf("%v: %+v diagnostics=%q", args, outcome, diagnostics.String())
}
}
}

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/http"
"net/http/httptest"
@@ -377,3 +378,32 @@ func TestServeListenAndConfigurationFailures(t *testing.T) {
}
}
}
func TestServeCaptureFailureDoesNotListen(t *testing.T) {
nativePromptFixture(t, "http://127.0.0.1:1/v1")
writeNativeFile(t, "framework.yml", `prompts:
- id: missing
messages:
- role: user
content_file: absent-message.txt
output:
format: text
validation_mode: none
`)
listened := false
listen := func(context.Context, string, string) (net.Listener, error) {
listened = true
return nil, errors.New("unexpected listen")
}
app, err := commandkit.New(commandkit.Identity{Name: "scriptorium"}, commandkit.NewCommand(commandkit.CommandSpec[serveInput]{Name: "serve", Flags: serveFlags(), Run: func(inv commandkit.Invocation, input serveInput) commandkit.Outcome {
return serveWithListener(inv, input, listen)
}}))
if err != nil {
t.Fatal(err)
}
var output, diagnostics bytes.Buffer
outcome := app.Execute(context.Background(), []string{"serve", "--config=app.yml"}, commandkit.Streams{Out: &output, Err: &diagnostics})
if outcome.Status != 1 || !errors.Is(outcome.Cause, promptkit.ErrInvalidConfig) || !errors.Is(outcome.Cause, fs.ErrNotExist) || output.Len() != 0 || listened {
t.Fatalf("capture failure: outcome=%+v stdout=%q diagnostics=%q listened=%t", outcome, output.String(), diagnostics.String(), listened)
}
}

View File

@@ -1706,11 +1706,11 @@ func TestHandlerPublicErrorMapping(t *testing.T) {
{name: "artifact", err: wrap(promptkit.ErrArtifactLoad, fmt.Errorf("read failed")), status: http.StatusBadRequest, code: "artifact_read_failed", message: "failed to read input artifact", avoidCause: "read failed"},
{name: "prompt render", err: wrap(promptkit.ErrPromptRender, fmt.Errorf("render failed")), status: http.StatusBadRequest, code: "prompt_render_failed", message: "failed to render prompt", avoidCause: "render failed"},
{name: "capacity", err: &promptkit.CapacityError{BackendID: "private-backend"}, status: http.StatusServiceUnavailable, code: "capacity_exceeded", message: "model backend capacity is exhausted", avoidCause: "private-backend"},
{name: "llm", err: wrap(promptkit.ErrLLMGenerate, fmt.Errorf("llm failed")), status: http.StatusBadGateway, code: "llm_failed", message: "model generation request failed", avoidCause: "llm failed"},
{name: "transport", err: wrap(promptkit.ErrTransport, fmt.Errorf("private transport detail")), status: http.StatusBadGateway, code: "llm_failed", message: "model generation request failed", avoidCause: "private transport detail"},
{name: "malformed response", err: wrap(promptkit.ErrMalformedResponse, fmt.Errorf("private response detail")), status: http.StatusBadGateway, code: "llm_failed", message: "model generation request failed", avoidCause: "private response detail"},
{name: "retry exhaustion", err: wrap(promptkit.ErrRetriesExhausted, fmt.Errorf("private provider detail")), status: http.StatusBadGateway, code: "llm_failed", message: "model generation request failed", avoidCause: "private provider detail"},
{name: "retry exhaustion value", err: fmt.Errorf("generation: %w", &promptkit.RetryExhaustedError{}), status: http.StatusBadGateway, code: "llm_failed", message: "model generation request failed"},
{name: "llm", err: wrap(promptkit.ErrLLMGenerate, fmt.Errorf("llm failed")), status: http.StatusBadGateway, code: "llm_failed", avoidCause: "llm failed"},
{name: "transport", err: wrap(promptkit.ErrTransport, fmt.Errorf("private transport detail")), status: http.StatusBadGateway, code: "llm_failed", avoidCause: "private transport detail"},
{name: "malformed response", err: wrap(promptkit.ErrMalformedResponse, fmt.Errorf("private response detail")), status: http.StatusBadGateway, code: "llm_failed", avoidCause: "private response detail"},
{name: "retry exhaustion", err: wrap(promptkit.ErrRetriesExhausted, fmt.Errorf("private provider detail")), status: http.StatusBadGateway, code: "llm_failed", avoidCause: "private provider detail"},
{name: "retry exhaustion value", err: fmt.Errorf("generation: %w", &promptkit.RetryExhaustedError{}), status: http.StatusBadGateway, code: "llm_failed"},
{name: "validation runtime", err: wrap(promptkit.ErrValidation, fmt.Errorf("validator broke")), status: http.StatusInternalServerError, code: "validation_runtime_failed", message: "validation runtime failed", avoidCause: "validator broke"},
}
@@ -1733,7 +1733,11 @@ func TestHandlerPublicErrorMapping(t *testing.T) {
if errBody["code"] != tc.code {
t.Fatalf("expected code %q, got %#v", tc.code, errBody["code"])
}
if errBody["message"] != tc.message {
message, ok := errBody["message"].(string)
if !ok || strings.TrimSpace(message) == "" {
t.Fatalf("expected a usable message, got %#v", errBody["message"])
}
if tc.code != "llm_failed" && message != tc.message {
t.Fatalf("expected message %q, got %#v", tc.message, errBody["message"])
}
if tc.avoidCause != "" && strings.Contains(w.Body.String(), tc.avoidCause) {
@@ -1941,4 +1945,8 @@ func assertHTTPErrorCode(t *testing.T, w *httptest.ResponseRecorder, status int,
if errBody["code"] != code {
t.Fatalf("expected code %q, got %#v", code, errBody["code"])
}
message, ok := errBody["message"].(string)
if !ok || strings.TrimSpace(message) == "" {
t.Fatalf("expected a usable error message, got %#v", errBody["message"])
}
}