<commit_before>package simulation

import (
	"fmt"
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"nimona.io/internal/rand"
	"nimona.io/pkg/simulation/node"
)

func TestSimulation(t *testing.T) {
	// Setup
	env, err := node.NewEnvironment()
	require.NoError(t, err)
	require.NotNil(t, env)

	dockerImage := "docker.io/nimona/nimona:v0.5.0-alpha"
	if img := os.Getenv("E2E_DOCKER_IMAGE"); img != "" {
		dockerImage = img
	}

	// Setup Nodes
	nodes, err := node.New(
		dockerImage,
		env,
		node.WithName("nimona-e2e-"+rand.String(8)),
		node.WithNodePort(8000),
		node.WithCount(5),
	)
	require.NoError(t, err)
	require.NotNil(t, nodes)

	done := make(chan bool)

	for _, nd := range nodes {
		loch, errCh := nd.Logs()
		lineCounter := 0

		go func() {
			for {
				select {
				case ll := <-loch:
					fmt.Println(ll)
					assert.NotEmpty(t, ll)
					lineCounter++
				case <-time.After(1 * time.Second):
					done <- true
					return
				}
			}
		}()

		<-done
		assert.Empty(t, errCh)
		assert.NotZero(t, lineCounter)
	}

	// Teardown
	err = node.Stop(nodes)
	require.NoError(t, err)
	err = env.Stop()
	require.NoError(t, err)
}
<commit_msg>chore(simulation): add e2e test that checks peers receiving object<commit_after>package simulation

import (
	"fmt"
	"os"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"nimona.io/internal/rand"
	"nimona.io/pkg/client"
	"nimona.io/pkg/simulation/node"
	"nimona.io/pkg/stream"
)

func TestSimulation(t *testing.T) {
	// Setup
	env, err := node.NewEnvironment()
	require.NoError(t, err)
	require.NotNil(t, env)

	dockerImage := "nimona:dev"
	if img := os.Getenv("E2E_DOCKER_IMAGE"); img != "" {
		dockerImage = img
	}

	// Setup Nodes
	nodes, err := node.New(
		dockerImage,
		env,
		node.WithName("nimona-e2e-"+rand.String(8)),
		node.WithNodePort(28000),
		node.WithCount(3),
		node.WithEnv([]string{
			"BIND_PRIVATE=true",
			"DEBUG_BLOCKS=true",
		}),
	)
	require.NoError(t, err)
	require.NotNil(t, nodes)

	// create clients for all nodes
	clients := make([]*client.Client, len(nodes))
	for i, node := range nodes {
		baseURL := fmt.Sprintf("http://%s", node.Address())
		c, err := client.New(baseURL)
		require.NoError(t, err)
		clients[i] = c
	}

	// gather recipients to send the obj to
	recipients := []string{}
	// skip first client as it's the one we'll be sending from
	for _, c := range clients[1:] {
		info, err := c.Info()
		assert.NoError(t, err)
		recipients = append(recipients, info.Fingerprint)
	}

	// create an obj, and attach recipients to policy
	nonce := rand.String(24)
	streamCreated := stream.Created{
		Nonce: nonce,
		Policies: []*stream.Policy{
			&stream.Policy{
				Subjects:  recipients,
				Resources: []string{"*"},
				Action:    "allow",
			},
		},
	}

	// wait for the network to settle
	// time.Sleep(time.Second * 10)

	err = clients[0].PostObject(streamCreated.ToObject())
	assert.NoError(t, err)

	wg := &sync.WaitGroup{}
	wg.Add(len(recipients))

	for _, nd := range nodes[1:] {
		go func(nd *node.Node) {
			loch, _ := nd.Logs()
			for {
				select {
				case ll := <-loch:
					fmt.Println(ll)
					if strings.Contains(ll, nonce) {
						fmt.Println("Node received object")
						wg.Done()
						return
					}
				case <-time.After(time.Second * 20):
					t.Log("node didn't get obj in time")
					t.Fail()
					wg.Done()
					return
				}
			}
		}(nd)
	}

	wg.Wait()

	err = node.Stop(nodes)
	assert.NoError(t, err)

	err = env.Stop()
	assert.NoError(t, err)
}

{'fraction_non_alphanumeric': 0.16596814752724226, 'fraction_numerical': 0.008941044984632579, 'mean_word_length': 3.196951934349355, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3725619', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1407, 'n_words': 284}