@@ -3,6 +3,7 @@ package redis
33import (
44 "context"
55 "encoding/json"
6+ "fmt"
67 "reflect"
78 "strings"
89 "testing"
@@ -715,5 +716,68 @@ func TestVIsMember(t *testing.T) {
715716 cmd := m .lastCmd .(* BoolCmd )
716717 if cmd .args [0 ] != "vismember" || cmd .args [1 ] != "k" || cmd .args [2 ] != "e" {
717718 t .Errorf ("unexpected args: %v" , cmd .args )
719+ }
720+ }
721+
722+ // TestVectorScoreSliceCmdReadReply tests that VectorScoreSliceCmd.readReply handles
723+ // both RESP2 (flat array) and RESP3 (map) protocol responses.
724+ func TestVectorScoreSliceCmdReadReply (t * testing.T ) {
725+ tests := []struct {
726+ name string
727+ respData []byte
728+ want []VectorScore
729+ wantErr bool
730+ }{
731+ {
732+ // RESP3 map: %2\r\n +elem1\r\n ,0.9\r\n +elem2\r\n ,0.8\r\n
733+ name : "RESP3 map response" ,
734+ respData : []byte ("%2\r \n +elem1\r \n ,0.9\r \n +elem2\r \n ,0.8\r \n " ),
735+ want : []VectorScore {
736+ {Name : "elem1" , Score : 0.9 },
737+ {Name : "elem2" , Score : 0.8 },
738+ },
739+ },
740+ {
741+ // RESP2 flat array: *4\r\n $5\r\nelem1\r\n $3\r\n0.9\r\n $5\r\nelem2\r\n $3\r\n0.8\r\n
742+ name : "RESP2 flat array response" ,
743+ respData : []byte (fmt .Sprintf ("*4\r \n $5\r \n elem1\r \n $%d\r \n %s\r \n $5\r \n elem2\r \n $%d\r \n %s\r \n " ,
744+ len ("0.9" ), "0.9" , len ("0.8" ), "0.8" )),
745+ want : []VectorScore {
746+ {Name : "elem1" , Score : 0.9 },
747+ {Name : "elem2" , Score : 0.8 },
748+ },
749+ },
750+ {
751+ // RESP3 empty map
752+ name : "RESP3 empty map" ,
753+ respData : []byte ("%0\r \n " ),
754+ want : []VectorScore {},
755+ },
756+ {
757+ // RESP2 empty array
758+ name : "RESP2 empty array" ,
759+ respData : []byte ("*0\r \n " ),
760+ want : []VectorScore {},
761+ },
762+ {
763+ // RESP2 odd-length array should return an error
764+ name : "RESP2 odd array returns error" ,
765+ respData : []byte ("*3\r \n $5\r \n elem1\r \n $3\r \n 0.9\r \n $5\r \n elem2\r \n " ),
766+ wantErr : true ,
767+ },
768+ }
769+
770+ for _ , tt := range tests {
771+ t .Run (tt .name , func (t * testing.T ) {
772+ rd := proto .NewReader (newMockConn (tt .respData ))
773+ cmd := NewVectorScoreSliceCmd (context .Background (), "vsim" , "key" , "withscores" )
774+ err := cmd .readReply (rd )
775+ if (err != nil ) != tt .wantErr {
776+ t .Fatalf ("readReply() error = %v, wantErr %v" , err , tt .wantErr )
777+ }
778+ if ! tt .wantErr && ! reflect .DeepEqual (cmd .Val (), tt .want ) {
779+ t .Errorf ("Val() = %v, want %v" , cmd .Val (), tt .want )
780+ }
781+ })
718782 }
719783}
0 commit comments