Bläddra i källkod

:recycle: test channels

master
j 2 år sedan
förälder
incheckning
a51c11384c
3 ändrade filer med 44 tillägg och 15 borttagningar
  1. 3
    0
      src/channel.js
  2. 36
    0
      test/channel.spec.js
  3. 5
    15
      test/system.spec.js

+ 3
- 0
src/channel.js Visa fil

29
     get interval() {
29
     get interval() {
30
         return this._interval
30
         return this._interval
31
     }
31
     }
32
+    get lastUpdate() {
33
+        return this._reader.created
34
+    }
32
     get val() {
35
     get val() {
33
         return this._reader.val
36
         return this._reader.val
34
     }
37
     }

+ 36
- 0
test/channel.spec.js Visa fil

1
+import { expect, test } from 'vitest'
2
+import { Channel } from '../src/channel.js'
3
+import { Reading } from '../src/channel.js'
4
+
5
+test('channel - instantiate channel with readers correctly', () => {
6
+    let now = Date.now()
7
+    let mockTemp = 70
8
+    let testReader = new Reading({
9
+        onRead: () => mockTemp,
10
+        unit: 'F',
11
+        max: 100,
12
+        min: 50,
13
+    })
14
+    let testChan = new Channel({ interval: 1, reader: testReader })
15
+
16
+    // Test that the channel and channel.reader are working
17
+    expect(testChan.lastUpdate == now).toStrictEqual(true)
18
+    expect(testChan.val).toStrictEqual(70)
19
+    expect(testChan.unit).toStrictEqual('F')
20
+
21
+    mockTemp = 101
22
+    testChan = testChan.update()
23
+    expect(testChan.lastUpdate > now).toStrictEqual(true)
24
+    expect(testChan.val).toStrictEqual(101)
25
+    expect(testChan.inRange).toStrictEqual(false)
26
+    expect(testChan.aboveRange).toStrictEqual(true)
27
+    expect(testChan.belowRange).toStrictEqual(false)
28
+
29
+    mockTemp = 49
30
+    testChan = testChan.update()
31
+    now = Date.now()
32
+    expect(testChan.lastUpdate == now).toStrictEqual(true)
33
+    expect(testChan.inRange).toStrictEqual(false)
34
+    expect(testChan.aboveRange).toStrictEqual(false)
35
+    expect(testChan.belowRange).toStrictEqual(true)
36
+})

+ 5
- 15
test/system.spec.js Visa fil

1
 import { expect, test } from 'vitest'
1
 import { expect, test } from 'vitest'
2
 import { System, makeContainerT } from '../src/system.js'
2
 import { System, makeContainerT } from '../src/system.js'
3
-import { Reading } from '../src/channel.js'
4
 import { Container } from '../src/container.js'
3
 import { Container } from '../src/container.js'
5
 import { controllerTypes } from '../src/controller.js'
4
 import { controllerTypes } from '../src/controller.js'
6
 import { InputConf } from '../src/index.js'
5
 import { InputConf } from '../src/index.js'
18
     const outputs = testSystem.outputsFor({ id: 'aaa' })
17
     const outputs = testSystem.outputsFor({ id: 'aaa' })
19
     expect(outputs).toStrictEqual(['output_test'])
18
     expect(outputs).toStrictEqual(['output_test'])
20
 
19
 
20
+    testSystem.replaceContainer({ id: 'aaa' })
21
+    expect(testSystem.containers[0].id).toStrictEqual('aaa')
22
+
21
     testSystem.remove('aaa')
23
     testSystem.remove('aaa')
22
     expect(testSystem.inventory).toStrictEqual({})
24
     expect(testSystem.inventory).toStrictEqual({})
23
 })
25
 })
24
 
26
 
25
-test('system - instiantes channels with readers correctly', () => {
27
+test('system - update channels with readers correctly', () => {
26
     let testSystem = new System()
28
     let testSystem = new System()
27
     let testContainer = new Container({ l: 10, w: 10, h: 10 })
29
     let testContainer = new Container({ l: 10, w: 10, h: 10 })
28
     let mockTemp = 70
30
     let mockTemp = 70
30
         controllerTypes.temp,
32
         controllerTypes.temp,
31
         () => mockTemp,
33
         () => mockTemp,
32
         0.1,
34
         0.1,
33
-        () => {
34
-            console.log('tick')
35
-            return 'tick'
36
-        }
35
+        () => 'tick'
37
     )
36
     )
38
     const cT = makeContainerT([testInConf], [], testContainer)
37
     const cT = makeContainerT([testInConf], [], testContainer)
39
     testSystem.add('aaa', cT)
38
     testSystem.add('aaa', cT)
42
 
41
 
43
     let chan = testSystem.inputs[0].channel
42
     let chan = testSystem.inputs[0].channel
44
     expect(chan.interval).toStrictEqual(0.1)
43
     expect(chan.interval).toStrictEqual(0.1)
45
-
46
-    // Test that the channel and channel.reader are working
47
-    expect(chan.val).toStrictEqual(70)
48
-    expect(chan.unit).toStrictEqual('F')
49
-    expect(chan.inRange).toStrictEqual(false)
50
     expect(testSystem.inputs[0].onUpdate()).toStrictEqual('tick')
44
     expect(testSystem.inputs[0].onUpdate()).toStrictEqual('tick')
51
-
52
-    mockTemp = 79
53
-    chan = chan.update()
54
-    expect(chan.val).toStrictEqual(79)
55
 })
45
 })

Laddar…
Avbryt
Spara