This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
55 lines (50 loc) · 1.97 KB
/
test.js
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
const assert = require('assert').equal
const { toAv, toBv } = require('./core')
const Av = require('./av'), Bv = require('./bv')
const invalidBv = 'BV1sZ411p7j9'
const videos = {
2: 'BV1xx411c7mD',
7: 'BV1xx411c7m9',
114514: 'BV1fx411c7Z8',
170001: 'BV17x411w7KC',
314159: 'BV1gx411F7pF',
1111111: 'BV1ox411K7kp',
3141592: 'BV12s41127if',
12345678: 'BV1fx411v7eo',
55553519: 'BV1K4411N7Vw',
56093550: 'BV1N4411V7Wi',
58793314: 'BV11t41157eU',
98393198: 'BV1iE411F7wR',
98630396: 'BV1p7411m7Uk',
}
const toAvAliases = [ toAv, Av.fromBv, Bv.toAv ]
const toBvAliases = [ toBv, Bv.fromAv, Av.toBv ]
for (let aid in videos) {
for (const fn of toAvAliases) {
assert(fn(videos[aid]), `av${aid}`, 'should encode AV correctly')
assert(fn('bv' + videos[aid].substring(2)), `av${aid}`, 'should encode AV with lowercase bv')
let err
try { fn(170001) } catch (e) { err = e }
assert(err && err.code, 'EINVALID_BV', 'should throw EINVALID_BV when passing a non-string')
err = null
try { fn('notBv') } catch (e) { err = e }
assert(err && err.code, 'EINVALID_BV', 'should throw EINVALID_BV when passing a non-BV string')
}
for (const fn of toBvAliases) {
assert(fn(Number(aid)), videos[aid], 'should encode BV correctly')
assert(fn(BigInt(aid)), videos[aid], 'should encode BV with BigInt')
assert(fn(aid), videos[aid], 'should encode BV with string')
assert(fn(`av${aid}`), videos[aid], 'should encode BV with avxxxxxx')
let err
try { fn({ not: 'av' }) } catch (e) { err = e }
assert(err && err.code, 'EINVALID_AV', 'should throw EINVALID_AV when passing a non-(string|number|bigint)')
err = null
try { fn('notAv') } catch (e) { err = e }
assert(err && err.code, 'EINVALID_AV', 'should throw EINVALID_AV when passing a non-AV string')
}
}
for (const fn of toAvAliases) {
let err
try { fn(invalidBv) } catch (e) { err = e }
assert(err && err.code, 'EAV_NEGATIVE', 'should throw EAV_NEGATIVE when passing invalid bv')
}