package e2e

import (
	"bufio"
	"fmt"
	"github.com/stretchr/testify/assert"
	"net/http"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/tests/helpers"
	"testing"
	"time"
)

func TestAccountPipe(t *testing.T) {
	jwtUtil := helpers.InitializeJWT()

	token, tokenErr := jwtUtil.Create("64ebda4387392e122e5d411f")
	if !assert.NoError(t, tokenErr) {
		return
	}
	fmt.Println(token)
	url := fmt.Sprintf("http://localhost:8082/account/pipe?Authorization=%s", token)
	client := &http.Client{
		Timeout: 100 * time.Second,
	}

	req, err := http.NewRequest("GET", url, nil)
	if !assert.NoError(t, err) {
		return
	}

	req.Header.Set("Accept", "text/event-stream")

	resp, err := client.Do(req)
	if !assert.NoError(t, err) {
		return
	}
	defer resp.Body.Close()

	assert.Equal(t, http.StatusOK, resp.StatusCode, "Expected status code 200")

	scanner := bufio.NewScanner(resp.Body)
	for scanner.Scan() {
		line := scanner.Text()
		fmt.Println("Received:", line)
	}

	if err := scanner.Err(); err != nil {
		t.Fatalf("Error reading response: %v", err)
	}
}
