This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathutils-ipfs-path.spec.js
79 lines (62 loc) · 2.7 KB
/
utils-ipfs-path.spec.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const CID = require('cids')
const ipfsPath = require('../src/utils/ipfs-path')
describe('utils/ipfs-path', () => {
it('should parse input as string CID', () => {
const input = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn'
const { cid, path } = ipfsPath(input)
expect(cid.toBaseEncodedString()).to.equal('QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn')
expect(path).to.equal('')
})
it('should parse input as buffer CID', () => {
const input = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
const { cid, path } = ipfsPath(input)
expect(cid.toBaseEncodedString()).to.equal('zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS')
expect(path).to.equal('')
})
it('should parse input as CID instance', () => {
const input = new CID('zdpuArHMUAYi3VtD3f7iSkXxYK9xo687SoNf5stAQNCMzd77k')
const { cid, path } = ipfsPath(input)
expect(cid.equals(input)).to.equal(true)
expect(path).to.equal('')
})
it('should parse input as string with path and without namespace', () => {
const input = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn/path/to'
const { cid, path } = ipfsPath(input)
expect(cid.toBaseEncodedString()).to.equal('QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn')
expect(path).to.equal('/path/to')
})
it('should parse input as string without leading slash', () => {
const input = 'ipfs/QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn/path/to'
const { cid, path } = ipfsPath(input)
expect(cid.toBaseEncodedString()).to.equal('QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn')
expect(path).to.equal('/path/to')
})
it('should parse input as string with trailing slash', () => {
const input = '/ipfs/QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn/path/to/'
const { cid, path } = ipfsPath(input)
expect(cid.toBaseEncodedString()).to.equal('QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn')
expect(path).to.equal('/path/to')
})
it('should throw on unknown namespace', () => {
const input = '/junk/stuff'
expect(() => ipfsPath(input)).to.throw('unknown namespace: junk')
})
it('should throw on invalid CID in string', () => {
const input = '/ipfs/notACID/some/path'
expect(() => ipfsPath(input)).to.throw('invalid CID')
})
it('should throw on invalid CID in buffer', () => {
const input = Buffer.from('notaCID')
expect(() => ipfsPath(input)).to.throw('invalid CID')
})
it('should throw on invalid path', () => {
const input = 42
expect(() => ipfsPath(input)).to.throw('invalid path')
})
})