// Copyright (C) 2026 Dcntrlized // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package instance import ( "os" "path/filepath" "testing" ) func TestLoadPrefersFileOverEnv(t *testing.T) { dir := t.TempDir() if err := Save(dir, Identity{Name: "home-node", Title: "Home"}); err != nil { t.Fatal(err) } t.Setenv("DCNTRLIZED_INSTANCE_NAME", "from-env") id := Load(dir) if id.Name != "home-node" || id.Title != "Home" { t.Fatalf("got %+v", id) } } func TestDefaultIdentityFallbacks(t *testing.T) { orig := osHostname defer func() { osHostname = orig }() t.Run("env wins over domain", func(t *testing.T) { t.Setenv("DCNTRLIZED_INSTANCE_NAME", "edge-1") t.Setenv("DCNTRLIZED_INSTANCE_DOMAIN", "maestro.dcntrlized.org") if id := defaultIdentity(); id.Name != "edge-1" { t.Fatalf("got %q", id.Name) } }) t.Run("announced domain used before hostname", func(t *testing.T) { t.Setenv("DCNTRLIZED_INSTANCE_DOMAIN", "maestro.dcntrlized.org") osHostname = func() (string, error) { return "archlinux", nil } if id := defaultIdentity(); id.Name != "maestro.dcntrlized.org" { t.Fatalf("got %q", id.Name) } }) t.Run("hostname used when human-readable", func(t *testing.T) { os.Unsetenv("DCNTRLIZED_INSTANCE_NAME") os.Unsetenv("DCNTRLIZED_INSTANCE_DOMAIN") osHostname = func() (string, error) { return "archlinux", nil } if id := defaultIdentity(); id.Name != "archlinux" { t.Fatalf("got %q", id.Name) } }) t.Run("container-id hostname replaced", func(t *testing.T) { os.Unsetenv("DCNTRLIZED_INSTANCE_NAME") os.Unsetenv("DCNTRLIZED_INSTANCE_DOMAIN") for _, host := range []string{"6079761d6e4d", "6079761d6e4dabcdef1234567890abcdef1234567890abcdef1234567890abcd"} { osHostname = func() (string, error) { return host, nil } if id := defaultIdentity(); id.Name != "Maestro" { t.Fatalf("host %q: got %q", host, id.Name) } } }) } func TestLooksLikeContainerID(t *testing.T) { cases := map[string]bool{ "6079761d6e4d": true, "ABCDEF0123456789abcdef0123456789": true, "archlinux": false, "maestro-1": false, "abc": false, "": false, } for s, want := range cases { if got := looksLikeContainerID(s); got != want { t.Errorf("looksLikeContainerID(%q) = %v, want %v", s, got, want) } } } func TestSaveRejectsEmptyName(t *testing.T) { dir := t.TempDir() if err := Save(dir, Identity{}); err == nil { t.Fatal("expected error for empty name") } if _, err := os.Stat(filepath.Join(dir, "instance.json")); !os.IsNotExist(err) { t.Fatal("no file should be written for an invalid identity") } }