<commit_before>package clc_test

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/mikebeyer/clc-sdk/clc"
	"github.com/stretchr/testify/assert"
)

func TestGetServer(t *testing.T) {
	assert := assert.New(t)

	name := "va1testserver01"
	resource := getServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{client(ms.URL)}
	server, err := service.Get(name)

	assert.Nil(err)
	assert.Equal(name, server.Name)
}

func TestGetServerByUUID(t *testing.T) {
	assert := assert.New(t)

	name := "5404cf5ece2042dc9f2ac16ab67416bb"
	resource := getServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{client(ms.URL)}
	server, err := service.Get(name)

	assert.Nil(err)
	assert.Equal("va1testserver01", server.Name)
}

func TestCreateServer(t *testing.T) {
	assert := assert.New(t)

	r := createServerResource(assert)
	ms := mockServer(r)
	defer ms.Close()

	service := clc.ServerService{client(ms.URL)}
	server := clc.Server{
		Name:           "va1testserver01",
		CPU:            1,
		MemoryGB:       1,
		GroupID:        "group",
		SourceServerID: "UBUNTU",
		Type:           "standard",
	}
	s, err := service.Create(server, nil)

	assert.Nil(err)
	assert.True(s.IsQueued)
	assert.Equal(s.Server, server.Name)
}

func TestDeleteServer(t *testing.T) {
	assert := assert.New(t)

	name := "va1testserver01"
	resource := deleteServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{client(ms.URL)}
	server, err := service.Delete(name)

	assert.Nil(err)
	assert.Equal(name, server.Server)
}

func TestCreateServer_Polling(t *testing.T) {
	assert := assert.New(t)

	r := createServerResource(assert)
	ms := mockServer(r)
	defer ms.Close()

	client := client(ms.URL)
	server := clc.Server{
		Name:           "va1testserver01",
		CPU:            1,
		MemoryGB:       1,
		GroupID:        "group",
		SourceServerID: "UBUNTU",
		Type:           "standard",
	}
	poll := make(chan *clc.StatusResponse, 1)
	_, err := client.Server.Create(server, poll)

	status := <-poll

	assert.Nil(err)
	assert.True(status.Complete())
}

func getServerResource(assert *assert.Assertions, name string) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			assert.Fail("GET server method should be GET", r.Method)
		}

		if r.URL.Path == "/servers/test/"+name && len(r.URL.Query()) == 0 {
			server := &clc.ServerResponse{Name: name}
			w.Header().Add("Content-Type", "application/json")
			json.NewEncoder(w).Encode(server)
			return
		}

		if r.URL.Path == "/servers/test/"+name && r.URL.Query().Get("uuid") == "true" {
			server := &clc.ServerResponse{Name: "va1testserver01"}
			w.Header().Add("Content-Type", "application/json")
			json.NewEncoder(w).Encode(server)
			return
		}<commit_msg>Fixing linting issues<commit_after>package clc_test

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/mikebeyer/clc-sdk/clc"
	"github.com/stretchr/testify/assert"
)

func TestGetServer(t *testing.T) {
	assert := assert.New(t)

	name := "va1testserver01"
	resource := getServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{Client: client(ms.URL)}
	server, err := service.Get(name)

	assert.Nil(err)
	assert.Equal(name, server.Name)
}

func TestGetServerByUUID(t *testing.T) {
	assert := assert.New(t)

	name := "5404cf5ece2042dc9f2ac16ab67416bb"
	resource := getServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{Client: client(ms.URL)}
	server, err := service.Get(name)

	assert.Nil(err)
	assert.Equal("va1testserver01", server.Name)
}

func TestCreateServer(t *testing.T) {
	assert := assert.New(t)

	r := createServerResource(assert)
	ms := mockServer(r)
	defer ms.Close()

	service := clc.ServerService{Client: client(ms.URL)}
	server := clc.Server{
		Name:           "va1testserver01",
		CPU:            1,
		MemoryGB:       1,
		GroupID:        "group",
		SourceServerID: "UBUNTU",
		Type:           "standard",
	}
	s, err := service.Create(server, nil)

	assert.Nil(err)
	assert.True(s.IsQueued)
	assert.Equal(s.Server, server.Name)
}

func TestCreateServer_Polling(t *testing.T) {
	assert := assert.New(t)

	r := createServerResource(assert)
	ms := mockServer(r)
	defer ms.Close()

	client := client(ms.URL)
	server := clc.Server{
		Name:           "va1testserver01",
		CPU:            1,
		MemoryGB:       1,
		GroupID:        "group",
		SourceServerID: "UBUNTU",
		Type:           "standard",
	}
	poll := make(chan *clc.StatusResponse, 1)
	_, err := client.Server.Create(server, poll)

	status := <-poll

	assert.Nil(err)
	assert.True(status.Complete())
}

func TestDeleteServer(t *testing.T) {
	assert := assert.New(t)

	name := "va1testserver01"
	resource := deleteServerResource(assert, name)
	ms := mockServer(resource)
	defer ms.Close()

	service := clc.ServerService{Client: client(ms.URL)}
	server, err := service.Delete(name)

	assert.Nil(err)
	assert.Equal(name, server.Server)
}

func getServerResource(assert *assert.Assertions, name string) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			assert.Fail("GET server method should be GET", r.Method)
		}

		if r.URL.Path == "/servers/test/"+name && len(r.URL.Query()) == 0 {
			server := &clc.ServerResponse{Name: name}
			w.Header().Add("Content-Type", "application/json")
			json.NewEncoder(w).Encode(server)
			return
		}

		if r.URL.Path == "/servers/test/"+name && r.URL.Query().Get("uuid") == "true" {
			server := &clc.ServerResponse{Name: "va1testserver01"}
			w.Header().Add("Content-Type", "application/json")
			json.NewEncoder(w).Encode(server)
			return
		}
{'fraction_non_alphanumeric': 0.16054421768707483, 'fraction_numerical': 0.014285714285714285, 'mean_word_length': 3.9131161236424394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6466500', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2136, 'n_words': 400}