64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package observation
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestResolveCostPrecedence(t *testing.T) {
|
|
zero, negative, invalid := 0.0, -1.5, math.Inf(1)
|
|
for _, tc := range []struct {
|
|
name string
|
|
reported *float64
|
|
fallback bool
|
|
want domain.ObservedCost
|
|
}{
|
|
{"unknown", nil, false, domain.ObservedCost{}},
|
|
{"configured zero", nil, true, domain.ObservedCost{Known: true, Complete: true, Source: domain.CostSourceConfiguredZero}},
|
|
{"reported zero", &zero, true, domain.ObservedCost{Known: true, Complete: true, Source: domain.CostSourceProvider}},
|
|
{"negative", &negative, false, domain.ObservedCost{Value: -1.5, Known: true, Complete: true, Source: domain.CostSourceProvider}},
|
|
{"invalid fallback", &invalid, true, domain.ObservedCost{Known: true, Complete: true, Source: domain.CostSourceConfiguredZero}},
|
|
{"invalid unknown", &invalid, false, domain.ObservedCost{}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := ResolveCost(tc.reported, tc.fallback); got != tc.want {
|
|
t.Fatalf("got %+v want %+v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCostAccumulatorMergesSourcesAndCoverage(t *testing.T) {
|
|
var acc CostAccumulator
|
|
if got := acc.Observed(); got != (domain.ObservedCost{Known: true, Complete: true}) {
|
|
t.Fatalf("empty: %+v", got)
|
|
}
|
|
acc.Add(domain.ObservedCost{})
|
|
if got := acc.Observed(); got != (domain.ObservedCost{}) {
|
|
t.Fatalf("unknown: %+v", got)
|
|
}
|
|
a, b := .1, .2
|
|
acc.Add(ResolveCost(&a, false))
|
|
acc.Add(ResolveCost(&b, false))
|
|
acc.Add(ResolveCost(nil, true))
|
|
got := acc.Observed()
|
|
if math.Abs(got.Value-.3) > 1e-12 || !got.Known || got.Complete || got.Source != domain.CostSourceMixed {
|
|
t.Fatalf("merged: %+v", got)
|
|
}
|
|
var zeros CostAccumulator
|
|
zero := 0.0
|
|
zeros.Add(ResolveCost(&zero, false))
|
|
zeros.Add(ResolveCost(nil, true))
|
|
if got := zeros.Observed(); got != (domain.ObservedCost{Known: true, Complete: true, Source: domain.CostSourceMixed}) {
|
|
t.Fatalf("zeros: %+v", got)
|
|
}
|
|
var aggregate CostAccumulator
|
|
aggregate.Add(domain.ObservedCost{Known: true, Complete: true})
|
|
aggregate.Add(got)
|
|
if aggregate.Observed() != got {
|
|
t.Fatalf("aggregate: %+v", aggregate.Observed())
|
|
}
|
|
}
|