github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/helper_test.go (about)

     1  package chat
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/chat/globals"
     7  	"github.com/keybase/client/go/chat/storage"
     8  	"github.com/keybase/client/go/chat/types"
     9  	"github.com/keybase/client/go/protocol/chat1"
    10  	"github.com/keybase/client/go/protocol/gregor1"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestRecentConversationParticipants(t *testing.T) {
    16  	maxUsers := 5
    17  	ctx, world, ri2, _, sender, _ := setupTest(t, maxUsers)
    18  	defer world.Cleanup()
    19  
    20  	u := world.GetUsers()[0]
    21  	tc := world.Tcs[u.Username]
    22  	uid := u.User.GetUID().ToBytes()
    23  
    24  	var refList []gregor1.UID
    25  	for i := 0; i < maxUsers; i++ {
    26  		tlfName := ""
    27  		for j := i; j >= 0; j-- {
    28  			tlfName += world.GetUsers()[j].Username
    29  			if j > 0 {
    30  				tlfName += ","
    31  			}
    32  		}
    33  
    34  		conv, _ := newConv(ctx, t, tc, uid, ri2, sender, tlfName)
    35  
    36  		// Each participant needs to say something
    37  		for j := i; j >= 0; j-- {
    38  			u := world.GetUsers()[j]
    39  			_, err := ri2.PostRemote(ctx, chat1.PostRemoteArg{
    40  				ConversationID: conv.GetConvID(),
    41  				MessageBoxed: chat1.MessageBoxed{
    42  					ClientHeader: chat1.MessageClientHeader{
    43  						Conv:      conv.Info.Triple,
    44  						Sender:    u.User.GetUID().ToBytes(),
    45  						TlfName:   tlfName,
    46  						TlfPublic: false,
    47  					},
    48  				},
    49  			})
    50  			require.NoError(t, err)
    51  		}
    52  
    53  		iuid := gregor1.UID(world.GetUsers()[i].User.GetUID().ToBytes())
    54  		if !iuid.Eq(uid) {
    55  			refList = append(refList, iuid)
    56  		}
    57  	}
    58  
    59  	require.NoError(t, storage.NewInbox(tc.Context()).Clear(ctx, uid))
    60  	_, _, err := tc.Context().InboxSource.Read(ctx, uid, types.ConversationLocalizerBlocking,
    61  		types.InboxSourceDataSourceAll, nil, nil)
    62  	require.NoError(t, err)
    63  
    64  	res, err := RecentConversationParticipants(ctx, tc.Context(), uid)
    65  	require.NoError(t, err)
    66  	require.Equal(t, maxUsers-1, len(res))
    67  	require.Equal(t, refList, res)
    68  }
    69  
    70  func TestSendTextByName(t *testing.T) {
    71  	runWithMemberTypes(t, func(mt chat1.ConversationMembersType) {
    72  		ctx, world, ri2, _, _, _ := setupTest(t, 1)
    73  		defer world.Cleanup()
    74  
    75  		u := world.GetUsers()[0]
    76  		tc := world.Tcs[u.Username]
    77  		uid := u.User.GetUID().ToBytes()
    78  		var name string
    79  		switch mt {
    80  		case chat1.ConversationMembersType_TEAM:
    81  			name = createTeam(tc.TestContext)
    82  		default:
    83  			name = u.Username
    84  		}
    85  
    86  		getRi := func() chat1.RemoteInterface { return ri2 }
    87  		helper := NewHelper(tc.Context(), getRi)
    88  		require.NoError(t, helper.SendTextByName(ctx, name, nil,
    89  			mt, keybase1.TLFIdentifyBehavior_CHAT_CLI, "HI"))
    90  		inbox, _, err := tc.Context().InboxSource.Read(ctx, uid, types.ConversationLocalizerBlocking,
    91  			types.InboxSourceDataSourceAll, nil, nil)
    92  		require.NoError(t, err)
    93  		require.Equal(t, 1, len(inbox.Convs))
    94  		require.NoError(t, helper.SendTextByName(ctx, name, nil,
    95  			mt, keybase1.TLFIdentifyBehavior_CHAT_CLI, "HI"))
    96  		inbox, _, err = tc.Context().InboxSource.Read(ctx, uid, types.ConversationLocalizerBlocking,
    97  			types.InboxSourceDataSourceAll, nil, nil)
    98  		require.NoError(t, err)
    99  		require.Equal(t, 1, len(inbox.Convs))
   100  		tv, err := tc.Context().ConvSource.Pull(ctx, inbox.Convs[0].GetConvID(), uid,
   101  			chat1.GetThreadReason_GENERAL, nil,
   102  			&chat1.GetThreadQuery{
   103  				MessageTypes: []chat1.MessageType{chat1.MessageType_TEXT},
   104  			}, nil)
   105  		require.NoError(t, err)
   106  		require.Equal(t, 2, len(tv.Messages))
   107  
   108  		t.Logf("sending into new topic name")
   109  		topicName := "MIKE"
   110  		err = helper.SendTextByName(ctx, name, &topicName,
   111  			mt, keybase1.TLFIdentifyBehavior_CHAT_CLI, "HI")
   112  		require.NoError(t, err)
   113  		inbox, _, err = tc.Context().InboxSource.Read(ctx, uid, types.ConversationLocalizerBlocking,
   114  			types.InboxSourceDataSourceAll, nil, nil)
   115  		require.NoError(t, err)
   116  		switch mt {
   117  		case chat1.ConversationMembersType_TEAM:
   118  			require.Equal(t, 2, len(inbox.Convs))
   119  		default:
   120  			// No second topic name on KBFS chats
   121  			require.Equal(t, 1, len(inbox.Convs))
   122  		}
   123  	})
   124  }
   125  func TestTopicNameRace(t *testing.T) {
   126  	runWithMemberTypes(t, func(mt chat1.ConversationMembersType) {
   127  		switch mt {
   128  		case chat1.ConversationMembersType_KBFS:
   129  			return
   130  		default:
   131  			// Nothing to do for other member types.
   132  		}
   133  		ctc := makeChatTestContext(t, "TestTopicNameRace", 1)
   134  		defer ctc.cleanup()
   135  		users := ctc.users()
   136  
   137  		ctx := ctc.as(t, users[0]).startCtx
   138  		ri := ctc.as(t, users[0]).ri
   139  		tc := ctc.world.Tcs[users[0].Username]
   140  		uid := users[0].User.GetUID().ToBytes()
   141  		t.Logf("uid: %s", uid)
   142  		first := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_DEV, mt)
   143  
   144  		// spam create conversation with same name
   145  		type ncRes struct {
   146  			convID chat1.ConversationID
   147  			err    error
   148  		}
   149  		topicName := "LOSERS"
   150  		attempts := 2
   151  		retCh := make(chan ncRes, attempts)
   152  		for i := 0; i < attempts; i++ {
   153  			go func() {
   154  				ctx = globals.CtxAddLogTags(ctx, tc.Context())
   155  				conv, _, err := NewConversation(ctx, tc.Context(), uid, first.TlfName, &topicName,
   156  					chat1.TopicType_DEV, mt, keybase1.TLFVisibility_PRIVATE, nil,
   157  					func() chat1.RemoteInterface { return ri }, NewConvFindExistingNormal)
   158  				retCh <- ncRes{convID: conv.GetConvID(), err: err}
   159  			}()
   160  		}
   161  		var convID chat1.ConversationID
   162  		for i := 0; i < attempts; i++ {
   163  			res := <-retCh
   164  			require.NoError(t, res.err)
   165  			if convID.IsNil() {
   166  				convID = res.convID
   167  			} else {
   168  				require.Equal(t, convID, res.convID)
   169  			}
   170  		}
   171  	})
   172  }