v0.23.2 bfd5eb4

search_history_test.go RawHistoryFolder

1package usecase_test
2
3import (
4	"errors"
5	"testing"
6	"time"
7
8	"github.com/bnema/dumber/internal/application/usecase"
9	"github.com/bnema/dumber/internal/domain/entity"
10	repomocks "github.com/bnema/dumber/internal/domain/repository/mocks"
11	"github.com/stretchr/testify/assert"
12	"github.com/stretchr/testify/mock"
13	"github.com/stretchr/testify/require"
14)
15
16func TestSearchHistoryUseCase_GetRecentSince_ReturnsEntries(t *testing.T) {
17	ctx := testContext()
18
19	historyRepo := repomocks.NewMockHistoryRepository(t)
20
21	entries := []*entity.HistoryEntry{
22		{ID: 1, URL: "https://example.com", Title: "Example", LastVisited: time.Now()},
23		{ID: 2, URL: "https://github.com", Title: "GitHub", LastVisited: time.Now().Add(-24 * time.Hour)},
24	}
25
26	historyRepo.EXPECT().GetRecentSince(mock.Anything, 30).Return(entries, nil)
27
28	uc := usecase.NewSearchHistoryUseCase(historyRepo)
29
30	result, err := uc.GetRecentSince(ctx, 30)
31	require.NoError(t, err)
32	require.Len(t, result, 2)
33	assert.Equal(t, "https://example.com", result[0].URL)
34	assert.Equal(t, "https://github.com", result[1].URL)
35}
36
37func TestSearchHistoryUseCase_GetRecentSince_ZeroMeansAll(t *testing.T) {
38	ctx := testContext()
39
40	historyRepo := repomocks.NewMockHistoryRepository(t)
41
42	entries := []*entity.HistoryEntry{
43		{ID: 1, URL: "https://example.com", Title: "Example"},
44	}
45
46	// When days == 0, should call GetAllRecentHistory
47	historyRepo.EXPECT().GetAllRecentHistory(mock.Anything).Return(entries, nil)
48
49	uc := usecase.NewSearchHistoryUseCase(historyRepo)
50
51	result, err := uc.GetRecentSince(ctx, 0)
52	require.NoError(t, err)
53	require.Len(t, result, 1)
54}
55
56func TestSearchHistoryUseCase_GetRecentSince_NegativeDaysDefaultsTo30(t *testing.T) {
57	ctx := testContext()
58
59	historyRepo := repomocks.NewMockHistoryRepository(t)
60
61	entries := []*entity.HistoryEntry{}
62
63	historyRepo.EXPECT().GetRecentSince(mock.Anything, 30).Return(entries, nil)
64
65	uc := usecase.NewSearchHistoryUseCase(historyRepo)
66
67	result, err := uc.GetRecentSince(ctx, -5)
68	require.NoError(t, err)
69	require.Empty(t, result)
70}
71
72func TestSearchHistoryUseCase_GetRecentSince_ReturnsErrorOnRepoFailure(t *testing.T) {
73	ctx := testContext()
74
75	historyRepo := repomocks.NewMockHistoryRepository(t)
76
77	historyRepo.EXPECT().GetRecentSince(mock.Anything, 7).Return(nil, errors.New("db connection failed"))
78
79	uc := usecase.NewSearchHistoryUseCase(historyRepo)
80
81	result, err := uc.GetRecentSince(ctx, 7)
82	require.Error(t, err)
83	assert.Nil(t, result)
84	assert.Contains(t, err.Error(), "failed to get recent history")
85}
86
87func TestSearchHistoryUseCase_GetMostVisited_ReturnsEntries(t *testing.T) {
88	ctx := testContext()
89
90	historyRepo := repomocks.NewMockHistoryRepository(t)
91
92	entries := []*entity.HistoryEntry{
93		{ID: 1, URL: "https://github.com", Title: "GitHub", VisitCount: 100},
94		{ID: 2, URL: "https://example.com", Title: "Example", VisitCount: 50},
95	}
96
97	historyRepo.EXPECT().GetMostVisited(mock.Anything, 30).Return(entries, nil)
98
99	uc := usecase.NewSearchHistoryUseCase(historyRepo)
100
101	result, err := uc.GetMostVisited(ctx, 30)
102	require.NoError(t, err)
103	require.Len(t, result, 2)
104	assert.Equal(t, "https://github.com", result[0].URL)
105	assert.Equal(t, int64(100), result[0].VisitCount)
106}
107
108func TestSearchHistoryUseCase_GetMostVisited_ZeroMeansAll(t *testing.T) {
109	ctx := testContext()
110
111	historyRepo := repomocks.NewMockHistoryRepository(t)
112
113	entries := []*entity.HistoryEntry{}
114
115	// When days == 0, should call GetAllMostVisited
116	historyRepo.EXPECT().GetAllMostVisited(mock.Anything).Return(entries, nil)
117
118	uc := usecase.NewSearchHistoryUseCase(historyRepo)
119
120	result, err := uc.GetMostVisited(ctx, 0)
121	require.NoError(t, err)
122	require.Empty(t, result)
123}
124
125func TestSearchHistoryUseCase_GetMostVisited_ReturnsErrorOnRepoFailure(t *testing.T) {
126	ctx := testContext()
127
128	historyRepo := repomocks.NewMockHistoryRepository(t)
129
130	historyRepo.EXPECT().GetMostVisited(mock.Anything, 14).Return(nil, errors.New("query failed"))
131
132	uc := usecase.NewSearchHistoryUseCase(historyRepo)
133
134	result, err := uc.GetMostVisited(ctx, 14)
135	require.Error(t, err)
136	assert.Nil(t, result)
137	assert.Contains(t, err.Error(), "failed to get most visited history")
138}
139
140func TestSearchHistoryUseCase_GetMostVisited_ReturnsEmptyWhenNoHistory(t *testing.T) {
141	ctx := testContext()
142
143	historyRepo := repomocks.NewMockHistoryRepository(t)
144
145	historyRepo.EXPECT().GetMostVisited(mock.Anything, 30).Return([]*entity.HistoryEntry{}, nil)
146
147	uc := usecase.NewSearchHistoryUseCase(historyRepo)
148
149	result, err := uc.GetMostVisited(ctx, 30)
150	require.NoError(t, err)
151	require.NotNil(t, result)
152	assert.Empty(t, result)
153}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close