<commit_before>package autocomplete

import (
	"github.com/jideji/servicelauncher/service"
	"github.com/stretchr/testify/assert"
	"sort"
	"testing"
)

const noPrefix = ""

func TestIsAutoCompleteWhenFlagSet(t *testing.T) {
	assert.Equal(t, 1, indexOfAutocomplete("arg", "--autocomplete-options"), "index of flag")
}

func TestIsNotAutoCompleteWhenMissingFlag(t *testing.T) {
	assert.Equal(t, -1, indexOfAutocomplete("arg"))
}

func TestAutoCompleteFirstLevelWithoutPrefix(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		noPrefix)

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func TestAutoCompleteFirstLevelWithPrefix(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		"lis",
		"lis")

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func TestAutoCompleteServiceLevel(t *testing.T) {
	results := autocomplete(
		serviceLoader(srv("webserver"), srv("http-proxy")),
		2,
		noPrefix,
		"status")

	assert.Equal(t,
		[]string{"http-proxy", "webserver"},
		results,
		"expected commands")
}

func TestIgnoresEntriesAfterAutocompleteFlag(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		noPrefix,
		"--autocomplete-options", "status")

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func serviceLoaderThatShouldNotBeCalled(t *testing.T) service.Loader {
	return func() service.Services {
		t.Error("Service loader called")
		return service.Services{}
	}
}<commit_msg>Add missing test for filtering services already given for auto-completion.<commit_after>package autocomplete

import (
	"github.com/jideji/servicelauncher/service"
	"github.com/stretchr/testify/assert"
	"sort"
	"testing"
)

const noPrefix = ""

func TestIsAutoCompleteWhenFlagSet(t *testing.T) {
	assert.Equal(t, 1, indexOfAutocomplete("arg", "--autocomplete-options"), "index of flag")
}

func TestIsNotAutoCompleteWhenMissingFlag(t *testing.T) {
	assert.Equal(t, -1, indexOfAutocomplete("arg"))
}

func TestAutoCompleteFirstLevelWithoutPrefix(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		noPrefix)

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func TestAutoCompleteFirstLevelWithPrefix(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		"lis",
		"lis")

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func TestAutoCompleteServiceLevel(t *testing.T) {
	results := autocomplete(
		serviceLoader(srv("webserver"), srv("http-proxy")),
		2,
		noPrefix,
		"status")

	assert.Equal(t,
		[]string{"http-proxy", "webserver"},
		results,
		"expected commands")
}

func TestSkipsServicesAlreadyGiven(t *testing.T) {
	results := autocomplete(
		serviceLoader(srv("webserver"), srv("http-proxy")),
		2,
		noPrefix,
		"status", "webserver")

	assert.Equal(t,
		[]string{"http-proxy"},
		results,
		"expected commands")
}

func TestIgnoresEntriesAfterAutocompleteFlag(t *testing.T) {
	results := autocomplete(
		serviceLoaderThatShouldNotBeCalled(t),
		1,
		noPrefix,
		"--autocomplete-options", "status")

	sort.Strings(results)
	assert.Equal(t,
		[]string{
			"list:List available services",
			"restart:Restart services",
			"start:Start services",
			"status:Check status of services",
			"stop:Stop services"},
		results,
		"expected commands")
}

func serviceLoaderThatShouldNotBeCalled(t *testing.T) service.Loader {
	return func() service.Services {
		t.Error("Service loader called")
		return service.Services{}
	}
}
{'fraction_non_alphanumeric': 0.13870895689077506, 'fraction_numerical': 0.002903730176457449, 'mean_word_length': 5.245467224546722, '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': '3497057', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1478, 'n_words': 299}