|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | // @ts-expect-error Missing type definitions |
3 | | -import { toStream, toArrayStream, readToEnd, slice, pipe, ArrayStream, transform, transformAsync } from '@openpgp/web-stream-tools'; |
| 3 | +import { toStream, toArrayStream, getReader, readToEnd, slice, pipe, ArrayStream, transform, transformAsync } from '@openpgp/web-stream-tools'; |
4 | 4 |
|
5 | 5 | describe('Common integration tests', () => { |
6 | | - it('toStream/readToEnd', async () => { |
7 | | - const input = 'chunk'; |
8 | | - const streamedData = toStream('chunk'); |
9 | | - expect(await readToEnd(streamedData)).to.equal(input); |
| 6 | + it('readToEnd non-stream', async () => { |
| 7 | + const input = 'chunk\nanother chunk'; |
| 8 | + expect(await readToEnd(input)).to.equal('chunk\nanother chunk'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('readToEnd arraystream', async () => { |
| 12 | + const input = 'chunk\nanother chunk'; |
| 13 | + const inputStream = toArrayStream(input); |
| 14 | + expect(await readToEnd(inputStream)).to.equal('chunk\nanother chunk'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('readToEnd stream', async () => { |
| 18 | + const input = 'chunk\nanother chunk'; |
| 19 | + const inputStream = toStream(input); |
| 20 | + expect(await readToEnd(inputStream)).to.equal('chunk\nanother chunk'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('readToEnd partially-read non-stream', async () => { |
| 24 | + const input = new String('chunk\nanother chunk'); |
| 25 | + const reader = getReader(input); |
| 26 | + await reader.readLine(); |
| 27 | + reader.releaseLock(); |
| 28 | + // @ts-expect-error Passing String instead of string |
| 29 | + expect(await readToEnd(input)).to.equal('another chunk'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('readToEnd partially-read arraystream', async () => { |
| 33 | + const input = 'chunk\nanother chunk'; |
| 34 | + const inputStream = toArrayStream(input); |
| 35 | + const reader = getReader(inputStream); |
| 36 | + await reader.readLine(); |
| 37 | + reader.releaseLock(); |
| 38 | + expect(await readToEnd(inputStream)).to.equal('another chunk'); |
10 | 39 | }); |
11 | 40 |
|
12 | | - it('slice', async () => { |
13 | | - const input = 'another chunk'; |
14 | | - const streamedData = toStream(input); |
15 | | - const slicedStream = slice(streamedData, 8); |
16 | | - expect(await readToEnd(slicedStream)).to.equal('chunk'); |
| 41 | + it('readToEnd partially-read stream', async () => { |
| 42 | + const input = 'chunk\nanother chunk'; |
| 43 | + const inputStream = toStream(input); |
| 44 | + const reader = getReader(inputStream); |
| 45 | + await reader.readLine(); |
| 46 | + reader.releaseLock(); |
| 47 | + expect(await readToEnd(inputStream)).to.equal('another chunk'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('slice non-stream', async () => { |
| 51 | + const input = 'chunk\nanother chunk'; |
| 52 | + const sliced = slice(input, 6); |
| 53 | + expect(sliced).to.equal('another chunk'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('slice stream', async () => { |
| 57 | + const input = 'chunk\nanother chunk'; |
| 58 | + const inputStream = toStream(input); |
| 59 | + const sliced = slice(inputStream, 6); |
| 60 | + expect(await readToEnd(sliced)).to.equal('another chunk'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('slice partially-read non-stream', async () => { |
| 64 | + const input = new String('chunk\nanother chunk'); |
| 65 | + const reader = getReader(input); |
| 66 | + await reader.readLine(); |
| 67 | + reader.releaseLock(); |
| 68 | + // @ts-expect-error Passing String instead of string |
| 69 | + const sliced = slice(input, 8); |
| 70 | + expect(sliced).to.equal('chunk'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('slice partially-read stream', async () => { |
| 74 | + const input = 'chunk\nanother chunk'; |
| 75 | + const inputStream = toStream(input); |
| 76 | + const reader = getReader(inputStream); |
| 77 | + await reader.readLine(); |
| 78 | + reader.releaseLock(); |
| 79 | + const sliced = slice(inputStream, 8); |
| 80 | + expect(await readToEnd(sliced)).to.equal('chunk'); |
17 | 81 | }); |
18 | 82 |
|
19 | 83 | it('pipe from stream to stream', async () => { |
@@ -54,37 +118,95 @@ describe('Common integration tests', () => { |
54 | 118 | expect(transformed).to.equal('CHUNK'); |
55 | 119 | }); |
56 | 120 |
|
| 121 | + it('transform partially-read non-stream', async () => { |
| 122 | + const input = new String('chunk\nanother chunk'); |
| 123 | + const reader = getReader(input); |
| 124 | + await reader.readLine(); |
| 125 | + reader.releaseLock(); |
| 126 | + const transformed = transform(input, (str: string) => str.toUpperCase()); |
| 127 | + expect(transformed).to.equal('ANOTHER CHUNK'); |
| 128 | + }); |
| 129 | + |
57 | 130 | it('transform arraystream', async () => { |
58 | 131 | const input = 'chunk'; |
59 | | - const streamedData = toArrayStream(input); |
60 | | - const transformed = transform(streamedData, (str: string) => str.toUpperCase()); |
| 132 | + const inputStream = toArrayStream(input); |
| 133 | + const transformed = transform(inputStream, (str: string) => str.toUpperCase()); |
61 | 134 | expect(await readToEnd(transformed)).to.equal('CHUNK'); |
62 | 135 | }); |
63 | 136 |
|
| 137 | + it('transform partially-read arraystream', async () => { |
| 138 | + const input = 'chunk\nanother chunk'; |
| 139 | + const inputStream = toArrayStream(input); |
| 140 | + const reader = getReader(inputStream); |
| 141 | + await reader.readLine(); |
| 142 | + reader.releaseLock(); |
| 143 | + const transformed = transform(inputStream, (str: string) => str.toUpperCase()); |
| 144 | + expect(await readToEnd(transformed)).to.equal('ANOTHER CHUNK'); |
| 145 | + }); |
| 146 | + |
64 | 147 | it('transform stream', async () => { |
65 | 148 | const input = 'chunk'; |
66 | | - const streamedData = toStream(input); |
67 | | - const transformed = transform(streamedData, (str: string) => str.toUpperCase()); |
| 149 | + const inputStream = toStream(input); |
| 150 | + const transformed = transform(inputStream, (str: string) => str.toUpperCase()); |
68 | 151 | expect(await readToEnd(transformed)).to.equal('CHUNK'); |
69 | 152 | }); |
70 | 153 |
|
| 154 | + it('transform partially-read stream', async () => { |
| 155 | + const input = 'chunk\nanother chunk'; |
| 156 | + const inputStream = toStream(input); |
| 157 | + const reader = getReader(inputStream); |
| 158 | + await reader.readLine(); |
| 159 | + reader.releaseLock(); |
| 160 | + const transformed = transform(inputStream, (str: string) => str.toUpperCase()); |
| 161 | + expect(await readToEnd(transformed)).to.equal('ANOTHER CHUNK'); |
| 162 | + }); |
| 163 | + |
71 | 164 | it('transformAsync non-stream', async () => { |
72 | 165 | const input = 'chunk'; |
73 | 166 | const transformed = await transformAsync(input, async (str: string) => str.toUpperCase()); |
74 | 167 | expect(transformed).to.equal('CHUNK'); |
75 | 168 | }); |
76 | 169 |
|
| 170 | + it('transformAsync partially-read non-stream', async () => { |
| 171 | + const input = new String('chunk\nanother chunk'); |
| 172 | + const reader = getReader(input); |
| 173 | + await reader.readLine(); |
| 174 | + reader.releaseLock(); |
| 175 | + const transformed = await transformAsync(input, async (str: string) => str.toUpperCase()); |
| 176 | + expect(transformed).to.equal('ANOTHER CHUNK'); |
| 177 | + }); |
| 178 | + |
77 | 179 | it('transformAsync arraystream', async () => { |
78 | 180 | const input = 'chunk'; |
79 | | - const streamedData = toArrayStream(input); |
80 | | - const transformed = await transformAsync(streamedData, async (str: string) => str.toUpperCase()); |
| 181 | + const inputStream = toArrayStream(input); |
| 182 | + const transformed = await transformAsync(inputStream, async (str: string) => str.toUpperCase()); |
81 | 183 | expect(await readToEnd(transformed)).to.equal('CHUNK'); |
82 | 184 | }); |
83 | 185 |
|
| 186 | + it('transformAsync partially-read arraystream', async () => { |
| 187 | + const input = 'chunk\nanother chunk'; |
| 188 | + const inputStream = toArrayStream(input); |
| 189 | + const reader = getReader(inputStream); |
| 190 | + await reader.readLine(); |
| 191 | + reader.releaseLock(); |
| 192 | + const transformed = await transformAsync(inputStream, async (str: string) => str.toUpperCase()); |
| 193 | + expect(await readToEnd(transformed)).to.equal('ANOTHER CHUNK'); |
| 194 | + }); |
| 195 | + |
84 | 196 | it('transformAsync stream', async () => { |
85 | 197 | const input = 'chunk'; |
86 | | - const streamedData = toStream(input); |
87 | | - const transformed = await transformAsync(streamedData, async (str: string) => str.toUpperCase()); |
| 198 | + const inputStream = toStream(input); |
| 199 | + const transformed = await transformAsync(inputStream, async (str: string) => str.toUpperCase()); |
88 | 200 | expect(await readToEnd(transformed)).to.equal('CHUNK'); |
89 | 201 | }); |
| 202 | + |
| 203 | + it('transformAsync partially-read stream', async () => { |
| 204 | + const input = 'chunk\nanother chunk'; |
| 205 | + const inputStream = toStream(input); |
| 206 | + const reader = getReader(inputStream); |
| 207 | + await reader.readLine(); |
| 208 | + reader.releaseLock(); |
| 209 | + const transformed = await transformAsync(inputStream, async (str: string) => str.toUpperCase()); |
| 210 | + expect(await readToEnd(transformed)).to.equal('ANOTHER CHUNK'); |
| 211 | + }); |
90 | 212 | }); |
0 commit comments