package params_test

import (
	"encoding/gob"
	"errors"
	"os"
	"slices"
	"strconv"
	"syscall"
	"testing"

	"hakurei.app/internal/params"
)

func TestSetupReceive(t *testing.T) {
	t.Run("not set", func(t *testing.T) {
		const key = "TEST_ENV_NOT_SET"
		{
			v, ok := os.LookupEnv(key)
			t.Cleanup(func() {
				if ok {
					if err := os.Setenv(key, v); err != nil {
						t.Fatalf("Setenv: error = %v", err)
					}
				} else {
					if err := os.Unsetenv(key); err != nil {
						t.Fatalf("Unsetenv: error = %v", err)
					}
				}
			})
		}

		if _, err := params.Receive(key, nil, nil); !errors.Is(err, params.ErrReceiveEnv) {
			t.Errorf("Receive: error = %v, want %v", err, params.ErrReceiveEnv)
		}
	})

	t.Run("format", func(t *testing.T) {
		const key = "TEST_ENV_FORMAT"
		t.Setenv(key, "")

		if _, err := params.Receive(key, nil, nil); !errors.Is(err, strconv.ErrSyntax) {
			t.Errorf("Receive: error = %v, want %v", err, strconv.ErrSyntax)
		}
	})

	t.Run("range", func(t *testing.T) {
		const key = "TEST_ENV_RANGE"
		t.Setenv(key, "-1")

		if _, err := params.Receive(key, nil, nil); !errors.Is(err, syscall.EDOM) {
			t.Errorf("Receive: error = %v, want %v", err, syscall.EDOM)
		}
	})

	t.Run("setup receive", func(t *testing.T) {
		check := func(t *testing.T, useNilFdp bool) {
			const key = "TEST_SETUP_RECEIVE"
			payload := []uint64{syscall.MS_MGC_VAL, syscall.MS_MGC_MSK, syscall.MS_ASYNC, syscall.MS_ACTIVE}

			encoderDone := make(chan error, 1)
			extraFiles := make([]*os.File, 0, 1)
			if r, w, err := os.Pipe(); err != nil {
				t.Fatalf("Setup: error = %v", err)
			} else {
				t.Cleanup(func() {
					if err = errors.Join(r.Close(), w.Close()); err != nil {
						t.Fatal(err)
					}
				})

				extraFiles = append(extraFiles, r)
				if deadline, ok := t.Deadline(); ok {
					if err = w.SetDeadline(deadline); err != nil {
						t.Fatal(err)
					}
				}
				go func() { encoderDone <- gob.NewEncoder(w).Encode(payload) }()
			}

			if len(extraFiles) != 1 {
				t.Fatalf("extraFiles: len = %v, want 1", len(extraFiles))
			}

			var dupFd int
			if fd, err := syscall.Dup(int(extraFiles[0].Fd())); err != nil {
				t.Fatalf("Dup: error = %v", err)
			} else {
				syscall.CloseOnExec(fd)
				dupFd = fd
				t.Setenv(key, strconv.Itoa(fd))
			}

			var (
				gotPayload []uint64
				fdp        *int
			)
			if !useNilFdp {
				fdp = new(int)
			}
			var closeFile func() error
			if f, err := params.Receive(key, &gotPayload, fdp); err != nil {
				t.Fatalf("Receive: error = %v", err)
			} else {
				closeFile = f

				if !slices.Equal(payload, gotPayload) {
					t.Errorf("Receive: %#v, want %#v", gotPayload, payload)
				}
			}
			if !useNilFdp {
				if *fdp != dupFd {
					t.Errorf("Fd: %d, want %d", *fdp, dupFd)
				}
			}

			if err := <-encoderDone; err != nil {
				t.Errorf("Encode: error = %v", err)
			}

			if closeFile != nil {
				if err := closeFile(); err != nil {
					t.Errorf("Close: error = %v", err)
				}
			}
		}

		t.Run("fp", func(t *testing.T) { check(t, false) })
		t.Run("nil", func(t *testing.T) { check(t, true) })
	})
}
