|
| 1 | +// Copyright 2025 StreamNative |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package mcp |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + mcpgo "github.com/mark3labs/mcp-go/mcp" |
| 22 | + "github.com/streamnative/pulsarctl/pkg/cmdutils" |
| 23 | + "github.com/streamnative/streamnative-mcp-server/pkg/config" |
| 24 | + "github.com/streamnative/streamnative-mcp-server/pkg/kafka" |
| 25 | + pulsarsession "github.com/streamnative/streamnative-mcp-server/pkg/pulsar" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestHandleResetContextClearsSNCloudClusterSessions(t *testing.T) { |
| 30 | + snSession, err := config.NewSNCloudSession(config.SNCloudContext{ |
| 31 | + JWTToken: "token", |
| 32 | + APIURL: "https://api.example.com", |
| 33 | + LogAPIURL: "https://logs.example.com", |
| 34 | + Organization: "org", |
| 35 | + PulsarInstance: "instance-a", |
| 36 | + PulsarCluster: "cluster-a", |
| 37 | + }) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + pulsarSession := &pulsarsession.Session{ |
| 41 | + Ctx: pulsarsession.PulsarContext{ |
| 42 | + ServiceURL: "pulsar://pulsar.example.com:6650", |
| 43 | + WebServiceURL: "https://pulsar.example.com", |
| 44 | + Token: "token", |
| 45 | + }, |
| 46 | + PulsarCtlConfig: &cmdutils.ClusterConfig{WebServiceURL: "https://pulsar.example.com"}, |
| 47 | + } |
| 48 | + kafkaSession := &kafka.Session{ |
| 49 | + Ctx: kafka.KafkaContext{ |
| 50 | + BootstrapServers: "kafka.example.com:9093", |
| 51 | + SchemaRegistryURL: "https://kafka.example.com/kafka", |
| 52 | + ConnectURL: "https://api.example.com/admin/kafkaconnect/", |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + ctx := context.Background() |
| 57 | + ctx = WithSNCloudSession(ctx, snSession) |
| 58 | + ctx = WithPulsarSession(ctx, pulsarSession) |
| 59 | + ctx = WithKafkaSession(ctx, kafkaSession) |
| 60 | + |
| 61 | + result, err := handleResetContext(ctx, mcpgo.CallToolRequest{}) |
| 62 | + require.NoError(t, err) |
| 63 | + require.False(t, result.IsError) |
| 64 | + |
| 65 | + require.Empty(t, snSession.Ctx.PulsarInstance) |
| 66 | + require.Empty(t, snSession.Ctx.PulsarCluster) |
| 67 | + require.Equal(t, pulsarsession.PulsarContext{}, pulsarSession.Ctx) |
| 68 | + require.Equal(t, kafka.KafkaContext{}, kafkaSession.Ctx) |
| 69 | + |
| 70 | + _, err = pulsarSession.GetAdminClient() |
| 71 | + require.EqualError(t, err, "err: ContextNotSetErr: Please set the cluster context first") |
| 72 | + |
| 73 | + _, err = kafkaSession.GetAdminClient() |
| 74 | + require.EqualError(t, err, "err: ContextNotSetErr: Please set the cluster context first") |
| 75 | +} |
0 commit comments