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"net/http/httptest" 7"reflect" 8"strings" 9"testing" 10) 11 12func TestPageAliasesUseContents (t * testing.T ) { 13for _ ,tc := range []struct { 14name ,extra ,options string 15summary bool 16 }{ 17 {"extract" ,`,"max_chars":40` ,`,"text":{"maxCharacters":40}` ,false }, 18 {"preview" ,`,"query":"request limits"` ,`,"summary":{"query":"request limits"}` ,true }, 19 {"summarize" ,`` ,`,"summary":true` ,true }, 20 {"summarize" ,`,"instructions":"request limits","include_markdown":true,"max_chars":40` ,`,"summary":{"query":"request limits"},"text":{"maxCharacters":40}` ,true }, 21 } { 22for _ ,transport := range []string {"http" ,"mcp" } { 23t .Run (tc .name + tc .extra + "/" + transport ,func (t * testing.T ) { 24s ,page := fixtureService (t ) 25_ ,canonical := callHTTP (t ,s ,"/contents" ,fmt .Sprintf (`{"urls":[%q]%s}` ,page .URL ,tc .options )) 26body := fmt .Sprintf (`{"url":%q%s}` ,page .URL ,tc .extra ) 27path := "/v1/" + tc .name 28if transport == "mcp" { 29path = "/mcp" 30body = fmt .Sprintf (`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"web_%s","arguments":%s}}` ,tc .name ,body ) 31 } 32status ,payload := callHTTP (t ,s ,path ,body ) 33if status != 200 { 34t .Fatal (status ,payload ) 35 } 36if transport == "mcp" { 37var result struct { 38IsError bool `json:"isError"` 39StructuredContent map [string ]json.RawMessage `json:"structuredContent"` 40Content []struct {Text string }`json:"content"` 41 } 42if err := json .Unmarshal (payload ["result" ],& result );err != nil || result .IsError { 43t .Fatal (string (payload ["result" ]),err ) 44 } 45var text map [string ]json.RawMessage 46if len (result .Content )!= 1 || json .Unmarshal ([]byte (result .Content [0 ].Text ),& text )!= nil || ! reflect .DeepEqual (text ,result .StructuredContent ) { 47t .Fatal ("mcp text and structured output differ" ) 48 } 49payload = result .StructuredContent 50 } 51if string (payload ["results" ])!= string (canonical ["results" ]) { 52t .Fatalf ("alias diverged from contents: %s != %s" ,payload ["results" ],canonical ["results" ]) 53 } 54var statuses []ItemStatus 55if err := json .Unmarshal (payload ["statuses" ],& statuses );err != nil || len (statuses )!= 1 || statuses [0 ].Status != "success" { 56t .Fatal ("missing contents statuses" ,statuses ,err ) 57 } 58var id string 59_ = json .Unmarshal (payload ["requestId" ],& id ) 60var caller ,endpoint string 61if err := s .log .db .QueryRow ("select caller,endpoint from requests where request_id=? order by id limit 1" ,id ).Scan (& caller ,& endpoint );err != nil || caller != "admin" || endpoint != "contents" { 62t .Fatal ("alias lost canonical request logging" ,caller ,endpoint ,err ) 63 } 64var completions ,previews int 65if err := s .log .db .QueryRow ("select count(*),coalesce(sum(task='preview_summary'),0) from completions where request_id=?" ,id ).Scan (& completions ,& previews );err != nil { 66t .Fatal (err ) 67 } 68want := 0 69if tc .summary { 70want = 1 71 } 72if completions != want || previews != want { 73t .Fatal ("alias changed completion logging" ,completions ,previews ) 74 } 75 }) 76 } 77 } 78} 79 80func TestPageAliasValidationAndAuth (t * testing.T ) { 81s ,page := fixtureService (t ) 82for _ ,tc := range []struct {name ,body string }{ 83 {"extract" ,`{}` }, 84 {"extract" ,`{"url":"file:///etc/passwd"}` }, 85 {"extract" ,fmt .Sprintf (`{"url":%q,"max_chars":10001}` ,page .URL )}, 86 {"extract" ,fmt .Sprintf (`{"url":%q,"render_js":false}` ,page .URL )}, 87 {"preview" ,fmt .Sprintf (`{"url":%q,"query":" "}` ,page .URL )}, 88 {"summarize" ,fmt .Sprintf (`{"url":%q,"instructions":%q}` ,page .URL ,strings .Repeat ("a" ,4001 ))}, 89 } { 90if status ,_ := callHTTP (t ,s ,"/v1/" + tc .name ,tc .body );status != 400 { 91t .Fatal (tc .name ,"accepted invalid input" ,status ) 92 } 93_ ,payload := callHTTP (t ,s ,"/mcp" ,fmt .Sprintf (`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"web_%s","arguments":%s}}` ,tc .name ,tc .body )) 94if ! strings .Contains (string (payload ["result" ]),`"isError":true` ) { 95t .Fatal ("mcp accepted invalid alias input" ,payload ) 96 } 97 } 98for _ ,name := range []string {"extract" ,"preview" ,"summarize" } { 99r := httptest .NewRequest ("POST" ,"/v1/" + name ,strings .NewReader (`{}` )) 100w := httptest .NewRecorder () 101s .handler ().ServeHTTP (w ,r ) 102if w .Code != 401 { 103t .Fatal ("alias bypassed authentication" ,name ,w .Code ) 104 } 105 } 106}