Repository navigation
Expand file tree
/
Copy pathlib.test.js
More file actions
91 lines (73 loc) · 2.63 KB
/
Copy pathlib.test.js
File metadata and controls
91 lines (73 loc) · 2.63 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
import { describe, it, expect } from 'vitest';
import { isMastodonUrl, parseUrl, formatTime, stripHtml } from './lib.js';
describe('isMastodonUrl', () => {
it('accepts a standard post URL', () => {
expect(isMastodonUrl('https://mastodon.social/@alice/123456789')).toBe(true);
});
it('accepts a subdomain instance', () => {
expect(isMastodonUrl('https://fosstodon.org/@bob/987654321')).toBe(true);
});
it('rejects a profile URL (no numeric ID)', () => {
expect(isMastodonUrl('https://mastodon.social/@alice')).toBe(false);
});
it('rejects a non-Mastodon URL', () => {
expect(isMastodonUrl('https://example.com/foo/bar')).toBe(false);
});
it('rejects an empty string', () => {
expect(isMastodonUrl('')).toBe(false);
});
it('rejects a malformed URL', () => {
expect(isMastodonUrl('not a url')).toBe(false);
});
it('rejects a URL where the last segment is not purely numeric', () => {
expect(isMastodonUrl('https://mastodon.social/@alice/abc123')).toBe(false);
});
});
describe('parseUrl', () => {
it('returns instance and id for a valid URL', () => {
expect(parseUrl('https://mastodon.social/@alice/123456789')).toEqual({
instance: 'https://mastodon.social',
id: '123456789',
});
});
it('preserves the full origin as instance', () => {
expect(parseUrl('https://fosstodon.org/@bob/111')).toEqual({
instance: 'https://fosstodon.org',
id: '111',
});
});
it('returns null for a non-numeric trailing segment', () => {
expect(parseUrl('https://mastodon.social/@alice/notanid')).toBeNull();
});
it('returns null for a malformed URL', () => {
expect(parseUrl('garbage')).toBeNull();
});
it('returns null for an empty string', () => {
expect(parseUrl('')).toBeNull();
});
});
describe('stripHtml', () => {
it('strips tags and returns plain text', () => {
expect(stripHtml('<p>Hello <strong>world</strong></p>')).toBe('Hello world');
});
it('returns an empty string for empty input', () => {
expect(stripHtml('')).toBe('');
});
it('returns plain text unchanged', () => {
expect(stripHtml('no tags here')).toBe('no tags here');
});
it('handles nested tags', () => {
expect(stripHtml('<div><span>foo</span> <em>bar</em></div>')).toBe('foo bar');
});
});
describe('formatTime', () => {
it('returns a formatted date string', () => {
const result = formatTime('2025-03-15T14:30:00Z');
expect(result).toBeDefined();
expect(typeof result).toBe('string');
expect(result).toContain('2025');
});
it('returns "Invalid Date" for bad input', () => {
expect(formatTime('not-a-date')).toBe('Invalid Date');
});
});