github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsedits/user_history_test.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package kbfsedits 6 7 import ( 8 "strconv" 9 "testing" 10 "time" 11 12 "github.com/keybase/client/go/kbfs/tlf" 13 "github.com/keybase/client/go/libkb" 14 "github.com/keybase/client/go/logger" 15 "github.com/keybase/client/go/protocol/keybase1" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestUserHistorySimple(t *testing.T) { 20 aliceName, bobName := "alice", "bob" 21 aliceUID, bobUID := keybase1.MakeTestUID(1), keybase1.MakeTestUID(2) 22 23 privSharedTlfID, err := tlf.MakeRandomID(tlf.Private) 24 require.NoError(t, err) 25 privSharedNN := nextNotification{1, 0, privSharedTlfID, nil} 26 privSharedTH := NewTlfHistory() 27 privSharedName := tlf.CanonicalName("alice,bob") 28 29 privHomeTlfID, err := tlf.MakeRandomID(tlf.Private) 30 require.NoError(t, err) 31 privHomeNN := nextNotification{1, 0, privHomeTlfID, nil} 32 privHomeTH := NewTlfHistory() 33 privHomeName := tlf.CanonicalName("alice") 34 35 publicTlfID, err := tlf.MakeRandomID(tlf.Public) 36 require.NoError(t, err) 37 publicNN := nextNotification{1, 0, publicTlfID, nil} 38 publicTH := NewTlfHistory() 39 publicName := tlf.CanonicalName("alice") 40 41 now := time.Now() 42 // Alice writes to private shared TLF. 43 var privSharedAlice []string 44 for i := 0; i < 3; i++ { 45 now = now.Add(1 * time.Minute) 46 _ = privSharedNN.make( 47 strconv.Itoa(i), NotificationCreate, aliceUID, nil, now) 48 privSharedAlice = append(privSharedAlice, privSharedNN.encode(t)) 49 } 50 privSharedTimeAlice := keybase1.ToTime(now) 51 // Alice deletes something from private shared TLF (it shouldn't 52 // affect the overall server time for the TLF). 53 now = now.Add(1 * time.Minute) 54 _ = privSharedNN.make("a", NotificationDelete, aliceUID, nil, now) 55 privSharedAlice = append(privSharedAlice, privSharedNN.encode(t)) 56 57 // Alice writes to public TLF. 58 var publicAlice []string 59 for i := 0; i < 3; i++ { 60 now = now.Add(1 * time.Minute) 61 _ = publicNN.make( 62 strconv.Itoa(i), NotificationCreate, aliceUID, nil, now) 63 publicAlice = append(publicAlice, publicNN.encode(t)) 64 } 65 publicTime := keybase1.ToTime(now) 66 67 // Bob writes to private shared TLF. 68 var privSharedBob []string 69 for i := 3; i < 6; i++ { 70 now = now.Add(1 * time.Minute) 71 _ = privSharedNN.make( 72 strconv.Itoa(i), NotificationCreate, bobUID, nil, now) 73 privSharedBob = append(privSharedBob, privSharedNN.encode(t)) 74 } 75 privSharedTimeBob := keybase1.ToTime(now) 76 77 // Alice writes to private home TLF. 78 var privHomeAlice []string 79 for i := 0; i < 3; i++ { 80 now = now.Add(1 * time.Minute) 81 _ = privHomeNN.make( 82 strconv.Itoa(i), NotificationCreate, aliceUID, nil, now) 83 privHomeAlice = append(privHomeAlice, privHomeNN.encode(t)) 84 } 85 privHomeTime := keybase1.ToTime(now) 86 87 _, err = privSharedTH.AddNotifications(bobName, privSharedBob) 88 require.NoError(t, err) 89 _, err = privSharedTH.AddNotifications(aliceName, privSharedAlice) 90 require.NoError(t, err) 91 92 _, err = privHomeTH.AddNotifications(aliceName, privHomeAlice) 93 require.NoError(t, err) 94 95 _, err = publicTH.AddNotifications(aliceName, publicAlice) 96 require.NoError(t, err) 97 98 log := logger.New("UH") 99 uh := NewUserHistory(log, libkb.NewVDebugLog(log)) 100 uh.UpdateHistory(privSharedName, tlf.Private, privSharedTH, aliceName) 101 uh.UpdateHistory(privHomeName, tlf.Private, privHomeTH, aliceName) 102 uh.UpdateHistory(publicName, tlf.Public, publicTH, aliceName) 103 104 type expectInfo struct { 105 tlfName string 106 tlfType keybase1.FolderType 107 serverTime keybase1.Time 108 writer string 109 num int 110 numDeletes int 111 } 112 expected := []expectInfo{ 113 // private home alice 114 { 115 string(privHomeName), keybase1.FolderType_PRIVATE, 116 privHomeTime, aliceName, 3, 0, 117 }, 118 // private shared bob 119 { 120 string(privSharedName), keybase1.FolderType_PRIVATE, 121 privSharedTimeBob, bobName, 3, 0, 122 }, 123 // public alice 124 { 125 string(publicName), keybase1.FolderType_PUBLIC, 126 publicTime, aliceName, 3, 0, 127 }, 128 // private shared alice 129 { 130 string(privSharedName), keybase1.FolderType_PRIVATE, 131 privSharedTimeAlice, aliceName, 3, 1, 132 }, 133 } 134 135 check := func(expected []expectInfo) { 136 history := uh.Get(aliceName) 137 require.Len(t, history, len(expected)) 138 for i, wh := range history { 139 require.Len(t, wh.History, 1) 140 require.Equal(t, expected[i].tlfName, wh.Folder.Name) 141 require.Equal(t, expected[i].tlfType, wh.Folder.FolderType) 142 require.Equal(t, expected[i].serverTime, wh.ServerTime) 143 require.Equal(t, expected[i].writer, wh.History[0].WriterName) 144 require.Len(t, wh.History[0].Edits, expected[i].num) 145 require.Len(t, wh.History[0].Deletes, expected[i].numDeletes) 146 } 147 } 148 check(expected) 149 150 // Alice writes one more thing to the private shared folder. 151 now = now.Add(1 * time.Minute) 152 _ = privSharedNN.make("7", NotificationCreate, aliceUID, nil, now) 153 _, err = privSharedTH.AddNotifications( 154 aliceName, []string{privSharedNN.encode(t)}) 155 require.NoError(t, err) 156 uh.UpdateHistory(privSharedName, tlf.Private, privSharedTH, aliceName) 157 expected[3].serverTime = keybase1.ToTime(now) 158 expected[3].num++ 159 expected = append([]expectInfo{expected[3]}, expected[0:3]...) 160 check(expected) 161 }