okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
7fd8cf5
main
1// A controlled peer supplies invalid offsets and false completeness claims. 2// The real daemon cannot deterministically violate its own paging contract. 3package daemon 4 5import ( 6"encoding/json" 7"net/http" 8"strings" 9"testing" 10) 11 12func TestPagedContentValidatesOffsetsAndReconstructsUTF8 (t * testing.T ) { 13for _ ,failure := range []string {"" ,"offset" ,"false completeness" } { 14t .Run (failure ,func (t * testing.T ) { 15imageReads := 0 16image := map [string ]any {"mime_type" :"image/png" ,"width" :1 ,"height" :1 ,"original_bytes" :3 ,"reference" :map [string ]any {"url" :"/sessions/s/history/u/image-0" ,"field" :"image-0" ,"bytes" :3 }} 17conn := controlledConnection (t ,func (w http.ResponseWriter ,r * http.Request ) { 18if r .URL .Path == "/sessions/s/history" { 19entry := func (id ,kind string ,position int ,content ... any )map [string ]any { 20return map [string ]any {"id" :id ,"kind" :kind ,"position" :position ,"created_at" :nil ,"input_id" :nil ,"turn_id" :nil ,"content_complete" :false ,"content" :content ,"tool" :nil ,"checkpoint_id" :nil } 21 } 22partial := entry ("e" ,"assistant" ,2 ,map [string ]any {"kind" :"reference" ,"reference" :map [string ]any {"url" :"/sessions/s/history/e" ,"field" :"text" ,"bytes" :6 }}) 23partial ["content_complete" ]= failure == "false completeness" 24complete := entry ("full" ,"assistant" ,3 ,map [string ]any {"kind" :"text" ,"text" :strings .Repeat ("complete reply " ,128 )}) 25complete ["content_complete" ]= true 26_ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []any { 27entry ("u" ,"user" ,1 ,map [string ]any {"kind" :"text" ,"text" :"inspect this image" },map [string ]any {"kind" :"image" ,"image" :image }), 28partial ,complete , 29 },"older" :nil ,"newer" :nil ,"high_water" :3 }) 30return 31 } 32if strings .HasPrefix (r .URL .Path ,"/sessions/s/history/u" ) { 33imageReads ++ 34return 35 } 36if r .URL .Path != "/sessions/s/history/e" { 37t .Errorf ("wrong content URL %s" ,r .URL ) 38 } 39offset := 0 40text := "雪" 41complete := false 42var next any = "next-a" 43if r .URL .Query ().Get ("next" )!= "" { 44offset = 3 45text = "月" 46complete = true 47next = nil 48if failure == "offset" { 49offset = 1 50 } 51 } 52_ = json .NewEncoder (w ).Encode (map [string ]any {"entry_id" :"e" ,"parts" : []any {map [string ]any {"field" :"text" ,"offset_bytes" :offset ,"encoding" :"utf8" ,"text" :text ,"complete" :complete }},"next" :next }) 53 }) 54page ,err := NewChatClient (conn ,"s" ).History (t .Context (),0 ,10 ) 55if failure != "" { 56if err == nil { 57t .Fatalf ("invalid %s accepted" ,failure ) 58 } 59return 60 } 61if err != nil || len (page .Events )!= 6 || page .Events [0 ].Type != EventUser || page .Events [0 ].Text != "inspect this image" || len (page .Events [0 ].Images )!= 1 || page .Events [0 ].Images [0 ].Bytes != 3 || page .Events [2 ].Type != EventMessage || page .Events [2 ].Text != "雪月" || page .Events [4 ].Type != EventMessage || page .Events [4 ].Text != strings .Repeat ("complete reply " ,128 ) { 62t .Fatalf ("history lost full text or image metadata: %+v, %v" ,page ,err ) 63 } 64if imageReads != 0 { 65t .Fatal ("displaying image metadata fetched the image" ) 66 } 67 }) 68 } 69} 70 71// A malformed acknowledgement must retain the original login intent even when 72// the form is cleared or edited while its retained decision is being queried.