85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
package promptkit
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestSmokeReportConversionOwnsMutableValues(t *testing.T) {
|
|
applicable, reachable := true, true
|
|
status := 200
|
|
wait, duration := 3*time.Millisecond, 5*time.Millisecond
|
|
source := domain.SmokeTestReport{
|
|
Applicable: &applicable,
|
|
Target: domain.SmokeTestTarget{EffectiveSettings: domain.ExecutionTarget{
|
|
APIKey: "synthetic-private-key",
|
|
ExtraParams: map[string]any{"nested": []any{map[string]any{"value": "original"}}},
|
|
}},
|
|
Facts: domain.SmokeTestFacts{EndpointReachable: &reachable},
|
|
Timing: domain.SmokeTestTiming{CapacityWait: &wait},
|
|
Attempts: []domain.SmokeTestAttempt{{
|
|
Endpoint: "https://example.test/chat/completions",
|
|
HTTPStatus: &status,
|
|
Facts: domain.SmokeTestFacts{EndpointReachable: &reachable},
|
|
Timing: domain.SmokeTestTiming{RequestDuration: &duration},
|
|
}},
|
|
}
|
|
converted, err := fromDomainSmokeTestReport(source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other, err := fromDomainSmokeTestReport(source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encoded, err := json.Marshal(converted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(encoded), source.Target.EffectiveSettings.APIKey) {
|
|
t.Fatal("converted report included a credential")
|
|
}
|
|
|
|
*converted.Applicable = false
|
|
*converted.Facts.EndpointReachable = false
|
|
*converted.Timing.CapacityWait = time.Second
|
|
converted.Attempts[0].Endpoint = "changed"
|
|
*converted.Attempts[0].HTTPStatus = 503
|
|
*converted.Attempts[0].Facts.EndpointReachable = false
|
|
*converted.Attempts[0].Timing.RequestDuration = time.Second
|
|
converted.Target.EffectiveSettings.ExtraParams["nested"].([]any)[0].(map[string]any)["value"] = "changed"
|
|
|
|
if !applicable || !reachable || status != 200 || wait != 3*time.Millisecond || duration != 5*time.Millisecond {
|
|
t.Fatal("converted report shared optional scalar pointers with its source")
|
|
}
|
|
if source.Attempts[0].Endpoint != "https://example.test/chat/completions" {
|
|
t.Fatal("converted report shared its attempt slice with its source")
|
|
}
|
|
if got := source.Target.EffectiveSettings.ExtraParams["nested"].([]any)[0].(map[string]any)["value"]; got != "original" {
|
|
t.Fatalf("converted report changed source provider parameters: %v", got)
|
|
}
|
|
later, err := fromDomainSmokeTestReport(source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(other, later) {
|
|
t.Fatal("converted report mutation affected another result")
|
|
}
|
|
}
|
|
|
|
func TestSmokeReportConversionPreservesProviderMapCopyError(t *testing.T) {
|
|
report, err := fromDomainSmokeTestReport(domain.SmokeTestReport{
|
|
Target: domain.SmokeTestTarget{EffectiveSettings: domain.ExecutionTarget{
|
|
ExtraParams: map[string]any{"unsupported": func() {}},
|
|
}},
|
|
})
|
|
if err == nil || report != nil {
|
|
t.Fatalf("invalid provider data produced report=%+v, error=%v", report, err)
|
|
}
|
|
}
|