okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
70a1e29
main
1package app 2 3import ( 4"encoding/json" 5"net/http" 6"net/http/httptest" 7"path/filepath" 8"sync" 9"testing" 10) 11 12// decisionServer answers every noul question with the probability given for 13// its id (default 0.5) and records the question ids it was asked. 14func decisionServer (t * testing.T ,scores map [string ]float64 ,status int ) (* httptest.Server ,* [][]string ) { 15t .Helper () 16var mu sync.Mutex 17var asked [][]string 18srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 19var req struct { 20Questions map [string ]Question `json:"questions"` 21 } 22_ = json .NewDecoder (r .Body ).Decode (& req ) 23ids := []string {} 24answers := map [string ]any {} 25for id := range req .Questions { 26ids = append (ids ,id ) 27p ,ok := scores [id ] 28if ! ok { 29p = 0.5 30 } 31answers [id ]= map [string ]any {"type" :"noul" ,"noul" :p } 32 } 33mu .Lock () 34asked = append (asked ,ids ) 35mu .Unlock () 36if status != 200 { 37w .WriteHeader (status ) 38return 39 } 40writeJSON (w ,200 ,map [string ]any {"model" :"m" ,"answers" :answers }) 41 })) 42t .Cleanup (srv .Close ) 43return srv ,& asked 44} 45 46func TestLocalTierKeepsConfidentAnswersAndForwardsTheRest (t * testing.T ) { 47log ,err := openLog (filepath .Join (t .TempDir (),"log.db" )) 48if err != nil { 49t .Fatal (err ) 50 } 51t .Cleanup (func () {log .db .Close () }) 52local ,_ := decisionServer (t ,map [string ]float64 {"0_relevant" :0.02 ,"0_evidence" :0.97 ,"1_relevant" :0.5 },200 ) 53remote ,asked := decisionServer (t ,map [string ]float64 {"1_relevant" :0.8 ,"0_injection" :0.01 },200 ) 54mk := func (url string )* Jev { 55return & Jev {URL :url ,Key :"k" ,Model :"m" ,HTTP :http .DefaultClient ,Log :log ,slots :make (chan struct {},2 )} 56 } 57j := mk (remote .URL ) 58j .Local = & LocalTier {Jev :mk (local .URL ),Names :map [string ]bool {"relevant" :true ,"evidence" :true },Low :0.1 ,High :0.9 } 59qs := map [string ]Question {"0_relevant" :noul ("r" ),"0_evidence" :noul ("e" ),"1_relevant" :noul ("r" ),"0_injection" :noul ("i" )} 60out ,err := j .evaluate (t .Context (),"passage" ,map [string ]string {"query" :"q" },qs ) 61if err != nil { 62t .Fatal (err ) 63 } 64if len (* asked )!= 1 || len ((* asked )[0 ])!= 2 { 65t .Fatalf ("remote should get only the unsure and non-local questions, got %v" ,* asked ) 66 } 67want := map [string ]float64 {"0_relevant" :0.02 ,"0_evidence" :0.97 ,"1_relevant" :0.8 ,"0_injection" :0.01 } 68for id ,p := range want { 69if out .Answers [id ].Noul != p { 70t .Fatalf ("%s: got %v want %v" ,id ,out .Answers [id ].Noul ,p ) 71 } 72 } 73 74broken ,_ := decisionServer (t ,nil ,500 ) 75j .Local .Jev = mk (broken .URL ) 76* asked = nil 77if _ ,err := j .evaluate (t .Context (),"passage" ,map [string ]string {"query" :"q" },qs );err != nil || len (* asked )!= 1 || len ((* asked )[0 ])!= 4 { 78t .Fatalf ("a failing local model must send everything remote: %v %v" ,err ,* asked ) 79 } 80}