package container

import (
	"os"
	"testing"

	"hakurei.app/check"
	"hakurei.app/internal/stub"
	"hakurei.app/message"
)

func TestEarlyTerminationError(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name string
		err  error
		want string
		msg  string
	}{
		{"exited", &earlyTerminationError{
			`daemon providing "/run/user/1971/pulse/native"`, 127 << 8,
		}, "exit status 127", `daemon providing "/run/user/1971/pulse/native" exit status 127`},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			if got := tc.err.Error(); got != tc.want {
				t.Errorf("Error: %q, want %q", got, tc.want)
			}
			if got := tc.err.(message.Error).Message(); got != tc.msg {
				t.Errorf("Message: %s, want %s", got, tc.msg)
			}
		})
	}
}

func TestDaemonOp(t *testing.T) {
	t.Parallel()

	checkSimple(t, "DaemonOp.late", []simpleTestCase{
		{"success", func(k *kstub) error {
			state := setupState{Params: &Params{Env: []string{"\x00"}}, Context: t.Context(), Msg: k}
			return (&DaemonOp{
				Target: check.MustAbs("/run/user/1971/pulse/native"),
				Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
				Args:   []string{"-v"},
			}).late(&state, k)
		}, stub.Expect{Calls: []stub.Call{
			call("isVerbose", stub.ExpectArgs{}, true, nil),
			call("verbosef", stub.ExpectArgs{"starting %s", []any{`daemon providing "/run/user/1971/pulse/native"`}}, nil, nil),
			call("start", stub.ExpectArgs{"/run/current-system/sw/bin/pipewire-pulse", []string{"/run/current-system/sw/bin/pipewire-pulse", "-v"}, []string{"\x00"}, "/"}, &os.Process{Pid: 0xcafe}, nil),
			call("stat", stub.ExpectArgs{"/run/user/1971/pulse/native"}, isDirFi(false), os.ErrNotExist),
			call("stat", stub.ExpectArgs{"/run/user/1971/pulse/native"}, isDirFi(false), os.ErrNotExist),
			call("stat", stub.ExpectArgs{"/run/user/1971/pulse/native"}, isDirFi(false), os.ErrNotExist),
			call("stat", stub.ExpectArgs{"/run/user/1971/pulse/native"}, isDirFi(false), nil),
			call("verbosef", stub.ExpectArgs{"daemon process %d ready", []any{0xcafe}}, nil, nil),
		}}, nil},
	})

	checkOpsValid(t, []opValidTestCase{
		{"nil", (*DaemonOp)(nil), false},
		{"zero", new(DaemonOp), false},
		{"valid", &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
			Args:   []string{"-v"},
		}, true},
	})

	checkOpsBuilder(t, []opsBuilderTestCase{
		{"pipewire-pulse", new(Ops).Daemon(
			check.MustAbs("/run/user/1971/pulse/native"),
			check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"), "-v",
		), Ops{
			&DaemonOp{
				Target: check.MustAbs("/run/user/1971/pulse/native"),
				Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
				Args:   []string{"-v"},
			},
		}},
	})

	checkOpIs(t, []opIsTestCase{
		{"zero", new(DaemonOp), new(DaemonOp), false},

		{"args differs", &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
			Args:   []string{"-v"},
		}, &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, false},

		{"path differs", &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire"),
		}, &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, false},

		{"target differs", &DaemonOp{
			Target: check.MustAbs("/run/user/65534/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, false},

		{"equals", &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
			Path:   check.MustAbs("/run/current-system/sw/bin/pipewire-pulse"),
		}, true},
	})

	checkOpMeta(t, []opMetaTestCase{
		{"pipewire-pulse", &DaemonOp{
			Target: check.MustAbs("/run/user/1971/pulse/native"),
		}, zeroString, `daemon providing "/run/user/1971/pulse/native"`},
	})
}
