github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/giphystore_test.go (about)

     1  package chat
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/chat/storage"
     7  	"github.com/keybase/client/go/protocol/chat1"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestGiphyStorage(t *testing.T) {
    12  	useRemoteMock = false
    13  	defer func() { useRemoteMock = true }()
    14  	ctc := makeChatTestContext(t, "TestGiphyStorage", 1)
    15  	defer ctc.cleanup()
    16  
    17  	users := ctc.users()
    18  	tc := ctc.world.Tcs[users[0].Username]
    19  	ctx := ctc.as(t, users[0]).startCtx
    20  	uid := users[0].User.GetUID().ToBytes()
    21  
    22  	store := storage.NewGiphyStore(tc.Context())
    23  
    24  	giphyRes := store.GiphyResults(ctx, uid, 5)
    25  	require.Equal(t, len(giphyRes), 0)
    26  
    27  	giphy1 := chat1.GiphySearchResult{
    28  		TargetUrl: "url1",
    29  	}
    30  	err := store.Put(ctx, uid, giphy1)
    31  	require.NoError(t, err)
    32  
    33  	giphyRes = store.GiphyResults(ctx, uid, 5)
    34  	require.Equal(t, len(giphyRes), 1)
    35  	require.Equal(t, giphyRes[0], giphy1)
    36  
    37  	giphy2 := chat1.GiphySearchResult{
    38  		TargetUrl: "url2",
    39  	}
    40  	err = store.Put(ctx, uid, giphy2)
    41  	require.NoError(t, err)
    42  
    43  	giphyRes = store.GiphyResults(ctx, uid, 5)
    44  	require.Equal(t, len(giphyRes), 2)
    45  	require.Equal(t, giphyRes[0], giphy1)
    46  	require.Equal(t, giphyRes[1], giphy2)
    47  
    48  	err = store.Put(ctx, uid, giphy2)
    49  	require.NoError(t, err)
    50  
    51  	giphyRes = store.GiphyResults(ctx, uid, 5)
    52  	require.Equal(t, len(giphyRes), 2)
    53  	require.Equal(t, giphyRes[0], giphy2)
    54  	require.Equal(t, giphyRes[1], giphy1)
    55  
    56  	giphyRes = store.GiphyResults(ctx, uid, 1)
    57  	require.Equal(t, len(giphyRes), 1)
    58  	require.Equal(t, giphyRes[0], giphy2)
    59  }