multica/server/cmd/multica/cmd_auth_test.go
Jiayuan 8fa1b163a6 feat(daemon): add --profile flag for multi-environment isolation
Allow running multiple daemon instances against different servers (e.g.
production and local dev) simultaneously. Each profile gets isolated
config, PID file, log file, health port, and workspaces root.

Usage:
  multica login --profile dev --server-url http://localhost:8080
  multica daemon start --profile dev

Default profile (no --profile flag) behavior is unchanged.

Closes MUL-42
2026-03-30 20:21:23 +08:00

67 lines
2 KiB
Go

package main
import (
"testing"
"github.com/spf13/cobra"
)
// testCmd returns a minimal cobra.Command with the --profile persistent flag
// registered, matching the rootCmd setup used in production.
func testCmd() *cobra.Command {
cmd := &cobra.Command{}
cmd.PersistentFlags().String("profile", "", "")
return cmd
}
func TestResolveAppURL(t *testing.T) {
cmd := testCmd()
t.Run("prefers MULTICA_APP_URL", func(t *testing.T) {
t.Setenv("MULTICA_APP_URL", "http://localhost:14000")
t.Setenv("FRONTEND_ORIGIN", "http://localhost:13000")
if got := resolveAppURL(cmd); got != "http://localhost:14000" {
t.Fatalf("resolveAppURL() = %q, want %q", got, "http://localhost:14000")
}
})
t.Run("falls back to FRONTEND_ORIGIN", func(t *testing.T) {
t.Setenv("MULTICA_APP_URL", "")
t.Setenv("FRONTEND_ORIGIN", "http://localhost:13026")
if got := resolveAppURL(cmd); got != "http://localhost:13026" {
t.Fatalf("resolveAppURL() = %q, want %q", got, "http://localhost:13026")
}
})
t.Run("defaults to localhost 3000", func(t *testing.T) {
t.Setenv("MULTICA_APP_URL", "")
t.Setenv("FRONTEND_ORIGIN", "")
t.Setenv("HOME", t.TempDir()) // avoid reading real config
if got := resolveAppURL(cmd); got != "http://localhost:3000" {
t.Fatalf("resolveAppURL() = %q, want %q", got, "http://localhost:3000")
}
})
}
func TestNormalizeAPIBaseURL(t *testing.T) {
t.Run("converts websocket base URL", func(t *testing.T) {
if got := normalizeAPIBaseURL("ws://localhost:18106/ws"); got != "http://localhost:18106" {
t.Fatalf("normalizeAPIBaseURL() = %q, want %q", got, "http://localhost:18106")
}
})
t.Run("keeps http base URL", func(t *testing.T) {
if got := normalizeAPIBaseURL("http://localhost:8080"); got != "http://localhost:8080" {
t.Fatalf("normalizeAPIBaseURL() = %q, want %q", got, "http://localhost:8080")
}
})
t.Run("falls back to raw value for invalid URL", func(t *testing.T) {
if got := normalizeAPIBaseURL("://bad-url"); got != "://bad-url" {
t.Fatalf("normalizeAPIBaseURL() = %q, want %q", got, "://bad-url")
}
})
}