okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petweb-extract: the Go service running on the Mac mini (vassago)5a90548

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