This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathnormalise-input.spec.js
192 lines (151 loc) · 5.45 KB
/
normalise-input.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'
/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const blobToIt = require('blob-to-it')
const uint8ArrayFromString = require('uint8arrays/from-string')
const all = require('it-all')
const { File } = require('@web-std/file')
const { Blob, ReadableStream } = globalThis
const { isBrowser, isWebWorker, isElectronRenderer } = require('ipfs-utils/src/env')
let normalise = require('../../src/files/normalise-input')
if (isBrowser || isWebWorker || isElectronRenderer) {
normalise = require('../../src/files/normalise-input/index.browser')
}
const STRING = () => 'hello world'
const NEWSTRING = () => new String('hello world') // eslint-disable-line no-new-wrappers
const BUFFER = () => uint8ArrayFromString(STRING())
const ARRAY = () => Array.from(BUFFER())
const TYPEDARRAY = () => Uint8Array.from(ARRAY())
let BLOB
if (Blob) {
BLOB = () => new Blob([
STRING()
])
}
async function verifyNormalisation (input) {
expect(input.length).to.equal(1)
expect(input[0].path).to.equal('')
let content = input[0].content
if (isBrowser || isWebWorker || isElectronRenderer) {
expect(content).to.be.an.instanceOf(Blob)
content = blobToIt(content)
}
expect(content[Symbol.asyncIterator] || content[Symbol.iterator]).to.be.ok('Content should have been an iterable or an async iterable')
await expect(all(content)).to.eventually.deep.equal([BUFFER()])
}
async function testContent (input) {
const result = await all(normalise(input))
await verifyNormalisation(result)
}
function iterableOf (thing) {
return [thing]
}
function asyncIterableOf (thing) {
return (async function * () { // eslint-disable-line require-await
yield thing
}())
}
function browserReadableStreamOf (thing) {
return new ReadableStream({
start (controller) {
controller.enqueue(thing)
controller.close()
}
})
}
describe('normalise-input', function () {
function testInputType (content, name, isBytes) {
it(name, async function () {
await testContent(content())
})
if (isBytes) {
if (ReadableStream) {
it(`ReadableStream<${name}>`, async function () {
await testContent(browserReadableStreamOf(content()))
})
}
it(`Iterable<${name}>`, async function () {
await testContent(iterableOf(content()))
})
it(`AsyncIterable<${name}>`, async function () {
await testContent(asyncIterableOf(content()))
})
}
it(`{ path: '', content: ${name} }`, async function () {
await testContent({ path: '', content: content() })
})
if (isBytes) {
if (ReadableStream) {
it(`{ path: '', content: ReadableStream<${name}> }`, async function () {
await testContent({ path: '', content: browserReadableStreamOf(content()) })
})
}
it(`{ path: '', content: Iterable<${name}> }`, async function () {
await testContent({ path: '', content: iterableOf(content()) })
})
it(`{ path: '', content: AsyncIterable<${name}> }`, async function () {
await testContent({ path: '', content: asyncIterableOf(content()) })
})
}
if (ReadableStream) {
it(`ReadableStream<${name}>`, async function () {
await testContent(browserReadableStreamOf(content()))
})
}
it(`Iterable<{ path: '', content: ${name} }`, async function () {
await testContent(iterableOf({ path: '', content: content() }))
})
it(`AsyncIterable<{ path: '', content: ${name} }`, async function () {
await testContent(asyncIterableOf({ path: '', content: content() }))
})
if (isBytes) {
if (ReadableStream) {
it(`Iterable<{ path: '', content: ReadableStream<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: browserReadableStreamOf(content()) }))
})
}
it(`Iterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: iterableOf(content()) }))
})
it(`Iterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(iterableOf({ path: '', content: asyncIterableOf(content()) }))
})
if (ReadableStream) {
it(`AsyncIterable<{ path: '', content: ReadableStream<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: browserReadableStreamOf(content()) }))
})
}
it(`AsyncIterable<{ path: '', content: Iterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: iterableOf(content()) }))
})
it(`AsyncIterable<{ path: '', content: AsyncIterable<${name}> }>`, async function () {
await testContent(asyncIterableOf({ path: '', content: asyncIterableOf(content()) }))
})
}
}
describe('String', () => {
testInputType(STRING, 'String', true)
testInputType(NEWSTRING, 'new String()', true)
})
describe('Buffer', () => {
testInputType(BUFFER, 'Buffer', true)
})
describe('Blob', () => {
if (!Blob) {
return
}
testInputType(BLOB, 'Blob', false)
})
describe('@web-std/file', () => {
it('normalizes File input', async () => {
const FILE = new File([BUFFER()], 'test-file.txt')
await testContent(FILE)
})
})
describe('Iterable<Number>', () => {
testInputType(ARRAY, 'Iterable<Number>', false)
})
describe('TypedArray', () => {
testInputType(TYPEDARRAY, 'TypedArray', true)
})
})