Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

3.5 KB
Raw Blame
package v1

import (
	"context"
	"testing"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/require"

	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
	v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)

// mcpSettingRead is one answer from the MCP settings reader.
type mcpSettingRead struct {
	setting *storepb.MCPSetting
	err     error
}

func (r mcpSettingRead) GetMCPSettingsUncached(context.Context, string) (*storepb.MCPSetting, error) {
	return r.setting, r.err
}

// TestActuatorMCPSetting pins the two decisions in what actuator info
// discloses about the MCP policy: an unreadable row leaves the setting absent
// instead of failing the shared bootstrap response, and the resolution the gate
// already made for this request wins over a second read.
func TestActuatorMCPSetting(t *testing.T) {
	ctx := context.Background()
	for _, tc := range []struct {
		name string
		read mcpSettingRead
		ctx  context.Context
		want *v1pb.MCPSetting
	}{
		{
			name: "the stored ceiling reaches the response",
			read: mcpSettingRead{setting: &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_ONLY}},
			ctx:  ctx,
			want: &v1pb.MCPSetting{Capability: v1pb.MCPSetting_READ_ONLY},
		},
		{
			// Actuator info is a shared bootstrap response, so it remains
			// available and leaves the optional setting absent; the policy page
			// then withholds editing rather than showing a guessed ceiling.
			name: "a row that does not unmarshal leaves the setting absent",
			read: mcpSettingRead{err: errors.New("failed to unmarshal setting MCP")},
			ctx:  ctx,
			want: nil,
		},
		{
			// The stored ceiling says DISABLED; the gate admitted this request
			// under READ_ONLY. Answering from the store would report a ceiling
			// the request was not admitted under.
			name: "the gate's resolution wins over a second read",
			read: mcpSettingRead{setting: &storepb.MCPSetting{Capability: storepb.MCPSetting_DISABLED}},
			ctx:  withMCPSettings(ctx, &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_ONLY}),
			want: &v1pb.MCPSetting{Capability: v1pb.MCPSetting_READ_ONLY},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			got := actuatorMCPSetting(tc.ctx, tc.read, "ws-test")
			if tc.want == nil {
				require.Nil(t, got)
				return
			}
			require.Equal(t, tc.want.Capability, got.GetCapability())
		})
	}
}

func TestActuatorAIEnabled(t *testing.T) {
	for _, tc := range []struct {
		name    string
		setting *storepb.AISetting
		err     error
		want    bool
	}{
		{name: "absent setting"},
		{name: "disabled setting", setting: &storepb.AISetting{}},
		{name: "enabled setting", setting: &storepb.AISetting{Enabled: true}, want: true},
		{name: "unreadable setting", setting: &storepb.AISetting{Enabled: true}, err: errors.New("read failed")},
	} {
		t.Run(tc.name, func(t *testing.T) {
			require.Equal(t, tc.want, actuatorAIEnabled(tc.setting, tc.err))
		})
	}
}

func TestActuatorAISQLReviewEnabled(t *testing.T) {
	for _, tc := range []struct {
		name    string
		setting *storepb.AISetting
		err     error
		want    bool
	}{
		{name: "absent setting"},
		{name: "AI on without the review", setting: &storepb.AISetting{Enabled: true}},
		{name: "the review switched on", setting: &storepb.AISetting{Enabled: true, SqlReviewEnabled: true}, want: true},
		{name: "unreadable setting", setting: &storepb.AISetting{SqlReviewEnabled: true}, err: errors.New("read failed")},
	} {
		t.Run(tc.name, func(t *testing.T) {
			require.Equal(t, tc.want, actuatorAISQLReviewEnabled(tc.setting, tc.err))
		})
	}
}