|
| 1 | +// Test suite for the JS URL helpers, run via `node --test` |
| 2 | +// (or `npm test`, which the package.json "test" script wires to |
| 3 | +// `node --test js/`). |
| 4 | +// |
| 5 | +// The URL_MATCH_CASES array below is a MANUAL DUPLICATE of |
| 6 | +// src-tauri/src/url_fixtures.rs::URL_MATCH_CASES. The Rust file is the |
| 7 | +// source of truth — when you change a case there, change it here too. |
| 8 | +// A shared JSON file would avoid the duplication but would add a build |
| 9 | +// step; a duplicated array with this cross-reference is the pragmatic |
| 10 | +// zero-dependency choice. The Rust side independently asserts its own |
| 11 | +// table in urls::tests::urls_match_matches_shared_truth_table. |
| 12 | + |
| 13 | +import { test } from 'node:test'; |
| 14 | +import assert from 'node:assert/strict'; |
| 15 | +import { urlsMatch, normalizeUrlInput } from './urltest.mjs'; |
| 16 | + |
| 17 | +const URL_MATCH_CASES = [ |
| 18 | + ['https://example.com/', 'https://example.com/', true], |
| 19 | + ['https://example.com/', 'https://example.com', true], |
| 20 | + ['https://example.com/path', 'https://example.com/path/', true], |
| 21 | + ['https://example.com/?q=1', 'https://example.com?q=1', true], |
| 22 | + ['https://example.com/a', 'https://example.com/b', false], |
| 23 | + ['https://example.com/', 'http://example.com/', false], |
| 24 | + ['https://example.com/?q=1', 'https://example.com/?q=2', false], |
| 25 | + ['https://example.com/a#frag', 'https://example.com/a', true], |
| 26 | + ['https://a.example.com/', 'https://b.example.com/', false], |
| 27 | + ['https://user:pass@example.com/', 'https://example.com/', false], |
| 28 | + ['https://alice@example.com/', 'https://bob@example.com/', false], |
| 29 | + ['https://user:pass@example.com/path', 'https://user:pass@example.com/path/', true], |
| 30 | + ['https://u@example.com/', 'https://example.com/', false], |
| 31 | + ['not a url', 'not a url', true], |
| 32 | + ['not a url', 'https://example.com/', false], |
| 33 | +]; |
| 34 | + |
| 35 | +test('urlsMatch agrees with the shared Rust/JS truth table', () => { |
| 36 | + URL_MATCH_CASES.forEach(([a, b, expected], i) => { |
| 37 | + assert.equal( |
| 38 | + urlsMatch(a, b), |
| 39 | + expected, |
| 40 | + `case #${i}: urlsMatch(${JSON.stringify(a)}, ${JSON.stringify(b)}) expected ${expected}`, |
| 41 | + ); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +test('normalizeUrlInput adds https scheme to bare hosts', () => { |
| 46 | + assert.equal(normalizeUrlInput('example.com'), 'https://example.com/'); |
| 47 | +}); |
| 48 | + |
| 49 | +test('normalizeUrlInput sends multi-word input to DuckDuckGo', () => { |
| 50 | + assert.equal( |
| 51 | + normalizeUrlInput('rust web framework'), |
| 52 | + 'https://duckduckgo.com/?q=rust%20web%20framework', |
| 53 | + ); |
| 54 | +}); |
| 55 | + |
| 56 | +test('normalizeUrlInput sends dotless non-port input to DuckDuckGo', () => { |
| 57 | + assert.equal( |
| 58 | + normalizeUrlInput('localhost search term'), |
| 59 | + 'https://duckduckgo.com/?q=localhost%20search%20term', |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +test('normalizeUrlInput keeps an explicit https URL canonical', () => { |
| 64 | + assert.equal(normalizeUrlInput('https://example.com/foo?q=1'), 'https://example.com/foo?q=1'); |
| 65 | +}); |
| 66 | + |
| 67 | +test('normalizeUrlInput prepends https to a dotted non-http scheme (matches injection.js URL bar)', () => { |
| 68 | + // The injected URL bar's heuristic only special-cases spaces and |
| 69 | + // dotless/portless input; anything else with a `.` gets `https://` |
| 70 | + // prepended. So `ftp://example.com` (which contains a dot) becomes |
| 71 | + // `https://ftp//example.com` — odd, but it's the documented URL-bar |
| 72 | + // behavior, NOT a search. (The landing page `src/main.js` is stricter |
| 73 | + // and would send this to search; the two paths genuinely differ.) |
| 74 | + // Pinning the URL-bar semantics here so a future "cleanup" doesn't |
| 75 | + // silently change what the strip does. |
| 76 | + assert.equal(normalizeUrlInput('ftp://example.com'), 'https://ftp//example.com'); |
| 77 | +}); |
| 78 | + |
| 79 | +test('normalizeUrlInput trims whitespace', () => { |
| 80 | + assert.equal(normalizeUrlInput(' example.com '), 'https://example.com/'); |
| 81 | +}); |
| 82 | + |
| 83 | +test('normalizeUrlInput rejects empty input', () => { |
| 84 | + assert.equal(normalizeUrlInput(''), null); |
| 85 | + assert.equal(normalizeUrlInput(' '), null); |
| 86 | +}); |
0 commit comments