package remote
import (
"os"
"path/filepath"
"slices"
"testing"
)
func withHostMap(t *testing.T, content string) {
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
t.Setenv("HOME", dir)
if content == "" {
return
}
// not the xdg layout: macos keeps its config under ~/Library
p := hostMapPath()
if p == "" {
t.Fatal("no config dir")
}
if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0600); err != nil {
t.Fatal(err)
}
}
func TestHostMemory(t *testing.T) {
withHostMap(t, "")
for _, dest := range []string{"root@caladon", "dawn@caladon", "dawn@caladon", "caladon", "root@caladon"} {
RememberHost(dest)
}
if got, want := KnownDests("caladon"), []string{"caladon", "root@caladon", "dawn@caladon"}; !slices.Equal(got, want) {
t.Fatalf("KnownDests = %v, want %v", got, want)
}
if hostDest("caladon") != "root@caladon" || !slices.Equal(ProbeDests("caladon"), []string{"root@caladon", "dawn@caladon"}) {
t.Fatal("remembered destination ordering broken")
}
if hostDest("unknown") != "unknown" || !slices.Equal(ProbeDests("unknown"), []string{"unknown"}) {
t.Fatal("unknown host should stay bare")
}
}
func TestHostMemoryBackCompat(t *testing.T) {
for _, tc := range []struct {
data, host string
want []string
}{
{`{"caladon":"old@caladon"}`, "caladon", []string{"caladon", "old@caladon"}},
{`{"volta":["a@volta","b@volta"]}`, "volta", []string{"volta", "a@volta", "b@volta"}},
} {
withHostMap(t, tc.data)
if got := KnownDests(tc.host); !slices.Equal(got, tc.want) {
t.Fatalf("KnownDests = %v, want %v", got, tc.want)
}
}
}