okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
8af1784
main
1package app 2 3import ( 4"context" 5"errors" 6"math" 7"strings" 8"testing" 9"time" 10) 11 12func TestAdminCostsPerRequest (t * testing.T ) { 13s ,_ := fixtureService (t ) 14s .cfg .JevPricePerMillion = 0.042 15ctx := context .WithValue (context .Background (),contextKey {},callContext {ID :"costly" ,Caller :"test" }) 16s .log .request (ctx ,"search" ,map [string ]string {"query" :"q" },"ok" ) 17s .log .decision (ctx ,"rerank" ,"jev" , []byte (`{}` ), []byte (`{"answers":{},"usage":{"input_tokens":1000000,"output_tokens":20}}` ),nil ,time .Millisecond ) 18// the local model reports no usage: priced from the request size (400 bytes, about 100 tokens) 19s .log .decision (ctx ,"passage" ,"local" , []byte (strings .Repeat ("x" ,400 )), []byte (`{"answers":{}}` ),nil ,time .Millisecond ) 20s .log .decision (ctx ,"passage" ,"jev" , []byte (`{}` ),nil ,errors .New ("refused" ),time .Millisecond ) 21free := context .WithValue (context .Background (),contextKey {},callContext {ID :"free" ,Caller :"test" }) 22s .log .request (free ,"contents" ,map [string ]string {"urls" :"u" },"ok" ) 23 24page ,err := s .adminRequests (context .Background (),0 ,"" ,false ) 25if err != nil { 26t .Fatal (err ) 27 } 28got := map [string ]map [string ]any {} 29for _ ,row := range page .Rows { 30got [row ["requestId" ].(string )]= row 31 } 32c := got ["costly" ] 33if c ["decisionCalls" ]!= int64 (2 )|| c ["inputTokens" ]!= int64 (1000100 )|| math .Abs (c ["jevUsd" ].(float64 )- 0.0420042 )> 1e-9 { 34t .Fatalf ("costly request: %v" ,c ) 35 } 36if f := got ["free" ];f ["decisionCalls" ]!= int64 (0 )|| f ["jevUsd" ].(float64 )!= 0 { 37t .Fatalf ("request without decisions: %v" ,f ) 38 } 39o ,err := s .adminOverview (context .Background ()) 40if err != nil { 41t .Fatal (err ) 42 } 43d := o .(map [string ]any )["decisions" ].(map [string ]any ) 44if d ["requests" ]!= int64 (2 )|| d ["calls" ]!= int64 (2 )|| d ["estimatedCalls" ]!= int64 (1 ) { 45t .Fatalf ("overview decisions: %v" ,d ) 46 } 47}