Files
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

91 lines
2.1 KiB
Go

package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"monica-go-completion-backend/internal/completion"
)
func TestLoadConfigFileWithEnvOverride(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
content := `{
"port": "8081",
"requestTimeout": "3s",
"enableRedis": false,
"servers": [
{
"language": "go",
"languageId": "go",
"command": "gopls-from-file",
"args": []
}
]
}`
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write config file failed: %v", err)
}
t.Setenv("CONFIG_FILE", path)
t.Setenv("PORT", "9090")
t.Setenv("GO_LSP_COMMAND", "gopls-from-env")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("loadConfig() error = %v", err)
}
if cfg.Port != "9090" {
t.Fatalf("expected env to override port, got %s", cfg.Port)
}
if cfg.RequestTimeout != 3*time.Second {
t.Fatalf("expected requestTimeout=3s, got %s", cfg.RequestTimeout)
}
if cfg.EnableRedis {
t.Fatal("expected enableRedis=false from config file")
}
goSpec, ok := findServer(cfg.Servers, "go")
if !ok {
t.Fatal("expected go server in config")
}
if goSpec.Command != "gopls-from-env" {
t.Fatalf("expected go command overridden by env, got %s", goSpec.Command)
}
}
func TestLoadConfigFileInvalidDuration(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.json")
content := `{
"requestTimeout": "not-a-duration"
}`
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write config file failed: %v", err)
}
t.Setenv("CONFIG_FILE", path)
_, err := loadConfig()
if err == nil {
t.Fatal("expected loadConfig() to fail")
}
if !strings.Contains(err.Error(), "requestTimeout") {
t.Fatalf("expected requestTimeout parse error, got %v", err)
}
}
func findServer(specs []completion.LanguageServerSpec, language string) (completion.LanguageServerSpec, bool) {
target := strings.ToLower(strings.TrimSpace(language))
for _, spec := range specs {
if strings.ToLower(strings.TrimSpace(spec.Language)) == target {
return spec, true
}
}
return completion.LanguageServerSpec{}, false
}