71 lines
2.8 KiB
Go
71 lines
2.8 KiB
Go
package promptkit_test
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func TestDuplicateContextAndRequestGroupEnrollment(t *testing.T) {
|
|
charge := .5
|
|
engine := costEngine(t, false, nil, promptkit.WithLLMClient(observationClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
return &promptkit.GenerateResponse{Content: "ok", CostUSD: &charge}, nil
|
|
})))
|
|
var group promptkit.ExecutionGroup
|
|
ctx := promptkit.WithExecutionObserver(context.Background(), group.Observe)
|
|
if _, err := engine.Run(ctx, promptkit.RunRequest{PromptID: "prompt", Observer: group.Observe}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := group.Snapshot()
|
|
if len(got.Members) != 1 || got.Attempts != 1 || got.CostUSD.Value != .5 {
|
|
t.Fatalf("summary=%+v", got)
|
|
}
|
|
}
|
|
|
|
func TestCacheHitRatioUsesCompleteConsistentCounts(t *testing.T) {
|
|
known := func(v int) promptkit.ObservedTokenCount {
|
|
return promptkit.ObservedTokenCount{Value: v, Known: true, Complete: true}
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
input, reads promptkit.ObservedTokenCount
|
|
ratio float64
|
|
ok bool
|
|
}{
|
|
{name: "fraction", input: known(8), reads: known(3), ratio: .375, ok: true},
|
|
{name: "zero reads", input: known(8), reads: known(0), ok: true},
|
|
{name: "zero input", input: known(0), reads: known(0)},
|
|
{name: "negative reads", input: known(8), reads: known(-1)},
|
|
{name: "reads exceed input", input: known(8), reads: known(9)},
|
|
{name: "unknown", input: known(8)},
|
|
{name: "incomplete", input: known(8), reads: promptkit.ObservedTokenCount{Value: 3, Known: true}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
usage := promptkit.ObservedTokenUsage{PromptTokens: tc.input, CachedTokens: tc.reads}
|
|
got, ok := usage.CacheHitRatio()
|
|
if ok != tc.ok || math.Abs(got-tc.ratio) > 1e-12 {
|
|
t.Fatalf("ratio=%v available=%t", got, ok)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGroupCacheRatioUsesSummedTokensAndDeduplicatesTerminals(t *testing.T) {
|
|
known := func(v int) promptkit.ObservedTokenCount {
|
|
return promptkit.ObservedTokenCount{Value: v, Known: true, Complete: true}
|
|
}
|
|
var group promptkit.ExecutionGroup
|
|
first := promptkit.ExecutionEvent{RunID: "a", Kind: promptkit.ExecutionFinished, Usage: promptkit.ObservedTokenUsage{PromptTokens: known(10), CachedTokens: known(10), ReasoningTokens: known(1)}}
|
|
second := promptkit.ExecutionEvent{RunID: "b", Kind: promptkit.ExecutionFinished, Usage: promptkit.ObservedTokenUsage{PromptTokens: known(90), CachedTokens: known(0), ReasoningTokens: known(2)}}
|
|
group.Observe(first)
|
|
group.Observe(first)
|
|
group.Observe(second)
|
|
snapshot := group.Snapshot()
|
|
ratio, ok := snapshot.Usage.CacheHitRatio()
|
|
if !ok || math.Abs(ratio-.1) > 1e-12 || snapshot.Usage.ReasoningTokens != known(3) {
|
|
t.Fatalf("summary=%+v ratio=%v available=%t", snapshot, ratio, ok)
|
|
}
|
|
}
|