<commit_before>package functional_godless

import (
	"fmt"
	"sync"
	"testing"

	"github.com/johnny-morrice/godless/api"
	"github.com/johnny-morrice/godless/crdt"
	"github.com/johnny-morrice/godless/internal/testutil"
	"github.com/johnny-morrice/godless/query"
)

func RunRequestResultTests(t *testing.T, client api.Client, size int) {
	commands := makeJoins(size)
	queries := makeSelectReflects(size)

	commandWg := &sync.WaitGroup{}
	for _, c := range commands {
		commandWg.Add(1)
		command := c
		go func() {
			resp, err := client.Send(command)
			testutil.AssertNil(t, err)
			testutil.Assert(t, "Unexpected empty response", !resp.IsEmpty())
			commandWg.Done()
		}()
	}

	commandWg.Wait()

	queryWg := &sync.WaitGroup{}
	for _, q := range queries {
		queryWg.Add(1)
		query := q
		go func() {
			resp, err := client.Send(query)

			testutil.AssertNil(t, err)
			testutil.Assert(t, "Unexpected empty response", !resp.IsEmpty())
			hasResult := !resp.Namespace.IsEmpty()
			hasResult = hasResult || !resp.Index.IsEmpty()
			hasResult = hasResult || !crdt.IsNilPath(resp.Path)
			queryWg.Done()
		}()
	}

	queryWg.Wait()
}

func makeJoins(size int) []api.Request {
	joins := make([]api.Request, size)

	for i := 0; i < size; i++ {
		query := genJoinQuery(i)
		joins[i] = api.MakeQueryRequest(query)
	}

	return joins
}

func makeSelectReflects(size int) []api.Request {
	selects := make([]api.Request, size)

	for i := 0; i < size; i++ {
		query := genSelectQuery(i)
		selects[i] = api.MakeQueryRequest(query)
	}

	return selects
}

func genSelectQuery(seq int) *query.Query {
	// TODO use parametrised query.. once they exist :)
	queryText := fmt.Sprintf("select factory where str_eq(foreman, \"Foreman %d\")", seq)
	return forceCompile(queryText)
}

func genJoinQuery(seq int) *query.Query {
	// TODO use parametrised query.. once they exist :)
	queryText := fmt.Sprintf("join factory rows (@key=factory%d, foreman=\"Foreman %d\")", seq, seq)
	return forceCompile(queryText)
}

func forceCompile(source string) *query.Query {
	query, err := query.Compile(source)

	if err != nil {
		panic(err)
	}

	return query
}
<commit_msg>Fix test defect<commit_after>package functional_godless

import (
	"fmt"
	"sync"
	"testing"

	"github.com/johnny-morrice/godless/api"
	"github.com/johnny-morrice/godless/crdt"
	"github.com/johnny-morrice/godless/internal/testutil"
	"github.com/johnny-morrice/godless/query"
)

func RunRequestResultTests(t *testing.T, client api.Client, size int) {
	commands := makeJoins(size)
	queries := makeSelectReflects(size)

	commandWg := &sync.WaitGroup{}
	for _, c := range commands {
		commandWg.Add(1)
		command := c
		go func() {
			resp, err := client.Send(command)
			testutil.AssertNil(t, err)
			testutil.Assert(t, "Unexpected empty response", !resp.IsEmpty())
			commandWg.Done()
		}()
	}

	commandWg.Wait()

	queryWg := &sync.WaitGroup{}
	for _, q := range queries {
		queryWg.Add(1)
		query := q
		go func() {
			resp, err := client.Send(query)

			testutil.AssertNil(t, err)
			hasResult := !resp.Namespace.IsEmpty()
			hasResult = hasResult || !resp.Index.IsEmpty()
			hasResult = hasResult || !crdt.IsNilPath(resp.Path)
			testutil.Assert(t, "Expected result", hasResult)
			queryWg.Done()
		}()
	}

	queryWg.Wait()
}

func makeJoins(size int) []api.Request {
	joins := make([]api.Request, size)

	for i := 0; i < size; i++ {
		query := genJoinQuery(i)
		joins[i] = api.MakeQueryRequest(query)
	}

	return joins
}

func makeSelectReflects(size int) []api.Request {
	selects := make([]api.Request, size)

	for i := 0; i < size; i++ {
		query := genSelectQuery(i)
		selects[i] = api.MakeQueryRequest(query)
	}

	return selects
}

func genSelectQuery(seq int) *query.Query {
	// TODO use parametrised query.. once they exist :)
	queryText := fmt.Sprintf("select factory where str_eq(foreman, \"Foreman %d\")", seq)
	return forceCompile(queryText)
}

func genJoinQuery(seq int) *query.Query {
	// TODO use parametrised query.. once they exist :)
	queryText := fmt.Sprintf("join factory rows (@key=factory%d, foreman=\"Foreman %d\")", seq, seq)
	return forceCompile(queryText)
}

func forceCompile(source string) *query.Query {
	query, err := query.Compile(source)

	if err != nil {
		panic(err)
	}

	return query
}

{'fraction_non_alphanumeric': 0.153954802259887, 'fraction_numerical': 0.0018832391713747645, 'mean_word_length': 4.125452352231604, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1645153', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1546, 'n_words': 339}