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

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/keybase/client/go/chat/globals"
     9  	"github.com/keybase/client/go/chat/types"
    10  	"github.com/keybase/client/go/externalstest"
    11  	"github.com/keybase/client/go/kbtest"
    12  	"github.com/keybase/client/go/libkb"
    13  	"github.com/keybase/client/go/protocol/chat1"
    14  	"github.com/keybase/client/go/protocol/gregor1"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  type testGiphySearcher struct {
    19  	waitForCancel bool
    20  	waitingCh     chan struct{}
    21  }
    22  
    23  func newTestGiphySearcher() *testGiphySearcher {
    24  	return &testGiphySearcher{}
    25  }
    26  
    27  func (d *testGiphySearcher) Search(mctx libkb.MetaContext, apiKeySource types.ExternalAPIKeySource,
    28  	query *string, limit int, urlsrv types.AttachmentURLSrv) ([]chat1.GiphySearchResult, error) {
    29  	if d.waitForCancel {
    30  		if d.waitingCh != nil {
    31  			close(d.waitingCh)
    32  			d.waitingCh = nil
    33  		}
    34  		<-mctx.Ctx().Done()
    35  		return nil, mctx.Ctx().Err()
    36  	}
    37  	if query != nil && *query == "miketown" {
    38  		return []chat1.GiphySearchResult{{
    39  			TargetUrl: "https://www.miketown.com",
    40  		}}, nil
    41  	}
    42  	return []chat1.GiphySearchResult{{
    43  		TargetUrl: "https://www.notmiketown.com",
    44  	}}, nil
    45  }
    46  
    47  func TestGiphyPreview(t *testing.T) {
    48  	tc := externalstest.SetupTest(t, "giphy", 0)
    49  	defer tc.Cleanup()
    50  
    51  	g := globals.NewContext(tc.G, &globals.ChatContext{})
    52  	giphy := NewGiphy(g)
    53  	searcher := newTestGiphySearcher()
    54  	giphy.searcher = searcher
    55  	ctx := context.TODO()
    56  	uid := gregor1.UID{1, 2, 3, 4}
    57  	convID := chat1.ConversationID{1, 2, 3, 4}
    58  	ui := kbtest.NewChatUI()
    59  	g.UIRouter = kbtest.NewMockUIRouter(ui)
    60  	g.AttachmentURLSrv = types.DummyAttachmentHTTPSrv{}
    61  	timeout := 20 * time.Second
    62  
    63  	giphy.Preview(ctx, uid, convID, "", "/giph")
    64  	select {
    65  	case <-ui.GiphyResults:
    66  		require.Fail(t, "no results")
    67  	default:
    68  	}
    69  
    70  	giphy.Preview(ctx, uid, convID, "", "/giphy")
    71  	select {
    72  	case show := <-ui.GiphyWindow:
    73  		require.True(t, show)
    74  	case <-time.After(timeout):
    75  		require.Fail(t, "no window")
    76  	}
    77  	select {
    78  	case res := <-ui.GiphyResults:
    79  		require.Equal(t, 1, len(res.Results))
    80  		require.Equal(t, "https://www.notmiketown.com", res.Results[0].TargetUrl)
    81  	case <-time.After(timeout):
    82  		require.Fail(t, "no results")
    83  	}
    84  	giphy.Preview(ctx, uid, convID, "", "/giphy ")
    85  	select {
    86  	case <-ui.GiphyResults:
    87  		require.Fail(t, "no results")
    88  	default:
    89  	}
    90  	giphy.Preview(ctx, uid, convID, "", "")
    91  	select {
    92  	case show := <-ui.GiphyWindow:
    93  		require.False(t, show)
    94  	case <-time.After(timeout):
    95  		require.Fail(t, "no window")
    96  	}
    97  
    98  	waitingCh := make(chan struct{})
    99  	searcher.waitingCh = waitingCh
   100  	searcher.waitForCancel = true
   101  	firstDoneCh := make(chan struct{})
   102  	go func() {
   103  		giphy.Preview(ctx, uid, convID, "", "/giphy")
   104  		close(firstDoneCh)
   105  	}()
   106  	select {
   107  	case <-waitingCh:
   108  		searcher.waitForCancel = false
   109  	case <-time.After(timeout):
   110  		require.Fail(t, "no waiting ch")
   111  	}
   112  	giphy.Preview(ctx, uid, convID, "", "/giphy miketown")
   113  	for i := 0; i < 2; i++ {
   114  		select {
   115  		case show := <-ui.GiphyWindow:
   116  			require.True(t, show)
   117  		case <-time.After(timeout):
   118  			require.Fail(t, "no window")
   119  		}
   120  	}
   121  	select {
   122  	case res := <-ui.GiphyResults:
   123  		require.Equal(t, 1, len(res.Results))
   124  		require.Equal(t, "https://www.miketown.com", res.Results[0].TargetUrl)
   125  	case <-time.After(timeout):
   126  		require.Fail(t, "no results")
   127  	}
   128  	select {
   129  	case <-firstDoneCh:
   130  	case <-time.After(timeout):
   131  		require.Fail(t, "no first done")
   132  	}
   133  	giphy.Preview(ctx, uid, convID, "", "")
   134  	select {
   135  	case show := <-ui.GiphyWindow:
   136  		require.False(t, show)
   137  	case <-time.After(timeout):
   138  		require.Fail(t, "no window")
   139  	}
   140  	select {
   141  	case <-ui.GiphyResults:
   142  		require.Fail(t, "no more results")
   143  	default:
   144  	}
   145  }