| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | "github.com/stretchr/testify/require" |
| 8 | ) |
| 9 | |
| 10 | func TestAutoConfDefaults(t *testing.T) { |
| 11 | // Test that AutoConf has the correct default values |
| 12 | cfg := &Config{ |
| 13 | AutoConf: AutoConf{ |
| 14 | URL: NewOptionalString(DefaultAutoConfURL), |
| 15 | Enabled: True, |
| 16 | }, |
| 17 | } |
| 18 | |
| 19 | assert.Equal(t, DefaultAutoConfURL, cfg.AutoConf.URL.WithDefault(DefaultAutoConfURL)) |
| 20 | assert.True(t, cfg.AutoConf.Enabled.WithDefault(DefaultAutoConfEnabled)) |
| 21 | |
| 22 | // Test default refresh interval |
| 23 | if cfg.AutoConf.RefreshInterval == nil { |
| 24 | // This is expected - nil means use default |
| 25 | duration := (*OptionalDuration)(nil).WithDefault(DefaultAutoConfRefreshInterval) |
| 26 | assert.Equal(t, DefaultAutoConfRefreshInterval, duration) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestAutoConfProfile(t *testing.T) { |
| 31 | cfg := &Config{ |
| 32 | Bootstrap: []string{"some", "existing", "peers"}, |
| 33 | DNS: DNS{ |
| 34 | Resolvers: map[string]string{ |
| 35 | "eth.": "https://example.com", |
| 36 | }, |
| 37 | }, |
| 38 | Routing: Routing{ |
| 39 | DelegatedRouters: []string{"https://existing.router"}, |
| 40 | }, |
| 41 | Ipns: Ipns{ |
| 42 | DelegatedPublishers: []string{"https://existing.publisher"}, |
| 43 | }, |
| 44 | AutoConf: AutoConf{ |
| 45 | Enabled: False, |
| 46 | }, |
| 47 | } |
| 48 | |
| 49 | // Apply autoconf profile |
| 50 | profile, ok := Profiles["autoconf-on"] |
| 51 | require.True(t, ok, "autoconf-on profile not found") |
| 52 | |
| 53 | err := profile.Transform(cfg) |
| 54 | require.NoError(t, err) |
| 55 | |
| 56 | // Check that values were set to "auto" |
| 57 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Bootstrap) |
| 58 | assert.Equal(t, AutoPlaceholder, cfg.DNS.Resolvers["."]) |
| 59 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Routing.DelegatedRouters) |
| 60 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Ipns.DelegatedPublishers) |
| 61 | |
| 62 | // Check that AutoConf was enabled |
| 63 | assert.True(t, cfg.AutoConf.Enabled.WithDefault(DefaultAutoConfEnabled)) |
| 64 | |
| 65 | // Check that URL was set |
| 66 | assert.Equal(t, DefaultAutoConfURL, cfg.AutoConf.URL.WithDefault(DefaultAutoConfURL)) |
| 67 | } |
| 68 | |
| 69 | func TestInitWithAutoValues(t *testing.T) { |
| 70 | identity := Identity{ |
| 71 | PeerID: "QmTest", |
| 72 | } |
| 73 | |
| 74 | cfg, err := InitWithIdentity(identity) |
| 75 | require.NoError(t, err) |
| 76 | |
| 77 | // Check that Bootstrap is set to "auto" |
| 78 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Bootstrap) |
| 79 | |
| 80 | // Check that DNS resolver is set to "auto" |
| 81 | assert.Equal(t, AutoPlaceholder, cfg.DNS.Resolvers["."]) |
| 82 | |
| 83 | // Check that DelegatedRouters is set to "auto" |
| 84 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Routing.DelegatedRouters) |
| 85 | |
| 86 | // Check that DelegatedPublishers is set to "auto" |
| 87 | assert.Equal(t, []string{AutoPlaceholder}, cfg.Ipns.DelegatedPublishers) |
| 88 | |
| 89 | // Check that AutoConf is enabled with correct URL |
| 90 | assert.True(t, cfg.AutoConf.Enabled.WithDefault(DefaultAutoConfEnabled)) |
| 91 | assert.Equal(t, DefaultAutoConfURL, cfg.AutoConf.URL.WithDefault(DefaultAutoConfURL)) |
| 92 | } |