package client

import (
	"context"
	"log"
	"os"
	"testing"
	"time"
)

var addr = "http://127.0.0.1:9090"

func TestQueryWithRange(t *testing.T) {
	if os.Getenv("RUN_LOCAL_O11Y_TESTS") != "1" {
		t.Skip("set RUN_LOCAL_O11Y_TESTS=1 to run local query tests")
	}
	ctx := context.Background()
	query := `sum(tidb_server_connections{})`
	now := time.Now()

	thanosResp, _, err := QueryMetricDataWithRange(ctx, addr, "", query, now.Add(-1*time.Minute), now)
	if err != nil {
		t.Fatalf("QueryMetricDataWithRange failed: %v", err)
	}
	if thanosResp == nil || len(thanosResp.Data.Result) == 0 {
		t.Fatalf("QueryMetricDataWithRange returned no data")
	}
	log.Println(thanosResp.Data.Result[0].Value)
}

func TestQueryWithTime(t *testing.T) {
	if os.Getenv("RUN_LOCAL_O11Y_TESTS") != "1" {
		t.Skip("set RUN_LOCAL_O11Y_TESTS=1 to run local query tests")
	}
	ctx := context.Background()
	query := `sum(tidb_server_connections{})`
	now := time.Now()

	thanosResp, _, err := QueryMetricDataWithTime(ctx, addr, "", query, now)
	if err != nil {
		t.Fatalf("QueryMetricDataWithTime failed: %v", err)
	}
	if thanosResp == nil || len(thanosResp.Data.Result) == 0 {
		t.Fatalf("QueryMetricDataWithTime returned no data")
	}
	log.Println(thanosResp.Data.Result[0].Values)
}
