1package sqlite
2
3import (
4 "testing"
5 "time"
6
7 "github.com/bnema/dumber/internal/domain/entity"
8)
9
10func TestSortRankedHistoryMatches(t *testing.T) {
11 now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
12 older := now.Add(-24 * time.Hour)
13
14 items := []rankedHistoryMatch{
15 {
16 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 1, URL: "z-url", VisitCount: 5, LastVisited: now}, Score: 10},
17 score: 10,
18 exactURLMatch: false,
19 prefixURL: false,
20 urlMatch: false,
21 prefixTitle: false,
22 },
23 {
24 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 2, URL: "a-url", VisitCount: 5, LastVisited: now}, Score: 20},
25 score: 20,
26 exactURLMatch: false,
27 prefixURL: false,
28 urlMatch: false,
29 prefixTitle: false,
30 },
31 {
32 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 3, URL: "b-url", VisitCount: 5, LastVisited: now}, Score: 10},
33 score: 10,
34 exactURLMatch: true,
35 prefixURL: false,
36 urlMatch: false,
37 prefixTitle: false,
38 },
39 {
40 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 4, URL: "c-url", VisitCount: 5, LastVisited: now}, Score: 10},
41 score: 10,
42 exactURLMatch: false,
43 prefixURL: true,
44 urlMatch: false,
45 prefixTitle: false,
46 },
47 {
48 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 5, URL: "d-url", VisitCount: 5, LastVisited: now}, Score: 10},
49 score: 10,
50 exactURLMatch: false,
51 prefixURL: false,
52 urlMatch: true,
53 prefixTitle: false,
54 },
55 {
56 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 6, URL: "e-url", VisitCount: 5, LastVisited: now}, Score: 10},
57 score: 10,
58 exactURLMatch: false,
59 prefixURL: false,
60 urlMatch: false,
61 prefixTitle: true,
62 },
63 {
64 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 7, URL: "f-url", VisitCount: 10, LastVisited: now}, Score: 10},
65 score: 10,
66 exactURLMatch: false,
67 prefixURL: false,
68 urlMatch: false,
69 prefixTitle: false,
70 },
71 {
72 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 8, URL: "g-url", VisitCount: 5, LastVisited: now}, Score: 10},
73 score: 10,
74 exactURLMatch: false,
75 prefixURL: false,
76 urlMatch: false,
77 prefixTitle: false,
78 },
79 {
80 match: entity.HistoryMatch{Entry: &entity.HistoryEntry{ID: 9, URL: "h-url", VisitCount: 5, LastVisited: older}, Score: 10},
81 score: 10,
82 exactURLMatch: false,
83 prefixURL: false,
84 urlMatch: false,
85 prefixTitle: false,
86 },
87 }
88
89 sortRankedHistoryMatches(items)
90
91 wantOrder := []int64{2, 3, 4, 5, 6, 7, 8, 1, 9}
92 for i, want := range wantOrder {
93 got := items[i].match.Entry.ID
94 if got != want {
95 t.Errorf("position %d: want ID %d, got ID %d", i, want, got)
96 }
97 }
98}