package restic

import (
	"path/filepath"
	"testing"
)

func TestEnvAtLeastVersion(t *testing.T) {
	t.Parallel()
	cases := []struct {
		ver       string
		major     int
		minor     int
		want      bool
		shortDesc string
	}{
		{"0.17.0", 0, 17, true, "exact match"},
		{"0.17.1", 0, 17, true, "patch above"},
		{"0.18.0", 0, 17, true, "minor above"},
		{"1.0.0", 0, 17, true, "major above"},
		{"0.16.4", 0, 17, false, "minor below"},
		{"0.16", 0, 17, false, "two-part minor below"},
		{"", 0, 17, false, "empty"},
		{"v0.17", 0, 17, false, "prefixed v rejected"},
		{"unknown", 0, 17, false, "non-numeric rejected"},
	}
	for _, c := range cases {
		got := Env{Version: c.ver}.AtLeastVersion(c.major, c.minor)
		if got != c.want {
			t.Errorf("AtLeastVersion(%q, %d, %d): got %v want %v · %s",
				c.ver, c.major, c.minor, got, c.want, c.shortDesc)
		}
	}
}

func TestExpandHome(t *testing.T) {
	// Not parallel — t.Setenv on HOME would race with sibling tests.
	tmp := t.TempDir()
	t.Setenv("HOME", tmp)

	cases := []struct {
		in, want string
	}{
		{"$HOME/rm-restore/job-1/", filepath.Join(tmp, "rm-restore/job-1")},
		{"${HOME}/rm-restore/job-2/", filepath.Join(tmp, "rm-restore/job-2")},
		{"~/rm-restore/job-3/", filepath.Join(tmp, "rm-restore/job-3")},
		{"$HOME", tmp},
		{"~", tmp},
		{"/var/lib/x/y", "/var/lib/x/y"}, // absolute path passes through
		{"", ""},
		{"$PATH/foo", "$PATH/foo"}, // other env vars not expanded
	}
	for _, c := range cases {
		got := expandHome(c.in)
		if got != c.want {
			t.Errorf("expandHome(%q): got %q want %q", c.in, got, c.want)
		}
	}

	// Sanity: an absolute path always passes through regardless of HOME.
	if got := expandHome("/abs"); got != "/abs" {
		t.Errorf("expandHome(/abs): got %q", got)
	}
}
