@ray-js/lamp-schedule-core 1.0.0-beta-1

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.
Files changed (246) hide show
  1. package/README.md +565 -0
  2. package/lib/config/dpCodes.d.ts +27 -0
  3. package/lib/config/dpCodes.js +30 -0
  4. package/lib/config/index.d.ts +2 -0
  5. package/lib/config/index.js +2 -0
  6. package/lib/config/rhythms/defaultValue.d.ts +18 -0
  7. package/lib/config/rhythms/defaultValue.js +72 -0
  8. package/lib/config/rhythms/index.d.ts +10 -0
  9. package/lib/config/rhythms/index.js +35 -0
  10. package/lib/conflict/ConflictResolver.d.ts +2 -0
  11. package/lib/conflict/ConflictResolver.js +284 -0
  12. package/lib/conflict/__test__/ConflictResolver.test.d.ts +1 -0
  13. package/lib/conflict/__test__/ConflictResolver.test.js +183 -0
  14. package/lib/conflict/__test__/index.test.d.ts +1 -0
  15. package/lib/conflict/__test__/index.test.js +385 -0
  16. package/lib/conflict/__test__/scheduleDataManager.test.d.ts +1 -0
  17. package/lib/conflict/__test__/scheduleDataManager.test.js +170 -0
  18. package/lib/conflict/__test__/transform.test.d.ts +1 -0
  19. package/lib/conflict/__test__/transform.test.js +454 -0
  20. package/lib/conflict/__test__/updateDeviceDp.d.ts +1 -0
  21. package/lib/conflict/__test__/updateDeviceDp.js +114 -0
  22. package/lib/conflict/index.d.ts +42 -0
  23. package/lib/conflict/index.js +103 -0
  24. package/lib/conflict/scheduleDataManager.d.ts +12 -0
  25. package/lib/conflict/scheduleDataManager.js +79 -0
  26. package/lib/conflict/transform.d.ts +71 -0
  27. package/lib/conflict/transform.js +251 -0
  28. package/lib/conflict/type.d.ts +55 -0
  29. package/lib/conflict/type.js +17 -0
  30. package/lib/conflict/updateDeviceDp.d.ts +1 -0
  31. package/lib/conflict/updateDeviceDp.js +41 -0
  32. package/lib/constant/index.d.ts +5 -0
  33. package/lib/constant/index.js +5 -0
  34. package/lib/context/index.d.ts +3 -0
  35. package/lib/context/index.js +3 -0
  36. package/lib/context/rhythms/actions.d.ts +20 -0
  37. package/lib/context/rhythms/actions.js +52 -0
  38. package/lib/context/rhythms/index.d.ts +5 -0
  39. package/lib/context/rhythms/index.js +51 -0
  40. package/lib/context/rhythms/reducer.d.ts +24 -0
  41. package/lib/context/rhythms/reducer.js +38 -0
  42. package/lib/context/schedule/actions.d.ts +7 -0
  43. package/lib/context/schedule/actions.js +51 -0
  44. package/lib/context/schedule/context.d.ts +6 -0
  45. package/lib/context/schedule/context.js +52 -0
  46. package/lib/context/schedule/index.d.ts +1 -0
  47. package/lib/context/schedule/index.js +1 -0
  48. package/lib/context/schedule/reducer.d.ts +16 -0
  49. package/lib/context/schedule/reducer.js +104 -0
  50. package/lib/context/schedule/type.d.ts +41 -0
  51. package/lib/context/schedule/type.js +11 -0
  52. package/lib/context/timer/actions.d.ts +8 -0
  53. package/lib/context/timer/actions.js +52 -0
  54. package/lib/context/timer/context.d.ts +10 -0
  55. package/lib/context/timer/context.js +53 -0
  56. package/lib/context/timer/index.d.ts +1 -0
  57. package/lib/context/timer/index.js +1 -0
  58. package/lib/context/timer/reducer.d.ts +15 -0
  59. package/lib/context/timer/reducer.js +70 -0
  60. package/lib/context/timer/type.d.ts +46 -0
  61. package/lib/context/timer/type.js +24 -0
  62. package/lib/dpParser/README.md +63 -0
  63. package/lib/dpParser/__test__/cycle.test.d.ts +1 -0
  64. package/lib/dpParser/__test__/cycle.test.js +258 -0
  65. package/lib/dpParser/__test__/random.test.d.ts +1 -0
  66. package/lib/dpParser/__test__/random.test.js +211 -0
  67. package/lib/dpParser/__test__/rhythms.test.d.ts +1 -0
  68. package/lib/dpParser/__test__/rhythms.test.js +71 -0
  69. package/lib/dpParser/__test__/rtcTimer.test.d.ts +1 -0
  70. package/lib/dpParser/__test__/rtcTimer.test.js +235 -0
  71. package/lib/dpParser/__test__/sleep.test.d.ts +1 -0
  72. package/lib/dpParser/__test__/sleep.test.js +71 -0
  73. package/lib/dpParser/__test__/sleepCommon.test.d.ts +1 -0
  74. package/lib/dpParser/__test__/sleepCommon.test.js +197 -0
  75. package/lib/dpParser/__test__/timerReport.test.d.ts +1 -0
  76. package/lib/dpParser/__test__/timerReport.test.js +100 -0
  77. package/lib/dpParser/__test__/wakeup.test.d.ts +1 -0
  78. package/lib/dpParser/__test__/wakeup.test.js +69 -0
  79. package/lib/dpParser/__test__/wakeupCommon.test.d.ts +1 -0
  80. package/lib/dpParser/__test__/wakeupCommon.test.js +131 -0
  81. package/lib/dpParser/cycle/index.d.ts +15 -0
  82. package/lib/dpParser/cycle/index.js +134 -0
  83. package/lib/dpParser/index.d.ts +23 -0
  84. package/lib/dpParser/index.js +43 -0
  85. package/lib/dpParser/random/index.d.ts +15 -0
  86. package/lib/dpParser/random/index.js +127 -0
  87. package/lib/dpParser/rhythms.d.ts +41 -0
  88. package/lib/dpParser/rhythms.js +116 -0
  89. package/lib/dpParser/rtcTimer.d.ts +17 -0
  90. package/lib/dpParser/rtcTimer.js +300 -0
  91. package/lib/dpParser/sleep/index.d.ts +9 -0
  92. package/lib/dpParser/sleep/index.js +29 -0
  93. package/lib/dpParser/sleep/sleepCommon.d.ts +11 -0
  94. package/lib/dpParser/sleep/sleepCommon.js +117 -0
  95. package/lib/dpParser/sleep/sleepSigmesh.d.ts +0 -0
  96. package/lib/dpParser/sleep/sleepSigmesh.js +101 -0
  97. package/lib/dpParser/timerReport.d.ts +11 -0
  98. package/lib/dpParser/timerReport.js +43 -0
  99. package/lib/dpParser/utils.d.ts +27 -0
  100. package/lib/dpParser/utils.js +53 -0
  101. package/lib/dpParser/wakeup/index.d.ts +10 -0
  102. package/lib/dpParser/wakeup/index.js +34 -0
  103. package/lib/dpParser/wakeup/wakeupCommon.d.ts +11 -0
  104. package/lib/dpParser/wakeup/wakeupCommon.js +106 -0
  105. package/lib/dpParser/wakeup/wakeupSigmesh.d.ts +15 -0
  106. package/lib/dpParser/wakeup/wakeupSigmesh.js +86 -0
  107. package/lib/hooks/__test__/useCommon.test.d.ts +1 -0
  108. package/lib/hooks/__test__/useCommon.test.js +72 -0
  109. package/lib/hooks/__test__/useCountdownDp.test.d.ts +1 -0
  110. package/lib/hooks/__test__/useCountdownDp.test.js +82 -0
  111. package/lib/hooks/__test__/useCycleDp.test.d.ts +1 -0
  112. package/lib/hooks/__test__/useCycleDp.test.js +92 -0
  113. package/lib/hooks/__test__/useRandomDp.test.d.ts +1 -0
  114. package/lib/hooks/__test__/useRandomDp.test.js +88 -0
  115. package/lib/hooks/__test__/useRhythmsDp.test.d.ts +1 -0
  116. package/lib/hooks/__test__/useRhythmsDp.test.js +81 -0
  117. package/lib/hooks/__test__/useSleepDp.test.d.ts +1 -0
  118. package/lib/hooks/__test__/useSleepDp.test.js +127 -0
  119. package/lib/hooks/__test__/useTimeFlush.test.d.ts +1 -0
  120. package/lib/hooks/__test__/useTimeFlush.test.js +109 -0
  121. package/lib/hooks/__test__/useTimerDp.test.d.ts +1 -0
  122. package/lib/hooks/__test__/useTimerDp.test.js +79 -0
  123. package/lib/hooks/__test__/useTimerOperate.test.d.ts +1 -0
  124. package/lib/hooks/__test__/useTimerOperate.test.js +286 -0
  125. package/lib/hooks/__test__/useTimerOperateCloud.test.d.ts +1 -0
  126. package/lib/hooks/__test__/useTimerOperateCloud.test.js +190 -0
  127. package/lib/hooks/__test__/useTimerOperateLocal.test.d.ts +1 -0
  128. package/lib/hooks/__test__/useTimerOperateLocal.test.js +216 -0
  129. package/lib/hooks/__test__/useTimerReportDp.test.d.ts +1 -0
  130. package/lib/hooks/__test__/useTimerReportDp.test.js +93 -0
  131. package/lib/hooks/__test__/useTimerSupport.test.d.ts +1 -0
  132. package/lib/hooks/__test__/useTimerSupport.test.js +110 -0
  133. package/lib/hooks/__test__/useWakeUpDp.test.d.ts +1 -0
  134. package/lib/hooks/__test__/useWakeUpDp.test.js +129 -0
  135. package/lib/hooks/useBaseLightDp.d.ts +21 -0
  136. package/lib/hooks/useBaseLightDp.js +165 -0
  137. package/lib/hooks/useCommon.d.ts +29 -0
  138. package/lib/hooks/useCommon.js +59 -0
  139. package/lib/hooks/useCommonSupport.d.ts +25 -0
  140. package/lib/hooks/useCommonSupport.js +241 -0
  141. package/lib/hooks/useCountdownDp.d.ts +13 -0
  142. package/lib/hooks/useCountdownDp.js +28 -0
  143. package/lib/hooks/useCountdownSupport.d.ts +9 -0
  144. package/lib/hooks/useCountdownSupport.js +13 -0
  145. package/lib/hooks/useCycleDp.d.ts +7 -0
  146. package/lib/hooks/useCycleDp.js +21 -0
  147. package/lib/hooks/useCycleSupport.d.ts +12 -0
  148. package/lib/hooks/useCycleSupport.js +16 -0
  149. package/lib/hooks/useRandomDp.d.ts +7 -0
  150. package/lib/hooks/useRandomDp.js +21 -0
  151. package/lib/hooks/useRandomSupport.d.ts +12 -0
  152. package/lib/hooks/useRandomSupport.js +16 -0
  153. package/lib/hooks/useRhythmsDp.d.ts +7 -0
  154. package/lib/hooks/useRhythmsDp.js +23 -0
  155. package/lib/hooks/useSleepDp.d.ts +6 -0
  156. package/lib/hooks/useSleepDp.js +21 -0
  157. package/lib/hooks/useSleepSupport.d.ts +8 -0
  158. package/lib/hooks/useSleepSupport.js +13 -0
  159. package/lib/hooks/useTimeFlush.d.ts +8 -0
  160. package/lib/hooks/useTimeFlush.js +31 -0
  161. package/lib/hooks/useTimerDp.d.ts +5 -0
  162. package/lib/hooks/useTimerDp.js +25 -0
  163. package/lib/hooks/useTimerFlushList.d.ts +11 -0
  164. package/lib/hooks/useTimerFlushList.js +53 -0
  165. package/lib/hooks/useTimerOperate.d.ts +68 -0
  166. package/lib/hooks/useTimerOperate.js +403 -0
  167. package/lib/hooks/useTimerOperateCloud.d.ts +31 -0
  168. package/lib/hooks/useTimerOperateCloud.js +233 -0
  169. package/lib/hooks/useTimerOperateLocal.d.ts +40 -0
  170. package/lib/hooks/useTimerOperateLocal.js +310 -0
  171. package/lib/hooks/useTimerReportDp.d.ts +15 -0
  172. package/lib/hooks/useTimerReportDp.js +48 -0
  173. package/lib/hooks/useTimerSupport.d.ts +13 -0
  174. package/lib/hooks/useTimerSupport.js +38 -0
  175. package/lib/hooks/useWakeUpDp.d.ts +6 -0
  176. package/lib/hooks/useWakeUpDp.js +21 -0
  177. package/lib/hooks/useWakeUpSupport.d.ts +8 -0
  178. package/lib/hooks/useWakeUpSupport.js +13 -0
  179. package/lib/hooks/useWakeupDp.d.ts +6 -0
  180. package/lib/hooks/useWakeupDp.js +21 -0
  181. package/lib/hooks/useWakeupSupport.d.ts +8 -0
  182. package/lib/hooks/useWakeupSupport.js +13 -0
  183. package/lib/i18n/index.d.ts +142 -0
  184. package/lib/i18n/index.js +7 -0
  185. package/lib/i18n/rhythms.d.ts +73 -0
  186. package/lib/i18n/rhythms.js +72 -0
  187. package/lib/i18n/strings.d.ts +73 -0
  188. package/lib/i18n/strings.js +6 -0
  189. package/lib/index.d.ts +51 -0
  190. package/lib/index.js +64 -0
  191. package/lib/types/countdown.d.ts +3 -0
  192. package/lib/types/countdown.js +1 -0
  193. package/lib/types/cycle.d.ts +22 -0
  194. package/lib/types/cycle.js +1 -0
  195. package/lib/types/global.d.ts +9 -0
  196. package/lib/types/index.d.ts +331 -0
  197. package/lib/types/index.js +76 -0
  198. package/lib/types/random.d.ts +20 -0
  199. package/lib/types/random.js +1 -0
  200. package/lib/types/rhythms.d.ts +79 -0
  201. package/lib/types/rhythms.js +15 -0
  202. package/lib/types/sleep.d.ts +36 -0
  203. package/lib/types/sleep.js +1 -0
  204. package/lib/types/timer.d.ts +107 -0
  205. package/lib/types/timer.js +1 -0
  206. package/lib/types/wakeup.d.ts +38 -0
  207. package/lib/types/wakeup.js +1 -0
  208. package/lib/utils/ScheduleCache.d.ts +19 -0
  209. package/lib/utils/ScheduleCache.js +39 -0
  210. package/lib/utils/ScheduleCloudProperty.d.ts +75 -0
  211. package/lib/utils/ScheduleCloudProperty.js +220 -0
  212. package/lib/utils/ScheduleDataSync.d.ts +82 -0
  213. package/lib/utils/ScheduleDataSync.js +127 -0
  214. package/lib/utils/ScheduleEmit.d.ts +67 -0
  215. package/lib/utils/ScheduleEmit.js +61 -0
  216. package/lib/utils/ScheduleLogger.d.ts +19 -0
  217. package/lib/utils/ScheduleLogger.js +71 -0
  218. package/lib/utils/ScheduleSupport.d.ts +131 -0
  219. package/lib/utils/ScheduleSupport.js +373 -0
  220. package/lib/utils/ScheduleUtils.d.ts +12 -0
  221. package/lib/utils/ScheduleUtils.js +51 -0
  222. package/lib/utils/__test__/ScheduleCloudProperty.test.d.ts +1 -0
  223. package/lib/utils/__test__/ScheduleCloudProperty.test.js +320 -0
  224. package/lib/utils/__test__/ScheduleSupport.test.d.ts +1 -0
  225. package/lib/utils/__test__/ScheduleSupport.test.js +211 -0
  226. package/lib/utils/__test__/ScheduleUtils.test.d.ts +1 -0
  227. package/lib/utils/__test__/ScheduleUtils.test.js +119 -0
  228. package/lib/utils/__test__/common.test.d.ts +1 -0
  229. package/lib/utils/__test__/common.test.js +147 -0
  230. package/lib/utils/__test__/storage.test.d.ts +1 -0
  231. package/lib/utils/__test__/storage.test.js +98 -0
  232. package/lib/utils/__test__/time.test.d.ts +1 -0
  233. package/lib/utils/__test__/time.test.js +146 -0
  234. package/lib/utils/common.d.ts +23 -0
  235. package/lib/utils/common.js +109 -0
  236. package/lib/utils/dpState.d.ts +5 -0
  237. package/lib/utils/dpState.js +27 -0
  238. package/lib/utils/getCachedSystemInfo.d.ts +39 -0
  239. package/lib/utils/getCachedSystemInfo.js +20 -0
  240. package/lib/utils/isInIDE.d.ts +1 -0
  241. package/lib/utils/isInIDE.js +5 -0
  242. package/lib/utils/storage.d.ts +5 -0
  243. package/lib/utils/storage.js +32 -0
  244. package/lib/utils/time.d.ts +40 -0
  245. package/lib/utils/time.js +84 -0
  246. package/package.json +78 -0
@@ -0,0 +1,51 @@
1
+ import "core-js/modules/esnext.iterator.constructor.js";
2
+ import "core-js/modules/esnext.iterator.find.js";
3
+ /* eslint-disable no-console */
4
+
5
+ import { getScheduleCache } from './ScheduleCache';
6
+
7
+ /**
8
+ * 根据dpId获取dpCode
9
+ * @param dpId dpId
10
+ * @returns dpCode
11
+ */
12
+ export const getDpCodeByDpId = dpId => {
13
+ const devInfo = getScheduleCache('devInfo');
14
+ const groupInfo = getScheduleCache('groupInfo');
15
+
16
+ // 优先从设备信息中查找
17
+ if (devInfo !== null && devInfo !== void 0 && devInfo.schema) {
18
+ const dp = devInfo.schema.find(item => +item.id === +dpId);
19
+ if (dp) return dp.code;
20
+ }
21
+
22
+ // 如果设备信息中没有找到,则从群组信息中查找
23
+ if (groupInfo !== null && groupInfo !== void 0 && groupInfo.schema) {
24
+ const dp = groupInfo.schema.find(item => +item.id === +dpId);
25
+ if (dp) return dp.code;
26
+ }
27
+ return null;
28
+ };
29
+
30
+ /**
31
+ * 根据dpCode获取dpId
32
+ * @param dpCode dpCode
33
+ * @returns dpId
34
+ */
35
+ export const getDpIdByDpCode = dpCode => {
36
+ const devInfo = getScheduleCache('devInfo');
37
+ const groupInfo = getScheduleCache('groupInfo');
38
+
39
+ // 优先从设备信息中查找
40
+ if (devInfo !== null && devInfo !== void 0 && devInfo.schema) {
41
+ const dp = devInfo.schema.find(item => item.code === dpCode);
42
+ if (dp) return dp.id;
43
+ }
44
+
45
+ // 如果设备信息中没有找到,则从群组信息中查找
46
+ if (groupInfo !== null && groupInfo !== void 0 && groupInfo.schema) {
47
+ const dp = groupInfo.schema.find(item => item.code === dpCode);
48
+ if (dp) return dp.id;
49
+ }
50
+ return null;
51
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,320 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import { renderHook } from '@testing-library/react-hooks';
3
+ import { getDevProperty, saveDevProperty, requestCloud, getDeviceProperty } from '@ray-js/ray';
4
+ import { saveList, save, update, get, getAll, remove, useCloudProperty, useLocalTimerCloudProperty } from '../ScheduleCloudProperty';
5
+ import { devIdOrGroupIdCache } from '../ScheduleCache';
6
+ import { scheduleLogger } from '../ScheduleLogger';
7
+ import { getTimerState } from '../../context/timer';
8
+ import { findMinMissingNum } from '../common';
9
+ import { LOCAL_TIMER_CATEGORY } from '../../constant';
10
+
11
+ // 模拟依赖
12
+ jest.mock('@ray-js/ray', () => ({
13
+ getDevProperty: jest.fn(),
14
+ saveDevProperty: jest.fn(),
15
+ requestCloud: jest.fn(),
16
+ getDeviceProperty: jest.fn()
17
+ }));
18
+ jest.mock('../ScheduleLogger', () => ({
19
+ scheduleLogger: {
20
+ debug: jest.fn(),
21
+ error: jest.fn(),
22
+ info: jest.fn(),
23
+ warn: jest.fn()
24
+ }
25
+ }));
26
+ jest.mock('../ScheduleCache', () => ({
27
+ devIdOrGroupIdCache: {
28
+ get: jest.fn()
29
+ },
30
+ getScheduleCache: jest.fn(),
31
+ setScheduleCache: jest.fn(),
32
+ clearScheduleCache: jest.fn()
33
+ }));
34
+ jest.mock('../../context/timer', () => ({
35
+ getTimerState: jest.fn()
36
+ }));
37
+ jest.mock('../common', () => ({
38
+ findMinMissingNum: jest.fn()
39
+ }));
40
+ describe('ScheduleCloudProperty', () => {
41
+ const mockDevId = 'device123';
42
+ const mockCode = 'testCode';
43
+ const mockValue = {
44
+ timerId: '22',
45
+ id: 1,
46
+ name: 'test',
47
+ time: '10:00',
48
+ loops: '1111111',
49
+ dps: {},
50
+ aliasName: '',
51
+ isAppPush: false
52
+ };
53
+ const mockTimerId = '1';
54
+ jest.spyOn(require('../ScheduleCloudProperty'), 'saveList');
55
+ beforeEach(() => {
56
+ jest.clearAllMocks();
57
+ devIdOrGroupIdCache.get.mockReturnValue({
58
+ devId: mockDevId,
59
+ groupId: 'group456'
60
+ });
61
+ saveDevProperty.mockResolvedValue(true);
62
+ getDevProperty.mockResolvedValue([{
63
+ code: mockCode,
64
+ value: mockValue
65
+ }]);
66
+ requestCloud.mockImplementation(_ref => {
67
+ let {
68
+ success
69
+ } = _ref;
70
+ return success({
71
+ properties: {}
72
+ });
73
+ });
74
+ getDeviceProperty.mockImplementation(_ref2 => {
75
+ let {
76
+ success
77
+ } = _ref2;
78
+ return success({
79
+ properties: {}
80
+ });
81
+ });
82
+ getTimerState.mockReturnValue({
83
+ rtcTimerList: [{
84
+ timerId: '2'
85
+ }]
86
+ });
87
+ findMinMissingNum.mockReturnValue(1);
88
+ });
89
+ describe('saveList', () => {
90
+ it('应该保存云端定时任务列表', async () => {
91
+ const dataList = [{
92
+ code: mockCode,
93
+ value: mockValue
94
+ }];
95
+ const result = await saveList(dataList);
96
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
97
+ expect(saveDevProperty).toHaveBeenCalledWith({
98
+ devId: mockDevId,
99
+ bizType: 99,
100
+ propertyList: JSON.stringify(dataList)
101
+ });
102
+ expect(result).toBe(true);
103
+ expect(scheduleLogger.debug).toHaveBeenCalled();
104
+ });
105
+ });
106
+ describe('save', () => {
107
+ it('应该保存单个云端定时任务', async () => {
108
+ const result = await save(mockCode, mockValue);
109
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
110
+ expect(saveDevProperty).toHaveBeenCalledWith({
111
+ devId: mockDevId,
112
+ bizType: 99,
113
+ propertyList: JSON.stringify([{
114
+ code: mockCode,
115
+ value: mockValue
116
+ }])
117
+ });
118
+ expect(result).toBe(true);
119
+ expect(scheduleLogger.debug).toHaveBeenCalled();
120
+ });
121
+ });
122
+ describe('update', () => {
123
+ it('应该更新云端定时任务', async () => {
124
+ const result = await update(mockCode, mockValue);
125
+ expect(saveDevProperty).toHaveBeenCalledWith({
126
+ devId: mockDevId,
127
+ bizType: 99,
128
+ propertyList: JSON.stringify([{
129
+ code: mockCode,
130
+ value: mockValue
131
+ }])
132
+ });
133
+ expect(result).toBe(true);
134
+ });
135
+ });
136
+ describe('get', () => {
137
+ it('应该获取云端定时任务', async () => {
138
+ const result = await get(mockCode);
139
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
140
+ expect(getDevProperty).toHaveBeenCalledWith({
141
+ devId: mockDevId,
142
+ code: mockCode,
143
+ bizType: 99
144
+ });
145
+ expect(result).toEqual({
146
+ code: mockCode,
147
+ value: mockValue
148
+ });
149
+ expect(scheduleLogger.debug).toHaveBeenCalled();
150
+ });
151
+ it('当没有匹配的结果时应该返回null', async () => {
152
+ getDevProperty.mockResolvedValue([{
153
+ code: 'otherCode',
154
+ value: mockValue
155
+ }]);
156
+ const result = await get(mockCode);
157
+ expect(result).toBeNull();
158
+ });
159
+ });
160
+ describe('getAll', () => {
161
+ it('当有groupId时应该调用getDevAllProperty', async () => {
162
+ const result = await getAll();
163
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
164
+ expect(requestCloud).toHaveBeenCalled();
165
+ expect(result).toEqual({
166
+ properties: {}
167
+ });
168
+ });
169
+ it('当没有groupId时应该调用getDeviceProperty', async () => {
170
+ devIdOrGroupIdCache.get.mockReturnValue({
171
+ devId: mockDevId,
172
+ groupId: ''
173
+ });
174
+ const result = await getAll();
175
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
176
+ expect(getDeviceProperty).toHaveBeenCalledWith({
177
+ deviceId: mockDevId,
178
+ success: expect.any(Function),
179
+ fail: expect.any(Function)
180
+ });
181
+ expect(result).toEqual({
182
+ properties: {}
183
+ });
184
+ });
185
+ });
186
+ describe('remove', () => {
187
+ it('应该删除云端定时任务', async () => {
188
+ const result = await remove(mockTimerId, mockCode);
189
+ expect(devIdOrGroupIdCache.get).toHaveBeenCalled();
190
+ expect(saveDevProperty).toHaveBeenCalledWith({
191
+ devId: mockDevId,
192
+ bizType: 99,
193
+ propertyList: JSON.stringify([{
194
+ code: mockCode,
195
+ value: null
196
+ }])
197
+ });
198
+ expect(result).toBe(true);
199
+ expect(scheduleLogger.debug).toHaveBeenCalled();
200
+ });
201
+ });
202
+ describe('useCloudProperty', () => {
203
+ it('应该返回正确的方法', () => {
204
+ const {
205
+ result
206
+ } = renderHook(() => useCloudProperty(mockCode));
207
+ expect(result.current).toHaveProperty('get');
208
+ expect(result.current).toHaveProperty('getAll');
209
+ expect(result.current).toHaveProperty('remove');
210
+ expect(result.current).toHaveProperty('set');
211
+ expect(result.current).toHaveProperty('update');
212
+ });
213
+ it('get方法应该调用get函数', async () => {
214
+ const {
215
+ result
216
+ } = renderHook(() => useCloudProperty(mockCode));
217
+ await result.current.get();
218
+ expect(getDevProperty).toHaveBeenCalled();
219
+ });
220
+ it('set方法应该调用save函数', async () => {
221
+ const {
222
+ result
223
+ } = renderHook(() => useCloudProperty(mockCode));
224
+ await result.current.set(mockValue);
225
+ expect(saveDevProperty).toHaveBeenCalled();
226
+ });
227
+ it('update方法应该调用update函数', async () => {
228
+ const {
229
+ result
230
+ } = renderHook(() => useCloudProperty(mockCode));
231
+ await result.current.update(mockValue);
232
+ expect(saveDevProperty).toHaveBeenCalled();
233
+ });
234
+ it('remove方法应该调用remove函数', async () => {
235
+ const {
236
+ result
237
+ } = renderHook(() => useCloudProperty(mockCode));
238
+ await result.current.remove(mockTimerId);
239
+ expect(saveDevProperty).toHaveBeenCalled();
240
+ });
241
+ });
242
+ describe('useLocalTimerCloudProperty', () => {
243
+ it('应该返回正确的方法', () => {
244
+ const {
245
+ result
246
+ } = renderHook(() => useLocalTimerCloudProperty());
247
+ expect(result.current).toHaveProperty('getAll');
248
+ expect(result.current).toHaveProperty('remove');
249
+ expect(result.current).toHaveProperty('set');
250
+ expect(result.current).toHaveProperty('update');
251
+ expect(result.current).toHaveProperty('updateList');
252
+ expect(result.current).toHaveProperty('updateStatus');
253
+ });
254
+ it('remove方法应该使用正确的key', async () => {
255
+ const {
256
+ result
257
+ } = renderHook(() => useLocalTimerCloudProperty());
258
+ await result.current.remove(mockTimerId);
259
+ expect(saveDevProperty).toHaveBeenCalledWith({
260
+ devId: mockDevId,
261
+ bizType: 99,
262
+ propertyList: JSON.stringify([{
263
+ code: `${LOCAL_TIMER_CATEGORY}_${mockTimerId}`,
264
+ value: 'null'
265
+ }])
266
+ });
267
+ });
268
+ it('set方法应该使用正确的key和timerId', async () => {
269
+ const {
270
+ result
271
+ } = renderHook(() => useLocalTimerCloudProperty());
272
+ await result.current.set(mockValue, mockTimerId);
273
+ expect(saveDevProperty).toHaveBeenCalledWith({
274
+ devId: mockDevId,
275
+ bizType: 99,
276
+ propertyList: JSON.stringify([{
277
+ code: `${LOCAL_TIMER_CATEGORY}_${mockTimerId}`,
278
+ value: mockValue
279
+ }])
280
+ });
281
+ });
282
+ it('update方法应该使用正确的key', async () => {
283
+ const updateValue = _objectSpread(_objectSpread({}, mockValue), {}, {
284
+ timerId: mockTimerId
285
+ });
286
+ const {
287
+ result
288
+ } = renderHook(() => useLocalTimerCloudProperty());
289
+ await result.current.update(updateValue);
290
+ expect(saveDevProperty).toHaveBeenCalledWith({
291
+ devId: mockDevId,
292
+ bizType: 99,
293
+ propertyList: JSON.stringify([{
294
+ code: `${LOCAL_TIMER_CATEGORY}_${mockTimerId}`,
295
+ value: updateValue
296
+ }])
297
+ });
298
+ });
299
+ it('updateList方法应该调用saveList函数', async () => {
300
+ const updateValues = [_objectSpread(_objectSpread({}, mockValue), {}, {
301
+ timerId: mockTimerId
302
+ })];
303
+ const {
304
+ result
305
+ } = renderHook(() => useLocalTimerCloudProperty());
306
+ await result.current.updateList(updateValues);
307
+ expect(saveList).toHaveBeenCalledWith([{
308
+ code: `${LOCAL_TIMER_CATEGORY}_${mockTimerId}`,
309
+ value: updateValues[0]
310
+ }]);
311
+ });
312
+ it('updateStatus方法应该更新状态', async () => {
313
+ const {
314
+ result
315
+ } = renderHook(() => useLocalTimerCloudProperty());
316
+ await result.current.updateStatus(mockTimerId, true);
317
+ expect(saveDevProperty).toHaveBeenCalled();
318
+ });
319
+ });
320
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,211 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import { Support, DeviceCapability, getBitValue } from '../ScheduleSupport';
3
+ import { setScheduleCache, getScheduleCache } from '../ScheduleCache';
4
+ import { getDpCodeByDpId, getDpIdByDpCode } from '../ScheduleUtils';
5
+
6
+ // 模拟依赖
7
+ jest.mock('../ScheduleCache', () => ({
8
+ setScheduleCache: jest.fn(),
9
+ getScheduleCache: jest.fn(),
10
+ clearScheduleCache: jest.fn()
11
+ }));
12
+ jest.mock('../dpState', () => ({
13
+ setDpState: jest.fn(),
14
+ clearDpState: jest.fn()
15
+ }));
16
+ jest.mock('../ScheduleUtils', () => ({
17
+ getDpCodeByDpId: jest.fn(),
18
+ getDpIdByDpCode: jest.fn()
19
+ }));
20
+ describe('ScheduleSupport', () => {
21
+ beforeEach(() => {
22
+ jest.clearAllMocks();
23
+ });
24
+ describe('getBitValue', () => {
25
+ it('应该正确获取数字指定位的值', () => {
26
+ expect(getBitValue(0b1010, 0)).toBe(0); // 第0位是0
27
+ expect(getBitValue(0b1010, 1)).toBe(1); // 第1位是1
28
+ expect(getBitValue(0b1010, 2)).toBe(0); // 第2位是0
29
+ expect(getBitValue(0b1010, 3)).toBe(1); // 第3位是1
30
+ expect(getBitValue(0b1010, 4)).toBe(0); // 第4位是0(超出范围)
31
+ });
32
+ it('当数字为0时应该返回0', () => {
33
+ expect(getBitValue(0, 0)).toBe(0);
34
+ expect(getBitValue(0, 1)).toBe(0);
35
+ expect(getBitValue(0, 10)).toBe(0);
36
+ });
37
+ });
38
+ describe('DeviceCapability枚举', () => {
39
+ it('应该定义了正确的设备能力枚举值', () => {
40
+ expect(DeviceCapability.WIFI).toBe(0);
41
+ expect(DeviceCapability.ZIGBEE).toBe(12);
42
+ expect(DeviceCapability.SIGMESH).toBe(15);
43
+ expect(DeviceCapability.BLUETOOTH).toBe(10);
44
+ });
45
+ });
46
+ describe('Support类', () => {
47
+ let support;
48
+ const mockDevInfo = {
49
+ devId: 'test-device-id',
50
+ capability: 12,
51
+ dps: {
52
+ 1: true
53
+ },
54
+ schema: [{
55
+ id: 1,
56
+ code: 'switch_led'
57
+ }, {
58
+ id: 2,
59
+ code: 'bright_value'
60
+ }]
61
+ };
62
+ const mockGroupInfo = {
63
+ groupId: 'test-group-id',
64
+ capability: DeviceCapability.ZIGBEE,
65
+ dps: {
66
+ 1: true
67
+ },
68
+ schema: [{
69
+ id: 1,
70
+ code: 'switch_led'
71
+ }, {
72
+ id: 2,
73
+ code: 'bright_value'
74
+ }]
75
+ };
76
+ beforeEach(() => {
77
+ support = new Support({
78
+ devId: 'test-device-id'
79
+ });
80
+ // 模拟getScheduleCache返回null,表示缓存中没有数据
81
+ getScheduleCache.mockReturnValue(null);
82
+ });
83
+ describe('初始化', () => {
84
+ it('应该正确初始化设备信息', () => {
85
+ support.setDevInfo(mockDevInfo);
86
+ expect(setScheduleCache).toHaveBeenCalledWith('devInfo', mockDevInfo);
87
+ });
88
+ it('应该正确初始化群组信息', () => {
89
+ support.setGroupDevInfo(mockGroupInfo);
90
+ expect(setScheduleCache).toHaveBeenCalledWith('devInfo', mockGroupInfo);
91
+ });
92
+ });
93
+ describe('设备信息获取', () => {
94
+ it('getCachedDevInfo应该返回缓存的设备信息', () => {
95
+ getScheduleCache.mockReturnValue(mockDevInfo);
96
+ const result = support.getCachedDevInfo();
97
+ expect(result).toEqual(mockDevInfo);
98
+ expect(getScheduleCache).toHaveBeenCalledWith('devInfo');
99
+ });
100
+ it('getDevInfo应该返回设备信息', () => {
101
+ getScheduleCache.mockReturnValue(mockDevInfo);
102
+ const result = support.getDevInfo();
103
+ expect(result).toEqual(mockDevInfo);
104
+ });
105
+ it('getGroupDevInfo应该返回群组信息', () => {
106
+ support.setGroupDevInfo('mock-group-id');
107
+ const result = support.getGroupDevInfo();
108
+ expect(result).toEqual(mockGroupInfo);
109
+ });
110
+ });
111
+ describe('设备类型判断', () => {
112
+ beforeEach(() => {
113
+ getScheduleCache.mockReturnValue(mockDevInfo);
114
+ });
115
+ it('isWifiDevice应该正确判断WIFI设备', () => {
116
+ // 修改capability
117
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
118
+ capability: 1 << DeviceCapability.WIFI
119
+ }));
120
+ expect(support.isWifiDevice()).toBe(true);
121
+ });
122
+ it('isZigbeeDevice应该正确判断ZIGBEE设备', () => {
123
+ // 修改capability
124
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
125
+ capability: 1 << DeviceCapability.ZIGBEE
126
+ }));
127
+ expect(support.isZigbeeDevice()).toBe(true);
128
+ });
129
+ it('isSigMeshDevice应该正确判断SIGMESH设备', () => {
130
+ expect(support.isSigMeshDevice()).toBe(false);
131
+
132
+ // 修改capability
133
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
134
+ capability: 1 << DeviceCapability.SIGMESH
135
+ }));
136
+ expect(support.isSigMeshDevice()).toBe(true);
137
+ });
138
+ it('isBleDevice应该正确判断BLE设备', () => {
139
+ expect(support.isBleDevice()).toBe(false);
140
+
141
+ // 修改capability
142
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
143
+ capability: 1 << DeviceCapability.BLUETOOTH
144
+ }));
145
+ expect(support.isBleDevice()).toBe(true);
146
+ });
147
+ });
148
+ describe('设备能力判断', () => {
149
+ it('hasCapability应该正确判断设备能力', () => {
150
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
151
+ capability: 1 << DeviceCapability.WIFI | 1 << DeviceCapability.ZIGBEE
152
+ }));
153
+ expect(support.hasCapability(DeviceCapability.WIFI)).toBe(true);
154
+ expect(support.hasCapability(DeviceCapability.ZIGBEE)).toBe(true);
155
+ expect(support.hasCapability(DeviceCapability.SIGMESH)).toBe(false);
156
+ });
157
+ it('isGroupDevice应该正确判断是否为群组设备', () => {
158
+ // 没有groupId时返回false
159
+ getScheduleCache.mockReturnValue(mockDevInfo);
160
+ expect(support.isGroupDevice()).toBe(false);
161
+
162
+ // 有groupId时返回true
163
+ getScheduleCache.mockReturnValue(mockGroupInfo);
164
+ expect(support.isGroupDevice()).toBe(true);
165
+ });
166
+ it('isSupportCloudTimer应该正确判断是否支持云端定时', () => {
167
+ // 设置支持云端定时的设备信息
168
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
169
+ panelConfig: {
170
+ bic: [{
171
+ code: 'timer',
172
+ selected: true
173
+ }]
174
+ }
175
+ }));
176
+ expect(support.isSupportCloudTimer()).toBe(true);
177
+
178
+ // 测试不支持云端定时的情况
179
+ getScheduleCache.mockReturnValue(_objectSpread(_objectSpread({}, mockDevInfo), {}, {
180
+ panelConfig: {
181
+ bic: [{
182
+ code: 'timer',
183
+ selected: false
184
+ }]
185
+ }
186
+ }));
187
+ expect(support.isSupportCloudTimer()).toBe(false);
188
+ });
189
+ });
190
+ describe('DP点支持判断', () => {
191
+ beforeEach(() => {
192
+ getScheduleCache.mockReturnValue(mockDevInfo);
193
+ getDpCodeByDpId.mockImplementation(dpId => {
194
+ if (dpId === 1) return 'switch_led';
195
+ if (dpId === 2) return 'bright_value';
196
+ return null;
197
+ });
198
+ getDpIdByDpCode.mockImplementation(dpCode => {
199
+ if (dpCode === 'switch_led') return 1;
200
+ if (dpCode === 'bright_value') return 2;
201
+ return null;
202
+ });
203
+ });
204
+ it('supportDp应该正确判断是否支持DP点(通过dpCode)', () => {
205
+ expect(support.supportDp('switch_led')).toBe(true);
206
+ expect(support.supportDp('bright_value')).toBe(true);
207
+ expect(support.supportDp('unsupported_dp')).toBe(false);
208
+ });
209
+ });
210
+ });
211
+ });
@@ -0,0 +1 @@
1
+ export {};