|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +// serveDataDirEnv isolates $HOME and cwd and clears every variable that could |
| 16 | +// relocate the data dir, so a test only sees what it sets itself. |
| 17 | +func serveDataDirEnv(t *testing.T) (home string) { |
| 18 | + t.Helper() |
| 19 | + saveServeGlobals(t) |
| 20 | + home = t.TempDir() |
| 21 | + t.Setenv("HOME", home) |
| 22 | + t.Setenv("USERPROFILE", home) |
| 23 | + t.Setenv(config.EnvDataDir, "") |
| 24 | + t.Setenv(config.EnvDataDirLegacy, "") |
| 25 | + t.Setenv("MCPPROXY_API_KEY", "") |
| 26 | + // Seeding a first-run config probes Docker with a `docker info` subprocess |
| 27 | + // (2 s timeout). On Windows a probe killed at the deadline can still hold |
| 28 | + // the test's working directory for a moment and make t.TempDir cleanup |
| 29 | + // fail; with no docker on PATH the probe returns without spawning anything. |
| 30 | + t.Setenv("PATH", t.TempDir()) |
| 31 | + t.Chdir(t.TempDir()) |
| 32 | + configFile, dataDir = "", "" |
| 33 | + return home |
| 34 | +} |
| 35 | + |
| 36 | +// `serve --data-dir X` reads X/mcp_config.json (the file the management |
| 37 | +// commands already read), instead of $HOME/.mcpproxy. |
| 38 | +func TestServeReadsConfigFromDataDir(t *testing.T) { |
| 39 | + home := serveDataDirEnv(t) |
| 40 | + d := filepath.Join(t.TempDir(), "state") |
| 41 | + writeListenConfig(t, filepath.Join(d, "mcp_config.json"), "127.0.0.1:18997") |
| 42 | + dataDir = d |
| 43 | + |
| 44 | + cfg, saver, err := loadConfig(newServeFlagTestCmd()) |
| 45 | + require.NoError(t, err) |
| 46 | + assert.Equal(t, "127.0.0.1:18997", cfg.Listen, "config in the data dir was ignored") |
| 47 | + assert.Equal(t, d, cfg.DataDir) |
| 48 | + assert.Equal(t, filepath.Join(d, "mcp_config.json"), saver.path, "later saves must go to the file that was read") |
| 49 | + _, statErr := os.Stat(filepath.Join(home, ".mcpproxy", "mcp_config.json")) |
| 50 | + assert.True(t, os.IsNotExist(statErr), "a default config was created under $HOME") |
| 51 | +} |
| 52 | + |
| 53 | +// The same through MCPPROXY_DATA_DIR, which registerRootPathFlags turns into |
| 54 | +// the --data-dir default. |
| 55 | +func TestServeReadsConfigFromDataDirEnv(t *testing.T) { |
| 56 | + serveDataDirEnv(t) |
| 57 | + d := filepath.Join(t.TempDir(), "state") |
| 58 | + writeListenConfig(t, filepath.Join(d, "mcp_config.json"), "127.0.0.1:18996") |
| 59 | + t.Setenv(config.EnvDataDir, d) |
| 60 | + |
| 61 | + root := &cobra.Command{Use: "mcpproxy"} |
| 62 | + registerRootPathFlags(root) |
| 63 | + require.NoError(t, root.ParseFlags(nil)) |
| 64 | + require.Equal(t, d, dataDir) |
| 65 | + assert.Equal(t, "", root.PersistentFlags().Lookup("data-dir").DefValue, "--help must not change when the variable is set") |
| 66 | + |
| 67 | + cfg, _, err := loadConfig(newServeFlagTestCmd()) |
| 68 | + require.NoError(t, err) |
| 69 | + assert.Equal(t, "127.0.0.1:18996", cfg.Listen) |
| 70 | + assert.Equal(t, d, cfg.DataDir) |
| 71 | +} |
| 72 | + |
| 73 | +// An explicit --data-dir beats MCPPROXY_DATA_DIR. |
| 74 | +func TestServeDataDirFlagBeatsEnv(t *testing.T) { |
| 75 | + serveDataDirEnv(t) |
| 76 | + envDir := filepath.Join(t.TempDir(), "env") |
| 77 | + flagDir := filepath.Join(t.TempDir(), "flag") |
| 78 | + writeListenConfig(t, filepath.Join(envDir, "mcp_config.json"), "127.0.0.1:18001") |
| 79 | + writeListenConfig(t, filepath.Join(flagDir, "mcp_config.json"), "127.0.0.1:18002") |
| 80 | + t.Setenv(config.EnvDataDir, envDir) |
| 81 | + |
| 82 | + root := &cobra.Command{Use: "mcpproxy"} |
| 83 | + registerRootPathFlags(root) |
| 84 | + require.NoError(t, root.ParseFlags([]string{"--data-dir", flagDir})) |
| 85 | + |
| 86 | + cfg, _, err := loadConfig(newServeFlagTestCmd()) |
| 87 | + require.NoError(t, err) |
| 88 | + assert.Equal(t, "127.0.0.1:18002", cfg.Listen) |
| 89 | + assert.Equal(t, flagDir, cfg.DataDir) |
| 90 | +} |
| 91 | + |
| 92 | +// An explicit --config still wins over a config in the data dir. |
| 93 | +func TestServeConfigFlagBeatsDataDirConfig(t *testing.T) { |
| 94 | + serveDataDirEnv(t) |
| 95 | + d := filepath.Join(t.TempDir(), "state") |
| 96 | + writeListenConfig(t, filepath.Join(d, "mcp_config.json"), "127.0.0.1:18001") |
| 97 | + explicit := filepath.Join(t.TempDir(), "explicit.json") |
| 98 | + writeListenConfig(t, explicit, "127.0.0.1:18003") |
| 99 | + dataDir, configFile = d, explicit |
| 100 | + |
| 101 | + cfg, _, err := loadConfig(newServeFlagTestCmd()) |
| 102 | + require.NoError(t, err) |
| 103 | + assert.Equal(t, "127.0.0.1:18003", cfg.Listen) |
| 104 | +} |
| 105 | + |
| 106 | +// First run: nothing anywhere, so the default config (and its generated API |
| 107 | +// key) is seeded inside the named data dir, not under $HOME. |
| 108 | +func TestServeFirstRunSeedsConfigInDataDir(t *testing.T) { |
| 109 | + home := serveDataDirEnv(t) |
| 110 | + d := filepath.Join(t.TempDir(), "fresh") |
| 111 | + dataDir = d |
| 112 | + |
| 113 | + cfg, _, err := loadConfig(newServeFlagTestCmd()) |
| 114 | + require.NoError(t, err) |
| 115 | + assert.Equal(t, d, cfg.DataDir) |
| 116 | + _, statErr := os.Stat(filepath.Join(d, "mcp_config.json")) |
| 117 | + assert.NoError(t, statErr, "default config was not seeded in the data dir") |
| 118 | + _, statErr = os.Stat(filepath.Join(home, ".mcpproxy", "mcp_config.json")) |
| 119 | + assert.True(t, os.IsNotExist(statErr), "a default config was created under $HOME") |
| 120 | +} |
| 121 | + |
| 122 | +// Compatibility: with no config in the data dir but one in the legacy location, |
| 123 | +// serve keeps using the legacy file and only relocates the data dir. |
| 124 | +func TestServeKeepsLegacyConfigWhenDataDirHasNone(t *testing.T) { |
| 125 | + home := serveDataDirEnv(t) |
| 126 | + writeListenConfig(t, filepath.Join(home, ".mcpproxy", "mcp_config.json"), "127.0.0.1:18004") |
| 127 | + d := filepath.Join(t.TempDir(), "state") |
| 128 | + require.NoError(t, os.MkdirAll(d, 0o700)) |
| 129 | + dataDir = d |
| 130 | + |
| 131 | + cfg, _, err := loadConfig(newServeFlagTestCmd()) |
| 132 | + require.NoError(t, err) |
| 133 | + assert.Equal(t, "127.0.0.1:18004", cfg.Listen) |
| 134 | + assert.Equal(t, d, cfg.DataDir) |
| 135 | + _, statErr := os.Stat(filepath.Join(d, "mcp_config.json")) |
| 136 | + assert.True(t, os.IsNotExist(statErr), "no config should be seeded when a legacy one exists") |
| 137 | +} |
| 138 | + |
| 139 | +// MCPPROXY_DATA_DIR is the documented name and beats the legacy MCPPROXY_DATA |
| 140 | +// when the loader applies env overrides; the legacy name keeps working alone. |
| 141 | +func TestDataDirEnvNames(t *testing.T) { |
| 142 | + cases := []struct { |
| 143 | + name, newVar, legacy, want string |
| 144 | + }{ |
| 145 | + {"new only", "/n", "", "/n"}, |
| 146 | + {"legacy only", "", "/l", "/l"}, |
| 147 | + {"both: new wins", "/n", "/l", "/n"}, |
| 148 | + } |
| 149 | + for _, tc := range cases { |
| 150 | + t.Run(tc.name, func(t *testing.T) { |
| 151 | + t.Setenv(config.EnvDataDir, tc.newVar) |
| 152 | + t.Setenv(config.EnvDataDirLegacy, tc.legacy) |
| 153 | + cfg := config.DefaultConfig() |
| 154 | + config.ApplyEnvOverrides(cfg) |
| 155 | + assert.Equal(t, tc.want, cfg.DataDir) |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments