package m2mcore_test

import (
	"bytes"
	"strings"
	"testing"
	"time"

	"git.zetit.ru/zuevav/Bridge-and-Join-s/internal/m2mcore"
)

func TestMemoryRecorderPrometheus(t *testing.T) {
	r := m2mcore.NewMemoryRecorder()
	r.StageDuration(m2mcore.StateAwaitingDecision, 12*time.Second)
	r.StageDuration(m2mcore.StateAwaitingDecision, 8*time.Second)
	r.IncDeal(m2mcore.StateConfirmed)
	r.IncDeal(m2mcore.StateConfirmed)
	r.IncDeal(m2mcore.StateRejected)
	r.IncSLABreach(m2mcore.StateAwaitingDecision, "5m")

	var buf bytes.Buffer
	if err := r.WritePrometheus(&buf); err != nil {
		t.Fatal(err)
	}
	out := buf.String()
	for _, want := range []string{
		`m2m_stage_duration_seconds_count{stage="awaiting_decision"} 2`,
		`m2m_stage_duration_seconds_sum{stage="awaiting_decision"} 20`,
		`m2m_deals_total{state="confirmed"} 2`,
		`m2m_deals_total{state="rejected"} 1`,
		`m2m_sla_breaches_total{stage="awaiting_decision",budget="5m"} 1`,
	} {
		if !strings.Contains(out, want) {
			t.Errorf("в выводе нет %q\n---\n%s", want, out)
		}
	}
}

func TestNoopRecorder(t *testing.T) {
	var r m2mcore.Recorder = m2mcore.NoopRecorder{}
	r.StageDuration(m2mcore.StateConfirmed, time.Second)
	r.IncDeal(m2mcore.StateConfirmed)
	r.IncSLABreach(m2mcore.StateAwaitingDecision, "5m")
}
