|
|
|
|
@@ -8,6 +8,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
|
|
|
|
@@ -142,8 +143,8 @@ func TestRunDistinguishesCompletedInvalidContentAndOperationalFailure(t *testing
|
|
|
|
|
}))
|
|
|
|
|
defer server.Close()
|
|
|
|
|
nativePromptFixture(t, server.URL+"/v1")
|
|
|
|
|
writeNativeFile(t, "invalid-content.yml", "profiles:\n - id: local\n endpoint: "+server.URL+"/v1\n model: controlled\nprompts:\n - id: json\n default_profile: local\n messages:\n - role: user\n content: Return JSON\n output:\n format: json\n validation_mode: json\n")
|
|
|
|
|
writeNativeFile(t, "json-app.yml", "promptkit:\n configuration: invalid-content.yml\ntransient_retries: 0\n")
|
|
|
|
|
writeNativeFile(t, "invalid-content.yml", "profiles:\n - id: local\n endpoint: "+server.URL+"/v1\n model: controlled\nprompts:\n - id: json\n default_profile: local\n messages:\n - role: user\n content: Return JSON\n output:\n format: json\n validation_mode: json\n repair_attempts: 0\n")
|
|
|
|
|
writeNativeFile(t, "json-app.yml", "config: {imports: [{path: invalid-content.yml, destination: [promptkit]}]}\ntransient_retries: 0\n")
|
|
|
|
|
args := []string{"run", "json", "--config=json-app.yml"}
|
|
|
|
|
outcome, output, diagnostics := executeNative(t, args...)
|
|
|
|
|
if outcome.Status != 4 || outcome.Kind != commandkit.OutcomeDomain || outcome.Domain != "validation_failed" || output != "not-json" || diagnostics == "" {
|
|
|
|
|
@@ -165,6 +166,115 @@ func TestRunDistinguishesCompletedInvalidContentAndOperationalFailure(t *testing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNativeUsageReporting(t *testing.T) {
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
fmt.Fprint(w, `{"usage":{"prompt_tokens":100,"completion_tokens":20,"total_tokens":120,"prompt_tokens_details":{"cached_tokens":40,"cache_write_tokens":5},"completion_tokens_details":{"reasoning_tokens":3},"cost":0.02},"choices":[{"message":{"content":"artifact"}}]}`)
|
|
|
|
|
}))
|
|
|
|
|
defer server.Close()
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n backends:\n - {id: controlled, provider: openrouter, endpoint: %q}\n profiles:\n - {id: local, backend: controlled, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output: {format: text, validation_mode: none}\n", server.URL+"/v1"))
|
|
|
|
|
base := []string{"run", "selected", "--config=app.yml"}
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
flags []string
|
|
|
|
|
visible bool
|
|
|
|
|
quiet bool
|
|
|
|
|
}{
|
|
|
|
|
{nil, false, false}, {[]string{"--show-tokens=false"}, false, false}, {[]string{"--show-tokens"}, true, false},
|
|
|
|
|
{[]string{"--show-tokens", "--show-tokens=false"}, false, false}, {[]string{"--show-tokens", "--quiet"}, false, true},
|
|
|
|
|
} {
|
|
|
|
|
outcome, output, diagnostic := executeNative(t, append(append([]string{}, base...), test.flags...)...)
|
|
|
|
|
if outcome.Status != 0 || (!test.quiet && output != "artifact") || (test.quiet && output != "") || strings.Contains(diagnostic, "120") != test.visible {
|
|
|
|
|
t.Fatalf("%v: %+v %q %q", test.flags, outcome, output, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
if test.visible && (!strings.Contains(diagnostic, "3") || !strings.Contains(diagnostic, "40") || !strings.Contains(diagnostic, "0.02")) {
|
|
|
|
|
t.Fatalf("usage detail: %q", diagnostic)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
outcome, output, diagnostic := executeNative(t, append(append([]string{}, base...), "--show-tokens", "--format=json", "--report=report.json", "--report-format=json")...)
|
|
|
|
|
report, err := os.ReadFile("report.json")
|
|
|
|
|
if err != nil || outcome.Status != 0 || !strings.Contains(output, "reasoning_tokens") || !strings.Contains(string(report), "cost_usd") || !strings.Contains(diagnostic, "120") {
|
|
|
|
|
t.Fatalf("structured: %+v %q %q %q %v", outcome, output, diagnostic, report, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNativeRepairBudgetPresence(t *testing.T) {
|
|
|
|
|
var calls atomic.Int64
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
content := "invalid"
|
|
|
|
|
if calls.Add(1) > 1 {
|
|
|
|
|
content = `{"ok":true}`
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"choices": []any{map[string]any{"message": map[string]any{"content": content}}}})
|
|
|
|
|
}))
|
|
|
|
|
defer server.Close()
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
name, budget string
|
|
|
|
|
status int
|
|
|
|
|
attempts int64
|
|
|
|
|
}{{"default", "", 0, 2}, {"zero", " repair_attempts: 0\n", 4, 1}} {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
calls.Store(0)
|
|
|
|
|
writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n profiles:\n - {id: local, endpoint: %q, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output:\n format: json\n validation_mode: json\n%s", server.URL+"/v1", strings.ReplaceAll(test.budget, " repair", " repair")))
|
|
|
|
|
outcome, _, diagnostic := executeNative(t, "run", "selected", "--config=app.yml", "--format=json")
|
|
|
|
|
if outcome.Status != test.status || calls.Load() != test.attempts {
|
|
|
|
|
t.Fatalf("%s: %+v calls=%d %q", test.name, outcome, calls.Load(), diagnostic)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNativeProviderMetadataInspection(t *testing.T) {
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path != "/v1/models" {
|
|
|
|
|
t.Errorf("unexpected diagnostic endpoint: %s", r.URL.Path)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprint(w, `{"object":"list","data":[]}`)
|
|
|
|
|
}))
|
|
|
|
|
defer server.Close()
|
|
|
|
|
writeNativeFile(t, "app.yml", fmt.Sprintf("promptkit:\n backends:\n - {id: local, provider: vllm, endpoint: %q, zero_cost: true}\n", server.URL+"/v1"))
|
|
|
|
|
outcome, output, diagnostic := executeNative(t, "promptkit", "show", "backend", "local", "--config=app.yml", "--format=json")
|
|
|
|
|
if outcome.Status != 0 || !strings.Contains(output, "vllm") || !strings.Contains(output, "zero_cost") {
|
|
|
|
|
t.Fatalf("provider: %+v %q %q", outcome, output, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
outcome, output, diagnostic = executeNative(t, "promptkit", "test", "authentication", "--backend=local", "--config=app.yml", "--format=json")
|
|
|
|
|
if outcome.Status != 0 || !strings.Contains(output, `"credentials_accepted": true`) {
|
|
|
|
|
t.Fatalf("authentication: %+v %q %q", outcome, output, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNativeUsageSurvivesUnsuccessfulGeneration(t *testing.T) {
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
fmt.Fprint(w, `{"usage":{"prompt_tokens":30,"completion_tokens":6,"total_tokens":36,"cost":0.01},"choices":[]}`)
|
|
|
|
|
}))
|
|
|
|
|
defer server.Close()
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
writeNativeFile(t, "app.yml", fmt.Sprintf("transient_retries: 0\npromptkit:\n backends:\n - {id: local, provider: openrouter, endpoint: %q}\n profiles:\n - {id: local, backend: local, model: model}\n prompts:\n - id: selected\n default_profile: local\n messages: [{role: user, content: test}]\n output: {format: text, validation_mode: none}\n", server.URL+"/v1"))
|
|
|
|
|
outcome, output, diagnostic := executeNative(t, "run", "selected", "--config=app.yml", "--show-tokens")
|
|
|
|
|
if outcome.Status != 1 || output != "" || !strings.Contains(diagnostic, "0.01") {
|
|
|
|
|
t.Fatalf("failure usage: %+v %q %q", outcome, output, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNativeUsageWithoutExecution(t *testing.T) {
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
writeNativeFile(t, "app.yml", "{}\n")
|
|
|
|
|
outcome, _, diagnostic := executeNative(t, "config", "validate", "--config=app.yml", "--show-tokens")
|
|
|
|
|
if outcome.Status != 0 || diagnostic == "" {
|
|
|
|
|
t.Fatalf("zero-execution usage: %+v %q", outcome, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
for _, args := range [][]string{{"run", "--help", "--show-tokens"}, {"config", "explain", "--show-tokens"}} {
|
|
|
|
|
outcome, _, diagnostic := executeNative(t, args...)
|
|
|
|
|
if outcome.Status != 0 || diagnostic != "" {
|
|
|
|
|
t.Fatalf("help: %+v %q", outcome, diagnostic)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRunUsageAndConfigurationFailures(t *testing.T) {
|
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
|
for _, args := range [][]string{
|
|
|
|
|
|