<commit_before>package daemon

import (
	"io"
	"syscall"
	"testing"

	"src.elv.sh/pkg/daemon/client"
	"src.elv.sh/pkg/daemon/daemondefs"
	"src.elv.sh/pkg/daemon/internal/api"
	. "src.elv.sh/pkg/prog/progtest"
	"src.elv.sh/pkg/store/storetest"
	"src.elv.sh/pkg/testutil"
)

func TestProgram_ServesClientRequests(t *testing.T) {
	testutil.Umask(t, 0)
	testutil.InTempDir(t)

	// Set up server.
	serverDone := make(chan struct{})
	go func() {
		exit, _, stderr := Run(Program, "elvish", "-daemon", "-sock", "sock", "-db", "db")
		if exit != 0 {
			t.Logf("daemon exited with %v; stderr:\n%v", exit, stderr)
		}
		close(serverDone)
	}()
	defer func() { <-serverDone }()

	// Set up client.
	client, err := client.Activate(io.Discard,
		&daemondefs.SpawnConfig{SockPath: "sock", DbPath: "db", RunDir: "."})
	if err != nil {
		t.Fatal("failed to activate client: ", err)
	}
	defer client.Close()

	// Test server state requests.
	gotVersion, err := client.Version()
	if gotVersion != api.Version || err != nil {
		t.Errorf(".Version() -> (%v, %v), want (%v, nil)", gotVersion, err, api.Version)
	}

	gotPid, err := client.Pid()
	wantPid := syscall.Getpid()
	if gotPid != wantPid || err != nil {
		t.Errorf(".Pid() -> (%v, %v), want (%v, nil)", gotPid, err, wantPid)
	}

	// Test store requests.
	storetest.TestCmd(t, client)
	storetest.TestDir(t, client)
	storetest.TestSharedVar(t, client)
}

func TestProgram_BadCLI(t *testing.T) {
	Test(t, Program,
		ThatElvish().
			ExitsWith(2).
			WritesStderr("internal error: no suitable subprogram\n"),

		ThatElvish("-daemon", "x").
			ExitsWith(2).
			WritesStderrContaining("arguments are not allowed with -daemon"),
	)
}
<commit_msg>pkg/daemon: Terminate test correctly if server did not come up.<commit_after>package daemon

import (
	"io"
	"syscall"
	"testing"

	"src.elv.sh/pkg/daemon/client"
	"src.elv.sh/pkg/daemon/daemondefs"
	"src.elv.sh/pkg/daemon/internal/api"
	. "src.elv.sh/pkg/prog/progtest"
	"src.elv.sh/pkg/store/storetest"
	"src.elv.sh/pkg/testutil"
)

func TestProgram_ServesClientRequests(t *testing.T) {
	testutil.Umask(t, 0)
	testutil.InTempDir(t)

	// Set up server.
	serverDone := make(chan struct{})
	go func() {
		exit, _, stderr := Run(Program, "elvish", "-daemon", "-sock", "sock", "-db", "db")
		if exit != 0 {
			t.Logf("daemon exited with %v; stderr:\n%v", exit, stderr)
		}
		close(serverDone)
	}()
	defer func() { <-serverDone }()

	// Set up client.
	client, err := client.Activate(io.Discard,
		&daemondefs.SpawnConfig{SockPath: "sock", DbPath: "db", RunDir: "."})
	if err != nil {
		close(serverDone)
		t.Fatal("failed to activate client: ", err)
	}
	defer client.Close()

	// Test server state requests.
	gotVersion, err := client.Version()
	if gotVersion != api.Version || err != nil {
		t.Errorf(".Version() -> (%v, %v), want (%v, nil)", gotVersion, err, api.Version)
	}

	gotPid, err := client.Pid()
	wantPid := syscall.Getpid()
	if gotPid != wantPid || err != nil {
		t.Errorf(".Pid() -> (%v, %v), want (%v, nil)", gotPid, err, wantPid)
	}

	// Test store requests.
	storetest.TestCmd(t, client)
	storetest.TestDir(t, client)
	storetest.TestSharedVar(t, client)
}

func TestProgram_BadCLI(t *testing.T) {
	Test(t, Program,
		ThatElvish().
			ExitsWith(2).
			WritesStderr("internal error: no suitable subprogram\n"),

		ThatElvish("-daemon", "x").
			ExitsWith(2).
			WritesStderrContaining("arguments are not allowed with -daemon"),
	)
}

{'fraction_non_alphanumeric': 0.1863662960795787, 'fraction_numerical': 0.0023405500292568754, 'mean_word_length': 3.877318116975749, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1972139', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1355, 'n_words': 290}