import assert from 'node:assert/strict'; import { test } from 'node:test'; import { browsersFor, detectOs, openUrlFor } from '../public/browsers.js'; const URL = 'https://www.instagram.com/p/ABC/?a=1&b=2'; test('each browser gets the scheme it actually answers to', () => { assert.equal(openUrlFor(URL, 'chrome', 'macos'), 'googlechromes://www.instagram.com/p/ABC/?a=1&b=2'); assert.equal(openUrlFor(URL, 'chrome', 'ios'), 'googlechromes://www.instagram.com/p/ABC/?a=1&b=2'); assert.equal(openUrlFor('http://example.com/a', 'chrome', 'ios'), 'googlechrome://example.com/a'); // Edge folds the scheme into its own on iOS and prefixes it on macOS. assert.equal(openUrlFor(URL, 'edge', 'macos'), `microsoft-edge:${URL}`); assert.equal(openUrlFor(URL, 'edge', 'ios'), 'microsoft-edge-https://www.instagram.com/p/ABC/?a=1&b=2'); // The parameter form has to be encoded, or the original query string ends // up read as the opener's own. assert.equal(openUrlFor(URL, 'orion', 'macos'), `orion://open-url?url=${encodeURIComponent(URL)}`); assert.equal(openUrlFor(URL, 'firefox', 'ios'), `firefox://open-url?url=${encodeURIComponent(URL)}`); }); test('a browser with no scheme on this system keeps the plain link', () => { // Firefox on macOS has none, and a dead scheme opens nothing at all -- // worse than opening in the wrong browser. assert.equal(openUrlFor(URL, 'firefox', 'macos'), URL); assert.equal(openUrlFor(URL, 'default', 'ios'), URL); assert.equal(openUrlFor(URL, 'nonesuch', 'ios'), URL); }); test('only ordinary web links are rewritten', () => { assert.equal(openUrlFor('mailto:a@b.c', 'chrome', 'ios'), 'mailto:a@b.c'); assert.equal(openUrlFor('/reddit/r/a/comments/b', 'chrome', 'ios'), '/reddit/r/a/comments/b'); }); test('only the browsers reachable on that system are offered', () => { assert.deepEqual(browsersFor('macos').map((b) => b.id), ['default', 'chrome', 'edge', 'orion']); assert.deepEqual(browsersFor('ios').map((b) => b.id), ['default', 'chrome', 'edge', 'firefox', 'orion']); }); test('an iPad is told from a Mac by its touch points, not its platform string', () => { assert.equal(detectOs({ platform: 'iPhone', maxTouchPoints: 5 }), 'ios'); assert.equal(detectOs({ platform: 'MacIntel', maxTouchPoints: 5 }), 'ios'); assert.equal(detectOs({ platform: 'MacIntel', maxTouchPoints: 0 }), 'macos'); assert.equal(detectOs({}), 'macos'); });