import assert from 'node:assert/strict'; import test from 'node:test'; import { carriesLink, dropUrl, placementFor, webUrl } from '../src/linkDrop.ts'; const data = (entries: Record) => (type: string) => entries[type] ?? ''; test('only plain http(s) links are accepted', () => { assert.equal(webUrl('https://x.com/jack/status/20'), 'https://x.com/jack/status/20'); assert.equal(webUrl(' http://example.com '), 'http://example.com/'); for (const bad of ['javascript:alert(1)', 'file:///C:/Windows/win.ini', 'data:text/html,hi', 'about:blank', 'https://user:pw@example.com/', 'x.com/home', '', null, undefined, `https://example.com/${'a'.repeat(5000)}`]) { assert.equal(webUrl(bad), null, String(bad)); } }); test('uri-list wins, skipping comments and blank lines', () => { const get = data({ 'text/uri-list': '# dragged from a browser\r\n\r\nhttps://x.com/home\r\nhttps://example.com/', 'text/plain': 'https://other.test/' }); assert.equal(dropUrl(get), 'https://x.com/home'); }); test('falls back to text/x-moz-url, then text/plain, then the reported link', () => { assert.equal(dropUrl(data({ 'text/x-moz-url': 'https://example.com/a\nExample' })), 'https://example.com/a'); assert.equal(dropUrl(data({ 'text/plain': 'https://x.com/i/lists/1\nsecond line' })), 'https://x.com/i/lists/1'); assert.equal(dropUrl(data({ 'text/plain': 'just some words' }), 'https://x.com/jack'), 'https://x.com/jack'); assert.equal(dropUrl(() => { throw new Error('protected mode'); }, 'https://x.com/jack'), 'https://x.com/jack'); }); test('a dangerous dropped link never beats a safe reported one, and nothing unsafe opens', () => { assert.equal(dropUrl(data({ 'text/uri-list': 'javascript:alert(1)' }), 'https://x.com/home'), 'https://x.com/home'); assert.equal(dropUrl(data({ 'text/uri-list': 'file:///etc/passwd' })), null); assert.equal(dropUrl(data({}), 'javascript:alert(1)'), null); }); test('file drags are left to the page; link drags are recognised', () => { assert.equal(carriesLink(['text/uri-list', 'text/plain']), true); assert.equal(carriesLink(['text/x-moz-url']), true); assert.equal(carriesLink(['Files', 'text/uri-list']), false); assert.equal(carriesLink(['text/plain']), false); assert.equal(carriesLink([]), false); }); test('pane edges split in that direction, center and tabs open a tab in that group', () => { assert.deepEqual(placementFor({ position: 'right', groupId: 'g1' }), { referenceGroup: 'g1', direction: 'right' }); assert.deepEqual(placementFor({ position: 'left', groupId: 'g1' }), { referenceGroup: 'g1', direction: 'left' }); assert.deepEqual(placementFor({ position: 'top', groupId: 'g1' }), { referenceGroup: 'g1', direction: 'above' }); assert.deepEqual(placementFor({ position: 'bottom', groupId: 'g1' }), { referenceGroup: 'g1', direction: 'below' }); assert.deepEqual(placementFor({ position: 'center', groupId: 'g1' }), { referenceGroup: 'g1', direction: 'within' }); assert.deepEqual(placementFor({ position: 'center', groupId: 'g1', tabIndex: 2 }), { referenceGroup: 'g1', direction: 'within', index: 2 }); assert.deepEqual(placementFor({ position: 'center', groupId: 'g1', tabIndex: 0 }), { referenceGroup: 'g1', direction: 'within', index: 0 }); }); test('dock outer edges split the whole dock; no target means the default tab', () => { assert.deepEqual(placementFor({ position: 'left' }), { direction: 'left' }); assert.deepEqual(placementFor({ position: 'bottom' }), { direction: 'below' }); assert.equal(placementFor({ position: 'center' }), undefined); });