package client_test

import (
	"fmt"
	"testing"

	"coopcloud.tech/abra/pkg/client"
)

// use at the start to ensure testContext[0, 1, ..., amnt-1] exist and
// testContextFail[0, 1, ..., failAmnt-1] don't exist
func ensureTestState(amnt, failAmnt int) error {
	for i := 0; i < amnt; i++ {
		err := client.CreateContext(fmt.Sprintf("testContext%d", i), "", "")
		if err != nil {
			return err
		}
	}
	for i := 0; i < failAmnt; i++ {
		if _, er := client.GetContext(fmt.Sprintf("testContextFail%d", i)); er == nil {
			err := client.DeleteContext(fmt.Sprintf("testContextFail%d", i))
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func TestNew(t *testing.T) {
	err := ensureTestState(1, 1)
	if err != nil {
		t.Errorf("Couldn't ensure existence/nonexistence of contexts: %s", err)
	}
	contextName := "testContext0"
	_, err = client.New(contextName)
	if err != nil {
		t.Errorf("couldn't initialise a new client with context %s: %s", contextName, err)
	}
	contextName = "testContextFail0"
	_, err = client.New(contextName)
	if err == nil {
		t.Errorf("client.New(\"testContextFail0\") should have failed but didn't return an error")
	}

}