v0.29.0 21b3150

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_GetRecent_ZeroLimitMeansAll(t *testing.T) {
17	ctx := testContext()
18	historyRepo := repomocks.NewMockHistoryRepository(t)
19	entries := []*entity.HistoryEntry{{ID: 1, URL: "https://example.com", Title: "Example"}}
20	historyRepo.EXPECT().GetRecent(mock.Anything, 0, 0).Return(entries, nil).Once()
21
22	uc := usecase.NewSearchHistoryUseCase(historyRepo)
23	result, err := uc.GetRecent(ctx, 0, 0)
24
25	require.NoError(t, err)
26	require.Len(t, result, 1)
27}
28
29func TestSearchHistoryUseCase_GetRecent_NegativeLimitDefaultsToPageSize(t *testing.T) {
30	ctx := testContext()
31	historyRepo := repomocks.NewMockHistoryRepository(t)
32	historyRepo.EXPECT().GetRecent(mock.Anything, 50, 0).Return([]*entity.HistoryEntry{}, nil).Once()
33
34	uc := usecase.NewSearchHistoryUseCase(historyRepo)
35	result, err := uc.GetRecent(ctx, -1, 0)
36
37	require.NoError(t, err)
38	assert.Empty(t, result)
39}
40
41func TestSearchHistoryUseCase_GetRecentWindow_InvalidDomainReturnsValidationError(t *testing.T) {
42	ctx := testContext()
43	historyRepo := repomocks.NewMockHistoryRepository(t)
44	uc := usecase.NewSearchHistoryUseCase(historyRepo)
45
46	result, err := uc.GetRecentWindow(ctx, time.Now(), "///")
47
48	require.Error(t, err)
49	assert.Nil(t, result)
50	assert.Contains(t, err.Error(), "domain is required")
51}
52
53func TestSearchHistoryUseCase_GetRecentSince_ReturnsEntries(t *testing.T) {
54	ctx := testContext()
55
56	historyRepo := repomocks.NewMockHistoryRepository(t)
57
58	entries := []*entity.HistoryEntry{
59		{ID: 1, URL: "https://example.com", Title: "Example", LastVisited: time.Now()},
60		{ID: 2, URL: "https://github.com", Title: "GitHub", LastVisited: time.Now().Add(-24 * time.Hour)},
61	}
62
63	historyRepo.EXPECT().GetRecentSince(mock.Anything, 30).Return(entries, nil)
64
65	uc := usecase.NewSearchHistoryUseCase(historyRepo)
66
67	result, err := uc.GetRecentSince(ctx, 30)
68	require.NoError(t, err)
69	require.Len(t, result, 2)
70	assert.Equal(t, "https://example.com", result[0].URL)
71	assert.Equal(t, "https://github.com", result[1].URL)
72}
73
74func TestSearchHistoryUseCase_GetRecentSince_ZeroMeansAll(t *testing.T) {
75	ctx := testContext()
76
77	historyRepo := repomocks.NewMockHistoryRepository(t)
78
79	entries := []*entity.HistoryEntry{
80		{ID: 1, URL: "https://example.com", Title: "Example"},
81	}
82
83	// When days == 0, should call GetAllRecentHistory
84	historyRepo.EXPECT().GetAllRecentHistory(mock.Anything).Return(entries, nil)
85
86	uc := usecase.NewSearchHistoryUseCase(historyRepo)
87
88	result, err := uc.GetRecentSince(ctx, 0)
89	require.NoError(t, err)
90	require.Len(t, result, 1)
91}
92
93func TestSearchHistoryUseCase_GetRecentSince_NegativeDaysDefaultsTo30(t *testing.T) {
94	ctx := testContext()
95
96	historyRepo := repomocks.NewMockHistoryRepository(t)
97
98	entries := []*entity.HistoryEntry{}
99
100	historyRepo.EXPECT().GetRecentSince(mock.Anything, 30).Return(entries, nil)
101
102	uc := usecase.NewSearchHistoryUseCase(historyRepo)
103
104	result, err := uc.GetRecentSince(ctx, -5)
105	require.NoError(t, err)
106	require.Empty(t, result)
107}
108
109func TestSearchHistoryUseCase_GetRecentSince_ReturnsErrorOnRepoFailure(t *testing.T) {
110	ctx := testContext()
111
112	historyRepo := repomocks.NewMockHistoryRepository(t)
113
114	historyRepo.EXPECT().GetRecentSince(mock.Anything, 7).Return(nil, errors.New("db connection failed"))
115
116	uc := usecase.NewSearchHistoryUseCase(historyRepo)
117
118	result, err := uc.GetRecentSince(ctx, 7)
119	require.Error(t, err)
120	assert.Nil(t, result)
121	assert.Contains(t, err.Error(), "failed to get recent history")
122}
123
124func TestSearchHistoryUseCase_GetMostVisited_ReturnsEntries(t *testing.T) {
125	ctx := testContext()
126
127	historyRepo := repomocks.NewMockHistoryRepository(t)
128
129	entries := []*entity.HistoryEntry{
130		{ID: 1, URL: "https://github.com", Title: "GitHub", VisitCount: 100},
131		{ID: 2, URL: "https://example.com", Title: "Example", VisitCount: 50},
132	}
133
134	historyRepo.EXPECT().GetMostVisited(mock.Anything, 30).Return(entries, nil)
135
136	uc := usecase.NewSearchHistoryUseCase(historyRepo)
137
138	result, err := uc.GetMostVisited(ctx, 30)
139	require.NoError(t, err)
140	require.Len(t, result, 2)
141	assert.Equal(t, "https://github.com", result[0].URL)
142	assert.Equal(t, int64(100), result[0].VisitCount)
143}
144
145func TestSearchHistoryUseCase_GetMostVisited_ZeroMeansAll(t *testing.T) {
146	ctx := testContext()
147
148	historyRepo := repomocks.NewMockHistoryRepository(t)
149
150	entries := []*entity.HistoryEntry{}
151
152	// When days == 0, should call GetAllMostVisited
153	historyRepo.EXPECT().GetAllMostVisited(mock.Anything).Return(entries, nil)
154
155	uc := usecase.NewSearchHistoryUseCase(historyRepo)
156
157	result, err := uc.GetMostVisited(ctx, 0)
158	require.NoError(t, err)
159	require.Empty(t, result)
160}
161
162func TestSearchHistoryUseCase_GetMostVisited_ReturnsErrorOnRepoFailure(t *testing.T) {
163	ctx := testContext()
164
165	historyRepo := repomocks.NewMockHistoryRepository(t)
166
167	historyRepo.EXPECT().GetMostVisited(mock.Anything, 14).Return(nil, errors.New("query failed"))
168
169	uc := usecase.NewSearchHistoryUseCase(historyRepo)
170
171	result, err := uc.GetMostVisited(ctx, 14)
172	require.Error(t, err)
173	assert.Nil(t, result)
174	assert.Contains(t, err.Error(), "failed to get most visited history")
175}
176
177func TestSearchHistoryUseCase_GetMostVisited_ReturnsEmptyWhenNoHistory(t *testing.T) {
178	ctx := testContext()
179
180	historyRepo := repomocks.NewMockHistoryRepository(t)
181
182	historyRepo.EXPECT().GetMostVisited(mock.Anything, 30).Return([]*entity.HistoryEntry{}, nil)
183
184	uc := usecase.NewSearchHistoryUseCase(historyRepo)
185
186	result, err := uc.GetMostVisited(ctx, 30)
187	require.NoError(t, err)
188	require.NotNil(t, result)
189	assert.Empty(t, result)
190}
191
192func TestSearchHistoryUseCase_ClearRange_RecentRangeDeletesSinceCutoff(t *testing.T) {
193	ctx := testContext()
194	historyRepo := repomocks.NewMockHistoryRepository(t)
195	now := time.Now()
196	expectedCutoff := now.Add(-time.Hour)
197	historyRepo.EXPECT().DeleteSince(mock.Anything, mock.MatchedBy(func(since time.Time) bool {
198		return since.After(expectedCutoff.Add(-time.Minute)) && since.Before(expectedCutoff.Add(time.Minute))
199	})).Return(nil).Once()
200
201	uc := usecase.NewSearchHistoryUseCase(historyRepo)
202
203	err := uc.ClearRange(ctx, "hour")
204	require.NoError(t, err)
205}
206
207func TestSearchHistoryUseCase_ClearRange_TodayDeletesSinceLocalMidnight(t *testing.T) {
208	ctx := testContext()
209	historyRepo := repomocks.NewMockHistoryRepository(t)
210	now := time.Now()
211	expectedCutoff := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
212	historyRepo.EXPECT().DeleteSince(mock.Anything, mock.MatchedBy(func(since time.Time) bool {
213		return since.Equal(expectedCutoff)
214	})).Return(nil).Once()
215
216	uc := usecase.NewSearchHistoryUseCase(historyRepo)
217
218	err := uc.ClearRange(ctx, "day")
219	require.NoError(t, err)
220}
221
222func TestSearchHistoryUseCase_ClearRange_AllUsesClearAll(t *testing.T) {
223	ctx := testContext()
224
225	historyRepo := repomocks.NewMockHistoryRepository(t)
226
227	historyRepo.EXPECT().DeleteAll(mock.Anything).Return(nil)
228
229	uc := usecase.NewSearchHistoryUseCase(historyRepo)
230
231	err := uc.ClearRange(ctx, "all")
232	require.NoError(t, err)
233}
234
235func TestSearchHistoryUseCase_ClearRange_UnknownRangeDoesNotDelete(t *testing.T) {
236	ctx := testContext()
237
238	historyRepo := repomocks.NewMockHistoryRepository(t)
239
240	uc := usecase.NewSearchHistoryUseCase(historyRepo)
241
242	err := uc.ClearRange(ctx, "bogus")
243	require.Error(t, err)
244	assert.Contains(t, err.Error(), "unknown history delete range")
245}
246
247func TestSearchHistoryUseCase_GetRecentByDomainAllowsUnderscoreDomains(t *testing.T) {
248	ctx := testContext()
249	historyRepo := repomocks.NewMockHistoryRepository(t)
250	want := []*entity.HistoryEntry{{URL: "https://example_.com"}}
251	historyRepo.EXPECT().GetRecentByDomain(mock.Anything, "example_.com", 20, 0).Return(want, nil).Once()
252	uc := usecase.NewSearchHistoryUseCase(historyRepo)
253
254	result, err := uc.GetRecentByDomain(ctx, "example_.com", 20, 0)
255
256	require.NoError(t, err)
257	assert.Equal(t, want, result)
258}
259
260func TestSearchHistoryUseCase_DeleteByDomainAllowsUnderscoreDomains(t *testing.T) {
261	ctx := testContext()
262	historyRepo := repomocks.NewMockHistoryRepository(t)
263	historyRepo.EXPECT().DeleteByDomain(mock.Anything, "example_.com").Return(nil).Once()
264	uc := usecase.NewSearchHistoryUseCase(historyRepo)
265
266	err := uc.DeleteByDomain(ctx, "example_.com")
267
268	require.NoError(t, err)
269}

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