<commit_before>package server_test

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

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

var name = "va1testserver01"

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

	ms, service := mockServerAPI()
	defer ms.Close()

	resp, err := service.Get(name)

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

	resp, err := service.Get("5404cf5ece2042dc9f2ac16ab67416bb")

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

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

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

	server, err := service.Delete(name)

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

func mockServerAPI() (*httptest.Server, *server.Service) {
	mux := http.NewServeMux()
	mux.HandleFunc("/servers/test", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
			return
		}

		server := &server.Server{}
		err := json.NewDecoder(r.Body).Decode(server)
		if err != nil {
			http.Error(w, "server err", http.StatusInternalServerError)
			return
		}

		if !server.Valid() {
			http.Error(w, "bad request", http.StatusBadRequest)
			return
		}

		w.Header().Add("Content-Type", "application/json")
		fmt.Fprint(w, `{"server":"va1testserver01","isQueued":true,"links":[{"rel":"status","href":"/v2/operations/test/status/12345","id":"12345"},{"rel":"self","href":"/v2/servers/test/12345?uuid=True","id":"12345","verbs":["GET"]}]}`)
	})

	mux.HandleFunc("/servers/test/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			if 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.Query().Get("uuid") == "true" {
				server := &clc.ServerResponse{Name: "va1testserver01"}
				w.Header().Add("Content-Type", "application/json")
				json.NewEncoder(w).Encode(server)
				return
			}
		}

		if r.Method == "DELETE" {
			w.Header().Add("Content-Type", "application/json")
			fmt.Fprint(w, `{"server":"va1testserver01","isQueued":true,"links":[{"rel":"status","href":"/v2/operations/test/status/12345","id":"12345"}]}`)
			return
		}

		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
	})

	mockAPI := httptest.NewServer(mux)
	config := api.Config{
		User: api.User{
			Username: "test.user",
			Password: "<PASSWORD>",
		},
		Alias:   "test",
		BaseURL: mockAPI.URL,
	}

	client := api.New(config)
	client.Token = api.Token{Token: "validtoken"}
	return mockAPI, server.New(client)
}
<commit_msg>removed global state from test<commit_after>package server_test

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

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

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

	name := "va1testserver01"
	ms, service := mockServerAPI()
	defer ms.Close()

	resp, err := service.Get(name)

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

	resp, err := service.Get("5404cf5ece2042dc9f2ac16ab67416bb")

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

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

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

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

	ms, service := mockServerAPI()
	defer ms.Close()

	name := "va1testserver01"
	server, err := service.Delete(name)

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

func mockServerAPI() (*httptest.Server, *server.Service) {
	mux := http.NewServeMux()
	mux.HandleFunc("/servers/test", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
			return
		}

		server := &server.Server{}
		err := json.NewDecoder(r.Body).Decode(server)
		if err != nil {
			http.Error(w, "server err", http.StatusInternalServerError)
			return
		}

		if !server.Valid() {
			http.Error(w, "bad request", http.StatusBadRequest)
			return
		}

		w.Header().Add("Content-Type", "application/json")
		fmt.Fprint(w, `{"server":"va1testserver01","isQueued":true,"links":[{"rel":"status","href":"/v2/operations/test/status/12345","id":"12345"},{"rel":"self","href":"/v2/servers/test/12345?uuid=True","id":"12345","verbs":["GET"]}]}`)
	})

	mux.HandleFunc("/servers/test/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			if len(r.URL.Query()) == 0 {
				parts := strings.Split(r.RequestURI, "/")
				name := parts[len(parts)-1]

				server := &clc.ServerResponse{Name: name}
				w.Header().Add("Content-Type", "application/json")
				json.NewEncoder(w).Encode(server)
				return
			}

			if r.URL.Query().Get("uuid") == "true" {
				server := &clc.ServerResponse{Name: "va1testserver01"}
				w.Header().Add("Content-Type", "application/json")
				json.NewEncoder(w).Encode(server)
				return
			}
		}

		if r.Method == "DELETE" {
			w.Header().Add("Content-Type", "application/json")
			fmt.Fprint(w, `{"server":"va1testserver01","isQueued":true,"links":[{"rel":"status","href":"/v2/operations/test/status/12345","id":"12345"}]}`)
			return
		}

		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
	})

	mockAPI := httptest.NewServer(mux)
	config := api.Config{
		User: api.User{
			Username: "test.user",
			Password: "<PASSWORD>",
		},
		Alias:   "test",
		BaseURL: mockAPI.URL,
	}

	client := api.New(config)
	client.Token = api.Token{Token: "validtoken"}
	return mockAPI, server.New(client)
}

{'fraction_non_alphanumeric': 0.187527512839325, 'fraction_numerical': 0.021716801173881143, 'mean_word_length': 4.124812030075188, 'pattern_counts': {'":': 32, '<': 5, '<?xml version=': 0, '>': 5, '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': '1650441', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2533, 'n_words': 410}