okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
61c43ca
main
1// Controlled page boundaries catch ordering and turn selection bugs without 2// requiring hundreds of provider turns to cross the daemon's page limit. 3package app 4 5import ( 6"context" 7"encoding/json" 8"net" 9"net/http" 10"net/http/httptest" 11"reflect" 12"testing" 13 14"albedo/cli/internal/daemon" 15"albedo/cli/internal/testwire" 16) 17 18func TestReadFlattensHistoryPagesInChronologicalOrder (t * testing.T ) { 19var cursors []string 20server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 21switch r .URL .Path { 22case "/sessions" : 23_ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []any {testwire .Session ("s" ,testwire .GenerationA ,0 )},"next" :nil }) 24case "/sessions/s" : 25session := testwire .Session ("s" ,testwire .GenerationA ,0 ) 26session ["status" ].(map [string ]any )["phase" ]= "preparing" 27_ = json .NewEncoder (w ).Encode (session ) 28case "/sessions/s/history" : 29cursor := r .URL .Query ().Get ("before" ) 30cursors = append (cursors ,cursor ) 31position := int64 (5 ) 32text := "third" 33var older any = "older" 34switch cursor { 35case "5" : 36position ,text = 3 ,"second" 37case "3" : 38position ,text ,older = 1 ,"first" ,nil 39 } 40makeEntry := func (id ,kind ,text string ,position int64 )map [string ]any { 41return map [string ]any {"id" :id ,"position" :position ,"kind" :kind ,"created_at" :nil ,"input_id" :nil ,"turn_id" :nil ,"checkpoint_id" :nil ,"content_complete" :true ,"content" : []any {map [string ]any {"kind" :"text" ,"text" :text }},"tool" :nil } 42 } 43_ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []any {makeEntry (text ,"user" ,text ,position ),makeEntry (text + "-reply" ,"assistant" ,text + " reply" ,position + 1 )},"older" :older ,"newer" :nil ,"high_water" :6 }) 44default : 45t .Errorf ("unexpected request %s" ,r .URL .Path ) 46w .WriteHeader (404 ) 47 } 48 })) 49defer server .Close () 50conn := daemon .NewConnection (daemon.ConnectionSnapshot {Port :server .Listener .Addr ().(* net.TCPAddr ).Port },nil ) 51service := Service {Connect :func (context.Context ) (* daemon.Connection ,error ) {return conn ,nil }} 52for _ ,turns := range []int {3 ,2 } { 53cursors = nil 54result ,err := service .Read (context .Background (),"s" ,turns ) 55if err != nil { 56t .Fatal (err ) 57 } 58var texts []string 59for _ ,event := range result .Events { 60if event .Type != daemon .EventCommitted { 61texts = append (texts ,event .Text ) 62 } 63if ! event .Replayed { 64t .Fatal ("history event not marked replayed" ) 65 } 66 } 67wanted := []string {"first" ,"first reply" ,"second" ,"second reply" ,"third" ,"third reply" } 68if turns == 2 { 69wanted = wanted [2 :] 70 } 71if ! reflect .DeepEqual (texts ,wanted )|| ! result .Running { 72data ,_ := json .Marshal (result ) 73t .Fatalf ("history changed order or running state: %s" ,data ) 74 } 75expectedCursors := []string {"" ,"5" ,"3" } 76if turns == 2 { 77expectedCursors = expectedCursors [:2 ] 78 } 79if ! reflect .DeepEqual (cursors ,expectedCursors ) { 80t .Fatalf ("read fetched wrong pages: %v" ,cursors ) 81 } 82 } 83}