-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-address.ts
73 lines (62 loc) · 2.26 KB
/
test-address.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
/* eslint-disable max-nested-callbacks */
import { assert } from 'aegir/chai'
import { CID } from 'multiformats/cid'
import { Address } from '@/manifest/address.js'
describe('Address', () => {
let address: Address
const prefix = '/hldb/'
const hash = 'bafyreiewienqflf5skie6c5xlmasqj7ndj24qwt6fxkwqdwzggfmpdmqai'
const string = prefix + hash
describe('Class', () => {
it('exposes class properties', () => {
assert.isOk(Address.asAddress)
})
describe('.asAddress', () => {
it('returns an address from a cid', () => {
address = Address.asAddress({ cid: CID.parse(hash) }) as Address
assert.strictEqual(address.toString(), string)
})
it('returns the same instance if possible', () => {
const _address = Address.asAddress(address)
assert.strictEqual(_address, address)
})
it('returns null if unable to coerce', () => {
assert.strictEqual(Address.asAddress(), null)
assert.strictEqual(Address.asAddress(''), null)
assert.strictEqual(Address.asAddress({ cid: hash }), null)
})
})
describe('.fromString', () => {
it('returns address from string', () => {
address = Address.fromString(string)
assert.strictEqual(address.cid.toString(), hash)
})
it('throws given invalid string', () => {
assert.throws(() => Address.fromString(''))
assert.throws(() => Address.fromString(prefix))
assert.throws(() => Address.fromString(hash))
})
})
})
describe('Instance', () => {
it('exposes instance properties', () => {
assert.strictEqual(address.toString(), string)
})
it('.toString', () => {
assert.strictEqual(address.toString(), string)
})
describe('.equals', () => {
it('returns true if the addresses are the same', () => {
const _address = Address.fromString(string)
assert.isOk(address.equals(address))
assert.isOk(address.equals(_address))
})
it('returns false if the addresses are different', () => {
const _hash =
'bafyreib2caa6txg46uhpt43bgfnfk3wzfdbz4n2frn2brbrjtjuambgd6i'
const _address = Address.fromString(prefix + _hash)
assert.strictEqual(address.equals(_address), false)
})
})
})
})