okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
5a90548
main
1package app 2 3import ( 4"encoding/json" 5"fmt" 6"strings" 7"testing" 8"time" 9"unicode/utf8" 10) 11 12func TestMCPCharacterLimits (t * testing.T ) { 13s ,_ := fixtureService (t ) 14status ,body := callHTTP (t ,s ,"/mcp" ,`{"jsonrpc":"2.0","id":1,"method":"tools/list"}` ) 15if status != 200 { 16t .Fatal (status ) 17 } 18var inventory struct { 19Tools []struct { 20Name string `json:"name"` 21InputSchema map [string ]any `json:"inputSchema"` 22 }`json:"tools"` 23 } 24if err := json .Unmarshal (body ["result" ],& inventory );err != nil { 25t .Fatal (err ) 26 } 27count := 0 28var check func (any ) 29check = func (value any ) { 30switch node := value .(type ) { 31case map [string ]any : 32for key ,child := range node { 33if key == "maxCharacters" || key == "max_chars" { 34limit ,ok := child .(map [string ]any ) 35if ! ok || limit ["type" ]!= "integer" || limit ["minimum" ]!= float64 (0 )|| limit ["maximum" ]!= float64 (10000 ) { 36t .Fatalf ("%s has mismatched character limits: %v" ,key ,child ) 37 } 38count ++ 39 } 40check (child ) 41 } 42case []any : 43for _ ,child := range node { 44check (child ) 45 } 46 } 47 } 48for _ ,tool := range inventory .Tools { 49check (tool .InputSchema ) 50 } 51if count != 7 { 52t .Fatalf ("expected four content limits and three alias limits, got %d" ,count ) 53 } 54} 55 56func TestContentCharacterLimitValidation (t * testing.T ) { 57for _ ,feature := range []string {"text" ,"highlights" } { 58for _ ,limit := range []int {- 1 ,0 ,1 ,10000 ,10001 ,16000 } { 59t .Run (fmt .Sprintf ("%s/%d" ,feature ,limit ),func (t * testing.T ) { 60var q ContentsRequest 61err := strictJSON (fmt .Appendf (nil ,`{"urls":["https://example.com"],%q:{"maxCharacters":%d}}` ,feature ,limit ),& q ) 62if err == nil { 63_ ,err = q .validate () 64 } 65valid := limit >= 0 && limit <= 10000 66if (err == nil )!= valid { 67t .Fatalf ("limit %d valid=%v: %v" ,limit ,valid ,err ) 68 } 69 }) 70 } 71 } 72} 73 74func TestCharacterLimitsOverHTTPAndMCP (t * testing.T ) { 75s ,page := fixtureService (t ) 76text := strings .Repeat ("request limits. " ,800 ) 77s .fetch .cache .put (page .URL ,Document {URL :page .URL ,FinalURL :page .URL ,Title :"limits" ,Markdown :text ,Fetched :time .Now ()}) 78for _ ,limit := range []int {0 ,1 ,10000 ,10001 ,16000 } { 79for _ ,transport := range []string {"http" ,"mcp" } { 80t .Run (fmt .Sprintf ("%d/%s" ,limit ,transport ),func (t * testing.T ) { 81args := fmt .Sprintf (`{"urls":[%q],"text":{"maxCharacters":%d}}` ,page .URL ,limit ) 82path := "/contents" 83if transport == "mcp" { 84path = "/mcp" 85args = fmt .Sprintf (`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"web_contents","arguments":%s}}` ,args ) 86 } 87status ,payload := callHTTP (t ,s ,path ,args ) 88valid := limit <= 10000 89if transport == "mcp" { 90var result struct { 91IsError bool `json:"isError"` 92StructuredContent map [string ]json.RawMessage `json:"structuredContent"` 93 } 94if err := json .Unmarshal (payload ["result" ],& result );err != nil { 95t .Fatal (err ) 96 } 97if status != 200 || result .IsError == valid { 98t .Fatalf ("unexpected mcp result: %s" ,payload ["result" ]) 99 } 100payload = result .StructuredContent 101 }else if valid && status != 200 || ! valid && status != 400 { 102t .Fatalf ("unexpected http status %d: %v" ,status ,payload ) 103 } 104if ! valid { 105return 106 } 107var results []Result 108if err := json .Unmarshal (payload ["results" ],& results );err != nil { 109t .Fatal (err ) 110 } 111want := utf8 .RuneCountInString (text ) 112if limit > 0 { 113want = min (want ,limit ) 114 } 115if len (results )!= 1 || results [0 ].Text == nil || utf8 .RuneCountInString (* results [0 ].Text )!= want { 116t .Fatalf ("text was not capped to %d characters: %+v" ,want ,results ) 117 } 118 }) 119 } 120 } 121}