Define observed cost values and aggregation
This commit is contained in:
@@ -49,6 +49,34 @@ func TestGenerateResponseUsagePresenceJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCostJSONPreservesUnknownAndReportedZero(t *testing.T) {
|
||||
var old promptkit.RunResult
|
||||
if err := json.Unmarshal([]byte(`{"prompt_id":"old"}`), &old); err != nil || old.CostUSD != (promptkit.ObservedCost{}) {
|
||||
t.Fatalf("old result=%+v err=%v", old, err)
|
||||
}
|
||||
value := promptkit.RunResult{CostUSD: promptkit.ObservedCost{Value: .125, Known: true, Complete: false, Source: promptkit.CostSourceProvider}}
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var decoded promptkit.RunResult
|
||||
if err := json.Unmarshal(data, &decoded); err != nil || decoded.CostUSD != value.CostUSD {
|
||||
t.Fatalf("round trip=%+v err=%v", decoded, err)
|
||||
}
|
||||
zero := 0.0
|
||||
data, err = json.Marshal(promptkit.GenerateResponse{CostUSD: &zero})
|
||||
if err != nil || string(decodeJSONObject(t, data)["cost_usd"]) != "0" {
|
||||
t.Fatalf("reported zero=%s err=%v", data, err)
|
||||
}
|
||||
data, err = json.Marshal(promptkit.GenerateResponse{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, present := decodeJSONObject(t, data)["cost_usd"]; present {
|
||||
t.Fatalf("unknown cost=%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemovedPromptVersionIsAbsentFromJSON(t *testing.T) {
|
||||
values := map[string]any{
|
||||
"prepared": promptkit.PreparedRun{PromptID: "prompt"},
|
||||
@@ -176,6 +204,7 @@ func TestRunResultJSONContractRoundTripsAllFields(t *testing.T) {
|
||||
"effective_model_params",
|
||||
"input_hashes",
|
||||
"usage",
|
||||
"cost_usd",
|
||||
"start_time",
|
||||
"end_time",
|
||||
"duration_ms",
|
||||
|
||||
Reference in New Issue
Block a user