github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/tlfupgrade/tlfupgrade_test.go (about) 1 package tlfupgrade 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/keybase/client/go/kbtest" 9 "github.com/keybase/client/go/libkb" 10 "github.com/keybase/client/go/protocol/chat1" 11 "github.com/keybase/client/go/protocol/keybase1" 12 "github.com/keybase/clockwork" 13 "github.com/stretchr/testify/require" 14 ) 15 16 type testAPIServer struct { 17 libkb.API 18 responseFn func() getUpgradeRes 19 } 20 21 func (t *testAPIServer) GetDecode(mctx libkb.MetaContext, arg libkb.APIArg, resp libkb.APIResponseWrapper) error { 22 *(resp.(*getUpgradeRes)) = t.responseFn() 23 return nil 24 } 25 26 type testChatHelper struct { 27 libkb.ChatHelper 28 } 29 30 func (t *testChatHelper) UpgradeKBFSToImpteam(context.Context, string, chat1.TLFID, bool) error { 31 return nil 32 } 33 34 func TestBackgroundTLFUpdater(t *testing.T) { 35 tc := libkb.SetupTest(t, "TestBackgroundTLFUpdater", 1) 36 defer tc.Cleanup() 37 _, err := kbtest.CreateAndSignupFakeUser("gregr", tc.G) 38 require.NoError(t, err) 39 40 api := &testAPIServer{} 41 u := NewBackgroundTLFUpdater(tc.G) 42 u.testingDisableKBFS = true 43 u.testingAPIServer = api 44 u.testingChatHelper = &testChatHelper{} 45 upgradeCh := make(chan keybase1.TLFID, 5) 46 u.upgradeCh = &upgradeCh 47 48 refTLFID := keybase1.TLFID("hi") 49 f := func() getUpgradeRes { 50 return getUpgradeRes{ 51 GetTLFForUpgradeRes: NewGetTLFForUpgradeResWithTlfavailable(GetTLFForUpgradeAvailableRes{ 52 TlfID: refTLFID, 53 })} 54 } 55 api.responseFn = f 56 clock := clockwork.NewFakeClock() 57 u.clock = clock 58 u.Run() 59 attempt := func(attempt int) { 60 clock.BlockUntil(attempt) 61 clock.Advance(time.Hour) 62 select { 63 case tlfID := <-upgradeCh: 64 require.Equal(t, refTLFID, tlfID) 65 case <-time.After(20 * time.Second): 66 require.Fail(t, "no upgrade") 67 } 68 } 69 attempt(1) 70 mctx := libkb.NewMetaContextForTest(tc) 71 err = u.Shutdown(mctx) 72 require.NoError(t, err) 73 }