package main

import (
	"net"
	"os"
	"testing"

	"hakurei.app/internal/rosa"
)

func TestMain(m *testing.M) {
	rosa.Native().DropCaches("", rosa.OptLLVMNoLTO)
	os.Exit(m.Run())
}

func TestCureAll(t *testing.T) {
	t.Parallel()
	const env = "ROSA_TEST_DAEMON"

	if !testing.Verbose() {
		t.Skip("verbose flag not set")
	}

	pathname, ok := os.LookupEnv(env)
	if !ok {
		t.Skip(env + " not set")
	}

	addr := net.UnixAddr{Net: "unix", Name: pathname}
	t.Cleanup(func() {
		if t.Failed() {
			if err := abortRemote(t.Context(), &addr, false); err != nil {
				t.Fatal(err)
			}
		}
	})

	for _, handle := range rosa.Native().Collect() {
		_, a := rosa.Native().Std().MustLoad(handle)
		t.Run(handle.String(), func(t *testing.T) {
			_, err := cureRemote(t.Context(), &addr, a, 0)
			if err != nil {
				t.Error(err)
			}
		})
	}
}
