-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathurl-detection.test.ts
More file actions
215 lines (186 loc) · 7.25 KB
/
url-detection.test.ts
File metadata and controls
215 lines (186 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* URL Detection Tests
*
* Tests for the UrlRegexProvider to ensure plain text URLs
* are correctly detected and made clickable.
*/
import { describe, expect, test } from 'bun:test';
import { UrlRegexProvider } from './providers/url-regex-provider';
import type { ILink } from './types';
/**
* Mock terminal for testing
*/
function createMockTerminal(lineText: string) {
const cells = Array.from(lineText).map((char) => ({
getCodepoint: () => char.codePointAt(0) || 0,
}));
return {
buffer: {
active: {
getLine: (y: number) => {
if (y !== 0) return undefined;
return {
length: cells.length,
getCell: (x: number) => cells[x],
};
},
},
},
};
}
/**
* Helper to get links from provider
*/
function getLinks(lineText: string): Promise<ILink[] | undefined> {
const terminal = createMockTerminal(lineText) as any;
const provider = new UrlRegexProvider(terminal);
return new Promise((resolve) => {
provider.provideLinks(0, resolve);
});
}
describe('URL Detection', () => {
test('detects HTTPS URLs', async () => {
const links = await getLinks('Visit https://github.com for code');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://github.com');
expect(links?.[0].range.start.x).toBe(6);
// End is inclusive - last character is at index 23 (https://github.com is 19 chars, starts at 6)
expect(links?.[0].range.end.x).toBe(23);
});
test('detects HTTP URLs', async () => {
const links = await getLinks('Check http://example.com');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('http://example.com');
});
test('detects mailto: links', async () => {
const links = await getLinks('Email: mailto:test@example.com');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('mailto:test@example.com');
});
test('detects ssh:// URLs', async () => {
const links = await getLinks('Connect via ssh://user@server.com');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('ssh://user@server.com');
});
test('detects git:// URLs', async () => {
const links = await getLinks('Clone git://github.com/repo.git');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('git://github.com/repo.git');
});
test('detects ftp:// URLs', async () => {
const links = await getLinks('Download ftp://files.example.com/file');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('ftp://files.example.com/file');
});
test('strips trailing period', async () => {
const links = await getLinks('Check https://example.com.');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com');
// Should NOT include the trailing period
expect(links?.[0].text.endsWith('.')).toBe(false);
});
test('strips trailing comma', async () => {
const links = await getLinks('See https://example.com, or else');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com');
});
test('strips trailing parenthesis', async () => {
const links = await getLinks('(see https://example.com)');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com');
});
test('strips trailing exclamation', async () => {
const links = await getLinks('Visit https://example.com!');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com');
});
test('handles multiple URLs on same line', async () => {
const links = await getLinks('https://a.com and https://b.com');
expect(links).toBeDefined();
expect(links?.length).toBe(2);
expect(links?.[0].text).toBe('https://a.com');
expect(links?.[1].text).toBe('https://b.com');
});
test('returns undefined when no URL present', async () => {
const links = await getLinks('No URLs here');
expect(links).toBeUndefined();
});
test('handles URLs with query parameters', async () => {
const links = await getLinks('https://example.com?foo=bar&baz=qux');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com?foo=bar&baz=qux');
});
test('handles URLs with fragments', async () => {
const links = await getLinks('https://example.com/page#section');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com/page#section');
});
test('handles URLs with ports', async () => {
const links = await getLinks('https://example.com:8080/path');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com:8080/path');
});
test('does not detect file paths', async () => {
const links = await getLinks('/home/user/file.txt');
expect(links).toBeUndefined();
});
test('does not detect relative paths', async () => {
const links = await getLinks('./relative/path');
expect(links).toBeUndefined();
});
test('link has activate function', async () => {
const links = await getLinks('https://example.com');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(typeof links?.[0].activate).toBe('function');
});
test('detects tel: URLs', async () => {
const links = await getLinks('Call tel:+1234567890');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('tel:+1234567890');
});
test('detects URLs with balanced parentheses (Wikipedia)', async () => {
const links = await getLinks('https://en.wikipedia.org/wiki/Rust_(programming_language)');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://en.wikipedia.org/wiki/Rust_(programming_language)');
});
test('strips unbalanced trailing paren from wrapped URL', async () => {
const links = await getLinks('(see https://en.wikipedia.org/wiki/Rust_(programming_language))');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://en.wikipedia.org/wiki/Rust_(programming_language)');
});
test('handles URL with multiple parenthesized path segments', async () => {
const links = await getLinks('https://example.com/a_(b)/c_(d)');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com/a_(b)/c_(d)');
});
test('handles URL with nested parentheses', async () => {
const links = await getLinks('https://example.com/foo_(bar_(baz))');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toBe('https://example.com/foo_(bar_(baz))');
});
test('detects magnet: URLs', async () => {
const links = await getLinks('Download magnet:?xt=urn:btih:abc123');
expect(links).toBeDefined();
expect(links?.length).toBe(1);
expect(links?.[0].text).toContain('magnet:?xt=urn:btih:abc123');
});
});