Harden backend contracts and documentation
This commit is contained in:
@@ -28,39 +28,94 @@ func TestPreparedRunJSONOmitsZeroTimingValues(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBackendIdentityJSONNamesAndOmission(t *testing.T) {
|
||||
values := []struct {
|
||||
name string
|
||||
value any
|
||||
field string
|
||||
}{
|
||||
{name: "execution target", value: promptkit.ExecutionTarget{BackendID: promptkit.BackendOpenRouter}, field: "backend_id"},
|
||||
{name: "prepared run", value: promptkit.PreparedRun{SelectedBackendID: promptkit.BackendOpenRouter}, field: "selected_backend_id"},
|
||||
{name: "run result", value: promptkit.RunResult{SelectedBackendID: promptkit.BackendOpenRouter}, field: "selected_backend_id"},
|
||||
}
|
||||
for _, tt := range values {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
payload, err := json.Marshal(tt.value)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal populated value: %v", err)
|
||||
}
|
||||
var object map[string]any
|
||||
if err := json.Unmarshal(payload, &object); err != nil {
|
||||
t.Fatalf("decode populated value: %v", err)
|
||||
}
|
||||
if object[tt.field] != promptkit.BackendOpenRouter {
|
||||
t.Fatalf("expected %s=%q, got %s", tt.field, promptkit.BackendOpenRouter, payload)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
emptyValues := []any{promptkit.ExecutionTarget{}, promptkit.PreparedRun{}, promptkit.RunResult{}}
|
||||
for _, value := range emptyValues {
|
||||
t.Run("execution target round trip", func(t *testing.T) {
|
||||
value := promptkit.ExecutionTarget{BackendID: promptkit.BackendOpenRouter}
|
||||
payload, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal empty value: %v", err)
|
||||
t.Fatalf("marshal execution target: %v", err)
|
||||
}
|
||||
var decoded promptkit.ExecutionTarget
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal execution target: %v", err)
|
||||
}
|
||||
if decoded.BackendID != value.BackendID {
|
||||
t.Fatalf("backend identity did not round trip: got %q want %q", decoded.BackendID, value.BackendID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("prepared run round trip", func(t *testing.T) {
|
||||
value := promptkit.PreparedRun{SelectedBackendID: promptkit.BackendOpenRouter}
|
||||
payload, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal prepared run: %v", err)
|
||||
}
|
||||
var decoded promptkit.PreparedRun
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal prepared run: %v", err)
|
||||
}
|
||||
if decoded.SelectedBackendID != value.SelectedBackendID {
|
||||
t.Fatalf("backend identity did not round trip: got %q want %q", decoded.SelectedBackendID, value.SelectedBackendID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("run result round trip", func(t *testing.T) {
|
||||
value := promptkit.RunResult{SelectedBackendID: promptkit.BackendOpenRouter}
|
||||
payload, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal run result: %v", err)
|
||||
}
|
||||
var decoded promptkit.RunResult
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal run result: %v", err)
|
||||
}
|
||||
if decoded.SelectedBackendID != value.SelectedBackendID {
|
||||
t.Fatalf("backend identity did not round trip: got %q want %q", decoded.SelectedBackendID, value.SelectedBackendID)
|
||||
}
|
||||
})
|
||||
|
||||
payload, err := json.Marshal(promptkit.ExecutionTarget{})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal empty execution target: %v", err)
|
||||
}
|
||||
if strings.Contains(string(payload), `"backend_id"`) {
|
||||
t.Fatalf("empty backend identity was not omitted: %s", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointOnlyProfileOmitsBackendIdentityFromStableJSON(t *testing.T) {
|
||||
client := &fakeLLMClient{response: &promptkit.GenerateResponse{Content: "ok"}}
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{},
|
||||
promptkit.WithPromptFS(contractPromptFS("prompt", "profile", "message"), "."),
|
||||
promptkit.WithProfiles(promptkit.Profile{
|
||||
ID: "profile", Endpoint: "http://example.test/v1", Model: "model",
|
||||
}),
|
||||
promptkit.WithLLMClient(client),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("construct engine: %v", err)
|
||||
}
|
||||
|
||||
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "prompt"})
|
||||
if err != nil {
|
||||
t.Fatalf("prepare endpoint-only profile: %v", err)
|
||||
}
|
||||
result, err := engine.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt"})
|
||||
if err != nil {
|
||||
t.Fatalf("run endpoint-only profile: %v", err)
|
||||
}
|
||||
if prepared.SelectedBackendID != "" ||
|
||||
prepared.EffectiveModelParams.BackendID != "" ||
|
||||
result.SelectedBackendID != "" ||
|
||||
result.EffectiveModelParams.BackendID != "" {
|
||||
t.Fatalf("endpoint-only profile acquired backend identity: prepared=%+v result=%+v", prepared, result)
|
||||
}
|
||||
for _, value := range []any{prepared, result} {
|
||||
payload, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal endpoint-only value: %v", err)
|
||||
}
|
||||
if strings.Contains(string(payload), `"backend_id"`) || strings.Contains(string(payload), `"selected_backend_id"`) {
|
||||
t.Fatalf("empty backend identity was not omitted: %s", payload)
|
||||
t.Fatalf("endpoint-only backend identity was not omitted: %s", payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -537,10 +592,14 @@ func TestRepeatedOptionsUseLastValueInEachCategory(t *testing.T) {
|
||||
func TestEngineSupportsConcurrentPrepareAndRun(t *testing.T) {
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{},
|
||||
promptkit.WithPromptFS(contractPromptFS("prompt", "profile", "message"), "."),
|
||||
promptkit.WithProfiles(promptkit.Profile{
|
||||
ID: "profile",
|
||||
promptkit.WithBackend(promptkit.Backend{
|
||||
ID: "concurrent",
|
||||
Endpoint: "http://example.test/v1",
|
||||
Model: "model",
|
||||
}),
|
||||
promptkit.WithProfiles(promptkit.Profile{
|
||||
ID: "profile",
|
||||
BackendID: "concurrent",
|
||||
Model: "model",
|
||||
}),
|
||||
promptkit.WithLLMClient(countingLLMClient{}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user