返回 last30days-skill
research_exit_test.go
根目录 / mcp / internal / tools / research_exit_test.go
1 package tools
2
3 import (
4 "context"
5 "fmt"
6 "strings"
7 "testing"
8 "testing/fstest"
9
10 "github.com/mvanhorn/last30days-skill/mcp/internal/engine"
11 )
12
13 func TestResearchHandlerDegradedExit(t *testing.T) {
14 compact := "Evidence from a healthy source\n\n---\n" + canonicalOutputEnd + "\n"
15 html := "<!DOCTYPE html>\n<html><body>Evidence</body></html>\n"
16 receipt := "[last30days] strict-exit: degraded sources: x\n"
17
18 for _, tc := range []struct {
19 name, emit, stdout, stderr string
20 code int
21 wantStatus string
22 }{
23 {"clean", "compact", compact, "", 0, "complete"},
24 {"degraded compact", "compact", compact, receipt, 3, "degraded"},
25 {"degraded compact with CRLF", "compact", strings.ReplaceAll(compact, "\n", "\r\n"), strings.ReplaceAll(receipt, "\n", "\r\n"), 3, "degraded"},
26 {"degraded HTML", "html", html, receipt, 3, "degraded"},
27 {"needs clarification", "compact", "", "Clarify which topic\n", 3, "error"},
28 {"complete-looking unrelated output", "compact", compact, "Other failure\n", 3, "error"},
29 {"incomplete compact", "compact", "Evidence from a healthy source\n", receipt, 3, "error"},
30 {"incomplete HTML", "html", strings.TrimSuffix(html, "</html>\n"), receipt, 3, "error"},
31 {"hard exit even with receipt", "compact", compact, receipt, 1, "error"},
32 } {
33 t.Run(tc.name, func(t *testing.T) {
34 cacheRoot := t.TempDir()
35 t.Setenv(engine.CacheEnvOverride, cacheRoot)
36 const version = "test-strict-exit"
37 script := fmt.Sprintf(
38 "import sys\nsys.stdout.buffer.write(%q.encode())\nsys.stderr.buffer.write(%q.encode())\nraise SystemExit(%d)\n",
39 tc.stdout, tc.stderr, tc.code,
40 )
41 fakeFS := fstest.MapFS{
42 "last30days.py": {Data: []byte(script)},
43 }
44 handler := makeResearchHandler(Config{Version: version, EngineFS: fakeFS})
45 result, err := handler(context.Background(), newCallToolRequest(map[string]any{
46 "topic": "Evidence", "emit": tc.emit,
47 }))
48 if err != nil || result == nil {
49 t.Fatalf("result = %+v, error = %v", result, err)
50 }
51 switch tc.wantStatus {
52 case "error":
53 if !result.IsError || result.StructuredContent != nil || strings.Contains(resultText(result), "Evidence from a healthy source") {
54 t.Fatalf("expected only a tool error, got %+v", result)
55 }
56 case "degraded":
57 structured, ok := result.StructuredContent.(map[string]string)
58 if result.IsError || !ok || structured["status"] != "degraded" || structured["report"] != tc.stdout {
59 t.Fatalf("expected structured degraded report, got %+v", result)
60 }
61 if !strings.Contains(resultText(result), "degraded coverage") || !strings.Contains(resultText(result), tc.stdout) {
62 t.Fatalf("expected a degraded notice and the report, got %q", resultText(result))
63 }
64 case "complete":
65 if result.IsError || resultText(result) != tc.stdout {
66 t.Fatalf("expected the clean report unchanged, got %+v", result)
67 }
68 }
69 })
70 }
71 }
72
72 lines GO