package tenantoverlay

import (
	"context"
	"strings"
	"testing"

	"github.com/pingcap-inc/nexuscrm-controlplane/packs"
)

// TestReconcileTenantFlow_ConnectorSource_Rejected verifies that a tenant
// overlay flow declaring a connectorSource is rejected before any DB access,
// because only pack-owned standard flows may use a connector directory source.
func TestReconcileTenantFlow_ConnectorSource_Rejected(t *testing.T) {
	raw := []byte(`{
		"processType": "AutoLaunchedFlow",
		"start": {
			"connectorSource": {
				"ownerPack":   "some-pack",
				"profileName": "MyProfile",
				"objectKey":   "contacts",
				"itemsPath":   "$.results",
				"itemIdPath":  "$.id"
			}
		}
	}`)

	// Minimal deps — MetadataRepo is nil so the test panics if we ever reach
	// the DB lookup, which would mean the guard is absent.
	deps := packs.ApplyDeps{}

	err := reconcileTenantFlow(context.Background(), deps, "My_Overlay_Flow", raw)
	if err == nil {
		t.Fatal("reconcileTenantFlow() = nil, want error when overlay flow declares connectorSource")
	}
	if !strings.Contains(err.Error(), "cannot declare connectorSource") {
		t.Fatalf("error = %q, want substring %q", err.Error(), "cannot declare connectorSource")
	}
}
