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