waybetter/packages/shell/tests/linkDrop.test.ts
CRT b2a91ff7b3 Extract the wayBetter framework core from wayBetter for X
Site-agnostic parts of the X app, behind a small site adapter:

- crates/waybetter-host: webview-label security policy and tab rect
  clamp, account profiles with native-owned accounts (AccountRules),
  plugin store with bundled-plugin upgrades (PluginHost), signed
  data-only hotfix channel, update scheduling and crash handshake, tab
  payloads/link routing/tab-chrome validation, and (feature "tauri")
  webview helpers and the Windows system menu.
- packages/runtime: DOM hub, request hub, plugin API core, OKLab theme
  color model, split-view rail hiding, link routing and account
  reporting, each configured by a SiteAdapter.
- packages/shell: address bar, pane, link-drop, profile, palette and
  update-chip models, and window controls with the Tauri API injected.

Tests moved/adapted from the app: 45 Rust (headless), 40 TypeScript.
2026-09-28 03:01:33 +07:00

56 lines
3.5 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { carriesLink, dropUrl, placementFor, webUrl } from '../src/linkDrop.ts';
const data = (entries: Record<string, string>) => (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);
});