github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libpages/stats_test.go (about)

     1  package libpages
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type fileInfoForActivesGetterTest time.Time
    12  
    13  func (f fileInfoForActivesGetterTest) Name() string       { return "" }
    14  func (f fileInfoForActivesGetterTest) Size() int64        { return 0 }
    15  func (f fileInfoForActivesGetterTest) Mode() os.FileMode  { return 0 }
    16  func (f fileInfoForActivesGetterTest) ModTime() time.Time { return time.Time(f) }
    17  func (f fileInfoForActivesGetterTest) IsDir() bool        { return false }
    18  func (f fileInfoForActivesGetterTest) Sys() interface{}   { return nil }
    19  
    20  func makeFileInfoActivesGetterForTest(
    21  	tlfModTimes, hostModTimes []time.Time) fileinfoActivesGetter {
    22  	tlfs := make([]os.FileInfo, 0, len(tlfModTimes))
    23  	hosts := make([]os.FileInfo, 0, len(hostModTimes))
    24  	for _, m := range tlfModTimes {
    25  		tlfs = append(tlfs, fileInfoForActivesGetterTest(m))
    26  	}
    27  	for _, m := range hostModTimes {
    28  		hosts = append(hosts, fileInfoForActivesGetterTest(m))
    29  	}
    30  	return fileinfoActivesGetter{
    31  		tlfs:  tlfs,
    32  		hosts: hosts,
    33  	}
    34  }
    35  
    36  func TestGetActives(t *testing.T) {
    37  	now := time.Now()
    38  	getter := makeFileInfoActivesGetterForTest(
    39  		[]time.Time{
    40  			now.Add(-(time.Minute)),
    41  			now.Add(-(time.Minute * 12)),
    42  			now.Add(-(time.Hour + time.Minute)),
    43  			now.Add(-(time.Hour*24 + time.Minute)),
    44  			now.Add(-(time.Hour*24*7 + time.Minute)),
    45  		}, // tlfs
    46  		[]time.Time{
    47  			now.Add(-(time.Minute)),
    48  			now.Add(-(time.Minute * 12)),
    49  			now.Add(-(time.Hour + time.Minute)),
    50  			now.Add(-(time.Hour*24 + time.Minute)),
    51  			now.Add(-(time.Hour*24*7 + time.Minute)),
    52  		}, // hosts
    53  	)
    54  	tlfs, hosts, err := getter.GetActives(2 * time.Minute)
    55  	require.NoError(t, err)
    56  	require.Equal(t, 1, tlfs)
    57  	require.Equal(t, 1, hosts)
    58  
    59  	tlfs, hosts, err = getter.GetActives(time.Hour)
    60  	require.NoError(t, err)
    61  	require.Equal(t, 2, tlfs)
    62  	require.Equal(t, 2, hosts)
    63  
    64  	tlfs, hosts, err = getter.GetActives(time.Hour * 24)
    65  	require.NoError(t, err)
    66  	require.Equal(t, 3, tlfs)
    67  	require.Equal(t, 3, hosts)
    68  
    69  	tlfs, hosts, err = getter.GetActives(time.Hour * 24 * 7)
    70  	require.NoError(t, err)
    71  	require.Equal(t, 4, tlfs)
    72  	require.Equal(t, 4, hosts)
    73  
    74  	tlfs, hosts, err = getter.GetActives(time.Hour * 24 * 30)
    75  	require.NoError(t, err)
    76  	require.Equal(t, 5, tlfs)
    77  	require.Equal(t, 5, hosts)
    78  }