-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathextract_history_test.go
More file actions
106 lines (88 loc) · 3.28 KB
/
extract_history_test.go
File metadata and controls
106 lines (88 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package safari
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupSafariHistoryDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://github.com", "github.com", 100),
insertHistoryItem(2, "https://go.dev", "go.dev", 50),
insertHistoryItem(3, "https://example.com", "example.com", 200),
// Item 1 has two visits — extractHistories must deduplicate.
insertHistoryVisit(1, 1, 704067600.0, "GitHub"),
insertHistoryVisit(2, 1, 705067600.0, "GitHub - Latest"),
insertHistoryVisit(3, 2, 703067600.0, "The Go Programming Language"),
insertHistoryVisit(4, 3, 700067600.0, "Example Domain"),
)
}
func TestExtractHistories(t *testing.T) {
path := setupSafariHistoryDB(t)
got, err := extractHistories(path)
require.NoError(t, err)
require.Len(t, got, 3)
// Sorted by visit count descending (most visited first).
assert.Equal(t, 200, got[0].VisitCount)
assert.Equal(t, 100, got[1].VisitCount)
assert.Equal(t, 50, got[2].VisitCount)
// Verify field mapping.
assert.Equal(t, "https://example.com", got[0].URL)
assert.Equal(t, "https://github.com", got[1].URL)
assert.Equal(t, "https://go.dev", got[2].URL)
assert.False(t, got[0].LastVisit.IsZero())
}
func TestExtractHistories_Dedup(t *testing.T) {
path := setupSafariHistoryDB(t)
got, err := extractHistories(path)
require.NoError(t, err)
// 3 history_items, not 4 visits.
require.Len(t, got, 3)
// GitHub (item 1) should have the later visit_time and its title.
for _, h := range got {
if h.URL == "https://github.com" {
// 705067600 + 978307200 = 1683374800 (unix)
assert.Equal(t, int64(1683374800), h.LastVisit.Unix())
// Title must come from the latest visit row, not an arbitrary one.
assert.Equal(t, "GitHub - Latest", h.Title)
return
}
}
t.Fatal("expected https://github.com in results")
}
func TestCountHistories(t *testing.T) {
path := setupSafariHistoryDB(t)
count, err := countHistories(path)
require.NoError(t, err)
assert.Equal(t, 3, count)
}
func TestCountHistories_Empty(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema})
count, err := countHistories(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
func TestExtractHistories_NullTitle(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://null.test", "null.test", 1),
// Visit with NULL title — COALESCE should return empty string.
`INSERT INTO history_visits (id, history_item, visit_time) VALUES (1, 1, 700000000.0)`,
)
got, err := extractHistories(path)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "https://null.test", got[0].URL)
assert.Empty(t, got[0].Title)
}
func TestCoredataTimestamp(t *testing.T) {
// A zero Core Data value is treated as "no timestamp" and returns
// the zero time.Time rather than literal 2001-01-01 — matches the
// convention used by the Chromium and Firefox helpers.
assert.True(t, coredataTimestamp(0).IsZero())
// Known value: 700000000 Core Data = 1678307200 Unix
ts2 := coredataTimestamp(700000000)
assert.Equal(t, int64(1678307200), ts2.Unix())
}