This repository was archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcat.spec.ts
95 lines (74 loc) · 2.53 KB
/
cat.spec.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
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import drain from 'it-drain'
import toBuffer from 'it-to-buffer'
import { unixfs, type UnixFS } from '../src/index.js'
import { createShardedDirectory } from './fixtures/create-sharded-directory.js'
import { smallFile } from './fixtures/files.js'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
describe('cat', () => {
let blockstore: Blockstore
let fs: UnixFS
let emptyDirCid: CID
beforeEach(async () => {
blockstore = new MemoryBlockstore()
fs = unixfs({ blockstore })
emptyDirCid = await fs.addDirectory()
})
it('reads a small file', async () => {
const cid = await fs.addBytes(smallFile)
const bytes = await toBuffer(fs.cat(cid))
expect(bytes).to.equalBytes(smallFile)
})
it('reads a file with an offset', async () => {
const offset = 10
const cid = await fs.addBytes(smallFile)
const bytes = await toBuffer(fs.cat(cid, {
offset
}))
expect(bytes).to.equalBytes(smallFile.subarray(offset))
})
it('reads a file with a length', async () => {
const length = 10
const cid = await fs.addBytes(smallFile)
const bytes = await toBuffer(fs.cat(cid, {
length
}))
expect(bytes).to.equalBytes(smallFile.subarray(0, length))
})
it('reads a file with an offset and a length', async () => {
const offset = 2
const length = 5
const cid = await fs.addBytes(smallFile)
const bytes = await toBuffer(fs.cat(cid, {
offset,
length
}))
expect(bytes).to.equalBytes(smallFile.subarray(offset, offset + length))
})
it('refuses to read a directory', async () => {
await expect(drain(fs.cat(emptyDirCid))).to.eventually.be.rejected
.with.property('code', 'ERR_NOT_A_FILE')
})
it('refuses to read missing blocks', async () => {
const cid = await fs.addBytes(smallFile)
await blockstore.delete(cid)
expect(blockstore.has(cid)).to.be.false()
await expect(drain(fs.cat(cid, {
offline: true
}))).to.eventually.be.rejected
.with.property('code', 'ERR_NOT_FOUND')
})
it('reads file from inside a sharded directory', async () => {
const dirCid = await createShardedDirectory(blockstore)
const fileCid = await fs.addBytes(smallFile)
const path = 'new-file.txt'
const updatedCid = await fs.cp(fileCid, dirCid, path)
const bytes = await toBuffer(fs.cat(updatedCid, {
path
}))
expect(bytes).to.deep.equal(smallFile)
})
})