Skip to content

Commit 20861fe

Browse files
committed
refactor(tests): replace hardcoded log messages with constants
1 parent 9e2b19e commit 20861fe

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

dht_config_test.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13+
// Test log message constants to avoid duplication
14+
const (
15+
logMsgDHTModeServer = "DHT mode: server"
16+
logMsgDHTModeClient = "DHT mode: client"
17+
logMsgConfiguringCleanupInterval = "Configuring DHT cleanup interval"
18+
)
19+
1320
// testClientConfig is a helper to create a test client with log capture
1421
type testClientConfig struct {
1522
name string
@@ -56,19 +63,19 @@ func TestDHTModeSelection(t *testing.T) {
5663
{
5764
name: "default mode (empty string defaults to server)",
5865
dhtMode: "",
59-
expectedLogMsg: "DHT mode: server",
66+
expectedLogMsg: logMsgDHTModeServer,
6067
additionalMsgOpt: "will advertise and store provider records",
6168
},
6269
{
6370
name: "explicit server mode",
6471
dhtMode: "server",
65-
expectedLogMsg: "DHT mode: server",
72+
expectedLogMsg: logMsgDHTModeServer,
6673
additionalMsgOpt: "will advertise and store provider records",
6774
},
6875
{
6976
name: "client mode",
7077
dhtMode: "client",
71-
expectedLogMsg: "DHT mode: client",
78+
expectedLogMsg: logMsgDHTModeClient,
7279
additionalMsgOpt: "query-only, no provider storage",
7380
},
7481
}
@@ -148,12 +155,12 @@ func TestDHTCleanupIntervalConfiguration(t *testing.T) {
148155
})
149156

150157
if tt.expectCleanupConfigLog {
151-
assert.Contains(t, output, "Configuring DHT cleanup interval")
158+
assert.Contains(t, output, logMsgConfiguringCleanupInterval)
152159
if tt.expectedIntervalString != "" {
153160
assert.Contains(t, output, tt.expectedIntervalString)
154161
}
155162
} else {
156-
assert.NotContains(t, output, "Configuring DHT cleanup interval")
163+
assert.NotContains(t, output, logMsgConfiguringCleanupInterval)
157164
}
158165
})
159166
}
@@ -173,29 +180,29 @@ func TestDHTConfigurationCombinations(t *testing.T) {
173180
name: "default configuration (empty mode, no interval)",
174181
dhtMode: "",
175182
cleanupInterval: 0,
176-
expectedModeLog: "DHT mode: server",
183+
expectedModeLog: logMsgDHTModeServer,
177184
expectCleanupConfigLog: false,
178185
},
179186
{
180187
name: "server mode with 48h cleanup",
181188
dhtMode: "server",
182189
cleanupInterval: 48 * time.Hour,
183-
expectedModeLog: "DHT mode: server",
190+
expectedModeLog: logMsgDHTModeServer,
184191
expectCleanupConfigLog: true,
185192
expectedIntervalString: "48h0m0s",
186193
},
187194
{
188195
name: "client mode with cleanup specified (ignored)",
189196
dhtMode: "client",
190197
cleanupInterval: 24 * time.Hour,
191-
expectedModeLog: "DHT mode: client",
198+
expectedModeLog: logMsgDHTModeClient,
192199
expectCleanupConfigLog: false,
193200
},
194201
{
195202
name: "server mode without cleanup",
196203
dhtMode: "server",
197204
cleanupInterval: 0,
198-
expectedModeLog: "DHT mode: server",
205+
expectedModeLog: logMsgDHTModeServer,
199206
expectCleanupConfigLog: false,
200207
},
201208
}
@@ -211,12 +218,12 @@ func TestDHTConfigurationCombinations(t *testing.T) {
211218
assert.Contains(t, output, tt.expectedModeLog, "mode log should be present")
212219

213220
if tt.expectCleanupConfigLog {
214-
assert.Contains(t, output, "Configuring DHT cleanup interval", "cleanup config should be logged")
221+
assert.Contains(t, output, logMsgConfiguringCleanupInterval, "cleanup config should be logged")
215222
if tt.expectedIntervalString != "" {
216223
assert.Contains(t, output, tt.expectedIntervalString, "interval string should match")
217224
}
218225
} else {
219-
assert.NotContains(t, output, "Configuring DHT cleanup interval", "cleanup config should not be logged")
226+
assert.NotContains(t, output, logMsgConfiguringCleanupInterval, "cleanup config should not be logged")
220227
}
221228
})
222229
}
@@ -230,7 +237,7 @@ func TestDHTClientBasicFunctionality(t *testing.T) {
230237
})
231238

232239
// Verify client mode was selected
233-
assert.Contains(t, output, "DHT mode: client")
240+
assert.Contains(t, output, logMsgDHTModeClient)
234241

235242
// Verify client has basic functionality
236243
peerID := client.GetID()

0 commit comments

Comments
 (0)