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.4 KiB99 linesraw
1package app
2
3import (
4	"bytes"
5	"context"
6	"encoding/json"
7	"fmt"
8	"net/http"
9	"net/http/httptest"
10	"os"
11	"strings"
12	"testing"
13)
14
15func TestLiveBrowser(t *testing.T) {
16	if os.Getenv("LIVE_BROWSER") != "1" {
17		t.Skip("set LIVE_BROWSER=1 and BROWSER_PATH to exercise chrome")
18	}
19	page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20		fmt.Fprint(w, `<html><body><main id="content"></main><script>document.getElementById('content').textContent = 'javascript rendered evidence';</script></body></html>`)
21	}))
22	defer page.Close()
23	network := newNetwork(true)
24	defer network.transport.CloseIdleConnections()
25	browser := &Browser{path: os.Getenv("BROWSER_PATH"), network: network, slots: make(chan struct{}, 1)}
26	html, final, err := browser.render(context.Background(), page.URL, false, "", "")
27	if err != nil || !strings.Contains(html, `<main id="content">javascript rendered evidence</main>`) || final != page.URL+"/" {
28		t.Fatalf("browser render: final=%q err=%v html=%s", final, err, html)
29	}
30}
31
32func TestRelativeDatabasePath(t *testing.T) {
33	t.Chdir(t.TempDir())
34	log, err := openLog("data/completions.db")
35	if err != nil {
36		t.Fatal(err)
37	}
38	defer log.db.Close()
39	if err := log.db.Ping(); err != nil {
40		t.Fatal(err)
41	}
42}
43
44func TestLLMCompletionRecordsTrainingShape(t *testing.T) {
45	s, _ := fixtureService(t)
46	s.llm.Model = "bonsai-2-27b"
47	provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48		var body map[string]json.RawMessage
49		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
50			t.Error(err)
51		}
52		fmt.Fprint(w, `{"model":"bonsai-2-27b","choices":[{"message":{"role":"assistant","content":"a grounded plain paragraph."},"finish_reason":"stop"}]}`)
53	}))
54	defer provider.Close()
55	s.llm.URL = provider.URL
56	if _, err := s.llm.complete(context.Background(), "preview_summary", previewSystem, "evidence", 2048); err != nil {
57		t.Fatal(err)
58	}
59	var request string
60	if err := s.log.db.QueryRow("select request_json from completions").Scan(&request); err != nil {
61		t.Fatal(err)
62	}
63	if strings.Contains(request, "model-secret") {
64		t.Fatal("credential leaked into training input")
65	}
66}
67
68func TestStdioMCPRoundTrip(t *testing.T) {
69	s, page := fixtureService(t)
70	request := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}
71{"jsonrpc":"2.0","method":"notifications/initialized"}
72{"jsonrpc":"2.0","id":2,"method":"tools/list"}
73{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"web_extract","arguments":{"url":"` + page.URL + `"}}}
74`
75	var out bytes.Buffer
76	if err := s.serveStdio(context.Background(), strings.NewReader(request), &out); err != nil {
77		t.Fatal(err)
78	}
79	lines := strings.Split(strings.TrimSpace(out.String()), "\n")
80	if len(lines) != 3 {
81		t.Fatalf("expected three replies and no notification reply: %s", out.String())
82	}
83	for i, line := range lines {
84		var reply struct {
85			ID     int             `json:"id"`
86			Result json.RawMessage `json:"result"`
87		}
88		if err := json.Unmarshal([]byte(line), &reply); err != nil || reply.ID != i+1 || len(reply.Result) == 0 {
89			t.Fatalf("invalid reply: %s", line)
90		}
91	}
92	if !strings.Contains(lines[2], "8 mib") || strings.Contains(lines[2], `"isError":true`) {
93		t.Fatal(lines[2])
94	}
95	var caller string
96	if err := s.log.db.QueryRow("select caller from requests where endpoint = 'contents'").Scan(&caller); err != nil || caller != "codex" {
97		t.Fatalf("caller = %q, error = %v", caller, err)
98	}
99}