github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/seahist_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestGetIndex(t *testing.T) {
     9  	sh := make(SearchHistory)
    10  	now := time.Now()
    11  
    12  	// Add some entries to the search history
    13  	sh[now.Add(-1*time.Hour)] = "search1"
    14  	sh[now.Add(-2*time.Hour)] = "search2"
    15  	sh[now.Add(-3*time.Hour)] = "search3"
    16  
    17  	tests := []struct {
    18  		index         int
    19  		expectedTerm  string
    20  		expectedExist bool
    21  	}{
    22  		{index: 0, expectedTerm: "search1", expectedExist: true},
    23  		{index: 1, expectedTerm: "search2", expectedExist: true},
    24  		{index: 2, expectedTerm: "search3", expectedExist: true},
    25  		{index: 3, expectedTerm: "", expectedExist: false},
    26  	}
    27  
    28  	const newestFirst = true
    29  	for i, test := range tests {
    30  		got := sh.GetIndex(test.index, newestFirst)
    31  		if got != test.expectedTerm {
    32  			t.Errorf("Test %d: expected %q, got %q", i, test.expectedTerm, got)
    33  		}
    34  	}
    35  }
    36  
    37  func TestKeepNewest(t *testing.T) {
    38  	sh := make(SearchHistory)
    39  	now := time.Now()
    40  
    41  	// Add some entries to the search history
    42  	sh[now.Add(-1*time.Hour)] = "search1"
    43  	sh[now.Add(-2*time.Hour)] = "search2"
    44  	sh[now.Add(-3*time.Hour)] = "search3"
    45  
    46  	tests := []struct {
    47  		n           int
    48  		expectedLen int
    49  	}{
    50  		{n: 2, expectedLen: 2},
    51  		{n: 1, expectedLen: 1},
    52  		{n: 3, expectedLen: 3},
    53  		{n: 0, expectedLen: 0},
    54  	}
    55  
    56  	for i, test := range tests {
    57  		newSh := sh.KeepNewest(test.n)
    58  		gotLen := newSh.Len()
    59  		if gotLen != test.expectedLen {
    60  			t.Errorf("Test %d: expected length %d, got %d", i, test.expectedLen, gotLen)
    61  		}
    62  	}
    63  }