package main

import (
	"os"
	"testing"

	"git.p78su.freemyip.com/svi/gostore/internal/service"
)

type tester struct {
	t *testing.T
}

func TestMain(t *testing.T) {
	fnClear := func() {
		_ = os.RemoveAll("./store")
	}
	fnClear()
	defer fnClear()
	sf := &tester{
		t: t,
	}
	sf.createBad1()
	sf.createGood1()
}

func (sf *tester) createGood1() {
	sf.t.Log("createGood1")
	os.Unsetenv("STORE_HTTP_PORT")
	os.Setenv("STORE_HTTP_PORT", "18080")
	serv, err = service.NewService()
	if err != nil {
		sf.t.Fatalf("createGood1(): in create IStoreDisk, err=\n\t%v", err)
	}
	if serv == nil {
		sf.t.Fatalf("createGood1(): serv==nil")
	}
	if http := serv.ServHttp(); http == nil {
		sf.t.Fatalf("createGood1(): http==nil")
	}
	go main()
	serv.CancelApp()
	serv.Wg().Wait()
}

// Нет переменной окружения
func (sf *tester) createBad1() {
	sf.t.Log("createBad1")
	main()
	if serv != nil {
		serv.CancelApp()
		serv.Wg().Wait()
	}
	_ = os.RemoveAll("./store")
}
