Add Promptkit definition conformance coverage
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -30,3 +31,94 @@ func TestFormatPromptInspectionRejectsNil(t *testing.T) {
|
||||
t.Fatal("expected nil inspection error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatProfileInspectionPreservesSafeEffectiveValues(t *testing.T) {
|
||||
const secret = "sentinel-secret-must-not-appear"
|
||||
t.Setenv("FIXTURE_PROFILE_API_KEY", secret)
|
||||
|
||||
value := &promptkit.ProfileInspection{
|
||||
ProfileID: "custom-derived",
|
||||
EffectiveModelParams: promptkit.ExecutionTarget{
|
||||
BackendID: "fixture-custom",
|
||||
Endpoint: "http://127.0.0.1:11434/v1",
|
||||
Model: "fixture-model",
|
||||
Temperature: 0.25,
|
||||
MaxTokens: 640,
|
||||
TopP: 0.9,
|
||||
TimeoutSeconds: 45,
|
||||
ServiceTier: "flex",
|
||||
ReasoningEffort: "high",
|
||||
APIKeyEnv: "FIXTURE_PROFILE_API_KEY",
|
||||
ExtraParams: map[string]any{
|
||||
"zeta": true,
|
||||
"alpha": map[string]any{"nested": []any{"first", 2.0}},
|
||||
},
|
||||
},
|
||||
APIKeyRequired: true,
|
||||
}
|
||||
|
||||
jsonOutput, err := FormatProfileInspection(value, OutputFormatJSON)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondJSONOutput, err := FormatProfileInspection(value, OutputFormatJSON)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(jsonOutput) != string(secondJSONOutput) || !strings.HasSuffix(string(jsonOutput), "\n") {
|
||||
t.Fatalf("expected deterministic newline-terminated JSON, got %q", jsonOutput)
|
||||
}
|
||||
if strings.Contains(string(jsonOutput), secret) {
|
||||
t.Fatalf("inspection exposed an environment secret: %s", jsonOutput)
|
||||
}
|
||||
|
||||
var decoded ProfileInspection
|
||||
if err := json.Unmarshal(jsonOutput, &decoded); err != nil {
|
||||
t.Fatalf("decode profile inspection: %v", err)
|
||||
}
|
||||
if decoded.ProfileID != "custom-derived" || decoded.EffectiveModelParams.BackendID != "fixture-custom" {
|
||||
t.Fatalf("unexpected profile identity: %+v", decoded)
|
||||
}
|
||||
if decoded.EffectiveModelParams.APIKeyEnv != "FIXTURE_PROFILE_API_KEY" || !decoded.APIKeyRequired {
|
||||
t.Fatalf("unexpected credential metadata: %+v", decoded)
|
||||
}
|
||||
alpha, ok := decoded.EffectiveModelParams.ExtraParams["alpha"].(map[string]any)
|
||||
if !ok || len(alpha["nested"].([]any)) != 2 {
|
||||
t.Fatalf("nested extra parameters were not preserved: %#v", decoded.EffectiveModelParams.ExtraParams)
|
||||
}
|
||||
|
||||
textOutput, err := FormatProfileInspection(value, OutputFormatText)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
textValue := string(textOutput)
|
||||
if !strings.Contains(textValue, "backend_id: fixture-custom") ||
|
||||
!strings.Contains(textValue, "api_key_env: FIXTURE_PROFILE_API_KEY") ||
|
||||
!strings.Contains(textValue, `extra_params: {"alpha":{"nested":["first",2]},"zeta":true}`) ||
|
||||
!strings.Contains(textValue, "api_key_required: true") {
|
||||
t.Fatalf("unexpected text inspection: %s", textValue)
|
||||
}
|
||||
if strings.Contains(textValue, secret) {
|
||||
t.Fatalf("text inspection exposed an environment secret: %s", textValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatProfileInspectionPreservesEmptyBackendAndRejectsNil(t *testing.T) {
|
||||
output, err := FormatProfileInspection(&promptkit.ProfileInspection{
|
||||
ProfileID: "endpoint-only",
|
||||
EffectiveModelParams: promptkit.ExecutionTarget{
|
||||
Endpoint: "http://127.0.0.1:8000/v1",
|
||||
Model: "fixture-model",
|
||||
},
|
||||
}, OutputFormatJSON)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(output), `"backend_id": ""`) ||
|
||||
!strings.Contains(string(output), `"extra_params": {}`) {
|
||||
t.Fatalf("expected explicit empty backend and object extra params, got %s", output)
|
||||
}
|
||||
if _, err := FormatProfileInspection(nil, OutputFormatText); err == nil {
|
||||
t.Fatal("expected nil profile inspection error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user