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

test/specs/stream-comprehensive.ts   A

Complexity

Total Complexity 8
Complexity/F 0

Size

Lines of Code 80
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 65
mnd 8
bc 8
fnc 0
dl 0
loc 80
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
/** Comprehensive test for stream method. */
7
test('stream-comprehensive', timeout(1e4, () => new Promise<void>(async (ff, rj) => {
8
9
    const {port} = await mockServer()
10
    let to = setTimeout(() => rj('cannot create'), 2e2)
11
    const ws = await createNew({}, port)
12
    clearTimeout(to)
13
14
    to = setTimeout(() => rj('cannot ready'), 2e2)
15
    await ws.ready()
16
    clearTimeout(to)
17
18
    // Test 1: Basic stream functionality
19
    const msg1 = {stream: true, test: 'stream1', chunks: [1], delay: 10}
20
    to = setTimeout(() => rj('stream1 timeout'), 2e2)
21
22
    try {
23
      const stream1 = ws.stream(msg1)
24
      const result1 = await stream1.next()
25
26
      // Check that we got a valid response with the expected test property
27
      if (result1.done || !result1.value?.test || result1.value.test !== msg1.test) {
28
        clearTimeout(to)
29
        return rj('stream1 failed')
30
      }
31
32
      clearTimeout(to)
33
34
      // Test 2: Stream with for-await loop
35
      const msg2 = {stream: true, test: 'stream2'}
36
      to = setTimeout(() => rj('stream2 timeout'), 2e2)
37
38
      const stream2 = ws.stream<typeof msg2, any>(msg2)
39
      const results: any[] = []
40
41
      for await (const chunk of stream2) {
42
        results.push(chunk)
43
        break // We expect only one chunk
44
      }
45
46
      clearTimeout(to)
47
48
      // For streaming messages, check that we got a valid chunk with the expected properties
49
      if (results.length !== 1 || !results[0].test || results[0].test !== msg2.test) {
50
        return rj('stream2 failed')
51
      }
52
53
      // Test 3: Multiple concurrent streams
54
      const msg3a = {stream: true, test: 'stream3a'}
55
      const msg3b = {stream: true, test: 'stream3b'}
56
      to = setTimeout(() => rj('stream3 timeout'), 2e2)
57
58
      const stream3a = ws.stream(msg3a)
59
      const stream3b = ws.stream(msg3b)
60
61
      const result3a = await stream3a.next()
62
      const result3b = await stream3b.next()
63
64
      clearTimeout(to)
65
66
      // For streaming messages, check that we got valid chunks with the expected properties
67
      if (result3a.done || result3b.done ||
68
          !result3a.value?.test || result3a.value.test !== msg3a.test ||
69
          !result3b.value?.test || result3b.value.test !== msg3b.test) {
70
        return rj('stream3 failed')
71
      }
72
73
      ff()
74
    } catch (error) {
75
      clearTimeout(to)
76
      console.log('STREAM-COMPREHENSIVE TEST FAILED:', error)
77
      rj('stream comprehensive error: ' + error)
78
    }
79
  })
80
))