<commit_before>package main

import (
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

const HOST = "http://localhost:8765/"

func performCall(method string, url string) (string, error) {
	var res *http.Response
	var err error
	if method == "GET" {
		res, err = http.Get(url)
	} else if method == "POST" {
		res, err = http.Post(url, "text/plain", nil) //todo:
	} else if method == "PUT" {
		client := &http.Client{}
		request, err1 := http.NewRequest("PUT", url, nil)
		if err1 != nil {
			return "", err1
		}
		res, err = client.Do(request)
	}

	if err != nil {
		return "", err
	}

	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return "", err
	}
	found := string(body[:])
	return found, nil
}

func checkResult(method, url, expected string, t *testing.T) {
	found, err := performCall(method, url)
	if err != nil {
		t.Fatalf("Unexpected error found - %s", err.Error())
	}

	if found != expected {
		t.Errorf("Expected\n---\n%s\n---\nbut found\n---\n%s\n---\n", expected, found)
	}
}

func TestGetAlive(t *testing.T) {
	expected := "I'm alive"
	checkResult("GET", HOST, expected, t)
}

func TestPlay(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds/beep9.mp3")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayDir(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\",\"test_sounds/beep28.mp3\",\"test_sounds/beep36.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayPlaylist(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("sample_playlist")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\",\"test_sounds/beep28.mp3\",\"test_sounds/beep36.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayNonExistingFile(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds/beep1.mp3")
	expected := "{\"Code\":4,\"Message\":\"File cannot be found\"}"
	checkResult("PUT", url, expected, t)
}

func TestPlayInvalidFileFormat(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_broken/abc.txt")
	expected := "{\"Code\":7,\"Message\":\"Format is not supported\"}"
	checkResult("PUT", url, expected, t)
}

func TestPlayBrokenFile(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_broken/abc.txt")
	expected := "{\"Code\":2,\"Message\":\"SoX failed to open input file\"}"
	checkResult("PUT", url, expected, t)
}

func TestPause(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)
	url := HOST + "pause"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Song is paused\",\"Data\":[\"test_sounds/beep28.mp3\"]}"

	checkResult("POST", url, expected, t)
}

func TestPauseNoPlayback(t *testing.T) {
	url := HOST + "pause"
	expected := "{\"Code\":2,\"Message\":\"Cannot pause. No song is playing\"}"
	checkResult("POST", url, expected, t)
}

func TestResume(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)
	pause_url := HOST + "pause"
	performCall("PUT", pause_url)

	url := HOST + "resume"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Song is resumed\",\"Data\":[\"test_sounds/beep28.mp3\"]}"
	checkResult("POST", url, expected, t)

}

func TestResumeNoPlayback(t *testing.T) {
	url := HOST + "resume"
	expected := "{\"Code\":9,\"Message\":\"Cannot resume. No song was paused\"}"
	checkResult("POST", url, expected, t)
}

func TestResumeNoPaused(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)

	url := HOST + "resume"
	expected := "{\"Code\":9,\"Message\":\"Cannot resume. No song was paused\"}"
	checkResult("POST", url, expected, t)
}<commit_msg>add tests for stop<commit_after>package main

import (
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

const HOST = "http://localhost:8765/"

func performCall(method string, url string) (string, error) {
	var res *http.Response
	var err error
	if method == "GET" {
		res, err = http.Get(url)
	} else if method == "POST" {
		res, err = http.Post(url, "text/plain", nil) //todo:
	} else if method == "PUT" {
		client := &http.Client{}
		request, err1 := http.NewRequest("PUT", url, nil)
		if err1 != nil {
			return "", err1
		}
		res, err = client.Do(request)
	}

	if err != nil {
		return "", err
	}

	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return "", err
	}
	found := string(body[:])
	return found, nil
}

func checkResult(method, url, expected string, t *testing.T) {
	found, err := performCall(method, url)
	if err != nil {
		t.Fatalf("Unexpected error found - %s", err.Error())
	}

	if found != expected {
		t.Errorf("Expected\n---\n%s\n---\nbut found\n---\n%s\n---\n", expected, found)
	}
}

func TestGetAlive(t *testing.T) {
	expected := "I'm alive"
	checkResult("GET", HOST, expected, t)
}

func TestPlay(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds/beep9.mp3")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayDir(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\",\"test_sounds/beep28.mp3\",\"test_sounds/beep36.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayPlaylist(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("sample_playlist")
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Started playing\",\"Data\":[\"test_sounds/beep9.mp3\",\"test_sounds/beep28.mp3\",\"test_sounds/beep36.mp3\"]}"
	checkResult("PUT", url, expected, t)
}

func TestPlayNonExistingFile(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_sounds/beep1.mp3")
	expected := "{\"Code\":4,\"Message\":\"File cannot be found\"}"
	checkResult("PUT", url, expected, t)
}

func TestPlayInvalidFileFormat(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_broken/abc.txt")
	expected := "{\"Code\":7,\"Message\":\"Format is not supported\"}"
	checkResult("PUT", url, expected, t)
}

func TestPlayBrokenFile(t *testing.T) {
	url := HOST + "play/" + url.QueryEscape("test_broken/abc.txt")
	expected := "{\"Code\":2,\"Message\":\"SoX failed to open input file\"}"
	checkResult("PUT", url, expected, t)
}

func TestPause(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)
	url := HOST + "pause"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Song is paused\",\"Data\":[\"test_sounds/beep28.mp3\"]}"

	checkResult("POST", url, expected, t)
}

func TestPauseNoPlayback(t *testing.T) {
	url := HOST + "pause"
	expected := "{\"Code\":2,\"Message\":\"Cannot pause. No song is playing\"}"
	checkResult("POST", url, expected, t)
}

func TestResume(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)
	pause_url := HOST + "pause"
	performCall("PUT", pause_url)

	url := HOST + "resume"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Song is resumed\",\"Data\":[\"test_sounds/beep28.mp3\"]}"
	checkResult("POST", url, expected, t)

}

func TestResumeNoPlayback(t *testing.T) {
	url := HOST + "resume"
	expected := "{\"Code\":9,\"Message\":\"Cannot resume. No song was paused\"}"
	checkResult("POST", url, expected, t)
}

func TestResumeNoPaused(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)

	url := HOST + "resume"
	expected := "{\"Code\":9,\"Message\":\"Cannot resume. No song was paused\"}"
	checkResult("POST", url, expected, t)
}

func TestStop(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)

	url := HOST + "stop"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Playback is stopped and cleaned\",\"Data\":[]}"
	checkResult("PUT", url, expected, t)
}

func TestStopNoPlayback(t *testing.T) {
	url := HOST + "stop"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Playback is stopped and cleaned\",\"Data\":[]}"
	checkResult("PUT", url, expected, t)
}

func TestStopPaused(t *testing.T) {
	play_url := HOST + "play/" + url.QueryEscape("test_sounds/beep28.mp3")
	performCall("PUT", play_url)
	pause_url := HOST + "pause"
	performCall("PUT", pause_url)

	url := HOST + "stop"
	expected := "{\"Code\":0,\"Message\":\"Success\",\"Info\":\"Playback is stopped and cleaned\",\"Data\":[]}"
	checkResult("PUT", url, expected, t)
}
{'fraction_non_alphanumeric': 0.2286965308184574, 'fraction_numerical': 0.013360278432693388, 'mean_word_length': 4.299226650803093, 'pattern_counts': {'":': 76, '<': 3, '<?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': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3357288', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3413, 'n_words': 684}