@@ -23,6 +23,7 @@ import (
2323
2424 natsserver "github.com/nats-io/nats-server/v2/server"
2525 "github.com/nats-io/nats.go"
26+ "github.com/nats-io/nats.go/jetstream"
2627 "github.com/stretchr/testify/assert"
2728 "github.com/stretchr/testify/require"
2829)
@@ -250,6 +251,53 @@ func TestNATSKV_MaxBytesEvictsOldEntries(t *testing.T) {
250251 assert .False (t , ok , "oldest entry should have been evicted" )
251252}
252253
254+ func TestNATSKV_WithReplicas (t * testing.T ) {
255+ nc := startEmbeddedNATS (t )
256+
257+ tests := []struct {
258+ name string
259+ replicas int
260+ wantRep int
261+ }{
262+ {"no WithReplicas defaults to 1" , 0 , 1 },
263+ {"explicit 1 replica" , 1 , 1 },
264+ }
265+
266+ for _ , tt := range tests {
267+ t .Run (tt .name , func (t * testing.T ) {
268+ bucket := sanitizeBucketName ("test-replicas-" + tt .name )
269+ opts := []Option {
270+ WithTTL (5 * time .Second ),
271+ WithNATS (nc , bucket ),
272+ }
273+ if tt .replicas > 0 {
274+ opts = append (opts , WithReplicas (tt .replicas ))
275+ }
276+ c , err := New [string ](opts ... )
277+ require .NoError (t , err )
278+
279+ // Verify replica count via the backing stream config
280+ nkv := c .(* natsKVCache [string ])
281+ js , err := jetstream .New (nc )
282+ require .NoError (t , err )
283+ stream , err := js .Stream (context .Background (), "KV_" + nkv .bucket )
284+ require .NoError (t , err )
285+ assert .Equal (t , tt .wantRep , stream .CachedInfo ().Config .Replicas )
286+ })
287+ }
288+
289+ // Replicas > 1 requires a multi-node NATS cluster. Verify that the option
290+ // is actually passed through by confirming that a single-node server rejects it.
291+ t .Run ("replicas 3 rejected by single-node server" , func (t * testing.T ) {
292+ _ , err := New [string ](
293+ WithTTL (5 * time .Second ),
294+ WithNATS (nc , "test-replicas-3" ),
295+ WithReplicas (3 ),
296+ )
297+ require .Error (t , err , "single-node NATS should reject replicas > 1" )
298+ })
299+ }
300+
253301func TestNew_WithNATSReturnsNATSBackend (t * testing.T ) {
254302 nc := startEmbeddedNATS (t )
255303 c , err := New [string ](
0 commit comments