<commit_before>package api

import (
	"strings"
	"testing"

	"github.com/NebulousLabs/Sia/build"
)

// TestVersion checks that /daemon/version is responding with the correct
// version.
func TestVersion(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	st, err := createServerTester("TestVersion")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()
	var dv DaemonVersion
	st.getAPI("/daemon/version", &dv)
	if dv.Version != build.Version {
		t.Fatalf("/daemon/version reporting bad version: expected %v, got %v", build.Version, dv.Version)
	}
}

// TestUpdate checks that /daemon/update correctly asserts that an update is
// not available for the daemon (since the test build is always up to date).
func TestUpdate(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	st, err := createServerTester("TestUpdate")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	var update UpdateInfo
	if err = st.getAPI("/daemon/update", &update); err != nil {
		// Notify tester that the API call failed, but allow testing to continue.
		// Otherwise you have to be online to run tests.
		if strings.HasSuffix(err.Error(), errEmptyUpdateResponse.Error()) {
			t.Skip(err)
		}
		t.Fatal(err)
	}
	if update.Available && build.Version == update.Version {
		t.Fatal("daemon should not have an update available")
	}
}

/*
// TODO: enable this test again once proper daemon shutdown is implemented (shutting down modules and listener separately).
// TestStop tests the /daemon/stop handler.
func TestStop(t *testing.T) {
	if testing.Short() {
		t.SkipNow()<commit_msg>skip TestUpdate if offline<commit_after>package api

import (
	"strings"
	"testing"

	"github.com/NebulousLabs/Sia/build"
)

// TestVersion checks that /daemon/version is responding with the correct
// version.
func TestVersion(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	st, err := createServerTester("TestVersion")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()
	var dv DaemonVersion
	st.getAPI("/daemon/version", &dv)
	if dv.Version != build.Version {
		t.Fatalf("/daemon/version reporting bad version: expected %v, got %v", build.Version, dv.Version)
	}
}

// TestUpdate checks that /daemon/update correctly asserts that an update is
// not available for the daemon (since the test build is always up to date).
func TestUpdate(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	st, err := createServerTester("TestUpdate")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	var update UpdateInfo
	if err = st.getAPI("/daemon/update", &update); err != nil {
		// Skip test if the GitHub API call fails, since this seems to be a bug
		// with Travis Darwin.
		if strings.HasSuffix(err.Error(), errEmptyUpdateResponse.Error()) {
			t.Skip(err)
		}
		// Skip test if testing machine is offline.
		if strings.HasSuffix(err.Error(), "no such host") {
			t.Skipf("skipping since testing machine appears to be offline; call to /daemon/update failed with error %v", err)
		}
		t.Fatal(err)
	}
	// The /daemon/update call should report that the version is up to date and
	// no update is available.
	if update.Version != build.Version {
		// It would be especially worrisome if the call were to report that the
		// version was not up to date but no update was available.
		if !update.Available {
			t.Fatal("the /daemon/update API call is reporting a version that is not up to date but says no update is available")
		}
		t.Fatal("the /daemon/update API call is reporting that the daemon needs an update")
	}
	// It's also particularly concerning if the version is up to date but an
	// update is somehow available.
	if update.Available {
		t.Fatal("the /daemon/update API call is reporting an up to date version but says an update is available")
	}
}

/*
// TODO: enable this test again once proper daemon shutdown is implemented (shutting down modules and listener separately).
// TestStop tests the /daemon/stop handler.
func TestStop(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
{'fraction_non_alphanumeric': 0.11719140429785108, 'fraction_numerical': 0.0, 'mean_word_length': 3.6170703575547867, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3902876', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1269, 'n_words': 454}