1package homepage
2
3import (
4 "context"
5 "encoding/json"
6 "testing"
7 "time"
8
9 "github.com/bnema/dumber/internal/application/port"
10 portmocks "github.com/bnema/dumber/internal/application/port/mocks"
11 "github.com/bnema/dumber/internal/domain/entity"
12 "github.com/stretchr/testify/mock"
13 "github.com/stretchr/testify/require"
14)
15
16func TestHandleTimelineWindowRejectsIncompleteCursor(t *testing.T) {
17 t.Parallel()
18
19 tests := map[string]json.RawMessage{
20 "before without beforeId": json.RawMessage(`{"requestId":"req-1","before":"2026-04-25T09:00:00Z"}`),
21 "beforeId without before": json.RawMessage(`{"requestId":"req-1","beforeId":42}`),
22 "negative beforeId without before": json.RawMessage(`{"requestId":"req-1","beforeId":-1}`),
23 }
24 for name, payload := range tests {
25 t.Run(name, func(t *testing.T) {
26 t.Parallel()
27
28 history := portmocks.NewMockHomepageHistory(t)
29 handler := NewHistoryHandlers(history).HandleTimelineWindow()
30
31 got, err := handler.Handle(context.Background(), port.WebViewID(0), payload)
32 require.NoError(t, err)
33
34 resp, ok := got.(Response)
35 require.True(t, ok)
36 require.False(t, resp.Success)
37 require.Contains(t, resp.Error, "cursor")
38 })
39 }
40}
41
42func TestHandleTimelineWindowPassesCompleteCursor(t *testing.T) {
43 t.Parallel()
44
45 before := time.Date(2026, 4, 25, 9, 0, 0, 0, time.UTC)
46 history := portmocks.NewMockHomepageHistory(t)
47 history.EXPECT().
48 GetRecentWindow(mock.Anything, before, int64(42), "example.com").
49 Return(&entity.HistoryWindow{Before: before, CursorID: 42}, nil).
50 Once()
51
52 handler := NewHistoryHandlers(history).HandleTimelineWindow()
53 payload := json.RawMessage(`{"requestId":"req-2","before":"2026-04-25T09:00:00Z","beforeId":42,"domain":"example.com"}`)
54
55 got, err := handler.Handle(context.Background(), port.WebViewID(0), payload)
56 require.NoError(t, err)
57
58 resp, ok := got.(Response)
59 require.True(t, ok)
60 require.True(t, resp.Success)
61}