Files
MonocoEditor-With-Lsp-Backend/backend/cmd/server/main_test.go
meowrain 3284ce07c7 feat: enhance API and session management with Nacos and Redis integration
- Add Nacos registry for service registration and deregistration.
- Implement Redis registry for session management with heartbeat and session claiming.
- Improve completion service with session handling and request validation.
- Enhance WebSocket handling for completion requests with JSON-RPC support.
- Add tests for new registry implementations and completion manager functionalities.
- Refactor existing code for better readability and maintainability.
2026-02-15 17:46:34 +08:00

104 lines
2.9 KiB
Go

package main
import (
"os"
"testing"
)
func TestLoadConfigNacosDefaults(t *testing.T) {
withEnv(t, "ENABLE_NACOS_REGISTER", "")
withEnv(t, "NACOS_SERVER_ADDR", "")
withEnv(t, "NACOS_NAMESPACE", "")
withEnv(t, "NACOS_GROUP", "")
withEnv(t, "NACOS_SERVICE_NAME", "")
withEnv(t, "NACOS_CLUSTER_NAME", "")
withEnv(t, "NACOS_USERNAME", "")
withEnv(t, "NACOS_PASSWORD", "")
withEnv(t, "NACOS_IP", "")
withEnv(t, "NACOS_PORT", "")
withEnv(t, "PORT", "")
withEnv(t, "INSTANCE_URL", "")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("loadConfig() error = %v", err)
}
if cfg.EnableNacosRegister {
t.Fatalf("expected EnableNacosRegister default false")
}
if cfg.NacosServerAddr != "10.0.0.10:8848" {
t.Fatalf("expected default NacosServerAddr 10.0.0.10:8848, got %q", cfg.NacosServerAddr)
}
if cfg.NacosGroup != "DEFAULT_GROUP" {
t.Fatalf("expected default NacosGroup DEFAULT_GROUP, got %q", cfg.NacosGroup)
}
if cfg.NacosServiceName != "lsp-gateway" {
t.Fatalf("expected default NacosServiceName lsp-gateway, got %q", cfg.NacosServiceName)
}
if cfg.NacosRegisterPort != 8080 {
t.Fatalf("expected default NacosRegisterPort 8080, got %d", cfg.NacosRegisterPort)
}
}
func TestLoadConfigNacosFromEnv(t *testing.T) {
withEnv(t, "ENABLE_NACOS_REGISTER", "true")
withEnv(t, "NACOS_SERVER_ADDR", "10.0.0.10:8848")
withEnv(t, "NACOS_NAMESPACE", "prod-ns")
withEnv(t, "NACOS_GROUP", "editor")
withEnv(t, "NACOS_SERVICE_NAME", "editor-lsp")
withEnv(t, "NACOS_CLUSTER_NAME", "hz-a")
withEnv(t, "NACOS_USERNAME", "nacos")
withEnv(t, "NACOS_PASSWORD", "nacos")
withEnv(t, "NACOS_IP", "172.16.10.9")
withEnv(t, "NACOS_PORT", "19090")
withEnv(t, "PORT", "9999")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("loadConfig() error = %v", err)
}
if !cfg.EnableNacosRegister {
t.Fatalf("expected EnableNacosRegister true")
}
if cfg.NacosServerAddr != "10.0.0.10:8848" {
t.Fatalf("unexpected Nacos server %s", cfg.NacosServerAddr)
}
if cfg.NacosNamespace != "prod-ns" {
t.Fatalf("unexpected NacosNamespace %q", cfg.NacosNamespace)
}
if cfg.NacosGroup != "editor" {
t.Fatalf("unexpected NacosGroup %q", cfg.NacosGroup)
}
if cfg.NacosServiceName != "editor-lsp" {
t.Fatalf("unexpected NacosServiceName %q", cfg.NacosServiceName)
}
if cfg.NacosClusterName != "hz-a" {
t.Fatalf("unexpected NacosClusterName %q", cfg.NacosClusterName)
}
if cfg.NacosUsername != "nacos" || cfg.NacosPassword != "nacos" {
t.Fatalf("unexpected nacos auth")
}
if cfg.NacosRegisterIP != "172.16.10.9" || cfg.NacosRegisterPort != 19090 {
t.Fatalf("unexpected register endpoint %s:%d", cfg.NacosRegisterIP, cfg.NacosRegisterPort)
}
}
func withEnv(t *testing.T, key, value string) {
t.Helper()
old, existed := os.LookupEnv(key)
if value == "" {
_ = os.Unsetenv(key)
} else {
_ = os.Setenv(key, value)
}
t.Cleanup(func() {
if !existed {
_ = os.Unsetenv(key)
return
}
_ = os.Setenv(key, old)
})
}