package egress import ( "context" "encoding/json" "net" "net/http" "net/http/httptest" "sync/atomic" "testing" "github.com/modelcontextprotocol/go-sdk/mcp" "github.com/samcharles93/archie-core/internal/events" ) // A recording session forwards none of the run's deliveries: each is answered // with a synthetic success and reported on the task, so an operator sees what // the draft would have sent. Reads still pass, and so do model calls whatever // their method -- a recorded one would leave the run nothing to read. func TestRecordedSessionRecordsDeliveries(t *testing.T) { t.Parallel() cases := []struct { name string method string host string modelHosts []string wantDetail string // empty means the request must be forwarded }{ {name: "a write is recorded and not forwarded", method: http.MethodPost, host: "hooks.example", wantDetail: "would POST hooks.example/hook"}, {name: "a read is forwarded", method: http.MethodGet, host: "hooks.example"}, {name: "a write to a model host is forwarded", method: http.MethodPost, host: "api.model.example", modelHosts: []string{"api.model.example"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { t.Parallel() var forwarded atomic.Bool upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { forwarded.Store(true) w.WriteHeader(http.StatusNoContent) })) defer upstream.Close() var recorded []events.Event proxy := NewProxy(nil, ProxyOptions{ Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String()) }, Events: func(event events.Event) { recorded = append(recorded, event) }, }) session, err := proxy.Register(SessionOptions{Token: "proxy-password", Record: &RecordOptions{TaskID: 7, ModelHosts: tc.modelHosts}}) if err != nil { t.Fatalf("register session: %v", err) } req := httptest.NewRequestWithContext(context.Background(), tc.method, "http://"+tc.host+"/hook", nil) rec := httptest.NewRecorder() proxy.forward(context.Background(), rec, req, session, "http", tc.host, 80) if tc.wantDetail == "" { if !forwarded.Load() { t.Fatalf("%s was not forwarded: %d %q", tc.method, rec.Code, rec.Body.String()) } if len(recorded) != 0 { t.Fatalf("a forwarded request was recorded: %+v", recorded) } return } if forwarded.Load() { t.Fatalf("%s reached the upstream", tc.method) } if rec.Code != http.StatusOK || rec.Body.Len() != 0 { t.Fatalf("recorded request answered %d %q, want an empty 200", rec.Code, rec.Body.String()) } if len(recorded) != 1 || recorded[0].Kind != events.KindDeliveryRecorded || recorded[0].TaskID != 7 || recorded[0].Detail != tc.wantDetail { t.Fatalf("recorded %+v, want one %q on task 7", recorded, tc.wantDetail) } }) } } // A recording session performs no central tool call, so a mutating tool cannot // act for real during a test run. Discovery still reaches the gateway: a run // that cannot list its tools cannot use any. func TestRecordedToolsPerformNoCall(t *testing.T) { t.Parallel() var listed, called atomic.Int32 backend := stubBackend{ list: func(context.Context, string) ([]*mcp.Tool, error) { listed.Add(1) return []*mcp.Tool{{Name: "memory_create"}}, nil }, call: func(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) { called.Add(1) return nil, nil }, } var recorded []events.Event proxy := NewProxy(nil, ProxyOptions{Events: func(event events.Event) { recorded = append(recorded, event) }}) session := &Session{record: &RecordOptions{TaskID: 7}} tools := recordedTools{Backend: backend, proxy: proxy, session: session} if _, err := tools.List(context.Background(), "grant"); err != nil || listed.Load() != 1 { t.Fatalf("discovery did not reach the gateway: listed=%d err=%v", listed.Load(), err) } result, err := tools.Call(context.Background(), "grant", "memory_create", nil) if err != nil { t.Fatalf("a recorded call reports success: %v", err) } if called.Load() != 0 { t.Fatalf("a recorded call reached the gateway") } text, ok := result.Content[0].(*mcp.TextContent) if !ok || text.Text != "would call memory_create" { t.Fatalf("answered %+v", result.Content) } if len(recorded) != 1 || recorded[0].Kind != events.KindDeliveryRecorded || recorded[0].TaskID != 7 || recorded[0].Detail != "would call memory_create" { t.Fatalf("recorded %+v, want one \"would call memory_create\" on task 7", recorded) } } type stubBackend struct { list func(context.Context, string) ([]*mcp.Tool, error) call func(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) } func (b stubBackend) List(ctx context.Context, credential string) ([]*mcp.Tool, error) { return b.list(ctx, credential) } func (b stubBackend) Call(ctx context.Context, credential, tool string, args json.RawMessage) (*mcp.CallToolResult, error) { return b.call(ctx, credential, tool, args) } func (b stubBackend) Close(context.Context, string) error { return nil }