-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
101 lines (79 loc) · 2.95 KB
/
test.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
/* eslint-env browser */
/* global TextDecoderStream */
import * as dagJSON from '@ipld/dag-json'
import { Parse, Stringify } from './index.js'
/**
* @param {string[]} items
* @returns {ReadableStream<Uint8Array>}
*/
const toParseSource = items => new ReadableStream({
pull (controller) {
const item = items.shift()
return item ? controller.enqueue(new TextEncoder().encode(item)) : controller.close()
}
})
/** @type {Record<string, import('entail').Test>} */
export const test = {
'should split 1 item from 1 chunk': async assert => {
const source = toParseSource(['{ "id": 1 }\n'])
const results = []
await source.pipeThrough(new Parse()).pipeTo(new WritableStream({
write: (item) => { results.push(item) }
}))
assert.deepEqual(results, [{ id: 1 }])
},
'should split 1 item from 2 chunks': async assert => {
const source = toParseSource(['{ "id', '": 1 }\n'])
const results = []
await source.pipeThrough(new Parse()).pipeTo(new WritableStream({
write: (item) => { results.push(item) }
}))
assert.deepEqual(results, [{ id: 1 }])
},
'should split 2 items from 2 chunks': async assert => {
const source = toParseSource(['{ "id": 1 }\n', '{ "id": 2 }'])
const results = []
await source.pipeThrough(new Parse()).pipeTo(new WritableStream({
write: (item) => { results.push(item) }
}))
assert.deepEqual(results, [{ id: 1 }, { id: 2 }])
},
'should split 2 items from 1 chunk': async assert => {
const source = toParseSource(['{ "id": 1 }\n{ "id": 2 }'])
const results = []
await source.pipeThrough(new Parse()).pipeTo(new WritableStream({
write: (item) => { results.push(item) }
}))
assert.deepEqual(results, [{ id: 1 }, { id: 2 }])
},
'should split 3 items from 2 chunks': async assert => {
const source = toParseSource(['{ "id": 1 }\n{ "i', 'd": 2 }', '\n{"id":3}'])
const results = []
await source.pipeThrough(new Parse()).pipeTo(new WritableStream({
write: (item) => { results.push(item) }
}))
assert.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
},
'should round trip': async assert => {
const input = '{"id":1}\n{"id":2}\n{"id":3}\n'
const source = toParseSource([input])
const results = []
await source
.pipeThrough(new Parse())
.pipeThrough(new Stringify())
.pipeThrough(new TextDecoderStream())
.pipeTo(new WritableStream({ write: (item) => { results.push(item) } }))
assert.equal(results.join(''), input)
},
'should custom parser and siringifier': async assert => {
const input = '{"id":1}\n{"id":2}\n{"id":3}\n'
const source = toParseSource([input])
const results = []
await source
.pipeThrough(new Parse(dagJSON.parse))
.pipeThrough(new Stringify(dagJSON.stringify))
.pipeThrough(new TextDecoderStream())
.pipeTo(new WritableStream({ write: (item) => { results.push(item) } }))
assert.equal(results.join(''), input)
}
}