-
Notifications
You must be signed in to change notification settings - Fork 554
/
handle.test.ts
99 lines (89 loc) · 3.64 KB
/
handle.test.ts
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
import {
ensureValid,
ensureServiceConstraints,
normalizeAndEnsureValid,
} from '../src'
describe('handle validation', () => {
const domains = ['.bsky.app', '.test']
it('allows valid handles', () => {
ensureValid('john.test')
ensureValid('jan.test')
ensureValid('a234567890123456789.test')
ensureValid('john2.test')
ensureValid('john-john.test')
ensureValid('john.bsky.app')
ensureValid('0john.test')
ensureValid('12345.test')
ensureValid('this.has.many.sub.domains.test')
})
it('allows handles that pass service constraints', () => {
ensureServiceConstraints('john.test', domains)
ensureServiceConstraints('jan.test', domains)
ensureServiceConstraints('a234567890123456789.test', domains)
ensureServiceConstraints('john2.test', domains)
ensureServiceConstraints('john-john.test', domains)
ensureServiceConstraints('john.bsky.app', domains)
ensureServiceConstraints('0john.test', domains)
ensureServiceConstraints('12345.test', domains)
})
it('allows punycode handles', () => {
ensureValid('xn--ls8h.test') // 💩.test
ensureValid('xn--bcher-kva.tld') // bücher.tld
})
it('throws on invalid handles', () => {
const expectThrow = (handle: string, err: string) => {
expect(() => ensureValid(handle)).toThrow(err)
}
expectThrow(
'did:john.test',
'Cannot register a handle that starts with `did:`',
)
expectThrow('jaymome-johnber123456.test', 'Handle too long')
expectThrow('jay.mome-johnber1234567890.subdomain.test', 'Handle too long')
expectThrow(
'short.loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.test',
'Handle too long',
)
expectThrow(
'short.short.short.short.short.short.short.test',
'Handle too long',
)
expectThrow('john..test', 'Invalid characters in handle')
expectThrow('jo_hn.test', 'Invalid characters in handle')
expectThrow('-john.test', 'Invalid characters in handle')
expectThrow('john-.test', 'Invalid characters in handle')
expectThrow('.john.test', 'Invalid characters in handle')
expectThrow('jo!hn.test', 'Invalid characters in handle')
expectThrow('jo%hn.test', 'Invalid characters in handle')
expectThrow('jo&hn.test', 'Invalid characters in handle')
expectThrow('jo@hn.test', 'Invalid characters in handle')
expectThrow('jo*hn.test', 'Invalid characters in handle')
expectThrow('jo|hn.test', 'Invalid characters in handle')
expectThrow('jo:hn.test', 'Invalid characters in handle')
expectThrow('jo/hn.test', 'Invalid characters in handle')
expectThrow('john💩.test', 'Invalid characters in handle')
expectThrow('bücher.test', 'Invalid characters in handle')
})
it('throw on handles that violate service constraints', () => {
const expectThrow = (handle: string, err: string) => {
expect(() => ensureServiceConstraints(handle, domains)).toThrow(err)
}
expectThrow('john.bsky.io', 'Not a supported handle domain')
expectThrow('john.com', 'Not a supported handle domain')
expectThrow('j.test', 'Handle too short')
expectThrow('uk.test', 'Handle too short')
expectThrow('john.test.bsky.app', 'Invalid characters in handle')
expectThrow('about.test', 'Reserved handle')
expectThrow('atp.test', 'Reserved handle')
expectThrow('barackobama.test', 'Reserved handle')
})
it('normalizes handles', () => {
const normalized = normalizeAndEnsureValid('JoHn.TeST')
expect(normalized).toBe('john.test')
})
it('throws on invalid normalized handles', () => {
expect(() => normalizeAndEnsureValid('JoH!n.TeST')).toThrow(
'Invalid characters in handle',
)
})
})