返回 DeepSeek-Reasonix
proxy_e2e_test.go
根目录 / internal / plugin / proxy_e2e_test.go
1 package plugin_test
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "strings"
8 "testing"
9 "time"
10
11 "reasonix/internal/agent"
12 "reasonix/internal/capability"
13 "reasonix/internal/plugin"
14 "reasonix/internal/tool"
15 )
16
17 // mockSpec mirrors helperSpec (lazy_test.go): the test binary re-runs itself
18 // as a stdio MCP server via TestHelperProcess.
19 func mockSpec() plugin.Spec {
20 return plugin.Spec{
21 Name: "mock",
22 Command: os.Args[0],
23 Args: []string{"-test.run=TestHelperProcess", "--"},
24 Env: map[string]string{"GO_WANT_HELPER_PROCESS": "1"},
25 Authorized: true, // install/boot sets this; proxy never invents trust
26 }
27 }
28
29 // TestUseCapabilityFirstDiscoveryConnectListCall is the reviewer-demanded end
30 // to end: no schema cache, action=call on the server id resolves to a gated
31 // connect target (no process before approval), Execute connects and lists the
32 // tools, and a follow-up mcp-tool call executes against the live server.
33 func TestUseCapabilityFirstDiscoveryConnectListCall(t *testing.T) {
34 host := plugin.NewHost()
35 defer host.Close()
36 lifeCtx := t.Context()
37 ledger := capability.NewLedger()
38 audit := &capability.Audit{}
39 proxy := agent.NewUseCapabilityTool(lifeCtx, host, []plugin.Spec{mockSpec()}, tool.NewRegistry(), ledger, audit, nil)
40
41 resolved, err := proxy.ResolveCall(context.Background(), json.RawMessage(`{"action":"call","capability_id":"mcp-server:mock"}`))
42 if err != nil {
43 t.Fatalf("resolve server call: %v", err)
44 }
45 if resolved.SkipExecute || resolved.Target == nil {
46 t.Fatalf("expected a deferred connect target, got %+v", resolved)
47 }
48 if resolved.TargetName != plugin.MCPConnectPermissionName("mock") {
49 t.Fatalf("connect permission name = %q, want %q", resolved.TargetName, plugin.MCPConnectPermissionName("mock"))
50 }
51 if resolved.ReadOnly {
52 t.Fatal("connect must not claim read-only: it spawns a subprocess")
53 }
54 if host.HasClient("mock") {
55 t.Fatal("resolution must not start the server")
56 }
57
58 listing, err := resolved.Target.Execute(context.Background(), resolved.Args)
59 if err != nil {
60 t.Fatalf("connect execute: %v", err)
61 }
62 if !strings.Contains(listing, "mcp-tool:mock/echo") {
63 t.Fatalf("listing missing echo tool id:\n%s", listing)
64 }
65 if !host.HasClient("mock") {
66 t.Fatal("server should be connected after approved execute")
67 }
68 if got := proxy.ConnectedProxyTools(); len(got["mock"]) == 0 {
69 t.Fatalf("proxy snapshot empty after connect: %v", got)
70 }
71
72 // A repeated server-level call now lists side-effect-free.
73 again, err := proxy.ResolveCall(context.Background(), json.RawMessage(`{"action":"call","capability_id":"mcp-server:mock"}`))
74 if err != nil {
75 t.Fatal(err)
76 }
77 if !again.SkipExecute || !strings.Contains(again.Result, "mcp-tool:mock/echo") {
78 t.Fatalf("connected server call should list immediately, got %+v", again)
79 }
80 if _, err := proxy.Execute(context.Background(), json.RawMessage(`{"action":"call","capability_id":"mcp-server:mock"}`)); err != nil {
81 t.Fatalf("connected server call execute: %v", err)
82 }
83 if entry, ok := ledger.Get("mcp-server:mock"); !ok || entry.Outcome != capability.OutcomeSucceeded {
84 t.Fatalf("connected server call ledger = %+v, found=%v", entry, ok)
85 }
86 if snap := audit.Snapshot(); snap.MCPCall != 1 || snap.MCPCallFailures != 0 {
87 t.Fatalf("connected server call audit = %d/%d, want 1/0", snap.MCPCall, snap.MCPCallFailures)
88 }
89
90 // Then the discovered tool is callable end to end.
91 call, err := proxy.ResolveCall(context.Background(), json.RawMessage(`{"action":"call","capability_id":"mcp-tool:mock/echo","arguments":{"msg":"hi"}}`))
92 if err != nil {
93 t.Fatal(err)
94 }
95 callCtx, cancelCall := context.WithTimeout(context.Background(), 5*time.Second)
96 defer cancelCall()
97 out, err := call.Target.Execute(callCtx, call.Args)
98 if err != nil {
99 t.Fatalf("tool execute: %v", err)
100 }
101 if out != "echo: hi" {
102 t.Fatalf("echo result = %q", out)
103 }
104 }
105
106 // TestUseCapabilitySharedHostSnapshot covers the cross-tab gap: a server
107 // connected by another controller on the shared host must still populate this
108 // proxy's snapshot (and thus the catalog) through resolve and inspect.
109 func TestUseCapabilitySharedHostSnapshot(t *testing.T) {
110 host := plugin.NewHost()
111 defer host.Close()
112 lifeCtx := t.Context()
113 callCtx, cancelCall := context.WithTimeout(context.Background(), 5*time.Second)
114 defer cancelCall()
115 // "Tab A" connects directly (not through any proxy).
116 if _, err := host.AddWithLifecycle(lifeCtx, callCtx, mockSpec()); err != nil {
117 t.Fatalf("tab A connect: %v", err)
118 }
119
120 // "Tab B": empty registry (auto_start=false semantics), fresh proxy.
121 proxyB := agent.NewUseCapabilityTool(lifeCtx, host, []plugin.Spec{mockSpec()}, tool.NewRegistry(), capability.NewLedger(), nil, nil)
122 resolved, err := proxyB.ResolveCall(context.Background(), json.RawMessage(`{"action":"call","capability_id":"mcp-tool:mock/echo","arguments":{}}`))
123 if err != nil {
124 t.Fatal(err)
125 }
126 if resolved.Target == nil {
127 t.Fatalf("expected live target via shared host, got %+v", resolved)
128 }
129 if got := proxyB.ConnectedProxyTools(); len(got["mock"]) == 0 {
130 t.Fatalf("resolve on a shared-host server must build the snapshot, got %v", got)
131 }
132
133 // A third proxy sees it via inspect too.
134 proxyC := agent.NewUseCapabilityTool(lifeCtx, host, []plugin.Spec{mockSpec()}, tool.NewRegistry(), capability.NewLedger(), nil, func() capability.Catalog {
135 return capability.Catalog{Entries: []capability.Entry{{
136 ID: "mcp-server:mock", Kind: capability.KindMCPServer, Name: "mock", Source: "mock", Status: capability.StatusReady,
137 }}}
138 })
139 if _, err := proxyC.Execute(context.Background(), json.RawMessage(`{"action":"inspect","capability_id":"mcp-server:mock"}`)); err != nil {
140 t.Fatal(err)
141 }
142 if got := proxyC.ConnectedProxyTools(); len(got["mock"]) == 0 {
143 t.Fatalf("inspect on a shared-host server must build the snapshot, got %v", got)
144 }
145 }
146
146 lines GO