9bfd16c6e5
Markdown links, autolinks and bare http(s)/www. URLs open externally through url_launcher (http, https and mailto only) in assistant markdown and operator messages; code is never linkified. Links use the host primary colour with an underline that thickens on hover, and offer Copy link on long press and right-click without breaking text selection.
144 lines
4.6 KiB
Dart
144 lines
4.6 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown.dart';
|
|
|
|
List<String> _targets(String text) => [
|
|
for (final link in findTextLinks(text)) link.target,
|
|
];
|
|
|
|
void main() {
|
|
group('bare URL detection', () {
|
|
test('finds http, https and www. links in prose', () {
|
|
expect(
|
|
_targets('a http://x.com/a b https://y.org/b?q=1#h c www.z.net/c'),
|
|
['http://x.com/a', 'https://y.org/b?q=1#h', 'www.z.net/c'],
|
|
);
|
|
});
|
|
|
|
test('trims trailing sentence punctuation', () {
|
|
for (final suffix in ['.', ',', ';', ':', '!', '?', '...', '."', "'"]) {
|
|
expect(_targets('see https://x.com/a$suffix'), [
|
|
'https://x.com/a',
|
|
], reason: suffix);
|
|
}
|
|
});
|
|
|
|
test('trims an unbalanced closing bracket but keeps a balanced one', () {
|
|
expect(_targets('(see https://x.com/a)'), ['https://x.com/a']);
|
|
expect(_targets('[https://x.com/a]'), ['https://x.com/a']);
|
|
expect(_targets('https://en.wikipedia.org/wiki/Dart_(language).'), [
|
|
'https://en.wikipedia.org/wiki/Dart_(language)',
|
|
]);
|
|
expect(_targets('(https://x.com/Foo_(bar))'), [
|
|
'https://x.com/Foo_(bar)',
|
|
]);
|
|
});
|
|
|
|
test('reports exact ranges', () {
|
|
const text = 'go to www.example.com.';
|
|
expect(findTextLinks(text), [
|
|
const ChatTextLink(6, 21, 'www.example.com'),
|
|
]);
|
|
});
|
|
|
|
test('never starts inside a word, path or address', () {
|
|
expect(_targets('xhttps://x.com'), isEmpty);
|
|
expect(_targets('mail@www.example.com'), isEmpty);
|
|
expect(_targets('a/www.example.com'), isEmpty);
|
|
expect(_targets('node.www.example.com'), isEmpty);
|
|
});
|
|
|
|
test('rejects a scheme or www. without a host', () {
|
|
expect(_targets('https:// and http://'), isEmpty);
|
|
expect(_targets('www. and www.example'), isEmpty);
|
|
});
|
|
|
|
test('skips inline code and fenced code', () {
|
|
expect(_targets('run `curl https://x.com/a` then https://y.com/b'), [
|
|
'https://y.com/b',
|
|
]);
|
|
expect(_targets('```\ncurl https://x.com/a\n```\nhttps://y.com/b'), [
|
|
'https://y.com/b',
|
|
]);
|
|
expect(_targets('```\nunclosed https://x.com'), isEmpty);
|
|
});
|
|
|
|
test('a markdown link in plain text links its target in place', () {
|
|
const text = '[docs](https://x.com/d)';
|
|
expect(findTextLinks(text), [
|
|
const ChatTextLink(7, 22, 'https://x.com/d'),
|
|
]);
|
|
});
|
|
});
|
|
|
|
group('scheme filter', () {
|
|
test('allows http, https, mailto and www. hosts', () {
|
|
expect(chatLinkUri('https://x.com')?.toString(), 'https://x.com');
|
|
expect(chatLinkUri('HTTP://x.com')?.host, 'x.com');
|
|
expect(chatLinkUri('mailto:a@b.com')?.scheme, 'mailto');
|
|
expect(chatLinkUri('www.x.com/a')?.toString(), 'https://www.x.com/a');
|
|
});
|
|
|
|
test('neutralises every other scheme or a missing host', () {
|
|
for (final unsafe in [
|
|
'javascript:alert(1)',
|
|
'JavaScript:alert(1)',
|
|
'data:text/html,<h1>x</h1>',
|
|
'file:///etc/passwd',
|
|
'ftp://x.com',
|
|
'vbscript:msgbox',
|
|
'mailto:',
|
|
'https://',
|
|
'//x.com',
|
|
'/artifacts/a.txt',
|
|
'x.com',
|
|
]) {
|
|
expect(chatLinkUri(unsafe), isNull, reason: unsafe);
|
|
expect(isSafeMarkdownLink(unsafe), isFalse, reason: unsafe);
|
|
}
|
|
});
|
|
});
|
|
|
|
group('markdown inline links', () {
|
|
List<MarkdownLink> links(String text) =>
|
|
parseInline(text).whereType<MarkdownLink>().toList();
|
|
|
|
test('angle-bracket autolinks show their body without brackets', () {
|
|
final found = links('<https://x.com/a> and <me@x.com> and <b>');
|
|
expect(found.map((link) => link.url), [
|
|
'https://x.com/a',
|
|
'mailto:me@x.com',
|
|
]);
|
|
expect(
|
|
(found.first.spans.single as MarkdownPlain).text,
|
|
'https://x.com/a',
|
|
);
|
|
});
|
|
|
|
test(
|
|
'a markdown destination keeps balanced parentheses and drops a title',
|
|
() {
|
|
expect(
|
|
links(
|
|
'[w](https://x.org/Dart_(language)) and [t](https://x.com "T")',
|
|
).map((link) => link.url),
|
|
['https://x.org/Dart_(language)', 'https://x.com'],
|
|
);
|
|
},
|
|
);
|
|
|
|
test('bare www. and trimmed URLs become links outside inline code', () {
|
|
final spans = parseInline(
|
|
'see www.x.com, `https://code.com` (https://y.com).',
|
|
);
|
|
expect(spans.whereType<MarkdownLink>().map((link) => link.url), [
|
|
'www.x.com',
|
|
'https://y.com',
|
|
]);
|
|
expect(
|
|
spans.whereType<MarkdownInlineCode>().single.text,
|
|
'https://code.com',
|
|
);
|
|
});
|
|
});
|
|
}
|