Passed
Push — master ( 1b1907...0e6aeb )
by Michael
01:30
created

test/specs/stream-real.ts   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 58
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 44
mnd 6
bc 6
fnc 0
dl 0
loc 58
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { equals } from 'pepka'
2
import { createNew, timeout } from '../utils'
3
import mockServer from '../mock/server'
4
import { test } from '../suite'
5
6
/** Test real streaming functionality with multiple chunks. */
7
test('stream-real', timeout(1.5e4, () => new Promise<void>(async (ff, rj) => {
8
9
    const {port} = await mockServer()
10
    let to = setTimeout(() => {
11
      console.log('STREAM-REAL: Timeout - cannot create')
12
      rj('cannot create')
13
    }, 2e2)
14
    const ws = await createNew({}, port)
15
    clearTimeout(to)
16
17
    to = setTimeout(() => rj('cannot ready'), 2e2)
18
    await ws.ready()
19
    clearTimeout(to)
20
21
    // Test real streaming with multiple chunks
22
    const streamMsg = {stream: true, multi: true, test: 'real-stream', chunks: [1, 2, 3], delay: 50}
23
    to = setTimeout(() => rj('stream timeout'), 5e3)
24
25
    try {
26
      const stream = ws.stream<typeof streamMsg, any>(streamMsg)
27
      const chunks: any[] = []
28
29
      for await (const chunk of stream) {
30
        chunks.push(chunk)
31
      }
32
33
      clearTimeout(to)
34
35
      // Verify we got all chunks
36
      if (chunks.length !== 3) {
37
        return rj(`Expected 3 chunks, got ${chunks.length}`)
38
      }
39
40
      // Verify chunks are in order and have correct data
41
      for (let i = 0; i < 3; i++) {
42
        if (chunks[i].chunk !== i + 1) {
43
          return rj(`Chunk ${i} should be ${i + 1}, got ${chunks[i].chunk}`)
44
        }
45
      }
46
47
      // Verify last chunk has done flag
48
      if (!chunks[2].done) {
49
        return rj('Last chunk should have done flag')
50
      }
51
      ff()
52
    } catch (error) {
53
      clearTimeout(to)
54
      console.log('STREAM-REAL TEST FAILED:', error)
55
      rj('stream real error: ' + error)
56
    }
57
  })
58
))