@switchbot/homebridge-switchbot 5.0.0-beta.98 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/config.json +14 -0
- package/.github/copilot-instructions.md +39 -0
- package/.github/workflows/ci.yml +4 -1
- package/.github/workflows/manual-e2e.yml +6 -3
- package/.github/workflows/release.yml +64 -15
- package/.github/workflows/stale.yml +2 -4
- package/.husky/pre-push +15 -0
- package/CHANGELOG.md +126 -134
- package/MIGRATION.md +16 -6
- package/README.md +84 -3
- package/TODO.md +263 -0
- package/config.schema.json +229 -36
- package/dist/SwitchBotHAPPlatform.d.ts +133 -0
- package/dist/SwitchBotHAPPlatform.d.ts.map +1 -0
- package/dist/SwitchBotHAPPlatform.js +555 -0
- package/dist/SwitchBotHAPPlatform.js.map +1 -0
- package/dist/SwitchBotMatterPlatform.d.ts +141 -0
- package/dist/SwitchBotMatterPlatform.d.ts.map +1 -0
- package/dist/SwitchBotMatterPlatform.js +536 -0
- package/dist/SwitchBotMatterPlatform.js.map +1 -0
- package/dist/device-types.d.ts +31 -0
- package/dist/device-types.d.ts.map +1 -0
- package/dist/device-types.js +246 -0
- package/dist/device-types.js.map +1 -0
- package/dist/deviceCommandMapper.d.ts +10 -0
- package/dist/deviceCommandMapper.d.ts.map +1 -0
- package/dist/deviceCommandMapper.js +319 -0
- package/dist/deviceCommandMapper.js.map +1 -0
- package/dist/deviceFactory.d.ts +3 -2
- package/dist/deviceFactory.d.ts.map +1 -1
- package/dist/deviceFactory.js +107 -29
- package/dist/deviceFactory.js.map +1 -1
- package/dist/devices/genericDevice.d.ts +59 -37
- package/dist/devices/genericDevice.d.ts.map +1 -1
- package/dist/devices/genericDevice.js +376 -78
- package/dist/devices/genericDevice.js.map +1 -1
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +32 -0
- package/dist/errors.js.map +1 -0
- package/dist/homebridge-ui/device-types.js +246 -0
- package/dist/homebridge-ui/device-types.js.map +1 -0
- package/dist/homebridge-ui/deviceCommandMapper.js +319 -0
- package/dist/homebridge-ui/deviceCommandMapper.js.map +1 -0
- package/dist/homebridge-ui/endpoints/config.d.ts +3 -0
- package/dist/homebridge-ui/endpoints/config.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/config.js +90 -0
- package/dist/homebridge-ui/endpoints/config.js.map +1 -0
- package/dist/homebridge-ui/endpoints/devices.d.ts +6 -0
- package/dist/homebridge-ui/endpoints/devices.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/devices.js +144 -0
- package/dist/homebridge-ui/endpoints/devices.js.map +1 -0
- package/dist/homebridge-ui/endpoints/discovery.d.ts +7 -0
- package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -0
- package/dist/homebridge-ui/endpoints/discovery.js +219 -0
- package/dist/homebridge-ui/endpoints/discovery.js.map +1 -0
- package/dist/homebridge-ui/errors.js +32 -0
- package/dist/homebridge-ui/errors.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/config.js +90 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/config.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js +144 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/devices.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +219 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/server.js +11 -0
- package/dist/homebridge-ui/homebridge-ui/server.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js +108 -0
- package/dist/homebridge-ui/homebridge-ui/utils/config-parser.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js +111 -0
- package/dist/homebridge-ui/homebridge-ui/utils/device-migration.js.map +1 -0
- package/dist/homebridge-ui/homebridge-ui/utils/logger.js +17 -0
- package/dist/homebridge-ui/homebridge-ui/utils/logger.js.map +1 -0
- package/dist/homebridge-ui/public/css/styles.css +483 -0
- package/dist/homebridge-ui/public/index.html +197 -621
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts +3 -0
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/advanced-settings.js +95 -0
- package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -0
- package/dist/homebridge-ui/public/js/advanced-settings.ts +94 -0
- package/dist/homebridge-ui/public/js/api.d.ts +66 -0
- package/dist/homebridge-ui/public/js/api.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/api.js +295 -0
- package/dist/homebridge-ui/public/js/api.js.map +1 -0
- package/dist/homebridge-ui/public/js/api.ts +355 -0
- package/dist/homebridge-ui/public/js/app.d.ts +2 -0
- package/dist/homebridge-ui/public/js/app.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/app.js +3722 -0
- package/dist/homebridge-ui/public/js/app.js.map +7 -0
- package/dist/homebridge-ui/public/js/app.ts +22 -0
- package/dist/homebridge-ui/public/js/constants.d.ts +2 -0
- package/dist/homebridge-ui/public/js/constants.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/constants.js +2 -0
- package/dist/homebridge-ui/public/js/constants.js.map +1 -0
- package/dist/homebridge-ui/public/js/constants.ts +1 -0
- package/dist/homebridge-ui/public/js/credentials.d.ts +3 -0
- package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/credentials.js +99 -0
- package/dist/homebridge-ui/public/js/credentials.js.map +1 -0
- package/dist/homebridge-ui/public/js/credentials.ts +105 -0
- package/dist/homebridge-ui/public/js/devices-delete.d.ts +3 -0
- package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/devices-delete.js +199 -0
- package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -0
- package/dist/homebridge-ui/public/js/devices-delete.ts +227 -0
- package/dist/homebridge-ui/public/js/devices.d.ts +9 -0
- package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/devices.js +98 -0
- package/dist/homebridge-ui/public/js/devices.js.map +1 -0
- package/dist/homebridge-ui/public/js/devices.ts +106 -0
- package/dist/homebridge-ui/public/js/discovery.d.ts +9 -0
- package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/discovery.js +1201 -0
- package/dist/homebridge-ui/public/js/discovery.js.map +1 -0
- package/dist/homebridge-ui/public/js/discovery.ts +1335 -0
- package/dist/homebridge-ui/public/js/logger.d.ts +7 -0
- package/dist/homebridge-ui/public/js/logger.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/logger.js +17 -0
- package/dist/homebridge-ui/public/js/logger.js.map +1 -0
- package/dist/homebridge-ui/public/js/logger.ts +17 -0
- package/dist/homebridge-ui/public/js/modal.d.ts +5 -0
- package/dist/homebridge-ui/public/js/modal.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/modal.js +35 -0
- package/dist/homebridge-ui/public/js/modal.js.map +1 -0
- package/dist/homebridge-ui/public/js/modal.ts +35 -0
- package/dist/homebridge-ui/public/js/modals.d.ts +15 -0
- package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/modals.js +675 -0
- package/dist/homebridge-ui/public/js/modals.js.map +1 -0
- package/dist/homebridge-ui/public/js/modals.ts +765 -0
- package/dist/homebridge-ui/public/js/render.d.ts +71 -0
- package/dist/homebridge-ui/public/js/render.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/render.js +960 -0
- package/dist/homebridge-ui/public/js/render.js.map +1 -0
- package/dist/homebridge-ui/public/js/render.ts +1084 -0
- package/dist/homebridge-ui/public/js/toast.d.ts +6 -0
- package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/toast.js +38 -0
- package/dist/homebridge-ui/public/js/toast.js.map +1 -0
- package/dist/homebridge-ui/public/js/toast.ts +44 -0
- package/dist/homebridge-ui/public/js/types.d.ts +23 -0
- package/dist/homebridge-ui/public/js/types.d.ts.map +1 -0
- package/dist/homebridge-ui/public/js/types.js +2 -0
- package/dist/homebridge-ui/public/js/types.js.map +1 -0
- package/dist/homebridge-ui/public/js/types.ts +26 -0
- package/dist/homebridge-ui/server.d.ts +1 -3
- package/dist/homebridge-ui/server.d.ts.map +1 -1
- package/dist/homebridge-ui/server.js +8 -450
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/homebridge-ui/settings.js +8 -0
- package/dist/homebridge-ui/settings.js.map +1 -0
- package/dist/homebridge-ui/switchbotClient.js +247 -0
- package/dist/homebridge-ui/switchbotClient.js.map +1 -0
- package/dist/homebridge-ui/utils/config-parser.d.ts +39 -0
- package/dist/homebridge-ui/utils/config-parser.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/config-parser.js +108 -0
- package/dist/homebridge-ui/utils/config-parser.js.map +1 -0
- package/dist/homebridge-ui/utils/device-migration.d.ts +35 -0
- package/dist/homebridge-ui/utils/device-migration.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/device-migration.js +111 -0
- package/dist/homebridge-ui/utils/device-migration.js.map +1 -0
- package/dist/homebridge-ui/utils/logger.d.ts +7 -0
- package/dist/homebridge-ui/utils/logger.d.ts.map +1 -0
- package/dist/homebridge-ui/utils/logger.js +17 -0
- package/dist/homebridge-ui/utils/logger.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +1 -0
- package/dist/settings.js.map +1 -1
- package/dist/switchbotClient.d.ts +12 -10
- package/dist/switchbotClient.d.ts.map +1 -1
- package/dist/switchbotClient.js +156 -103
- package/dist/switchbotClient.js.map +1 -1
- package/dist/utils.d.ts +76 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1121 -4
- package/dist/utils.js.map +1 -1
- package/docs/assets/highlight.css +16 -2
- package/docs/assets/main.js +1 -1
- package/docs/index.html +82 -5
- package/docs/variables/default.html +3 -1
- package/eslint.config.js +9 -5
- package/nodemon.json +2 -2
- package/package.json +34 -21
- package/scripts/build-ui.js +37 -0
- package/scripts/free-dev-ports.mjs +105 -0
- package/scripts/generate-matter-maps.js +34 -17
- package/scripts/sync-device-types.mjs +31 -0
- package/src/SwitchBotHAPPlatform.ts +558 -0
- package/src/SwitchBotMatterPlatform.ts +538 -0
- package/src/device-types.js +246 -0
- package/src/device-types.js.map +1 -0
- package/src/device-types.ts +261 -0
- package/src/deviceCommandMapper.js +319 -0
- package/src/deviceCommandMapper.js.map +1 -0
- package/src/deviceCommandMapper.ts +333 -0
- package/src/deviceFactory.ts +125 -45
- package/src/devices/genericDevice.ts +411 -69
- package/src/errors.js +32 -0
- package/src/errors.js.map +1 -0
- package/src/errors.ts +35 -0
- package/src/homebridge-ui/endpoints/config.ts +110 -0
- package/src/homebridge-ui/endpoints/devices.ts +153 -0
- package/src/homebridge-ui/endpoints/discovery.ts +240 -0
- package/src/homebridge-ui/public/css/styles.css +483 -0
- package/src/homebridge-ui/public/index.html +197 -621
- package/src/homebridge-ui/public/js/advanced-settings.ts +94 -0
- package/src/homebridge-ui/public/js/api.ts +355 -0
- package/src/homebridge-ui/public/js/app.ts +22 -0
- package/src/homebridge-ui/public/js/constants.ts +1 -0
- package/src/homebridge-ui/public/js/credentials.ts +105 -0
- package/src/homebridge-ui/public/js/devices-delete.ts +227 -0
- package/src/homebridge-ui/public/js/devices.ts +106 -0
- package/src/homebridge-ui/public/js/discovery.ts +1335 -0
- package/src/homebridge-ui/public/js/logger.ts +17 -0
- package/src/homebridge-ui/public/js/modal.ts +35 -0
- package/src/homebridge-ui/public/js/modals.ts +765 -0
- package/src/homebridge-ui/public/js/render.ts +1084 -0
- package/src/homebridge-ui/public/js/toast.ts +44 -0
- package/src/homebridge-ui/public/js/types.ts +26 -0
- package/src/homebridge-ui/server.ts +9 -526
- package/src/homebridge-ui/utils/config-parser.ts +125 -0
- package/src/homebridge-ui/utils/device-migration.ts +144 -0
- package/src/homebridge-ui/utils/logger.ts +17 -0
- package/src/index.ts +12 -2
- package/src/settings.js +8 -0
- package/src/settings.js.map +1 -0
- package/src/settings.ts +2 -0
- package/src/switchbotClient.js +247 -0
- package/src/switchbotClient.js.map +1 -0
- package/src/switchbotClient.ts +177 -114
- package/src/utils.ts +1133 -5
- package/test/client/switchbot-client-debounce.spec.ts +35 -0
- package/test/client/switchbot-client-openapi.spec.ts +19 -0
- package/test/client/switchbotClient.spec.ts +64 -0
- package/test/device/device-mapping.spec.ts +23 -0
- package/test/device/deviceBase.spec.ts +26 -0
- package/test/device/deviceFactory-edge.spec.ts +15 -0
- package/test/device/deviceFactory.spec.ts +33 -0
- package/test/device/fan-swing.spec.ts +34 -0
- package/test/device/genericDevice-blepoll.spec.ts +47 -0
- package/test/device/irdevice.spec.ts +9 -0
- package/test/device/lock-users.spec.ts +35 -0
- package/test/device/matter-descriptors.spec.ts +22 -0
- package/test/device/matter-device-state.spec.ts +37 -0
- package/test/e2e/run-e2e.spec.ts +18 -19
- package/test/errors/errors.spec.ts +10 -0
- package/test/helpers/matter-harness.ts +20 -9
- package/test/homebridge-ui/server.spec.ts +9 -0
- package/test/platform/accessory-restore.spec.ts +37 -0
- package/test/platform/matter-childbridge.spec.ts +34 -0
- package/test/platform/matter-integration.spec.ts +33 -0
- package/test/platform/platform-edge.spec.ts +73 -0
- package/test/platform/platform.integration.spec.ts +34 -0
- package/test/utils/utils-extra.spec.ts +10 -0
- package/test/utils/utils.spec.ts +53 -0
- package/todo/TODO.md +80 -0
- package/tsconfig.ui.json +11 -0
- package/.github/npm-version-script-esm.js +0 -97
- package/.github/workflows/beta-release.yml +0 -52
- package/dist/platform.d.ts +0 -35
- package/dist/platform.d.ts.map +0 -1
- package/dist/platform.js +0 -850
- package/dist/platform.js.map +0 -1
- package/src/platform.ts +0 -867
- package/test/accessory-restore.spec.ts +0 -73
- package/test/device-mapping.spec.ts +0 -37
- package/test/deviceFactory.spec.ts +0 -18
- package/test/fan-swing.spec.ts +0 -29
- package/test/lock-users.spec.ts +0 -44
- package/test/matter-childbridge.spec.ts +0 -55
- package/test/matter-descriptors.spec.ts +0 -97
- package/test/matter-device-state.spec.ts +0 -101
- package/test/matter-integration.spec.ts +0 -70
- package/test/platform.integration.spec.ts +0 -55
- package/test/switchbot-client-debounce.spec.ts +0 -131
- package/test/switchbot-client-openapi.spec.ts +0 -56
- package/test/switchbotClient.spec.ts +0 -10
- package/test/utils.spec.ts +0 -20
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { SwitchBotClient } from '../src/switchbotClient'
|
|
3
|
-
|
|
4
|
-
describe('SwitchBotClient debounce/coalesce', () => {
|
|
5
|
-
let origFetch: any
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
origFetch = (global as any).fetch
|
|
9
|
-
vi.useFakeTimers()
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
(global as any).fetch = origFetch
|
|
14
|
-
vi.restoreAllMocks()
|
|
15
|
-
vi.useRealTimers()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('coalesces rapid writes for the same device (last write wins)', async () => {
|
|
19
|
-
const respBody = { status: 'ok' }
|
|
20
|
-
const fetchMock = vi.fn(async (url: string, opts: any) => ({ json: async () => respBody }))
|
|
21
|
-
;(global as any).fetch = fetchMock
|
|
22
|
-
|
|
23
|
-
// Set a short debounce in config to keep the test fast
|
|
24
|
-
const c = new SwitchBotClient({ openApiToken: 't', writeDebounceMs: 10 } as any)
|
|
25
|
-
await c.init()
|
|
26
|
-
|
|
27
|
-
const body1 = { command: 'one' }
|
|
28
|
-
const body2 = { command: 'two' }
|
|
29
|
-
|
|
30
|
-
const p1 = c.setDeviceState('d-debounce', body1)
|
|
31
|
-
const p2 = c.setDeviceState('d-debounce', body2)
|
|
32
|
-
|
|
33
|
-
// Fast-forward timers past debounce window
|
|
34
|
-
vi.advanceTimersByTime(20)
|
|
35
|
-
// Allow pending microtasks to resolve
|
|
36
|
-
await Promise.resolve()
|
|
37
|
-
await Promise.resolve()
|
|
38
|
-
|
|
39
|
-
const [r1, r2] = await Promise.all([p1, p2])
|
|
40
|
-
expect(r1).toEqual(respBody)
|
|
41
|
-
expect(r2).toEqual(respBody)
|
|
42
|
-
|
|
43
|
-
// Only one network call should have been made
|
|
44
|
-
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
45
|
-
const calledBody = JSON.parse(fetchMock.mock.calls[0][1].body)
|
|
46
|
-
expect(calledBody).toEqual(body2)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('does not coalesce writes for different devices', async () => {
|
|
50
|
-
const respBody = { status: 'ok' }
|
|
51
|
-
const fetchMock = vi.fn(async (url: string, opts: any) => ({ json: async () => respBody }))
|
|
52
|
-
;(global as any).fetch = fetchMock
|
|
53
|
-
|
|
54
|
-
const c = new SwitchBotClient({ openApiToken: 't', writeDebounceMs: 10 } as any)
|
|
55
|
-
await c.init()
|
|
56
|
-
|
|
57
|
-
const p1 = c.setDeviceState('d-A', { command: 'a' })
|
|
58
|
-
const p2 = c.setDeviceState('d-B', { command: 'b' })
|
|
59
|
-
|
|
60
|
-
vi.advanceTimersByTime(20)
|
|
61
|
-
await Promise.resolve()
|
|
62
|
-
await Promise.resolve()
|
|
63
|
-
|
|
64
|
-
await Promise.all([p1, p2])
|
|
65
|
-
|
|
66
|
-
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
67
|
-
const b1 = JSON.parse(fetchMock.mock.calls[0][1].body)
|
|
68
|
-
const b2 = JSON.parse(fetchMock.mock.calls[1][1].body)
|
|
69
|
-
expect(b1).toEqual({ command: 'a' })
|
|
70
|
-
expect(b2).toEqual({ command: 'b' })
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it('coalesces multiple rapid writes (three) and resolves all promises with last response', async () => {
|
|
74
|
-
const respBody = { status: 'ok', val: 'last' }
|
|
75
|
-
const fetchMock = vi.fn(async (url: string, opts: any) => ({ json: async () => respBody }))
|
|
76
|
-
;(global as any).fetch = fetchMock
|
|
77
|
-
|
|
78
|
-
const c = new SwitchBotClient({ openApiToken: 't', writeDebounceMs: 10 } as any)
|
|
79
|
-
await c.init()
|
|
80
|
-
|
|
81
|
-
const p1 = c.setDeviceState('d-debounce', { command: 'one' })
|
|
82
|
-
const p2 = c.setDeviceState('d-debounce', { command: 'two' })
|
|
83
|
-
const p3 = c.setDeviceState('d-debounce', { command: 'three' })
|
|
84
|
-
|
|
85
|
-
vi.advanceTimersByTime(20)
|
|
86
|
-
await Promise.resolve()
|
|
87
|
-
await Promise.resolve()
|
|
88
|
-
|
|
89
|
-
const [r1, r2, r3] = await Promise.all([p1, p2, p3])
|
|
90
|
-
expect(r1).toEqual(respBody)
|
|
91
|
-
expect(r2).toEqual(respBody)
|
|
92
|
-
expect(r3).toEqual(respBody)
|
|
93
|
-
|
|
94
|
-
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
95
|
-
const calledBody = JSON.parse(fetchMock.mock.calls[0][1].body)
|
|
96
|
-
expect(calledBody).toEqual({ command: 'three' })
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('separate writes outside debounce window produce multiple network calls', async () => {
|
|
100
|
-
const respBody1 = { status: 'ok', seq: 1 }
|
|
101
|
-
const respBody2 = { status: 'ok', seq: 2 }
|
|
102
|
-
const fetchMock = vi.fn()
|
|
103
|
-
.mockImplementationOnce(async () => ({ json: async () => respBody1 }))
|
|
104
|
-
.mockImplementationOnce(async () => ({ json: async () => respBody2 }))
|
|
105
|
-
;(global as any).fetch = fetchMock
|
|
106
|
-
|
|
107
|
-
const c = new SwitchBotClient({ openApiToken: 't', writeDebounceMs: 10 } as any)
|
|
108
|
-
await c.init()
|
|
109
|
-
|
|
110
|
-
const p1 = c.setDeviceState('d-debounce', { command: 'one' })
|
|
111
|
-
vi.advanceTimersByTime(20)
|
|
112
|
-
await Promise.resolve()
|
|
113
|
-
await Promise.resolve()
|
|
114
|
-
|
|
115
|
-
const p2 = c.setDeviceState('d-debounce', { command: 'two' })
|
|
116
|
-
vi.advanceTimersByTime(20)
|
|
117
|
-
await Promise.resolve()
|
|
118
|
-
await Promise.resolve()
|
|
119
|
-
|
|
120
|
-
const r1 = await p1
|
|
121
|
-
const r2 = await p2
|
|
122
|
-
|
|
123
|
-
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
124
|
-
const b1 = JSON.parse(fetchMock.mock.calls[0][1].body)
|
|
125
|
-
const b2 = JSON.parse(fetchMock.mock.calls[1][1].body)
|
|
126
|
-
expect(b1).toEqual({ command: 'one' })
|
|
127
|
-
expect(b2).toEqual({ command: 'two' })
|
|
128
|
-
expect(r1).toEqual(respBody1)
|
|
129
|
-
expect(r2).toEqual(respBody2)
|
|
130
|
-
})
|
|
131
|
-
})
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { SwitchBotClient } from '../src/switchbotClient'
|
|
3
|
-
|
|
4
|
-
describe('SwitchBotClient OpenAPI fallback', () => {
|
|
5
|
-
let origFetch: any
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
origFetch = (global as any).fetch
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
(global as any).fetch = origFetch
|
|
13
|
-
vi.restoreAllMocks()
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
it('throws when no client and no token', async () => {
|
|
17
|
-
const c = new SwitchBotClient({} as any)
|
|
18
|
-
await c.init()
|
|
19
|
-
await expect(c.getDevice('nope')).rejects.toThrow('No SwitchBot client available')
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('uses OpenAPI when token provided for getDevice', async () => {
|
|
23
|
-
const fakeResp = { body: { device: { id: 'd1', state: 'ok' } } }
|
|
24
|
-
const fetchMock = vi.fn(async (url: string, opts: any) => ({ json: async () => fakeResp }))
|
|
25
|
-
;(global as any).fetch = fetchMock
|
|
26
|
-
|
|
27
|
-
const c = new SwitchBotClient({ openApiToken: 'token-123' } as any)
|
|
28
|
-
await c.init()
|
|
29
|
-
const res = await c.getDevice('d1')
|
|
30
|
-
expect(res).toEqual(fakeResp)
|
|
31
|
-
expect(fetchMock).toHaveBeenCalledWith('https://api.switch-bot.com/v1.0/devices/d1', { headers: { Authorization: 'token-123' } })
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('posts commands to OpenAPI for setDeviceState', async () => {
|
|
35
|
-
const respBody = { status: 'ok' }
|
|
36
|
-
const fetchMock = vi.fn(async (url: string, opts: any) => ({ json: async () => respBody }))
|
|
37
|
-
;(global as any).fetch = fetchMock
|
|
38
|
-
|
|
39
|
-
const c = new SwitchBotClient({ openApiToken: 't' } as any)
|
|
40
|
-
await c.init()
|
|
41
|
-
const body = { command: 'turnOn', parameter: 'default', commandType: 'command' }
|
|
42
|
-
const out = await c.setDeviceState('d2', body)
|
|
43
|
-
expect(out).toEqual(respBody)
|
|
44
|
-
expect(fetchMock).toHaveBeenCalledWith('https://api.switch-bot.com/v1.0/devices/d2/commands', expect.objectContaining({ method: 'POST' }))
|
|
45
|
-
const calledBody = JSON.parse((fetchMock.mock.calls[0][1].body))
|
|
46
|
-
expect(calledBody).toEqual(body)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('propagates fetch errors', async () => {
|
|
50
|
-
const fetchMock = vi.fn(async () => { throw new Error('network') })
|
|
51
|
-
;(global as any).fetch = fetchMock
|
|
52
|
-
const c = new SwitchBotClient({ openApiToken: 't' } as any)
|
|
53
|
-
await c.init()
|
|
54
|
-
await expect(c.getDevice('x')).rejects.toThrow('network')
|
|
55
|
-
})
|
|
56
|
-
})
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { SwitchBotClient } from '../src/switchbotClient'
|
|
3
|
-
|
|
4
|
-
describe('SwitchBotClient', () => {
|
|
5
|
-
it('init does not throw and getDevice throws when no token/client', async () => {
|
|
6
|
-
const c = new SwitchBotClient({})
|
|
7
|
-
await c.init()
|
|
8
|
-
await expect(c.getDevice('nope')).rejects.toThrow()
|
|
9
|
-
})
|
|
10
|
-
})
|
package/test/utils.spec.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { createPlatformProxy } from '../src/utils'
|
|
3
|
-
|
|
4
|
-
describe('createPlatformProxy', () => {
|
|
5
|
-
it('returns Matter platform when preferMatter and enabled', () => {
|
|
6
|
-
class HAP {}
|
|
7
|
-
class Matter {}
|
|
8
|
-
const ProxyCtor: any = createPlatformProxy(HAP, Matter)
|
|
9
|
-
const impl = new (ProxyCtor as any)(console, { preferMatter: true, enableMatter: true }, undefined)
|
|
10
|
-
expect(impl).toBeInstanceOf(Matter)
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
it('returns HAP platform when preferMatter is false', () => {
|
|
14
|
-
class HAP {}
|
|
15
|
-
class Matter {}
|
|
16
|
-
const ProxyCtor: any = createPlatformProxy(HAP, Matter)
|
|
17
|
-
const impl = new (ProxyCtor as any)(console, { preferMatter: false, enableMatter: true }, undefined)
|
|
18
|
-
expect(impl).toBeInstanceOf(HAP)
|
|
19
|
-
})
|
|
20
|
-
})
|