package gitlab

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestSidekiqService_GetQueueMetrics(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/queue_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprintf(w, `{"queues": {"default": {"backlog": 0,"latency": 0}}}`)
	})

	qm, _, err := client.Sidekiq.GetQueueMetrics()
	require.NoError(t, err)

	want := &QueueMetrics{Queues: map[string]QueueMetricsQueue{"default": {Backlog: 0, Latency: 0}}}
	require.Equal(t, want, qm)
}

func TestSidekiqService_GetProcessMetrics(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/process_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{"processes": [{"hostname": "gitlab.example.com","pid": 5649,"tag": "gitlab","concurrency": 25,"busy": 0}]}`)
	})

	pm, _, err := client.Sidekiq.GetProcessMetrics()
	require.NoError(t, err)

	want := &ProcessMetrics{[]ProcessMetricsProcess{{Hostname: "gitlab.example.com", Pid: 5649, Tag: "gitlab", Concurrency: 25, Busy: 0}}}
	require.Equal(t, want, pm)
}

func TestSidekiqService_GetJobStats(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/job_stats", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{"jobs": {"processed": 2,"failed": 0,"enqueued": 0}}`)
	})

	js, _, err := client.Sidekiq.GetJobStats()
	require.NoError(t, err)

	want := &JobStats{JobStatsJobs{Processed: 2, Failed: 0, Enqueued: 0}}
	require.Equal(t, want, js)
}

func TestSidekiqService_GetCompoundMetrics(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/sidekiq/compound_metrics", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		mustWriteHTTPResponse(t, w, "testdata/get_compound_metrics.json")
	})

	cm, _, err := client.Sidekiq.GetCompoundMetrics()
	require.NoError(t, err)

	want := &CompoundMetrics{
		QueueMetrics: QueueMetrics{Queues: map[string]QueueMetricsQueue{"default": {
			Backlog: 0,
			Latency: 0,
		}}},
		ProcessMetrics: ProcessMetrics{Processes: []ProcessMetricsProcess{{Hostname: "gitlab.example.com", Pid: 5649, Tag: "gitlab", Concurrency: 25, Busy: 0}}},
		JobStats:       JobStats{Jobs: JobStatsJobs{Processed: 2, Failed: 0, Enqueued: 0}},
	}
	require.Equal(t, want, cm)
}
