package subprocessenv

import (
	"testing"
)

func TestConfigureUsesExecutableDirectoryForDLLSearchAndPreload(t *testing.T) {
	var gotDir string
	var gotDLL string

	err := Configure(`D:\Program Files\Mirage\MirageGUI.exe`,
		func(dir string) error {
			gotDir = dir
			return nil
		},
		func(path string) error {
			gotDLL = path
			return nil
		},
		func(string, ...any) {},
	)
	if err != nil {
		t.Fatalf("Configure returned error: %v", err)
	}
	if gotDir != `D:\Program Files\Mirage` {
		t.Fatalf("setDir called with %q, want %q", gotDir, `D:\Program Files\Mirage`)
	}
	if gotDLL != `D:\Program Files\Mirage\wintun.dll` {
		t.Fatalf("loadDLL called with %q, want %q", gotDLL, `D:\Program Files\Mirage\wintun.dll`)
	}
}

func TestConfigureReturnsSetDirError(t *testing.T) {
	want := errString("boom")
	err := Configure(`D:\Program Files\Mirage\MirageGUI.exe`,
		func(string) error { return want },
		func(string) error { t.Fatal("loadDLL should not be called after setDir failure"); return nil },
		func(string, ...any) {},
	)
	if err == nil || err.Error() == "" {
		t.Fatal("Configure returned nil error, want failure")
	}
}

type errString string

func (e errString) Error() string { return string(e) }
