package checker_test

import (
	"testing"

	"github.com/oasdiff/oasdiff/checker"
	"github.com/oasdiff/oasdiff/diff"
	"github.com/stretchr/testify/require"
)

// CL: adding propertyNames to request body
func TestRequestBodyPropertyNamesAdded(t *testing.T) {
	s1, err := open("../data/checker/property_names_base.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/property_names_revision.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPropertyNamesUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)

	found := false
	for _, e := range errs {
		if e.GetId() == checker.RequestBodyPropertyNamesAddedId {
			found = true
			break
		}
	}
	require.True(t, found, "expected request-body-property-names-added")
}

// CL: removing propertyNames from request body
func TestRequestBodyPropertyNamesRemoved(t *testing.T) {
	s1, err := open("../data/checker/property_names_revision.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/property_names_base.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPropertyNamesUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)

	found := false
	for _, e := range errs {
		if e.GetId() == checker.RequestBodyPropertyNamesRemovedId {
			found = true
			break
		}
	}
	require.True(t, found, "expected request-body-property-names-removed")
}

// CL: adding propertyNames to request property
func TestRequestPropertyPropertyNamesAdded(t *testing.T) {
	s1, err := open("../data/checker/property_names_prop_base.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/property_names_prop_revision.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPropertyNamesUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)
	require.True(t, containsId(errs, checker.RequestPropertyPropertyNamesAddedId), "expected request-property-property-names-added")
}

// CL: removing propertyNames from request property
func TestRequestPropertyPropertyNamesRemoved(t *testing.T) {
	s1, err := open("../data/checker/property_names_prop_revision.yaml")
	require.NoError(t, err)
	s2, err := open("../data/checker/property_names_prop_base.yaml")
	require.NoError(t, err)

	d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
	require.NoError(t, err)
	errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestPropertyPropertyNamesUpdatedCheck), d, osm, checker.INFO)
	require.NotEmpty(t, errs)
	require.True(t, containsId(errs, checker.RequestPropertyPropertyNamesRemovedId), "expected request-property-property-names-removed")
}
