2026-05-09 17:20:32 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetDefaultName(t *testing.T) {
|
|
|
|
|
// Test from env
|
|
|
|
|
os.Setenv("DISCOVER_APP", "test-app")
|
|
|
|
|
if name := GetDefaultName(); name != "test-app" {
|
|
|
|
|
t.Errorf("Expected test-app, got %s", name)
|
|
|
|
|
}
|
|
|
|
|
os.Unsetenv("DISCOVER_APP")
|
|
|
|
|
|
|
|
|
|
// Test from build info or args
|
|
|
|
|
name := GetDefaultName()
|
|
|
|
|
if name == "" {
|
|
|
|
|
t.Error("Expected non-empty name")
|
|
|
|
|
}
|
|
|
|
|
t.Logf("Detected name: %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetServerIp(t *testing.T) {
|
|
|
|
|
ip := GetServerIp()
|
|
|
|
|
if ip == "" || ip == "0.0.0.0" {
|
|
|
|
|
t.Errorf("Invalid IP detected: %s", ip)
|
|
|
|
|
}
|
|
|
|
|
t.Logf("Detected IP: %s", ip)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSmartStartup(t *testing.T) {
|
|
|
|
|
// Reset config
|
|
|
|
|
Config.Listen = ""
|
2026-05-09 22:51:14 +08:00
|
|
|
Config.App = "smart-test"
|
2026-05-09 17:20:32 +08:00
|
|
|
|
|
|
|
|
as := AsyncStart()
|
|
|
|
|
if as.Addr == "" {
|
|
|
|
|
t.Fatal("Server address should not be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Logf("Server started on %s", as.Addr)
|
|
|
|
|
|
|
|
|
|
if !as.useDiscover {
|
|
|
|
|
t.Error("Should have enabled discover")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
as.Stop()
|
|
|
|
|
}
|