96 lines
3.8 KiB
Go
96 lines
3.8 KiB
Go
package format
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func ptr[T any](value T) *T { return &value }
|
|
|
|
func TestPromptInspectionOptionalFieldsAndDisclosure(t *testing.T) {
|
|
inputs := []promptkit.PromptInputInspection{{Name: "first", Required: ptr(false), ContentType: ptr(""), Description: ptr("")}, {Name: "second"}}
|
|
value := &promptkit.PromptInspection{PromptID: "support/p", PromptHash: "hash", DefaultProfileID: ptr(""), Inputs: &inputs,
|
|
Messages: []promptkit.PromptMessageInspection{{Role: "user", Content: "private message"}}, Description: ptr("private description"), SessionID: ptr("private session"),
|
|
OutputContract: promptkit.PromptOutputInspection{Format: "text", ValidationMode: promptkit.ValidationNone, SchemaPath: ptr(""), RepairAttempts: ptr(0)}}
|
|
for _, format := range []OutputFormat{OutputFormatText, OutputFormatJSON} {
|
|
output, err := FormatPromptInspection(value, format)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := string(output)
|
|
for _, forbidden := range []string{"private message", "private description", "private session", "prompt_version"} {
|
|
if strings.Contains(s, forbidden) {
|
|
t.Fatalf("inspection exposed %q: %s", forbidden, s)
|
|
}
|
|
}
|
|
if strings.Index(s, "first") > strings.Index(s, "second") {
|
|
t.Fatalf("input order changed: %s", s)
|
|
}
|
|
if !strings.Contains(s, "default_profile_id") || !strings.Contains(s, "repair_attempts") || !strings.Contains(s, "required") {
|
|
t.Fatalf("present empty values missing: %s", s)
|
|
}
|
|
}
|
|
value.DefaultProfileID, value.Inputs = nil, nil
|
|
value.OutputContract.SchemaPath, value.OutputContract.RepairAttempts = nil, nil
|
|
for _, format := range []OutputFormat{OutputFormatText, OutputFormatJSON} {
|
|
output, err := FormatPromptInspection(value, format)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, absent := range []string{"default_profile_id", "inputs", "schema_path", "repair_attempts"} {
|
|
if strings.Contains(string(output), absent) {
|
|
t.Fatalf("absent %s rendered: %s", absent, output)
|
|
}
|
|
}
|
|
}
|
|
empty := []promptkit.PromptInputInspection{}
|
|
value.Inputs = &empty
|
|
output, err := FormatPromptInspection(value, OutputFormatJSON)
|
|
if err != nil || !strings.Contains(string(output), `"inputs": []`) {
|
|
t.Fatalf("empty inputs lost: %s %v", output, err)
|
|
}
|
|
if _, err := FormatPromptInspection(nil, OutputFormatText); err == nil {
|
|
t.Fatal("nil prompt accepted")
|
|
}
|
|
}
|
|
|
|
func TestProfileInspectionPreservesOnlyPresentValues(t *testing.T) {
|
|
params := map[string]any{}
|
|
value := &promptkit.ProfileInspection{ProfileID: "support/fast", Backend: ptr("backend"), Model: ptr(""), Temperature: ptr(0.0), MaxTokens: ptr(0), APIKeyRequired: ptr(false), ExtraParams: ¶ms}
|
|
for _, format := range []OutputFormat{OutputFormatText, OutputFormatJSON} {
|
|
output, err := FormatProfileInspection(value, format)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := string(output)
|
|
for _, key := range []string{"profile_model_params", "backend_id", "model", "temperature", "max_tokens", "api_key_required", "extra_params"} {
|
|
if !strings.Contains(s, key) {
|
|
t.Fatalf("present %s missing: %s", key, s)
|
|
}
|
|
}
|
|
for _, key := range []string{"effective_model_params", "endpoint", "api_key_env", "top_p"} {
|
|
if strings.Contains(s, key) {
|
|
t.Fatalf("absent %s appeared: %s", key, s)
|
|
}
|
|
}
|
|
}
|
|
output, err := FormatProfileInspection(value, OutputFormatJSON)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(output, &decoded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := decoded["profile_model_params"].(map[string]any)
|
|
if p["backend_id"] != "backend" || p["model"] != "" || p["temperature"] != float64(0) || decoded["api_key_required"] != false || len(p["extra_params"].(map[string]any)) != 0 {
|
|
t.Fatalf("wrong values: %#v", decoded)
|
|
}
|
|
if _, err := FormatProfileInspection(nil, OutputFormatText); err == nil {
|
|
t.Fatal("nil profile accepted")
|
|
}
|
|
}
|