package nix_test

import (
	"errors"
	"os"
	"os/exec"
	"slices"
	"syscall"
	"testing"

	"gensokyo.uk/nix"
)

func TestNixWriteStdin(t *testing.T) {
	t.Run("already set", func(t *testing.T) {
		ctx := nix.New(t.Context(), nil, os.Stdout, os.Stderr)
		cmd := exec.CommandContext(t.Context(), "/proc/nonexistent")
		cmd.Stdin = os.Stdin
		if _, err := ctx.WriteStdin(cmd, nil, nil); err == nil {
			t.Fatal("WriteStdinCommand unexpectedly succeeded")
		}
	})

	t.Run("f returns error", func(t *testing.T) {
		stubNixCommand(t)
		ctx := newStubContext(t.Context(), nil, os.Stdout, os.Stderr)
		cmd := ctx.Nix(t.Context(), "true")
		if _, err := ctx.WriteStdin(
			cmd,
			slices.Values(make([]string, 0)),
			func() error { return syscall.ENOSYS },
		); !errors.Is(err, syscall.ENOSYS) {
			t.Fatalf("WriteStdinCommand: error = %v, want %v", err, syscall.ENOSYS)
		}
	})
}
