// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0

package operations

import (
	"testing"

	_ "github.com/go-openapi/analysis/internal/antest"
	"github.com/go-openapi/spec"
	"github.com/go-openapi/testify/v2/require"
)

var _ Provider = mockOperationsProvider{}

type mockOperationsProvider struct {
	give map[string]map[string]*spec.Operation
}

func (m mockOperationsProvider) Operations() map[string]map[string]*spec.Operation {
	return m.give
}

func TestGatherOperations(t *testing.T) {
	t.Run("should handle empty operation IDs", func(_ *testing.T) {
		m := mockOperationsProvider{
			give: map[string]map[string]*spec.Operation{
				"get": {
					"/pth1": {
						OperationProps: spec.OperationProps{
							ID:          "",
							Description: "ok",
						},
					},
				},
			},
		}

		res := GatherOperations(m, nil)
		require.MapContainsT(t, res, "GetPth1")
	})

	t.Run("should handle duplicate operation IDs (when spec validation is skipped)", func(_ *testing.T) {
		m := mockOperationsProvider{
			give: map[string]map[string]*spec.Operation{
				"get": {
					"/pth1": {
						OperationProps: spec.OperationProps{
							ID:          "id1",
							Description: "ok",
						},
					},
				},
				"post": {
					"/pth2": {
						OperationProps: spec.OperationProps{
							ID:          "id1",
							Description: "ok",
						},
					},
				},
			},
		}

		res := GatherOperations(m, nil)
		require.MapContainsT(t, res, "id1")
		require.MapNotContainsT(t, res, "GetPth1")
		require.MapContainsT(t, res, "PostPth2")
	})
}
