-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
160 lines (115 loc) · 3.97 KB
/
Copy pathtest.js
File metadata and controls
160 lines (115 loc) · 3.97 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const test = require('brittle')
const Hypercore = require('hypercore')
const tmp = require('test-tmp')
const b4a = require('b4a')
const ByteStream = require('./')
test('basic', async function (t) {
const { id, core } = await create(t, ['a', 'b', 'c', 'd', 'e'])
const all = await collect(new ByteStream(core.session(), id))
t.alike(all, ['a', 'b', 'c', 'd', 'e'])
const half = await collect(new ByteStream(core.session(), id, { length: 3 }))
t.alike(half, ['a', 'b', 'c'])
const lastHalf = await collect(new ByteStream(core.session(), id, { start: 3 }))
t.alike(lastHalf, ['d', 'e'])
})
test('out or range', async function (t) {
const { id, core } = await create(t, ['Hello World'])
const all = await collect(new ByteStream(core.session(), id, { start: 0, length: 21 }))
t.alike(all, ['Hello World'])
})
test('negative length != -1', async function (t) {
const { id, core } = await create(t, ['Hello World'])
const all = await collect(new ByteStream(core.session(), id, { start: 0, length: -2 }))
t.alike(all, [])
})
test('multiple blobs', async function (t) {
const { id, core } = await create(t, ['a', 'b', 'c', 'd', 'e'])
id.blockOffset += 2
id.blockLength -= 2
id.byteOffset += 2
id.byteLength -= 2
const all = await collect(new ByteStream(core.session(), id))
t.alike(all, ['c', 'd', 'e'])
const half = await collect(new ByteStream(core.session(), id, { length: 2 }))
t.alike(half, ['c', 'd'])
const lastHalf = await collect(new ByteStream(core.session(), id, { start: 2 }))
t.alike(lastHalf, ['e'])
})
test('seeks', async function (t) {
const { id, core } = await create(t, ['aaaa', 'bb', 'ccc', 'd', 'eeeeeeeeee'], 2)
{
const result = await collect(new ByteStream(core.session(), id))
t.alike(result, ['aaaa', 'bb', 'ccc', 'd', 'eeeeeeeeee', 'aaaa', 'bb', 'ccc', 'd', 'eeeeeeeeee'])
}
{
const result = await collect(new ByteStream(core.session(), id, { start: 12 }))
t.alike(result, ['eeeeeeee', 'aaaa', 'bb', 'ccc', 'd', 'eeeeeeeeee'])
}
{
const result = await collect(new ByteStream(core.session(), id, { start: 12, length: 10 }))
t.alike(result, ['eeeeeeee', 'aa'])
}
{
const result = await collect(new ByteStream(core.session(), id, { start: 12, length: 5 }))
t.alike(result, ['eeeee'])
}
{
const result = await collect(new ByteStream(core.session(), id, { start: 12, end: 16 }))
t.alike(result, ['eeeee'])
}
{
const result = await collect(new ByteStream(core.session(), id, { start: 12, length: 9999 }))
t.alike(result, ['eeeeeeee', 'aaaa', 'bb', 'ccc', 'd', 'eeeeeeeeee'])
}
})
test('deferred', async function (t) {
const { id, core } = await create(t, ['hello', 'world'])
{
const b = new ByteStream(null, null)
b.start(core.session(), id)
const result = await collect(b)
t.alike(result, ['hello', 'world'])
}
{
const b = new ByteStream(null, null, { start: 2, length: 4 })
b.start(core.session(), id)
const result = await collect(b)
t.alike(result, ['llo', 'w'])
}
})
test('one', async function (t) {
const { core } = await create(t, ['hello', 'world'])
{
const b = ByteStream.one(core.session())
const result = await collect(b)
t.alike(result, ['hello', 'world'])
}
{
const b = ByteStream.one(core.session(), { start: 2, length: 4 })
const result = await collect(b)
t.alike(result, ['llo', 'w'])
}
})
async function create (t, blocks, repeat = 1) {
const dir = await tmp(t)
const core = new Hypercore(dir)
blocks = blocks.map(b => typeof b === 'string' ? b4a.from(b) : b)
const batch = []
for (let i = 0; i < repeat; i++) {
batch.push(...blocks)
}
await core.append(batch)
t.teardown(() => core.close())
const id = {
blockOffset: 0,
blockLength: core.length,
byteOffset: 0,
byteLength: core.byteLength
}
return { id, core }
}
async function collect (stream) {
const chunks = []
for await (const data of stream) chunks.push(b4a.toString(data))
return chunks
}