86 lines
3.7 KiB
Go
86 lines
3.7 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/defaults"
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestInspectProfileAllowsIncompleteResolvedTarget(t *testing.T) {
|
|
profile := &domain.ExecutionProfile{ID: "partial", ResolvedTarget: &domain.ExecutionTarget{
|
|
TimeoutSeconds: defaults.ExecutionDefaultTimeoutSeconds,
|
|
}}
|
|
runner := &Runner{profiles: staticExecutionProfileRepo{profile: profile}}
|
|
inspection, err := runner.InspectProfile(context.Background(), "partial")
|
|
if err != nil {
|
|
t.Fatalf("partial profile inspection failed: %v", err)
|
|
}
|
|
if inspection.EffectiveModelParams.Endpoint != "" || inspection.EffectiveModelParams.Model != "" {
|
|
t.Fatalf("inspection invented a runnable target: %+v", inspection)
|
|
}
|
|
}
|
|
|
|
func TestPreparationCompletesResolvedTargetThroughRequestOverrides(t *testing.T) {
|
|
profile := &domain.ExecutionProfile{ID: "partial", ResolvedTarget: &domain.ExecutionTarget{
|
|
TimeoutSeconds: defaults.ExecutionDefaultTimeoutSeconds,
|
|
Temperature: 0, ExtraParams: map[string]any{"configured": true},
|
|
}, TargetPresence: domain.ExecutionTargetPresence{Temperature: true}}
|
|
runner := &Runner{
|
|
promptDefs: staticPromptRepo{def: &domain.PromptDefinition{ID: "prompt", Version: stringPtr("v1"), Validation: domain.OutputContract{
|
|
Format: domain.FormatText, ValidationMode: domain.ValidationNone,
|
|
}}},
|
|
profiles: staticExecutionProfileRepo{profile: profile}, renderer: staticRenderer{},
|
|
}
|
|
if _, err := runner.Prepare(context.Background(), domain.RunRequest{PromptID: "prompt", ProfileID: "partial"}); err == nil {
|
|
t.Fatal("incomplete target was accepted without overrides")
|
|
}
|
|
zero := 0.0
|
|
prepared, err := runner.Prepare(context.Background(), domain.RunRequest{
|
|
PromptID: "prompt", ProfileID: "partial",
|
|
Execution: &domain.ExecutionTargetOverride{
|
|
Endpoint: "https://runtime.example/v1", Model: "runtime-model", Temperature: &zero,
|
|
ExtraParams: map[string]any{"request": true},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("request did not complete partial target: %v", err)
|
|
}
|
|
if prepared.EffectiveModelParams.Endpoint != "https://runtime.example/v1" ||
|
|
prepared.EffectiveModelParams.Model != "runtime-model" || !prepared.TargetPresence.Temperature ||
|
|
prepared.EffectiveModelParams.ExtraParams["request"] != true {
|
|
t.Fatalf("request overrides were not applied: %+v", prepared)
|
|
}
|
|
if _, exists := prepared.EffectiveModelParams.ExtraParams["configured"]; exists {
|
|
t.Fatal("nonempty request provider map did not replace configured map")
|
|
}
|
|
}
|
|
|
|
func TestResolvedTargetCredentialAvailability(t *testing.T) {
|
|
profile := &domain.ExecutionProfile{ID: "direct", ResolvedTarget: &domain.ExecutionTarget{
|
|
Endpoint: "https://provider.example/v1", Model: "model", APIKeyRequired: true,
|
|
}}
|
|
runner := &Runner{
|
|
promptDefs: staticPromptRepo{def: &domain.PromptDefinition{ID: "prompt", Version: stringPtr("v1"), Validation: domain.OutputContract{
|
|
Format: domain.FormatText, ValidationMode: domain.ValidationNone,
|
|
}}},
|
|
profiles: staticExecutionProfileRepo{profile: profile}, renderer: staticRenderer{},
|
|
}
|
|
request := domain.RunRequest{PromptID: "prompt", ProfileID: "direct"}
|
|
if _, err := runner.Prepare(context.Background(), request); !errors.Is(err, ErrAPIKeyRequired) {
|
|
t.Fatalf("missing required direct key: %v", err)
|
|
}
|
|
request.APIKey = "direct-secret"
|
|
if _, err := runner.Prepare(context.Background(), request); err != nil {
|
|
t.Fatalf("direct key should satisfy requirement: %v", err)
|
|
}
|
|
request.APIKey = ""
|
|
t.Setenv("REQUEST_KEY", "runtime-secret")
|
|
request.Execution = &domain.ExecutionTargetOverride{APIKeyEnv: "REQUEST_KEY"}
|
|
if _, err := runner.Prepare(context.Background(), request); err != nil {
|
|
t.Fatalf("request environment override should satisfy requirement: %v", err)
|
|
}
|
|
}
|