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 pathls.spec.ts
135 lines (110 loc) · 3.92 KB
/
ls.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import all from 'it-all'
import drain from 'it-drain'
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('ls', () => {
let blockstore: Blockstore
let fs: UnixFS
let emptyDirCid: CID
beforeEach(async () => {
blockstore = new MemoryBlockstore()
fs = unixfs({ blockstore })
emptyDirCid = await fs.addDirectory()
})
it('should require a path', async () => {
// @ts-expect-error invalid args
await expect(all(fs.ls())).to.eventually.be.rejected()
})
it('lists files in a directory', async () => {
const path = 'path'
const data = Uint8Array.from([0, 1, 2, 3])
const fileCid = await fs.addBytes(data)
const dirCid = await fs.cp(fileCid, emptyDirCid, path)
const files = await all(fs.ls(dirCid))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: fileCid,
name: path,
size: BigInt(data.byteLength),
type: 'raw'
}])
})
it('lists a file', async () => {
const path = 'path'
const data = Uint8Array.from([0, 1, 2, 3])
const fileCid = await fs.addBytes(data, {
rawLeaves: false
})
const dirCid = await fs.cp(fileCid, emptyDirCid, path)
const files = await all(fs.ls(dirCid, {
path
}))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: fileCid,
size: BigInt(data.byteLength),
type: 'file'
}])
})
it('lists a raw node', async () => {
const path = 'path'
const data = Uint8Array.from([0, 1, 2, 3])
const fileCid = await fs.addBytes(data)
const dirCid = await fs.cp(fileCid, emptyDirCid, path)
const files = await all(fs.ls(dirCid, {
path
}))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: fileCid,
size: BigInt(data.byteLength),
type: 'raw'
}])
})
it('lists a sharded directory contents', async () => {
const fileCount = 1001
const shardedDirCid = await createShardedDirectory(blockstore, fileCount)
const files = await all(fs.ls(shardedDirCid))
expect(files.length).to.equal(fileCount)
files.forEach(file => {
// should be a file
expect(file.type).to.equal('raw')
})
})
it('lists a file inside a sharded directory directly', async () => {
const shardedDirCid = await createShardedDirectory(blockstore)
const files = await all(fs.ls(shardedDirCid))
const fileName = files[0].name
// should be able to ls new file directly
const directFiles = await all(fs.ls(shardedDirCid, {
path: fileName
}))
expect(directFiles.length).to.equal(1)
expect(directFiles.filter(file => file.name === fileName)).to.be.ok()
})
it('lists the contents of a directory inside a sharded directory', async () => {
const shardedDirCid = await createShardedDirectory(blockstore)
const dirName = `subdir-${Math.random()}`
const fileName = `small-file-${Math.random()}.txt`
const fileCid = await fs.addBytes(Uint8Array.from([0, 1, 2, 3, 4]))
const containingDirectoryCid = await fs.cp(fileCid, emptyDirCid, fileName)
const updatedShardCid = await fs.cp(containingDirectoryCid, shardedDirCid, dirName)
const files = await all(fs.ls(updatedShardCid, {
path: dirName
}))
expect(files.length).to.equal(1)
expect(files.filter(file => file.name === fileName)).to.be.ok()
})
it('refuses to list missing blocks', async () => {
const cid = await fs.addBytes(smallFile)
await blockstore.delete(cid)
expect(blockstore.has(cid)).to.be.false()
await expect(drain(fs.ls(cid, {
offline: true
}))).to.eventually.be.rejected
.with.property('code', 'ERR_NOT_FOUND')
})
})