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

     1  package externals
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	libkb "github.com/keybase/client/go/libkb"
    10  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    11  	clockwork "github.com/keybase/clockwork"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func newTestResolverCache(g *libkb.GlobalContext) (*libkb.ResolverImpl, clockwork.FakeClock) {
    16  	clock := clockwork.NewFakeClockAt(time.Now())
    17  	g.SetClock(clock)
    18  	res := libkb.NewResolverImpl()
    19  	res.EnableCaching(libkb.NewMetaContextBackground(g))
    20  	return res, clock
    21  }
    22  
    23  var tracyUID = keybase1.UID("eb72f49f2dde6429e5d78003dae0c919")
    24  
    25  func TestResolveSimple(t *testing.T) {
    26  	t.Skip()
    27  	tc := setupTest(t, "resolveSimple", 1)
    28  	defer tc.Cleanup()
    29  
    30  	r, clock := newTestResolverCache(tc.G)
    31  	m := libkb.NewMetaContextForTest(tc)
    32  
    33  	goodResolve := func(s string) {
    34  		res := r.Resolve(m, s)
    35  		err := res.GetError()
    36  		require.NoError(t, err)
    37  		require.Equal(t, res.GetUID(), tracyUID)
    38  	}
    39  
    40  	goodResolve("eb72f49f2dde6429e5d78003dae0c919@uid")
    41  	require.True(t, r.Stats.Eq(0, 0, 0, 0, 0))
    42  
    43  	goodResolve("t_tracy@keybase")
    44  	require.True(t, r.Stats.Eq(1, 0, 0, 0, 0))
    45  
    46  	goodResolve("t_tracy@keybase")
    47  	require.True(t, r.Stats.Eq(1, 0, 0, 0, 1))
    48  
    49  	clock.Advance(libkb.ResolveCacheMaxAge * 10)
    50  	goodResolve("t_tracy@keybase")
    51  	require.True(t, r.Stats.Eq(1, 1, 0, 0, 1))
    52  
    53  	goodResolve("t_tracy@rooter")
    54  	require.True(t, r.Stats.Eq(2, 1, 0, 0, 1))
    55  
    56  	clock.Advance(libkb.ResolveCacheMaxAgeMutable / 2)
    57  	goodResolve("t_tracy@rooter")
    58  	require.True(t, r.Stats.Eq(2, 1, 0, 0, 2))
    59  
    60  	clock.Advance(libkb.ResolveCacheMaxAgeMutable * 2)
    61  	goodResolve("t_tracy@rooter")
    62  	require.True(t, r.Stats.Eq(2, 1, 1, 0, 2))
    63  
    64  	res := r.Resolve(m, "tacovontaco@twitter")
    65  	err := res.GetError()
    66  	require.Error(t, err)
    67  	terr, ok := err.(libkb.ResolutionError)
    68  	require.True(t, ok)
    69  	require.True(t, strings.Contains(terr.Msg, "ambiguous"))
    70  }
    71  
    72  func TestResolveNeedUsername(t *testing.T) {
    73  	ctx := context.Background()
    74  	tc := setupTest(t, "resolveSimple", 1)
    75  	defer tc.Cleanup()
    76  
    77  	r, clock := newTestResolverCache(tc.G)
    78  	goodResolve := func(s string) {
    79  		lctx := libkb.WithLogTag(ctx, "RSLV")
    80  		res := r.ResolveFullExpressionNeedUsername(libkb.NewMetaContext(lctx, tc.G), s)
    81  		err := res.GetError()
    82  		require.NoError(t, err)
    83  		require.Equal(t, res.GetUID(), tracyUID)
    84  	}
    85  	goodResolve("t_tracy")
    86  	require.True(t, r.Stats.Eq(1, 0, 0, 0, 0))
    87  
    88  	goodResolve("t_tracy")
    89  	require.True(t, r.Stats.Eq(1, 0, 0, 0, 1))
    90  
    91  	clock.Advance(libkb.ResolveCacheMaxAge * 10)
    92  	goodResolve("t_tracy")
    93  	require.True(t, r.Stats.EqWithDiskHits(1, 1, 0, 0, 1, 1))
    94  
    95  	goodResolve("uid:" + string(tracyUID))
    96  	require.True(t, r.Stats.EqWithDiskHits(2, 1, 0, 0, 1, 1))
    97  
    98  	goodResolve("uid:" + string(tracyUID))
    99  	require.True(t, r.Stats.EqWithDiskHits(2, 1, 0, 0, 2, 1))
   100  
   101  	clock.Advance(libkb.ResolveCacheMaxAge * 10)
   102  
   103  	// At this point, the uid resolution is out of memory cache,
   104  	// and we don't write it to disk. So we're going to totally miss
   105  	// the cache here.
   106  	goodResolve("uid:" + string(tracyUID))
   107  	require.True(t, r.Stats.EqWithDiskHits(2, 2, 0, 0, 2, 1))
   108  }