|
| 1 | +import { mkdtemp, rm } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 5 | +import { readBrowserSiteNames } from '@/main/browser-import/chromium-site-names' |
| 6 | + |
| 7 | +const sqliteAvailable = await import('node:sqlite').then( |
| 8 | + () => true, |
| 9 | + () => false |
| 10 | +) |
| 11 | + |
| 12 | +interface FixturePage { |
| 13 | + url: string |
| 14 | + title: string |
| 15 | + visitCount?: number |
| 16 | +} |
| 17 | + |
| 18 | +let directory: string |
| 19 | + |
| 20 | +beforeEach(async () => { |
| 21 | + directory = await mkdtemp(join(tmpdir(), 'sim-site-names-test-')) |
| 22 | +}) |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + await rm(directory, { recursive: true, force: true }) |
| 26 | +}) |
| 27 | + |
| 28 | +async function writeHistoryDatabase(pages: FixturePage[]): Promise<string> { |
| 29 | + const { DatabaseSync } = await import('node:sqlite') |
| 30 | + const path = join(directory, 'History') |
| 31 | + const database = new DatabaseSync(path) |
| 32 | + database.exec(` |
| 33 | + CREATE TABLE urls ( |
| 34 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 35 | + url LONGVARCHAR, |
| 36 | + title LONGVARCHAR, |
| 37 | + visit_count INTEGER DEFAULT 0 NOT NULL, |
| 38 | + typed_count INTEGER DEFAULT 0 NOT NULL, |
| 39 | + last_visit_time INTEGER NOT NULL, |
| 40 | + hidden INTEGER DEFAULT 0 NOT NULL |
| 41 | + ); |
| 42 | + `) |
| 43 | + const insert = database.prepare( |
| 44 | + 'INSERT INTO urls (url, title, visit_count, last_visit_time) VALUES (?, ?, ?, 0)' |
| 45 | + ) |
| 46 | + for (const page of pages) insert.run(page.url, page.title, page.visitCount ?? 1) |
| 47 | + database.close() |
| 48 | + return path |
| 49 | +} |
| 50 | + |
| 51 | +describe.skipIf(!sqliteAvailable)('readBrowserSiteNames', () => { |
| 52 | + it('learns a site’s name from the part of its titles that never changes', async () => { |
| 53 | + const path = await writeHistoryDatabase([ |
| 54 | + { |
| 55 | + url: 'https://mail.google.com/mail/u/0/#inbox', |
| 56 | + title: 'Inbox (12) - ada@example.com - Gmail', |
| 57 | + }, |
| 58 | + { url: 'https://mail.google.com/mail/u/0/#sent', title: 'Sent - ada@example.com - Gmail' }, |
| 59 | + { |
| 60 | + url: 'https://mail.google.com/mail/u/0/#drafts', |
| 61 | + title: 'Drafts (2) - ada@example.com - Gmail', |
| 62 | + }, |
| 63 | + ]) |
| 64 | + |
| 65 | + const names = await readBrowserSiteNames(path, new Set(['mail.google.com'])) |
| 66 | + |
| 67 | + // Nothing here hardcodes Gmail — it is the only segment on every page. |
| 68 | + expect(names.get('mail.google.com')).toBe('Gmail') |
| 69 | + }) |
| 70 | + |
| 71 | + it('finds a name that leads the title rather than trailing it', async () => { |
| 72 | + const path = await writeHistoryDatabase([ |
| 73 | + { url: 'https://github.com/', title: 'GitHub - Where the world builds software' }, |
| 74 | + { url: 'https://github.com/pulls', title: 'GitHub - Pull requests' }, |
| 75 | + ]) |
| 76 | + |
| 77 | + const names = await readBrowserSiteNames(path, new Set(['github.com'])) |
| 78 | + |
| 79 | + expect(names.get('github.com')).toBe('GitHub') |
| 80 | + }) |
| 81 | + |
| 82 | + it('handles a title with no separator at all', async () => { |
| 83 | + const path = await writeHistoryDatabase([{ url: 'https://example.com/', title: 'Example' }]) |
| 84 | + |
| 85 | + const names = await readBrowserSiteNames(path, new Set(['example.com'])) |
| 86 | + |
| 87 | + expect(names.get('example.com')).toBe('Example') |
| 88 | + }) |
| 89 | + |
| 90 | + it('prefers the shorter candidate when pages are split evenly', async () => { |
| 91 | + const path = await writeHistoryDatabase([ |
| 92 | + { url: 'https://linear.app/a', title: 'Linear - Issue tracking' }, |
| 93 | + { url: 'https://linear.app/b', title: 'Issue tracking - Linear' }, |
| 94 | + ]) |
| 95 | + |
| 96 | + const names = await readBrowserSiteNames(path, new Set(['linear.app'])) |
| 97 | + |
| 98 | + expect(names.get('linear.app')).toBe('Linear') |
| 99 | + }) |
| 100 | + |
| 101 | + it('names only the hosts being imported, never the rest of the history', async () => { |
| 102 | + const path = await writeHistoryDatabase([ |
| 103 | + { url: 'https://mail.google.com/', title: 'Gmail' }, |
| 104 | + { url: 'https://somewhere-private.example/', title: 'Private' }, |
| 105 | + ]) |
| 106 | + |
| 107 | + const names = await readBrowserSiteNames(path, new Set(['mail.google.com'])) |
| 108 | + |
| 109 | + expect([...names.keys()]).toEqual(['mail.google.com']) |
| 110 | + }) |
| 111 | + |
| 112 | + it('asks for nothing when there are no hosts to name', async () => { |
| 113 | + const path = await writeHistoryDatabase([{ url: 'https://example.com/', title: 'Example' }]) |
| 114 | + |
| 115 | + expect(await readBrowserSiteNames(path, new Set())).toEqual(new Map()) |
| 116 | + }) |
| 117 | + |
| 118 | + it('ignores pages that are not on the web', async () => { |
| 119 | + const path = await writeHistoryDatabase([ |
| 120 | + { url: 'chrome-extension://abc/page.html', title: 'Extension' }, |
| 121 | + { url: 'file:///Users/ada/notes.html', title: 'Notes' }, |
| 122 | + ]) |
| 123 | + |
| 124 | + const names = await readBrowserSiteNames(path, new Set(['abc', ''])) |
| 125 | + |
| 126 | + expect(names.size).toBe(0) |
| 127 | + }) |
| 128 | + |
| 129 | + it('rejects a whole-title tagline as a name', async () => { |
| 130 | + const long = 'A very long marketing sentence that is plainly not what this site is called' |
| 131 | + const path = await writeHistoryDatabase([{ url: 'https://example.com/', title: long }]) |
| 132 | + |
| 133 | + const names = await readBrowserSiteNames(path, new Set(['example.com'])) |
| 134 | + |
| 135 | + expect(names.has('example.com')).toBe(false) |
| 136 | + }) |
| 137 | + |
| 138 | + it('survives an unreadable history rather than failing the import', async () => { |
| 139 | + const names = await readBrowserSiteNames(join(directory, 'absent'), new Set(['example.com'])) |
| 140 | + |
| 141 | + expect(names).toEqual(new Map()) |
| 142 | + }) |
| 143 | + |
| 144 | + it('imports the same name every time for the same profile', async () => { |
| 145 | + const pages = [ |
| 146 | + { url: 'https://example.com/a', title: 'Alpha - Example' }, |
| 147 | + { url: 'https://example.com/b', title: 'Beta - Sample' }, |
| 148 | + ] |
| 149 | + const path = await writeHistoryDatabase(pages) |
| 150 | + |
| 151 | + const first = await readBrowserSiteNames(path, new Set(['example.com'])) |
| 152 | + const second = await readBrowserSiteNames(path, new Set(['example.com'])) |
| 153 | + |
| 154 | + expect(first.get('example.com')).toBe(second.get('example.com')) |
| 155 | + }) |
| 156 | +}) |
0 commit comments