Skip to content

Commit 015610d

Browse files
committed
Update wohumi.test.ts
1 parent 357a8d1 commit 015610d

File tree

1 file changed

+24
-73
lines changed

1 file changed

+24
-73
lines changed

src/test/wohumi.test.ts

Lines changed: 24 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,44 @@ import type { NobleTypes } from '../types/types.js'
22

33
import { Buffer } from 'node:buffer'
44

5-
import { describe, expect, it, vi } from 'vitest'
5+
import { describe, expect, it } from 'vitest'
66

7+
import { SwitchbotDevice } from '../device.js'
78
import { WoHumi } from '../device/wohumi.js'
8-
import { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName } from '../types/types.js'
99

1010
describe('woHumi', () => {
11-
const emitLog = vi.fn()
12-
13-
describe('parseServiceData', () => {
14-
it('should return parsed data for valid service data', async () => {
15-
const serviceData = Buffer.from([0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00])
16-
const result = await WoHumi.parseServiceData(serviceData, emitLog)
17-
expect(result).toEqual({
18-
model: SwitchBotBLEModel.Humidifier,
19-
modelName: SwitchBotBLEModelName.Humidifier,
20-
modelFriendlyName: SwitchBotBLEModelFriendlyName.Humidifier,
21-
onState: true,
22-
autoMode: true,
23-
percentage: 0,
24-
humidity: 0,
25-
})
26-
})
27-
28-
it('should return null for invalid service data length', async () => {
29-
const serviceData = Buffer.from([0x00, 0x80, 0x00])
30-
const result = await WoHumi.parseServiceData(serviceData, emitLog)
31-
expect(result).toBeNull()
32-
expect(emitLog).toHaveBeenCalledWith('error', '[parseServiceDataForWoHumi] Buffer length 3 !== 8!')
33-
})
11+
let wohumi: WoHumi
12+
let mockPeripheral: NobleTypes['peripheral']
13+
let mockNoble: NobleTypes['noble']
14+
15+
beforeEach(() => {
16+
mockPeripheral = {} as NobleTypes['peripheral']
17+
mockNoble = {} as NobleTypes['noble']
18+
wohumi = new WoHumi(mockPeripheral, mockNoble)
19+
jest.spyOn(SwitchbotDevice.prototype, 'command').mockResolvedValue(Buffer.from([0x01, 0x00, 0x00]))
3420
})
3521

36-
describe('operateHumi', () => {
37-
it('should throw an error if the device returns an error', async () => {
38-
const peripheral = {} as unknown as NobleTypes['peripheral']
39-
const wohumi = new WoHumi(peripheral, emitLog as any)
40-
vi.spyOn(wohumi, 'command').mockResolvedValue(Buffer.from([0x00, 0x00, 0x00]))
41-
await expect(wohumi.operateHumi([0x57, 0x01, 0x00])).rejects.toThrow('The device returned an error: 0x000000')
42-
})
22+
afterEach(() => {
23+
jest.restoreAllMocks()
4324
})
4425

45-
describe('press', () => {
46-
it('should call operateHumi with correct bytes', async () => {
47-
const peripheral = {} as unknown as NobleTypes['peripheral']
48-
const wohumi = new WoHumi(peripheral, emitLog as any)
49-
const operateHumiSpy = vi.spyOn(wohumi, 'operateHumi').mockResolvedValue()
50-
await wohumi.press()
51-
expect(operateHumiSpy).toHaveBeenCalledWith([0x57, 0x01, 0x00])
26+
describe('percentage', () => {
27+
it('should throw an error if level is less than 0', async () => {
28+
await expect(wohumi.percentage(-1)).rejects.toThrow('Level must be between 0 and 100')
5229
})
53-
})
5430

55-
describe('turnOn', () => {
56-
it('should call operateHumi with correct bytes', async () => {
57-
const peripheral = {} as unknown as NobleTypes['peripheral']
58-
const wohumi = new WoHumi(peripheral, emitLog as any)
59-
const operateHumiSpy = vi.spyOn(wohumi, 'operateHumi').mockResolvedValue()
60-
await wohumi.turnOn()
61-
expect(operateHumiSpy).toHaveBeenCalledWith([0x57, 0x01, 0x01])
31+
it('should throw an error if level is greater than 100', async () => {
32+
await expect(wohumi.percentage(101)).rejects.toThrow('Level must be between 0 and 100')
6233
})
63-
})
6434

65-
describe('turnOff', () => {
66-
it('should call operateHumi with correct bytes', async () => {
67-
const peripheral = {} as unknown as NobleTypes['peripheral']
68-
const wohumi = new WoHumi(peripheral, emitLog as any)
69-
const operateHumiSpy = vi.spyOn(wohumi, 'operateHumi').mockResolvedValue()
70-
await wohumi.turnOff()
71-
expect(operateHumiSpy).toHaveBeenCalledWith([0x57, 0x01, 0x02])
72-
})
73-
})
35+
it('should send the correct command for a valid level', async () => {
36+
const level = 50
37+
const expectedCommand = Buffer.from(`57010107${level.toString(16).padStart(2, '0')}`, 'hex')
38+
const operateHumiSpy = jest.spyOn(wohumi as any, 'operateHumi')
7439

75-
describe('down', () => {
76-
it('should call operateHumi with correct bytes', async () => {
77-
const peripheral = {} as unknown as NobleTypes['peripheral']
78-
const wohumi = new WoHumi(peripheral, emitLog as any)
79-
const operateHumiSpy = vi.spyOn(wohumi, 'operateHumi').mockResolvedValue()
80-
await wohumi.down()
81-
expect(operateHumiSpy).toHaveBeenCalledWith([0x57, 0x01, 0x03])
82-
})
83-
})
40+
await wohumi.percentage(level)
8441

85-
describe('up', () => {
86-
it('should call operateHumi with correct bytes', async () => {
87-
const peripheral = {} as unknown as NobleTypes['peripheral']
88-
const wohumi = new WoHumi(peripheral, emitLog as any)
89-
const operateHumiSpy = vi.spyOn(wohumi, 'operateHumi').mockResolvedValue()
90-
await wohumi.up()
91-
expect(operateHumiSpy).toHaveBeenCalledWith([0x57, 0x01, 0x04])
42+
expect(operateHumiSpy).toHaveBeenCalledWith(expectedCommand)
9243
})
9344
})
9445
})

0 commit comments

Comments
 (0)